]> gitweb.michael.orlitzky.com - dead/pictar.git/blob - Pictar.py
67a056fb42a08dc5e0e4ab5df947208c4e68fea7
[dead/pictar.git] / Pictar.py
1 import re
2 import os
3 import sys
4 import urllib
5
6 import Configuration
7
8 from string import Template
9 from TemplateParser import *
10 from Mime.XmlDataParser import XmlDataParser
11
12
13 class Pictar:
14
15 def __init__(self):
16 self.template_parser = TemplateParser()
17 self.__data_parser = XmlDataParser(Configuration.GetMimetypesFilePath())
18
19
20 # Compile the regular expressions only once
21 self.item_regexes = []
22
23 extensions = self.__data_parser.GetAllExtensions()
24
25 for extension in extensions:
26 pattern = '\\' + extension + '$'
27 self.item_regexes.append(re.compile(pattern, re.IGNORECASE))
28
29 return
30
31
32
33 def GetItemPaths(self):
34 # Create a list of the item paths based on which
35 # of the files in the target directory match at least
36 # one of the item regular expressions.
37 target_dir = Configuration.GetAbsTargetDir()
38
39 item_paths = []
40
41 dir_contents = os.listdir(target_dir)
42
43 for file_name in dir_contents:
44 for regex_obj in self.item_regexes:
45 if (regex_obj.search(file_name) != None):
46 absolute_item_path = os.path.join(target_dir, file_name)
47 encoded_path = urllib.quote(absolute_item_path)
48 item_paths.append(encoded_path)
49
50 return item_paths
51
52
53
54 def GetItemData(self, item_paths):
55 # Build the pictar markup
56 item_data = ""
57 current_item_number = 0
58
59 for item_path in item_paths:
60 current_item_number += 1
61 item_extension = os.path.splitext(item_path)[1]
62 item_template = Template(self.__data_parser.GetTemplateByExtension(item_extension))
63 item_mimetype = self.__data_parser.GetNameByExtension(item_extension)
64
65 current_item_data = item_template.substitute(header=item_path,
66 image_path=item_path,
67 item_number=current_item_number,
68 height=Configuration.Defaults.ITEM_HEIGHT,
69 width=Configuration.Defaults.ITEM_WIDTH,
70 mimetype=item_mimetype);
71
72 item_data += current_item_data
73
74 return item_data
75
76
77
78 def GetPageData(self, item_data):
79 # Fill in the template with our markup
80 stylesheet_file_name = Configuration.GetStyleSheetPath()
81 page_template = self.template_parser.GetPageTemplate()
82 page_data = page_template.substitute(stylesheet=stylesheet_file_name,
83 pictar_markup=item_data)
84
85 return page_data
86
87
88
89 def LaunchBrowser(self, url='about:blank'):
90 launch_result = os.spawnl(os.P_WAIT, Configuration.GetBrowserCommand(), Configuration.GetBrowserCommand(), url)
91 return launch_result
92
93
94
95 def WritePage(self, page_data):
96 # Write the output file
97 outfile_path = Configuration.GetOutfilePath()
98
99 outfile = open(outfile_path, "w")
100 outfile.write(page_data)
101 outfile.close()
102
103 return outfile_path