def _iter_testmethods(cls): """Iterate over song files to test.""" # Setting datadir # Load the default songbook config cls.config = config_model('default')['en'] cls.config['_datadir'] = ['datadir'] cls.song_renderer = files.load_renderer_plugins() 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 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:] renderers = files.load_renderer_plugins() 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 _add_songbook_defaults(user_songbook): """ Adds the defaults values to the songbook if missing from the user songbook Priority: - User values - Default values of the user lang (if set) - Default english values """ # Merge the default and user configs locale_default = config_model('default') # Initialize with default in english default_songbook = locale_default.get('en', {}) default_songbook = DictOfDict(default_songbook) if 'lang' in user_songbook.get('book', []): # Update default with current lang lang = user_songbook['book']['lang'] default_songbook.update(locale_default.get(lang, {})) # Update default with user_songbook default_songbook.update(user_songbook) return dict(default_songbook)