def assertConvert(self, basename, in_format, out_format): # pylint: disable=invalid-name """Test of the "patatools convert" subcommand""" sourcename = "{}.{}".format(basename, in_format) destname = "{}.{}".format(basename, out_format) controlname = "{}.{}.control".format(sourcename, out_format) for main, args in [ (tools_main, ["patatools", "convert"]), (convert_main, ["patatools-convert"]), ]: with self.subTest(main=main, args=args): with self.chdir("test_convert_success"): with open_read(controlname) as controlfile: with logging_reduced(): if os.path.exists(destname): os.remove(destname) self.assertEqual( self._system(main, args + [in_format, out_format, sourcename]), 0, ) expected = controlfile.read().strip().replace( "@TEST_FOLDER@", files.path2posix(resource_filename(__name__, "")), ) with open_read(destname) as destfile: self.assertMultiLineEqual( destfile.read().replace('\r\n', '\n').strip(), expected.strip(), )
def assertConvert(self, basename, in_format, out_format): # pylint: disable=invalid-name """Test of the "patatools convert" subcommand""" sourcename = "{}.{}".format(basename, in_format) destname = "{}.{}".format(basename, out_format) controlname = "{}.{}.control".format(sourcename, out_format) for main, args in [ (tools_main, ["patatools", "convert"]), (convert_main, ["patatools-convert"]), ]: with self.subTest(main=main, args=args): with self.chdir("test_convert_success"): with open_read(controlname) as controlfile: with logging_reduced(): if os.path.exists(destname): os.remove(destname) self.assertEqual( self._system( main, args + [in_format, out_format, sourcename]), 0, ) expected = controlfile.read().strip().replace( "@TEST_FOLDER@", files.path2posix( resource_filename(__name__, "")), ) with open_read(destname) as destfile: self.assertMultiLineEqual( destfile.read().replace('\r\n', '\n').strip(), expected.strip(), )
def config_model(key, alt_dir=None): """Get the model structure key can be: - schema - default - description If alt_dir is specified, the schema is loaded from `alt_dir`/templates/songbook_model.yaml if found. """ # default songbook model model_path = pkg_datapath('templates', 'songbook_model.yml') # when alt_dir is specified, try to load songbook model from there alt_path = None if alt_dir: alt_path = os.path.join(alt_dir, 'templates', 'songbook_model.yml') if os.path.isfile(alt_path): model_path = alt_path with encoding.open_read(model_path) as model_file: data = yaml.safe_load(model_file) return data.get(key, {})
def open_songbook(filename): """Open a songbook file, and prepare it to return a raw songbook object. :param str filename: Filename of the yaml songbook. :rvalue: dict :return: Songbook, as a dictionary. """ if os.path.exists(filename + ".yaml") and not os.path.exists(filename): filename += ".yaml" try: with patacrep.encoding.open_read(filename) as songbook_file: user_songbook = yaml.safe_load(songbook_file) if 'encoding' in user_songbook.get('book', []): with encoding.open_read( filename, encoding=user_songbook['book']['encoding'] ) as songbook_file: user_songbook = yaml.safe_load(songbook_file) except Exception as error: # pylint: disable=broad-except raise patacrep.errors.SongbookError(str(error)) songbookfile_dir = os.path.dirname(os.path.abspath(filename)) # Output at the same place as the songbook file outputdir = songbookfile_dir outputname = os.path.splitext(os.path.basename(filename))[0] return prepare_songbook(user_songbook, outputdir, outputname, songbookfile_dir)
def process_sxd(filename): """Parse sxd file. Return an Index object. """ data = [] index_file = None with encoding.open_read(filename) as index_file: for line in index_file: data.append(line.strip()) i = 1 idx = Index(data[0]) while len(data) > i and data[i].startswith('%'): keywords = KEYWORD_PATTERN.match(data[i]).groups() idx.add_keyword(keywords[0], keywords[1]) i += 1 idx.compile_keywords() for i in range(i, len(data), 3): entry = data[i:i + 3] idx.add(entry[0], entry[1], entry[2]) return idx
def open_songbook(filename): """Open a songbook file, and prepare it to return a raw songbook object. :param str filename: Filename of the yaml songbook. :rvalue: dict :return: Songbook, as a dictionary. """ if os.path.exists(filename + ".yaml") and not os.path.exists(filename): filename += ".yaml" try: with patacrep.encoding.open_read(filename) as songbook_file: user_songbook = yaml.load(songbook_file) if 'encoding' in user_songbook.get('book', []): with encoding.open_read( filename, encoding=user_songbook['book']['encoding'] ) as songbook_file: user_songbook = yaml.load(songbook_file) except Exception as error: # pylint: disable=broad-except raise patacrep.errors.SongbookError(str(error)) songbookfile_dir = os.path.dirname(os.path.abspath(filename)) # Output at the same place as the songbook file outputdir = songbookfile_dir outputname = os.path.splitext(os.path.basename(filename))[0] return prepare_songbook(user_songbook, outputdir, outputname, songbookfile_dir)
def _overwrite_clrf(cls): """Overwrite `*.crlf.source` files to force the CRLF line endings. """ with cls.chdir(): for crlfname in sorted(glob.glob('*.crlf.*.source')): [*base, _, in_format, source] = crlfname.split('.') sourcename = '.'.join(base + [in_format, source]) with open_read(sourcename) as sourcefile: with open(crlfname, 'w') as crlffile: for line in sourcefile: crlffile.write(line.replace('\n', '\r\n'))
def _parse(self): """Parse content, and return the dictionary of song data.""" with encoding.open_read(self.fullpath, encoding=self.encoding) as song: song = parse_song(song.read().strip()+"\n", self.fullpath) self.authors = song.authors self.titles = song.titles self.lang = song.get_data_argument('language', self.lang) self.data = song.meta self.errors = [error(song=self) for error in song.error_builders] self.cached = { 'song': song, }
def _parse(self): """Parse content, and return the dictionary of song data.""" with encoding.open_read(self.fullpath, encoding=self.encoding) as song: self.data = parse_song(song.read(), self.fullpath) self.titles = self.data['@titles'] del self.data['@titles'] self.set_lang(self.data['@language']) del self.data['@language'] if "by" in self.data: self.authors = [self.data['by']] del self.data['by'] else: self.authors = []
def config_model(key): """Get the model structure key can be: - schema - default - description """ model_path = pkg_datapath('templates', 'songbook_model.yml') with encoding.open_read(model_path) as model_file: data = yaml.load(model_file) return data.get(key, {})
def assertRender(self, base, in_format, out_format): # pylint: disable=invalid-name """Assert that `{base}.{in_format}.source` is correctly rendered in the `out_format`. """ sourcename = "{}.{}.source".format(base, in_format) destname = "{}.{}".format(base, out_format) with self.chdir(): with open_read(destname) as expectfile: with logging_reduced(): song = self.song_renderer[out_format][in_format](sourcename, self.config) expected = expectfile.read().strip().replace( "@TEST_FOLDER@", files.path2posix(resource_filename(__name__, "")), ) self.assertMultiLineEqual( song.render().strip(), expected, )
def parse(keyword, config, argument): """Include an external file content. Arguments: - keyword: the string 'include'; - config: the current songbook configuration dictionary; - argument: a list of file paths to be included or a string of the file to include """ new_contentlist = ContentList() if isinstance(argument, str): argument = [argument] for filename in argument: try: filepath = load_from_datadirs( filename, config['_songdir'], config.get('_songbookfile_dir') ) except ContentError as error: new_contentlist.append_error(error) continue content_file = None try: with encoding.open_read( filepath, encoding=config['book']['encoding'] ) as content_file: new_content = yaml.load(content_file) except Exception as error: # pylint: disable=broad-except new_contentlist.append_error(ContentError( keyword="include", message="Error while loading file '{}': {}".format(filepath, error), )) continue config['_datadir'].append(os.path.abspath(os.path.dirname(filepath))) new_contentlist.extend(process_content(new_content, config)) config['_datadir'].pop() return new_contentlist
def open_songbook(filename): """Open songbook, and return a raw songbook object. :param str filename: Filename of the yaml songbook. :rvalue: dict :return: Songbook, as a dictionary. """ if os.path.exists(filename + ".sb") and not os.path.exists(filename): filename += ".sb" try: with patacrep.encoding.open_read(filename) as songbook_file: songbook = yaml.load(songbook_file) if 'encoding' in songbook: with encoding.open_read( filename, encoding=songbook['encoding'] ) as songbook_file: songbook = yaml.load(songbook_file) except Exception as error: # pylint: disable=broad-except raise patacrep.errors.YAMLError(str(error)) songbook['_basename'] = os.path.basename(filename)[:-3] # Gathering datadirs datadirs = [] if 'datadir' in songbook: if isinstance(songbook['datadir'], str): songbook['datadir'] = [songbook['datadir']] datadirs += [ os.path.join( os.path.dirname(os.path.abspath(filename)), path ) for path in songbook['datadir'] ] # Default value datadirs.append(os.path.dirname(os.path.abspath(filename))) songbook['datadir'] = datadirs return songbook
def parse(keyword, config, argument, contentlist): """Include an external file content. Arguments: - keyword: the string 'include'; - config: the current songbook configuration dictionary; - argument: None; - contentlist: a list of file paths to be included. """ new_contentlist = ContentList() for path in contentlist: try: filepath = load_from_datadirs(path, config.get('datadir', [])) except ContentError as error: new_contentlist.append_error(error) continue content_file = None try: with encoding.open_read( filepath, encoding=config['encoding'] ) as content_file: new_content = json.load(content_file) except Exception as error: # pylint: disable=broad-except new_contentlist.append_error(ContentError( keyword="include", message="Error while loading file '{}': {}".format(filepath, error), )) continue config["datadir"].append(os.path.abspath(os.path.dirname(filepath))) new_contentlist.extend(process_content(new_content, config)) config["datadir"].pop() return new_contentlist