def _clean_path(cls, elem): """Shorten the path relative to the test directory""" if not isinstance(elem, dict): return elem test_path = files.path2posix(resource_filename(__name__, "")) + "/" for key in ['song', 'tex']: if key in elem: elem[key] = files.path2posix(os.path.normpath( elem[key])).replace(test_path, "") return elem
def _clean_path(cls, elem): """Shorten the path relative to the test directory""" if not isinstance(elem, dict): return elem test_path = files.path2posix(resource_filename(__name__, ""))+"/" for key in ['song', 'tex']: if key in elem: elem[key] = files.path2posix( os.path.normpath(elem[key]) ).replace(test_path, "") return elem
def render(self, context): return r'\input{{{}}}'.format( files.path2posix( files.relpath( self.filename, os.path.dirname(context['filename']), )))
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 test_generation(self): """Test that `base.tex` is correctly generated.""" songbook = "{}.yaml".format(base) # Check tex generation if onthefly: self.compile_songbook_onthefly(base, ['tex']) else: self.assertEqual(0, self.compile_songbook(songbook, "tex")) # Check generated tex control = "{}.tex.control".format(base) if not os.path.exists(control): raise unittest.SkipTest( 'No control file for {}'.format(songbook)) tex = "{}.tex".format(base) with open(control, mode="r", encoding="utf8") as expectfile: with open(tex, mode="r", encoding="utf8") as latexfile: expected = expectfile.read().strip() expected = expected.replace( "@TEST_FOLDER@", path2posix(os.path.dirname(__file__)), ) expected = expected.replace( "@LOCAL_DATA_FOLDER@", path2posix(__DATADIR__), ) expected = expected.replace( "@DATA_FOLDER@", path2posix( subprocess.check_output( [ sys.executable, "-c", 'import patacrep; print(patacrep.__DATADIR__)' ], # pylint: disable=line-too-long universal_newlines=True, cwd=os.path.dirname(songbook), ).strip()), ) self.assertMultiLineEqual( latexfile.read().strip(), expected, )
def test_generation(self): """Test that `base.tex` is correctly generated.""" songbook = "{}.yaml".format(base) # Check tex generation if onthefly: self.compile_songbook_onthefly(base, ['tex']) else: self.assertEqual(0, self.compile_songbook(songbook, "tex")) # Check generated tex control = "{}.tex.control".format(base) if not os.path.exists(control): raise unittest.SkipTest('No control file for {}'.format(songbook)) tex = "{}.tex".format(base) with open(control, mode="r", encoding="utf8") as expectfile: with open(tex, mode="r", encoding="utf8") as latexfile: expected = expectfile.read().strip() expected = expected.replace( "@TEST_FOLDER@", path2posix(os.path.dirname(__file__)), ) expected = expected.replace( "@LOCAL_DATA_FOLDER@", path2posix(__DATADIR__), ) expected = expected.replace( "@DATA_FOLDER@", path2posix( subprocess.check_output( [sys.executable, "-c", 'import patacrep; print(patacrep.__DATADIR__)'], # pylint: disable=line-too-long universal_newlines=True, cwd=os.path.dirname(songbook), ).strip() ), ) self.assertMultiLineEqual( latexfile.read().strip(), expected, )
def _clean_path(cls, elem): """Shorten the path relative to the `songs` directory""" latex_command_classes = ( section.Section, songsection.SongSection, setcounter.CounterSetter, ) if isinstance(elem, latex_command_classes): return elem.render(None)[1:] elif isinstance(elem, song.SongRenderer): songpath = os.path.join(os.path.dirname(__file__), 'datadir', 'songs') return files.path2posix(files.relpath(elem.song.fullpath, songpath)) elif isinstance(elem, tex.LaTeX): return files.path2posix(elem.filename) else: raise Exception(elem)
def _clean_path(cls, elem): """Shorten the path relative to the `songs` directory""" if isinstance(elem, song.SongRenderer): songpath = os.path.join(os.path.dirname(__file__), 'datadir', 'songs') return files.path2posix(files.relpath(elem.song.fullpath, songpath)) elif isinstance(elem, section.Section): if elem.short is None: return "{}:{}".format(elem.keyword, elem.name) else: return "{}:({}){}".format(elem.keyword, elem.short, elem.name) elif isinstance(elem, songsection.SongSection): return "{}:{}".format(elem.keyword, elem.name) elif isinstance(elem, tex.LaTeX): return files.path2posix(elem.filename) else: raise Exception(elem)
def render(self, context): """Return the string that will render the song.""" return textwrap.dedent("""\ {separator} %% {path} {song} """).format( separator="%" * 80, path=files.path2posix(self.song.subpath), song=self.song.render(), )
def render(self, context): """Return the string that will render the song.""" return textwrap.dedent("""\ {separator} %% {path} {song} """).format( separator="%"*80, path=files.path2posix(self.song.subpath), song=self.song.render(), )
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 test_content(self): """Test that `base.source` produces the correct file list""" sourcename = "{}.source".format(base) with open(sourcename, mode="r", encoding="utf8") as sourcefile: sbcontent = yaml.load(sourcefile) outputdir = os.path.dirname(base) config = cls._generate_config(sbcontent, outputdir, base) with logging_reduced('patacrep.content.song'): expandedlist = content.process_content(sbcontent, config) sourcelist = [cls._clean_path(elem) for elem in expandedlist] controlname = "{}.control".format(base) if not os.path.exists(controlname): raise Exception("Missing control:" + str(sourcelist).replace("'", '"')) with open(controlname, mode="r", encoding="utf8") as controlfile: controllist = [ elem.replace("@TEST_FOLDER@", files.path2posix(resource_filename(__name__, ""))) for elem in yaml.load(controlfile) ] self.assertEqual(controllist, sourcelist)
def render(self): """Return the code rendering the song.""" # pylint: disable=signature-differs filename = os.path.basename(self.fullpath) path = os.path.abspath(os.path.dirname(self.fullpath)) return r'\import{{{}/}}{{{}}}'.format(files.path2posix(path), filename)
def render(self, context): return r'\input{{{}}}'.format(files.path2posix(files.relpath( self.filename, os.path.dirname(context['filename']), )))