import re import os import sys import urllib import Configuration from string import Template from TemplateParser import * from Mime.XmlDataParser import XmlDataParser class Pictar: def __init__(self): self.template_parser = TemplateParser() self.__data_parser = XmlDataParser(Configuration.GetMimetypesFilePath()) # Compile the regular expressions only once self.item_regexes = [] extensions = self.__data_parser.GetAllExtensions() for extension in extensions: pattern = '\\' + extension + '$' self.item_regexes.append(re.compile(pattern, re.IGNORECASE)) return def GetItemPaths(self): """ Create a list of the item paths based on which of the files in the target directory match at least one of the item regular expressions. """ target_dir = Configuration.GetAbsTargetDir() item_paths = [] dir_contents = os.listdir(target_dir) for file_name in dir_contents: for regex_obj in self.item_regexes: if (regex_obj.search(file_name) != None): absolute_item_path = os.path.join(target_dir, file_name) encoded_path = urllib.quote(absolute_item_path) item_paths.append(encoded_path) return item_paths def GetItemData(self, item_paths): """ Build the pictar markup. """ item_data = "" current_item_number = 0 for item_path in item_paths: current_item_number += 1 item_extension = os.path.splitext(item_path)[1] item_template = Template(self.__data_parser.GetTemplateByExtension(item_extension)) item_mimetype = self.__data_parser.GetNameByExtension(item_extension) current_item_data = item_template.substitute(header=item_path, image_path=item_path, item_number=current_item_number, height=Configuration.Defaults.ITEM_HEIGHT, width=Configuration.Defaults.ITEM_WIDTH, mimetype=item_mimetype); item_data += current_item_data return item_data def GetPageData(self, item_data): """ Fill in the template with our markup. """ stylesheet_file_name = Configuration.GetStyleSheetPath() page_template = self.template_parser.GetPageTemplate() page_data = page_template.substitute(stylesheet=stylesheet_file_name, pictar_markup=item_data) return page_data def LaunchBrowser(self, url='about:blank'): launch_result = os.spawnl(os.P_WAIT, Configuration.GetBrowserCommand(), Configuration.GetBrowserCommand(), url) return launch_result def WritePage(self, page_data): # Write the output file outfile_path = Configuration.GetOutfilePath() outfile = open(outfile_path, "w") outfile.write(page_data) outfile.close() return outfile_path