def load_moby(self): import shakespeare.model as model pkg = 'shksprdata' fileobj = pkg_resources.resource_stream(pkg, '/moby/metadata.txt') def norm_work_name(out): out = out.replace('_moby', '') out = out.replace('life_and_death_of_king_john', 'john') out = out.replace('labor', 'labour') out = out.replace('part_iii', 'part_3') out = out.replace('part_ii', 'part_2') out = out.replace('part_i', 'part_1') return out material = model.load_material(fileobj, norm_work_name=norm_work_name) for item in material: if not item.resources: model.Resource( locator_type=u'cache', locator='moby/html/%s.html' % item.name, format=u'html', material=item, ) model.Resource( locator_type=u'cache', locator=u'moby/pdf/%s.pdf' % item.name, format=u'pdf', material=item, ) model.Session.commit()
def load_gutenberg(self): import shakespeare.model as model pkg = 'shksprdata' fileobj = pkg_resources.resource_stream(pkg, '/gutenberg/metadata.txt') def norm_work_name(out): if out.endswith('_f'): out = out[:-2] out = out.replace('_gut', '') out = out.replace('anthonie', 'antony') out = out.replace('errours', 'errors') out = out.replace('all_is', "alls") out = out.replace('loves_labour_', 'loves_labours_') out = out.replace('dreame', 'dream') out = out.replace('twelfe-', 'twelfth_') out = out.replace('tragedy_of_', '') return out material = model.load_material(fileobj, norm_work_name=norm_work_name) for item in material: if not item.resources: locator = u'%s::/gutenberg/%s.txt' % (pkg, item.name) res = model.Resource( locator_type=u'package', locator=locator, format=u'txt', material=item, ) model.Session.commit()
def make_fixture(self): import shakespeare.model as model sonnet18_work = model.Work.by_name(self.name) if not sonnet18_work: sonnet18_work = model.Work(name=self.name, title=u'Sonnet 18', creator=u'William Shakespeare', notes=u'''### Some Notes This is a paragraph about *sonnet 18*. ''') sonnet18 = model.Material.by_name(self.name) if not sonnet18: sonnet18 = model.Material( name=self.name, title=u'Sonnet 18 (First Edition)', work=sonnet18_work, ) if not sonnet18.resources: res = model.Resource(locator_type=u'inline', locator=sonnet18_text, format=u'txt') sonnet18.resources.append(res) assert len(sonnet18_work.materials) == 1 model.Session.commit() sonnet18.content = sonnet18_text return sonnet18
def test_get_stream_cache(self): import shakespeare.cache cache = shakespeare.cache.default sometext = u'baa baa' path = u'testcache.txt' cache.save(path, sometext) res = model.Resource(locator=path, locator_type=u'cache') out = res.get_stream() assert out.read() == sometext
def setup_class(self): self.name = u'test-123' self.title = u'Hamlet' work = model.Work(name=self.name, title=self.title) text = model.Material( title=self.title, work=work, ) resource = model.Resource( material=text, locator_type=u'package', locator=u'shksprdata::/gutenberg/phoenix_and_the_turtle_gut.txt', format=u'txt', ) model.Session.commit() self.workid = work.id self.textid = text.id self.resourceid = resource.id model.Session.remove()
def test_get_stream_inline(self): sometext = u'aaaaaaaaaaaaa' res = model.Resource(locator=sometext, locator_type=u'inline') out = res.get_stream() assert out.read() == sometext