]> gitweb.michael.orlitzky.com - dead/pictar.git/blob - Pictar.py
Updated XmlDataParser to use the Python 2.5 ElementTree interface.
[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 """
35 Create a list of the item paths based on which
36 of the files in the target directory match at least
37 one of the item regular expressions.
38 """
39 target_dir = Configuration.GetAbsTargetDir()
40
41 item_paths = []
42
43 dir_contents = os.listdir(target_dir)
44
45 for file_name in dir_contents:
46 for regex_obj in self.item_regexes:
47 if (regex_obj.search(file_name) != None):
48 absolute_item_path = os.path.join(target_dir, file_name)
49 encoded_path = urllib.quote(absolute_item_path)
50 item_paths.append(encoded_path)
51
52 return item_paths
53
54
55
56 def GetItemData(self, item_paths):
57 """ Build the pictar markup. """
58 item_data = ""
59 current_item_number = 0
60
61 for item_path in item_paths:
62 current_item_number += 1
63 item_extension = os.path.splitext(item_path)[1]
64 item_template = Template(self.__data_parser.GetTemplateByExtension(item_extension))
65 item_mimetype = self.__data_parser.GetNameByExtension(item_extension)
66
67 current_item_data = item_template.substitute(header=item_path,
68 image_path=item_path,
69 item_number=current_item_number,
70 height=Configuration.Defaults.ITEM_HEIGHT,
71 width=Configuration.Defaults.ITEM_WIDTH,
72 mimetype=item_mimetype);
73
74 item_data += current_item_data
75
76 return item_data
77
78
79
80 def GetPageData(self, item_data):
81 """ Fill in the template with our markup. """
82 stylesheet_file_name = Configuration.GetStyleSheetPath()
83 page_template = self.template_parser.GetPageTemplate()
84 page_data = page_template.substitute(stylesheet=stylesheet_file_name,
85 pictar_markup=item_data)
86
87 return page_data
88
89
90
91 def LaunchBrowser(self, url='about:blank'):
92 launch_result = os.spawnl(os.P_WAIT, Configuration.GetBrowserCommand(), Configuration.GetBrowserCommand(), url)
93 return launch_result
94
95
96
97 def WritePage(self, page_data):
98 # Write the output file
99 outfile_path = Configuration.GetOutfilePath()
100
101 outfile = open(outfile_path, "w")
102 outfile.write(page_data)
103 outfile.close()
104
105 return outfile_path