def save_source(self, source, name): logger.notice('saving the source {0} in the directory {1}'.format( name, self.source_dir)) filename = os.path.join(self.source_dir, name) with open(filename, 'w') as fp: fp.write(source.full_text) self.update_projects_file()
def save_template(self, template, name): logger.notice('saving the template {0} in the directory {1}'.format( name, self.template_dir)) filename = os.path.join(self.template_dir, name) with open(filename, 'w') as fp: fp.write(template.text) self.update_projects_file()
def render(self): logger.notice('starting the rendering process') self.read_config() template_language = self.config.get('general', 'template language') TemplateClass = get_template_class_by_template_language( template_language) for source_name, source in self.sources: sha256_source = hashlib.sha256(source.full_text).hexdigest() template_path = os.path.join( self.template_dir, source.template_path) sha256_template = hash_file(template_path) head, tail = os.path.split(source_name) filename = os.path.splitext(tail)[0] output_path = os.path.join(self.output_dir, filename) + '.html' if source_name in self.rendered_sources: # source ehas already been rendered once -> this is a necessary # but not sufficient requirement to skip rendering for # this source if self.rendered_sources.get(output_path) == sha256_source: # source file has not chnaged -> may require rendering if # the configuration file or the source's template has been # changed # compare the new calculated template's hash with the old # one, if it exists. otherwise, rendering will be # necessary source_hash = self.rendered_templates.get(template_path) if source_hash == sha256_template: # check if the config file has been changed since the # last rendering config_hash = hash_file(self.config_filename) if config_hash == self.config_hash: # skip the rendering process, because neither the # source nor its template file nor the config file # have been changed continue # pass the config settings of the template language being used # if there are settings for it in the config file if TemplateClass == GenshiTemplate: options = dict(self.config.items('genshi')) elif TemplateClass == Jinja2Template: options = dict(self.config.items('jinja')) else: options = {} output = source.render(TemplateClass, **options) logger.info('{0} + {1} -> {2}'.format( source_name, source.template_path, output_path)) # update the hashes after having rendered the sources self.rendered_sources[output_path] = sha256_source self.rendered_templates[template_path] = sha256_template self.config_hash = hash_file(self.config_filename) yield output_path, output logger.notice('finishing the rendering process') self.update_projects_file()
def reset_config(self): logger.notice( 'resetting the configuration file {0}'.format( self.config_filename)) # check if thee is a global config in CONFIGDIR if os.path.exists(GLOBAL_CONFIGFILE): shutil.copyfile(GLOBAL_CONFIGFILE, self.config_filename) return None for section, config_items in DEFAULT_SETTINGS.iteritems(): if not self.config.has_section(section): logger.info('add the section {0}'.format(section)) self.config.add_section(section) for option, value in config_items: logger.info( 'section {0}: setting the option {1} ' 'to the value {2}'.format(section, option, value)) self.config.set(section, option, value) with open(self.config_filename, 'w') as fp: self.config.write(fp) self.update_projects_file()
def update_projects_file(self, new_created=False): # create the directories where the projects file will be saved if they # do not exist yet path, file = os.path.split(self.projects_file_name) if not os.path.exists(path): logger.notice('creating the directory {0}'.format(path)) os.makedirs(path) now = datetime.now() if new_created: logger.notice( 'creating the projects file {0}'.format( self.projects_file_name)) self.created = now self.last_modified = now with contextlib.closing(shelve.open(self.projects_file_name)) as p: logger.notice( 'updating the projects file {0}'.format( self.projects_file_name)) p[self.project_dir] = self self.updated_projects_file = True
def read_config(self): logger.notice( 'reading the configuration file {0}'.format(self.config_filename)) with open(self.config_filename) as fp: self.config.readfp(fp)
def make_project_directories(self): logger.notice('creating project directories') for path_name in (self.source_dir, self.template_dir, self.output_dir): logger.info('creating the directory {0}'.format(path_name)) os.makedirs(path_name)