def test_duplicate_key(): text_str = """ \\newacronym{thekey}{OTHER}{Abbreviation of other} \\newglossaryentry{thekey}{ name={name}, description={the description}, type={symbols} } """ with pytest.raises(KeyError): parse_tex(text_str=text_str)
def test_parse_gterms(): text_str = """ \\newglossaryentry{otherkey}{ name={other name}, description={the description of other} } \\newglossaryentry{thekey}{ name={name}, description={the description}, type={symbols} } """ gterms, acronyms = parse_tex(text_str=text_str) assert acronyms == {} assert gterms == { "otherkey": { "description": "the description of other", "name": "other name" }, "thekey": { "description": "the description", "name": "name", "type": "symbols" }, }
def load_tex(self, text_str=None, path=None, encoding='utf8', skip_ioerrors=False, ignore_unknown_types=True): """load a tex file Parameters ---------- text_str=None: str or None string representing the bib file contents path=None: str or None path to bibfile bibdb=None: bibtexparser.bibdatabase.BibDatabase or None encoding="utf8": str bib file encoding skip_ioerrors: bool if False, an IOError will be raised if newglossaryterm or newacronym is badly formatted ignore_unknown_types: bool if True, strip unknown types, otherwise raise a ValueError Notes ----- the texsoup package is required. if a newglossaryterm has field 'type={symbols}', then it will be loaded as a symbol """ from ipypublish.bib2glossary.parse_tex import parse_tex gterms, acronyms = parse_tex( text_str=text_str, path=path, encoding=encoding, skip_ioerrors=skip_ioerrors ) entries = {} for key, fields in gterms.items(): fields["ENTRYTYPE"] = ETYPE_GLOSS if fields.get("type", None) == "symbols": fields["ENTRYTYPE"] = ETYPE_SYMBOL fields.pop("type") elif "type" in fields: if not ignore_unknown_types: raise ValueError("the 'type' is not recognised: " "{}".format(fields['type'])) fields.pop("type") fields["ID"] = key entry = BibGlossEntry(fields) entries[entry.key] = entry for key, fields in acronyms.items(): fields["ENTRYTYPE"] = ETYPE_ACRONYM fields["ID"] = key entry = BibGlossEntry(fields) entries[entry.key] = entry self._entries = entries return True
def test_ioerror_skip(): text_str = """ \\newacronym{thekey}{Abbreviation of other} \\newacronym{thekey2}{ABBR}{Abbreviation of other} """ gterms, acronyms = parse_tex(text_str=text_str, skip_ioerrors=True) assert gterms == {} assert acronyms == { "thekey2": { "abbreviation": "ABBR", "longname": "Abbreviation of other" } }
def test_ioerror_skip(): text_str = """ \\newacronym{thekey}{Abbreviation of other} \\newacronym{thekey2}{ABBR}{Abbreviation of other} """ gterms, acronyms = parse_tex(text_str=text_str, skip_ioerrors=True) assert gterms == {} assert acronyms == { "thekey2": { 'abbreviation': 'ABBR', 'longname': 'Abbreviation of other' } }
def test_parse_acronyms(): text_str = """ \\newacronym{otherkey}{OTHER}{Abbreviation of other} \\newacronym{thekey}{ABRV}{Abbreviation} """ gterms, acronyms = parse_tex(text_str=text_str) assert gterms == {} assert acronyms == { 'otherkey': { 'abbreviation': 'OTHER', 'longname': 'Abbreviation of other'}, 'thekey': { 'abbreviation': 'ABRV', 'longname': 'Abbreviation'} }
def test_parse_acronyms(): text_str = """ \\newacronym{otherkey}{OTHER}{Abbreviation of other} \\newacronym{thekey}{ABRV}{Abbreviation} """ gterms, acronyms = parse_tex(text_str=text_str) assert gterms == {} assert acronyms == { "otherkey": { "abbreviation": "OTHER", "longname": "Abbreviation of other" }, "thekey": { "abbreviation": "ABRV", "longname": "Abbreviation" }, }
def test_parse_acronyms_with_options(): text_str = """ \\newacronym[description={a description}]{otherkey}{OTHER}{Abbreviation of other} \\newacronym[plural={ABRVs},longplural={Abbreviations}]{thekey}{ABRV}{Abbreviation} """ # noqa: E501 gterms, acronyms = parse_tex(text_str=text_str) assert gterms == {} assert acronyms == { 'otherkey': { 'abbreviation': 'OTHER', 'longname': 'Abbreviation of other', 'description': 'a description'}, 'thekey': { 'abbreviation': 'ABRV', 'longname': 'Abbreviation', 'longplural': 'Abbreviations', 'plural': 'ABRVs'} }
def test_parse_acronyms_with_options(): text_str = """ \\newacronym[description={a description}]{otherkey}{OTHER}{Abbreviation of other} \\newacronym[plural={ABRVs},longplural={Abbreviations}]{thekey}{ABRV}{Abbreviation} """ # noqa: E501 gterms, acronyms = parse_tex(text_str=text_str) assert gterms == {} assert acronyms == { "otherkey": { "abbreviation": "OTHER", "longname": "Abbreviation of other", "description": "a description", }, "thekey": { "abbreviation": "ABRV", "longname": "Abbreviation", "longplural": "Abbreviations", "plural": "ABRVs", }, }
def test_parse_mixed(): text_str = """ \\newacronym{otherkey}{OTHER}{Abbreviation of other} \\newglossaryentry{thekey}{ name={name}, description={the description}, type={symbols} } """ gterms, acronyms = parse_tex(text_str=text_str) assert acronyms == { 'otherkey': { 'abbreviation': 'OTHER', 'longname': 'Abbreviation of other'} } assert gterms == { 'thekey': { 'description': 'the description', 'name': 'name', 'type': 'symbols'} }
def test_parse_mixed(): text_str = """ \\newacronym{otherkey}{OTHER}{Abbreviation of other} \\newglossaryentry{thekey}{ name={name}, description={the description}, type={symbols} } """ gterms, acronyms = parse_tex(text_str=text_str) assert acronyms == { "otherkey": { "abbreviation": "OTHER", "longname": "Abbreviation of other" } } assert gterms == { "thekey": { "description": "the description", "name": "name", "type": "symbols" } }
def test_parse_gterms(): text_str = """ \\newglossaryentry{otherkey}{ name={other name}, description={the description of other} } \\newglossaryentry{thekey}{ name={name}, description={the description}, type={symbols} } """ gterms, acronyms = parse_tex(text_str=text_str) assert acronyms == {} assert gterms == { 'otherkey': { 'description': 'the description of other', 'name': 'other name'}, 'thekey': { 'description': 'the description', 'name': 'name', 'type': 'symbols'} }
def test_gterm_ioerror(): text_str = """ \\newglossaryentry{} """ with pytest.raises(IOError): parse_tex(text_str=text_str)
def test_acronym_ioerror(): text_str = """ \\newacronym{thekey}{Abbreviation of other} """ with pytest.raises(IOError): parse_tex(text_str=text_str)