def from_file(cls, tmpl_path, name='', *args, **kwargs): """ Alternative constructor -> Creates a template from a file. If `name` is not proveded, the template's name attribute will default to the file's basename, without extension. """ with open(tmpl_path, 'r') as tmpl_f: template_string = tmpl_f.read() if not name: name = path_to_tmpl_name(tmpl_path) return cls(template_string, name=name, *args, **kwargs)
def _load_template(self, tmpl_name): """ Load the `tmpl_name` template from disk, add it to the internal managed list and return it. Will raise a TemplateError if no template matched. """ for t_f in self.list_watched_templates(): t_n = path_to_tmpl_name(t_f) if t_n == tmpl_name: template = Template.from_file(t_f, t_n) self.add(template) return template raise TemplateError('No template named %s' % tmpl_name)
def list_watched_templates(self, basenames=False): """ Yield the pathes of all files contained in all watched directories, recursively if the managers's instance's `recursive` attribute is set to True (which it is by default). Pathes will be absolute, unless the `basenames` argument is set to True, in which case only the files' base names will be returned. """ for d in self.dirs: for t_path in list_files(d, recursive=self.recursive): if basenames: yield path_to_tmpl_name(t_path) else: yield t_path