def to_cldf(self, dest, mdname='cldf-metadata.json'): """ Write the data from the db to a CLDF dataset according to the metadata in `self.dataset`. :param dest: :param mdname: :return: path of the metadata file """ dest = Path(dest) if not dest.exists(): dest.mkdir() data = self.read() if data[self.source_table_name]: sources = Sources() for src in data[self.source_table_name]: sources.add(Source( src['genre'], src['id'], **{k: v for k, v in src.items() if k not in ['id', 'genre']})) sources.write(dest / self.dataset.properties.get('dc:source', 'sources.bib')) for table_type, items in data.items(): try: table = self.dataset[table_type] table.common_props['dc:extent'] = table.write( [self.retranslate(table, item) for item in items], base=dest) except KeyError: assert table_type == self.source_table_name, table_type return self.dataset.write_metadata(dest / 'cldf-metadata.json')
def test_Sources_with_None_values(self): from pycldf.sources import Sources, Source src = Sources() src.add(Source('book', 'huber2005', title=None)) bib = self.tmp_path('test.bib') src.write(bib.name, bib.parent)
def to_cldf(self, dest, mdname='cldf-metadata.json', coordinate_precision=4): """ Write the data from the db to a CLDF dataset according to the metadata in `self.dataset`. :param dest: :param mdname: :return: path of the metadata file """ dest = pathlib.Path(dest) if not dest.exists(): dest.mkdir() data = self.read() if data[self.source_table_name]: sources = Sources() for src in data[self.source_table_name]: sources.add(Source( src['genre'], src['id'], **{k: v for k, v in src.items() if k not in ['id', 'genre']})) sources.write(dest / self.dataset.properties.get('dc:source', 'sources.bib')) for table_type, items in data.items(): try: table = self.dataset[table_type] items = [ self.round_geocoordinates(item, precision=coordinate_precision) for item in items] table.common_props['dc:extent'] = table.write( [self.retranslate(table, item) for item in items], base=dest) except KeyError: assert table_type == self.source_table_name, table_type return self.dataset.write_metadata(dest / mdname)
def test_field_order(tmpdir): srcs = Sources() src = Source('misc', 'x') # src is an OrderedDict and we add title *after* year. src['year'] = '2018' src['title'] = 'The Title' srcs.add(src) bib = tmpdir / 'test.bib' srcs.write(str(bib)) res = bib.read_text(encoding='utf8') # Still, title should be printed in the BibTeX before year: assert res.index('title =') < res.index('year =')
def test_Source_expand_refs(): sources = Sources() src = Source( 'book', 'Meier2005', author='Hans Meier', year='2005', title='The Book') assert 'Meier2005' in repr(src) sources.add(src) bib = sources._bibdata.to_string(bib_format='bibtex') assert len(bib.split('author')) == 2 assert len(list(sources.expand_refs('Meier2005'))) == 1 bib = sources._bibdata.to_string(bib_format='bibtex') assert len(bib.split('author')) == 2 assert len(list(sources.expand_refs('12345'))) == 1
def test_Source_expand_refs(self): from pycldf.sources import Sources, Source sources = Sources() src = Source( 'book', 'Meier2005', author='Hans Meier', year='2005', title='The Book') self.assertIn('Meier2005', repr(src)) sources.add(src) bib = sources._bibdata.to_string(bib_format='bibtex') self.assertEqual(len(bib.split('author')), 2) self.assertEqual(len(list(sources.expand_refs('Meier2005'))), 1) bib = sources._bibdata.to_string(bib_format='bibtex') self.assertEqual(len(bib.split('author')), 2)
def test_Sources(tmpdir): src = Sources() src.add(BIB, Source( 'book', 'huber2005', author='Herrmann Huber', year='2005', title='y')) for entry in src: assert entry.genre == 'book' break assert len(list(src.items())) == 3 assert len(list(src.keys())) == 3 refs = ['huber2005[1-6]', 'Obrazy', 'Elegie[34]'] assert src.format_refs(*list(src.expand_refs(refs))) == refs assert '%s' % src['huber2005'] == 'Huber, Herrmann. 2005. y.' with pytest.raises(ValueError): src.add(5) with pytest.raises(ValueError): src.add('@misc{a.b,\n author="a.b"\n}') with pytest.raises(ValueError): _ = src['unknown'] assert _ # pragma: no cover with pytest.raises(ValueError): src.parse('a[x') with pytest.raises(ValueError): src.parse('[x]') with pytest.raises(ValueError): src.validate(['x']) bib = str(tmpdir / 'test.bib') src.write(bib) src2 = Sources() src2.read(bib) src2.write(bib, ids=['huber2005']) src = Sources.from_file(bib) assert len(src) == 1
def test_Sources(self): from pycldf.sources import Sources, Source src = Sources() src.add(BIB, Source( 'book', 'huber2005', author='Herrmann Huber', year='2005', title='y')) self.assertEqual(len(list(src.items())), 3) self.assertEqual(len(list(src.keys())), 3) refs = 'huber2005[1-6];Obrazy;Elegie[34]' self.assertEqual(src.format_refs(*list(src.expand_refs(refs))), refs) self.assertEqual('%s' % src['huber2005'], 'Huber, Herrmann. 2005. y.') with self.assertRaises(ValueError): src.add(5) with self.assertRaises(ValueError): src.add('@misc{a.b,\n author="a.b"\n}') bib = self.tmp_path('test.bib') src.write(bib.name, bib.parent) src2 = Sources() src2.read(bib.name, bib.parent) bib = self.tmp_path('test.bib') src2.write(bib.name, bib.parent, ids=['huber2005']) src = Sources() src.read(bib.name, bib.parent) self.assertEqual(len(src), 1)
def test_Sources_roundtrip_latex(tmpdir, bibtex, expected): src = Sources() src.add(bibtex) bib = tmpdir / 'test.bib' src.write(str(bib)) assert expected in bib.read_text('utf8')
def test_Sources_with_None_values(tmpdir): src = Sources() src.add(Source('book', 'huber2005', title=None)) bib = tmpdir / 'test.bib' src.write(str(bib))