def parse_metadata(self, item): if item.path.is_dir(): z2meta_filename = item.path / '.z2meta' / '__this__' body = '' else: z2meta_filename = item.path.parent / '.z2meta' / item.path.name with item.path.open() as f: body = f.read() data = load_metadata(z2meta_filename) data['body'] = self.parse_content(item.path, body) data['slug'] = item.path.stem if item.path.suffix else item.path.name data['date'] = self.parse_date(data.get('date')) return data
def test_z2loader(self): path = pkg_resources.resource_filename( 'akllt', 'dataimport/tests/fixtures/naujienos/.z2meta/naujiena_0001', ) path = pathlib.Path(path) assert path.exists() meta = load_metadata(str(path)) self.assertEqual(meta, { 'date': '2002-10-15', 'title': 'Konkursas', 'blurb': meta['blurb'], }) self.assertIn('konkursas\n„Geriausias 2002 metų', meta['blurb'])
def import_news(directory): """Reads news from given directory. Directory should cantain Zope export, like this one: https://github.com/mgedmin/akl.lt-zope-export """ path = pathlib.Path(directory) assert path.exists() for item in path.iterdir(): if item.name.startswith('naujiena_'): z2meta_filename = item.parent / '.z2meta' / item.name news_story = load_metadata(z2meta_filename) news_story['date'] = datetime.datetime.strptime( news_story['date'], '%Y-%m-%d' ).date() with item.open() as f: news_story['body'] = f.read() yield news_story