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 update_config(self, section, config_items): '''Change the given values of ``config_items`` (a list of tuples) in the section ``section`` and write them into the configuration file ``self.config_filename``. ''' self.read_config() 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 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 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)