예제 #1
0
def test_encoding(tmppath):
    ini = tmppath / 'test.ini'
    write_text(ini, '[äöü]\näöü = äöü', encoding='cp1252')

    with pytest.raises(UnicodeDecodeError):
        INI.from_file(ini)

    assert INI.from_file(ini, encoding='cp1252')['äöü']['äöü'] == 'äöü'
예제 #2
0
    def from_dir(cls, directory: pathlib.Path, nodes=None, _api=None, **kw):
        """
        Create a `Languoid` from a directory, named with the Glottocode and containing `md.ini`.

        This method is used by :class:`pyglottolog.Glottolog` to read `Languoid`s from the
        repository's `languoids/tree` directory.
        """
        if _api and _api.cache and directory.name in _api.cache:
            return _api.cache[directory.name]

        if nodes is None:
            nodes = {}
        cfg = INI.from_file(directory.joinpath(INFO_FILENAME), interpolation=None)

        lineage = []
        for parent in directory.parents:
            id_ = parent.name
            assert id_ != directory.name
            if not Glottocode.pattern.match(id_):
                # we ignore leading non-languoid-dir path components.
                break

            if id_ not in nodes:
                l_ = Languoid.from_dir(parent, nodes=nodes, _api=_api, **kw)
                nodes[id_] = (l_.name, l_.id, l_.level)
            lineage.append(nodes[id_])

        res = cls(cfg, list(reversed(lineage)), directory=directory, _api=_api, **kw)
        nodes[res.id] = (res.name, res.id, res.level)
        return res
예제 #3
0
def get_ini(fname, **kw):
    fname = Path(fname)
    if not fname.exists():
        # For old-style (<=3.4) repository layout we ship the config data with pyglottolog:
        name = fname.name if fname.name != 'hhtype.ini' else 'document_types.ini'
        fname = Path(__file__).parent / name
    assert fname.exists()
    return INI.from_file(fname, **kw)
예제 #4
0
 def from_path(cls,
               path: typing.Union[str, pathlib.Path],
               api=None) -> 'BibFiles':
     """BibTeX files from `<path>/bibtex/*.bib` if listed in `<path>/BIBFILES.ini`."""
     if not isinstance(path, pathlib.Path):
         path = pathlib.Path(path)
     ini = INI.from_file(path / 'BIBFILES.ini', interpolation=None)
     return cls(cls._iterbibfiles(ini, path / 'bibtex', api=api))
예제 #5
0
    def __init__(self, api):
        ini = INI.from_file(api.references_path('BIBFILES.ini'), interpolation=None)
        res = []
        for sec in ini.sections():
            if sec.endswith('.bib'):
                fname = api.references_path('bibtex', sec)
                if not fname.exists():  # pragma: no cover
                    raise ValueError('invalid bibtex file referenced in BIBFILES.ini')
                res.append(BibFile(fname=fname, **ini[sec]))

        super(BibFiles, self).__init__(res)
        self._map = {b.fname.name: b for b in self}
예제 #6
0
    def test_INI(self):
        from clldutils.inifile import INI

        ini = INI()
        ini.set('äüü', 'äöü', ('ä', 'ö', 'ü'))
        ini.set('a', 'b', 5)
        self.assertEqual(ini['a'].getint('b'), 5)
        ini.set('a', 'c', None)
        self.assertNotIn('c', ini['a'])
        self.assertIn('ä\n', ini.write_string())
        self.assertEqual(len(ini.getlist('äüü', 'äöü')), 3)

        tmp = self.tmp_path('test')
        ini.write(tmp.as_posix())
        with tmp.open(encoding='utf8') as fp:
            res = fp.read()
        self.assertIn('coding: utf-8', res)

        ini2 = INI.from_file(tmp)
        self.assertEqual(ini2.write_string(), ini.write_string())
예제 #7
0
    def from_dir(cls, directory, nodes=None, **kw):
        if nodes is None:
            nodes = {}
        cfg = INI.from_file(directory.joinpath(INFO_FILENAME),
                            interpolation=None)

        lineage = []
        for parent in directory.parents:
            id_ = parent.name
            assert id_ != directory.name
            if not Glottocode.pattern.match(id_):
                # we ignore leading non-languoid-dir path components.
                break

            if id_ not in nodes:
                l_ = Languoid.from_dir(parent, nodes=nodes, **kw)
                nodes[id_] = (l_.name, l_.id, l_.level)
            lineage.append(nodes[id_])

        res = cls(cfg, list(reversed(lineage)), directory=directory, **kw)
        nodes[res.id] = (res.name, res.id, res.level)
        return res
예제 #8
0
def test_INI(tmppath):
    ini = INI()
    ini.set('äüü', 'äöü', ('ä', 'ö', 'ü'))
    ini.set('a', 'b', 5)
    assert ini['a'].getint('b') == 5
    ini.set('a', 'c', None)
    assert 'c' not in ini['a']
    assert 'ä\n' in ini.write_string()
    assert len(ini.getlist('äüü', 'äöü')) == 3

    mt = '- a\n  - aa\n  - ab\n- b'
    ini.settext('text', 'multi', mt)

    tmp = tmppath / 'test'
    ini.write(tmp.as_posix())
    with tmp.open(encoding='utf8') as fp:
        res = fp.read()
    assert 'coding: utf-8' in res

    ini2 = INI.from_file(tmp)
    assert ini2.gettext('text', 'multi') == mt
    assert ini2.write_string() == ini.write_string()
예제 #9
0
 def __init__(self, api):
     ini = INI.from_file(api.references_path('hhtype.ini'), interpolation=None)
     self._types = sorted([HHType(s, ini) for s in ini.sections()], reverse=True)
     self._type_by_id = {t.id: t for t in self._types}
예제 #10
0
 def from_path(cls, path, api=None):
     """BibTeX files from `<path>/bibtex/*.bib` if listed in `<path>/BIBFILES.ini`."""
     if not isinstance(path, Path):
         path = Path(path)
     ini = INI.from_file(path / 'BIBFILES.ini', interpolation=None)
     return cls(cls._iterbibfiles(ini, path / 'bibtex', api=api))
예제 #11
0
def read_ini(filename, interpolation=None):
    return INI.from_file(filename, interpolation=interpolation)