def test_to_latex_dict(): bibgloss = BibGlossDB() bibgloss.load_bib(text_str=dedent(bib_str), ignore_nongloss_types=True) latex_dict = bibgloss.to_latex_dict() print(latex_dict) assert latex_dict == { ("glsacronym", "akey1"): [ ("\\newacronym[description={a description}]{" "akey1}{ABRV}{Abbreviation}") ], ("glsacronym", "akey2"): [ ("\\newacronym[plural={OTHERs}]{" "akey2}{OTHER}{Abbrev of other}") ], ("glsterm", "gtkey1"): [ "\\newglossaryentry{gtkey1}{", " description={the description},", " name={name}", "}", ], ("glsterm", "gtkey2"): [ "\\newglossaryentry{gtkey2}{", " description={the description of other},", " name={other name}", "}", ], ("glssymbol", "skey1"): [ "\\newglossaryentry{skey1}{", " description={the description of symbol},", " name={\\pi},", " type={symbols}", "}", ], }
def run_postprocess(self, stream, mimetype, filepath, resources): if "bibglosspath" not in resources: return stream, filepath, resources bibpath = resources["bibglosspath"] if not os.path.exists(str(bibpath)): self.logger.warning( "the bibglossary could not be converted, " "since its path does not exist: {}".format(bibpath)) return stream, filepath, resources bibname, extension = os.path.splitext(os.path.basename(bibpath)) outstr = None outext = None if extension in [".bib"]: if mimetype == "text/restructuredtext": pass elif mimetype == "text/latex": self.logger.info("converting bibglossary to tex") bibdb = BibGlossDB() bibdb.load_bib(path=str(bibpath), encoding=self.encoding) outstr = bibdb.to_latex_string() outext = ".tex" elif extension in [".tex"]: if mimetype == "text/latex": pass elif mimetype == "text/restructuredtext": self.logger.info("converting bibglossary to bibtex") bibdb = BibGlossDB() bibdb.load_tex(path=str(bibpath), encoding=self.encoding) outstr = bibdb.to_bib_string() outext = ".bib" else: self.logger.warning("the bibglossary could not be converted, " "since its file extension was not one of: " "bib, tex") if outstr is None: return stream, filepath, resources if sys.version_info < (3, 0): outstr = unicode(outstr, encoding=self.encoding) # noqa: F821 output_folder = filepath.parent.joinpath(self.files_folder) if not output_folder.exists(): output_folder.mkdir(parents=True) outfile = output_folder.joinpath(bibname + outext) self.logger.info("writing bibglossary: {}".format(outfile)) with outfile.open("w", encoding=self.encoding) as fh: fh.write(outstr) self.logger.debug("finished") return stream, filepath, resources
def test_to_latex_dict(): bibgloss = BibGlossDB() bibgloss.load_bib(text_str=dedent(bib_str), ignore_nongloss_types=True) latex_dict = bibgloss.to_latex_dict() print(latex_dict) assert latex_dict == { ('glsacronym', 'akey1'): [('\\newacronym[description={a description}]{' 'akey1}{ABRV}{Abbreviation}')], ('glsacronym', 'akey2'): [('\\newacronym[plural={OTHERs}]{' 'akey2}{OTHER}{Abbrev of other}')], ('glsterm', 'gtkey1'): [ '\\newglossaryentry{gtkey1}{', ' description={the description},', ' name={name}', '}' ], ('glsterm', 'gtkey2'): [ '\\newglossaryentry{gtkey2}{', ' description={the description of other},', ' name={other name}', '}' ], ('glssymbol', 'skey1'): [ '\\newglossaryentry{skey1}{', ' description={the description of symbol},', ' name={\\pi},', ' type={symbols}', '}' ] }
def test_to_bib_string(): bibgloss = BibGlossDB() bibgloss.load_bib(text_str=dedent(bib_str), ignore_nongloss_types=True) string = bibgloss.to_bib_string() assert re.search( "@glsacronym\\{akey1,.*@glsterm\\{gtkey1,.*@glssymbol\\{skey1.*", string, re.DOTALL)
def test_to_dict(): bibgloss = BibGlossDB() bibgloss.load_bib(text_str=dedent(bib_str), ignore_nongloss_types=True) dct = bibgloss.to_dict() assert set(dct.keys()) == {"gtkey1", "gtkey2", "akey1", "akey2", "skey1"}
def test_load_bib_type_ignore(): bibgloss = BibGlossDB() bibgloss.load_bib(text_str=dedent(bib_str), ignore_nongloss_types=True) assert set(bibgloss.keys()) == {"gtkey1", "gtkey2", "akey1", "akey2", "skey1"}
def test_load_bib_type_error(): bibgloss = BibGlossDB() with pytest.raises(TypeError): bibgloss.load_bib(text_str=dedent(bib_str), ignore_nongloss_types=False)