def write_tex(self, output): """Build the '.tex' file corresponding to self. Arguments: - output: a file object, in which the file will be written. """ # Updating configuration self._config = self._raw_config.copy() renderer = TexBookRenderer( self._config['book']['template'], self._config['_datadir'], self._config['book']['lang'], self._config['book']['encoding'], ) try: self._config['_template'] = renderer.get_all_variables(self._config.get('template', {})) except errors.SchemaError as exception: exception.message = "The songbook file '{}' is not valid\n{}".format( self.basename, exception.message) raise exception self._config['_compiled_authwords'] = authors.compile_authwords( copy.deepcopy(self._config['authors']) ) # Loading custom plugins self._config['_content_plugins'] = files.load_plugins( datadirs=self._config['_datadir'], root_modules=['content'], keyword='CONTENT_PLUGINS', ) self._config['_song_plugins'] = files.load_plugins( datadirs=self._config['_datadir'], root_modules=['songs'], keyword='SONG_RENDERERS', )['tsg'] # Configuration set self._config['render'] = content.render self._config['content'] = content.process_content( self._config.get('content', []), self._config, ) self._config['filename'] = output.name[:-4] self._config['_bookoptions'] = iter_bookoptions(self._config) renderer.render_tex(output, self._config) # Get all errors, and maybe exit program self._errors.extend(renderer.errors) if self._config['_error'] == "failonbook": if self.has_errors(): raise errors.SongbookError("Some songs contain errors. Stopping as requested.")
def _iter_testmethods(cls): """Iterate over song files to test.""" # Setting datadir cls.config = DEFAULT_CONFIG if 'datadir' not in cls.config: cls.config['datadir'] = [] cls.config['datadir'].append('datadir') cls.song_plugins = files.load_plugins( datadirs=cls.config['datadir'], root_modules=['songs'], keyword='SONG_RENDERERS', ) with cls.chdir(): for source in sorted(glob.glob('*.*.source')): [*base, in_format, _] = source.split('.') base = '.'.join(base) for out_format in OUTPUTS[in_format]: outname = "{}.{}".format(base, out_format) if not os.path.exists(outname): continue yield ( "test_{}_{}_2_{}".format(base, in_format, out_format), cls._create_test(base, in_format, out_format), ) if os.path.isdir("errors"): with cls.chdir('errors'): for source in sorted(glob.glob('*.*.source')): [*base, in_format, _] = source.split('.') base = '.'.join(base) yield ( "test_{}_{}_failure".format(base, in_format), cls._create_failure(base, in_format), )
def main(args=None): """Main function: run from command line.""" if args is None: args = sys.argv if len(args) < 4: LOGGER.error("Invalid number of arguments.") LOGGER.error("Usage: %s", _usage()) sys.exit(1) source = args[1] dest = args[2] song_files = args[3:] # todo : what is the datadir argument used for? renderers = files.load_plugins( datadirs=[], root_modules=['songs'], keyword='SONG_RENDERERS', ) if dest not in renderers: LOGGER.error( "Unknown destination file format '%s'. Available ones are %s.", source, ", ".join(["'{}'".format(key) for key in renderers.keys()]) ) sys.exit(1) if source not in renderers[dest]: LOGGER.error( "Unknown source file format '%s'. Available ones are %s.", source, ", ".join(["'{}'".format(key) for key in renderers[dest].keys()]) ) sys.exit(1) for file in song_files: try: song = renderers[dest][source](file, config_model('default')['en']) destname = "{}.{}".format(".".join(file.split(".")[:-1]), dest) if os.path.exists(destname): if not confirm(destname): continue with open(destname, "w") as destfile: destfile.write(song.render()) except ContentError: LOGGER.error("Cannot parse file '%s'.", file) sys.exit(1) except NotImplementedError: LOGGER.error("Cannot convert to format '%s'.", dest) sys.exit(1) except KeyboardInterrupt: print() LOGGER.info("Aborted by user.") sys.exit(0) sys.exit(0)
def write_tex(self, output): """Build the '.tex' file corresponding to self. Arguments: - output: a file object, in which the file will be written. """ # Updating configuration self._config = DEFAULT_CONFIG.copy() self._config.update(self._raw_config) renderer = TexBookRenderer( self._config["template"], self._config["datadir"], self._config["lang"], self._config["encoding"] ) self._config.update(renderer.get_variables()) self._config.update(self._raw_config) self._config["_compiled_authwords"] = authors.compile_authwords(copy.deepcopy(self._config["authwords"])) # Loading custom plugins self._config["_content_plugins"] = files.load_plugins( datadirs=self._config.get("datadir", []), root_modules=["content"], keyword="CONTENT_PLUGINS" ) self._config["_song_plugins"] = files.load_plugins( datadirs=self._config.get("datadir", []), root_modules=["songs"], keyword="SONG_RENDERERS" )["tsg"] # Configuration set self._config["render"] = content.render self._config["content"] = content.process_content(self._config.get("content", []), self._config) self._config["filename"] = output.name[:-4] renderer.render_tex(output, self._config) # Get all errors, and maybe exit program self._errors.extend(renderer.errors) if self.config["_error"] == "failonbook": if self.has_errors(): raise errors.SongbookError("Some songs contain errors. Stopping as requested.")
def _generate_config(cls, sbcontent, outputdir, base): """Generate the config to process the content""" # Load the default songbook config config = prepare_songbook( {'book':{'datadir':'datadir'}, 'content': sbcontent}, outputdir, base, outputdir ) # Load the plugins config['_content_plugins'] = files.load_plugins( datadirs=config['_datadir'], root_modules=['content'], keyword='CONTENT_PLUGINS', ) config['_song_plugins'] = files.load_plugins( datadirs=config['_datadir'], root_modules=['songs'], keyword='SONG_RENDERERS', )['tsg'] return config
def _generate_config(cls): """Generate the config to process the content""" config = DEFAULT_CONFIG.copy() datadirpaths = [os.path.join(os.path.dirname(__file__), 'datadir')] config['datadir'] = datadirpaths config['_songdir'] = [ DataSubpath(path, 'songs') for path in datadirpaths ] config['_content_plugins'] = files.load_plugins( datadirs=datadirpaths, root_modules=['content'], keyword='CONTENT_PLUGINS', ) config['_song_plugins'] = files.load_plugins( datadirs=datadirpaths, root_modules=['songs'], keyword='SONG_RENDERERS', )['tsg'] cls.config = config