def compile_songbook_onthefly(base, steps=None): """Compile songbook "on the fly": without a physical songbook file.""" with open(base + ".yaml", mode="r", encoding="utf8") as sbfile: sbyaml = yaml.safe_load(sbfile) outputdir = os.path.dirname(base) outputname = os.path.basename(base) datadir_prefix = os.path.join(outputdir, '..') songbook = prepare_songbook(sbyaml, outputdir, outputname, datadir_prefix=datadir_prefix) songbook['_error'] = "fix" songbook['_cache'] = True sb_builder = SongbookBuilder(songbook) sb_builder.unsafe = True with chdir(outputdir): # Continuous Integration will be verbose if 'CI' in os.environ: with logging_reduced(level=logging.DEBUG): sb_builder.build_steps(steps) else: sb_builder.build_steps(steps)
def compile_songbook_onthefly(base, steps=None): """Compile songbook "on the fly": without a physical songbook file.""" with open(base + ".yaml", mode="r", encoding="utf8") as sbfile: sbyaml = yaml.load(sbfile) outputdir = os.path.dirname(base) outputname = os.path.basename(base) datadir_prefix = os.path.join(outputdir, '..') songbook = prepare_songbook(sbyaml, outputdir, outputname, datadir_prefix=datadir_prefix) songbook['_error'] = "fix" songbook['_cache'] = True sb_builder = SongbookBuilder(songbook) sb_builder.unsafe = True with chdir(outputdir): # Continuous Integration will be verbose if 'CI' in os.environ: with logging_reduced(level=logging.DEBUG): sb_builder.build_steps(steps) else: sb_builder.build_steps(steps)
def chdir(*pathlist): """Temporary change current directory, relative to this file directory""" with files.chdir(resource_filename(__name__, ""), *pathlist): yield
def parse(keyword, argument, config): """Parse data associated with keyword 'song'. Arguments: - keyword: unused; - argument: a list of strings, which are interpreted as regular expressions (interpreted using the glob module), referring to songs. - config: the current songbook configuration dictionary. Return a list of Song() instances. """ contentlist = argument if isinstance(contentlist, str): contentlist = [contentlist] plugins = files.load_renderer_plugins(config['_datadir'])['tsg'] if '_langs' not in config: config['_langs'] = set() songlist = ContentList() for songdir in config['_songdir']: if contentlist: break contentlist = files.recursive_find(songdir.fullpath, plugins.keys()) if contentlist is None: contentlist = [] # No content was set or found for elem in contentlist: before = len(songlist) for songdir in config['_songdir']: if not os.path.isdir(songdir.datadir): continue with files.chdir(songdir.datadir): # Starting with Python 3.5 glob can be recursive: **/*.csg for instance # for filename in glob.iglob(os.path.join(songdir.subpath, elem), recursive=True): for filename in glob.iglob(os.path.join(songdir.subpath, elem)): LOGGER.debug('Parsing file "{}"…'.format(filename)) extension = filename.split(".")[-1] if extension not in plugins: LOGGER.info( ('Cannot parse "%s": name does not end with one ' 'of %s. Ignored.'), os.path.join(songdir.datadir, filename), ", ".join([ "'.{}'".format(key) for key in plugins.keys() ])) continue try: renderer = SongRenderer(plugins[extension]( filename, config, datadir=songdir.datadir, )) except ContentError as error: songlist.append_error(error) if config['_error'] == "failonsong": raise errors.SongbookError( "Error in song '{}'. Stopping as requested.". format(os.path.join(songdir.fullpath, filename))) continue if renderer.has_errors( ) and config['_error'] == "failonsong": raise errors.SongbookError( "Error in song '{}'. Stopping as requested.". format(os.path.join(songdir.fullpath, filename))) songlist.append(renderer) config["_langs"].add(renderer.song.lang) if len(songlist) > before: break if len(songlist) == before: # No songs were added LOGGER.warning( errors.notfound( elem, [item.fullpath for item in config['_songdir']], message= 'Ignoring "{name}": did not match any file in {paths}.', )) return sorted(songlist)
def parse(keyword, argument, contentlist, config): """Parse data associated with keyword 'song'. Arguments: - keyword: unused; - argument: unused; - contentlist: a list of strings, which are interpreted as regular expressions (interpreted using the glob module), referring to songs. - config: the current songbook configuration dictionary. Return a list of Song() instances. """ plugins = config['_song_plugins'] if '_langs' not in config: config['_langs'] = set() songlist = ContentList() for songdir in config['_songdir']: if contentlist: break contentlist = files.recursive_find(songdir.fullpath, plugins.keys()) for elem in contentlist: before = len(songlist) for songdir in config['_songdir']: if not os.path.isdir(songdir.datadir): continue with files.chdir(songdir.datadir): # Starting with Python 3.5 glob can be recursive: **/*.csg for instance # for filename in glob.iglob(os.path.join(songdir.subpath, elem), recursive=True): for filename in glob.iglob(os.path.join(songdir.subpath, elem)): LOGGER.debug('Parsing file "{}"…'.format(filename)) extension = filename.split(".")[-1] if extension not in plugins: LOGGER.info( ( 'Cannot parse "%s": name does not end with one ' 'of %s. Ignored.' ), os.path.join(songdir.datadir, filename), ", ".join(["'.{}'".format(key) for key in plugins.keys()]) ) continue try: renderer = SongRenderer(plugins[extension]( filename, config, datadir=songdir.datadir, )) except ContentError as error: songlist.append_error(error) if config['_error'] == "failonsong": raise errors.SongbookError( "Error in song '{}'. Stopping as requested." .format(os.path.join(songdir.fullpath, filename)) ) continue if renderer.has_errors() and config['_error'] == "failonsong": raise errors.SongbookError( "Error in song '{}'. Stopping as requested." .format(os.path.join(songdir.fullpath, filename)) ) songlist.append(renderer) config["_langs"].add(renderer.song.lang) if len(songlist) > before: break if len(songlist) == before: # No songs were added LOGGER.warning(errors.notfound( elem, [item.fullpath for item in config['_songdir']], message='Ignoring "{name}": did not match any file in {paths}.', )) return sorted(songlist)
def _system(self, main, args): with chdir(os.path.dirname(__file__)): try: main(args) except SystemExit as systemexit: self.assertEqual(systemexit.code, 0)