def load_from_datadirs(path, datadirs): """Load 'path' from one of the datadirs. Raise an exception if it was found if none of the datadirs of 'config'. """ for filepath in files.iter_datadirs(datadirs, "songs", path): if os.path.exists(filepath): return filepath # File not found raise ContentError( "include", errors.notfound(path, list(files.iter_datadirs(datadirs))) )
def parse(keyword, argument, config): """Parse the tex files. Arguments: - keyword: unused; - argument: a list of tex files to include or a string of the tex file to include; - config: configuration dictionary of the current songbook. """ if isinstance(argument, str): argument = [argument] filelist = ContentList() basefolders = [path.fullpath for path in config['_songdir']] + list( files.iter_datadirs(config['_datadir'], 'latex')) for filename in argument: checked_file = None for path in basefolders: if os.path.exists(os.path.join(path, filename)): checked_file = os.path.relpath(os.path.join( path, filename, )) break if not checked_file: filelist.append_error( ContentError( keyword="tex", message=errors.notfound(filename, basefolders), )) continue filelist.append(LaTeX(checked_file)) return filelist
def parse(keyword, argument, contentlist, config): """Parse the contentlist. Arguments: - keyword: unused; - argument: unused; - contentlist: a list of name of tex files; - config: configuration dictionary of the current songbook. """ if not contentlist: LOGGER.warning( "Useless 'tex' content: list of files to include is empty." ) filelist = ContentList() basefolders = itertools.chain( (path.fullpath for path in config['_songdir']), files.iter_datadirs(config['datadir']), files.iter_datadirs(config['datadir'], 'latex'), ) for filename in contentlist: checked_file = None for path in basefolders: if os.path.exists(os.path.join(path, filename)): checked_file = os.path.relpath(os.path.join( path, filename, )) break if not checked_file: filelist.append_error( ContentError( keyword="tex", message=errors.notfound(filename, basefolders), ) ) continue filelist.append(LaTeX(checked_file)) return filelist
def parse(keyword, argument, config): """Parse the tex files. Arguments: - keyword: unused; - argument: a list of tex files to include or a string of the tex file to include; - config: configuration dictionary of the current songbook. """ if isinstance(argument, str): argument = [argument] filelist = ContentList() basefolders = itertools.chain( (path.fullpath for path in config['_songdir']), files.iter_datadirs(config['_datadir'], 'latex'), ) for filename in argument: checked_file = None for path in basefolders: if os.path.exists(os.path.join(path, filename)): checked_file = os.path.relpath(os.path.join( path, filename, )) break if not checked_file: filelist.append_error( ContentError( keyword="tex", message=errors.notfound(filename, basefolders), ) ) continue filelist.append(LaTeX(checked_file)) return filelist
def __init__(self, template, datadirs, lang, encoding=None): '''Start a new jinja2 environment for .tex creation. Arguments: - template: name of the template to use. - datadirs: list of locations of the data directory (which may contain file <datadir>/templates/<template>). - lang: main language of songbook. - encoding: if set, encoding of the template. ''' self.lang = lang # Load templates in filesystem ... loaders = [ FileSystemLoader(datadir) for datadir in files.iter_datadirs(datadirs, 'templates', 'songbook') ] jinjaenv = Environment( loader=ChoiceLoader(loaders), extensions=[VariablesExtension], ) try: super().__init__(template, jinjaenv, encoding) except TemplateNotFound as exception: # Only works if all loaders are FileSystemLoader(). paths = [ item for loader in self.jinjaenv.loader.loaders for item in loader.searchpath ] raise errors.TemplateError( exception, errors.notfound( exception.name, paths, message='Template "{name}" not found in {paths}.' ), )
def iter_datadirs(self, *subpath): """Return an iterator of existing datadirs (with an optionnal subpath) """ yield from files.iter_datadirs(self.config['_datadir'], *subpath)