def test_sources_lazy(): ''' Demonstration of ways to return 'lazy' and generally more advanced sources Lazy sources could be useful to do some conditional magic or make more defensive against imports, excra configuration. You'll know when you need it ;) ''' cfg = make(''' from promnesia import Source def lazy(): from promnesia.sources import demo print("Hello, I'm so lazy...") yield from demo.index() SOURCES = [ lazy, ] ''') srcs = [s if isinstance(s, Source) else throw(s) for s in cfg.sources] [s] = srcs assert s.name == 'cfg' # TODO this should be fixed... but not a big deal index(cfg)
def test_url_in_properties() -> None: items = [ v if isinstance(v, Visit) else throw(v) for v in extract_from_file(tdata('auto/orgs/file4.org')) ] assert len(items) == 2, items assert items[0].url == 'https://example.org/ref_example' assert items[1].url == 'http://example.org/a_test'
def test_org_indexer_2() -> None: items = [ v if isinstance(v, Visit) else throw(v) for v in extract_from_file(tdata('auto/orgs/file3.org')) ] assert len(items) == 6 assert items[ 0].url == 'https://www.reddit.com/r/androidapps/comments/4i36z9/how_you_use_your_android_to_the_maximum/d2uq24i' assert items[1].url == 'https://link.com' assert items[-2].url == 'https://en.wikipedia.org/wiki/Resilio_Sync'
def test_heading() -> None: items = [ v if isinstance(v, Visit) else throw(v) for v in extract_from_file(tdata('auto/orgs/file2.org')) ] assert {i.url for i in items} == { 'https://en.wikipedia.org/wiki/Computational_topology', 'http://graphics.stanford.edu/courses/cs468-09-fall/', 'https://en.wikipedia.org/wiki/Triangulation_(topology)', 'https://en.wikipedia.org/wiki/Digital_manifold', }
def test_org_indexer() -> None: [_, cpp, cozy] = [ v if isinstance(v, Visit) else throw(v) for v in extract_from_file(tdata('auto/orgs/file.org')) ] assert cpp.url == 'https://www.youtube.com/watch?v=rHIkrotSwcc' # TODO not sure about filetags? assert cpp.context == ''' xxx /r/cpp :cpp:programming: I've enjoyed [Chandler Carruth's _There Are No Zero-cost Abstractions_]( https://www.youtube.com/watch?v=rHIkrotSwcc) very much. '''.lstrip() assert cozy.url == 'https://twitter.com/Mappletons/status/1255221220263563269'
def test_sources_style_more(): ''' Now, sources are not magic -- they are just functions emitting visits ''' cfg = make(''' from typing import Iterable from promnesia.common import Visit, Source, Loc def my_indexer() -> Iterable[Visit]: from datetime import datetime for link in ['reddit.com', 'beepb00p.xyz']: yield Visit( url=link, dt=datetime.min, locator=Loc.make('test'), ) SOURCES = [ # you can just pass the function name here my_indexer, # or give it an explicit name (instead of a guess) Source(my_indexer, name='nice name'), ] class MyIndexer: def index(): from promnesia.sources import demo return list(demo.index()) SOURCES.append( MyIndexer, ) ''') [s1, s2, s3] = [s if isinstance(s, Source) else throw(s) for s in cfg.sources] assert s1.name == 'cfg' # TODO would be nice to guess 'my_indexer' instead... assert s2.name == 'nice name' assert s3.name == 'cfg' # TODO fix it, make MyIndexer? index(cfg)
def test_sources_style() -> None: ''' Testing 'styles' of specifying sources ''' cfg = make(''' from promnesia import Source from promnesia.sources import demo SOURCES = [ # you can pass arguments to index functions Source(demo.index, count=10, name='explicit name'), # or rely on the default argument! Source(demo.index, name='another name'), # or rely on default source name name (will be guessed as 'demo') Source(demo.index), # rely on default index function Source(demo), # no need for Source() either! demo.index, demo, # I guess this is as simple as it possibly gets... 'promnesia.sources.demo', ] ''') srcs = [s if isinstance(s, Source) else throw(s) for s in cfg.sources] [s1, s2, s3, s4, s5, s55, s6] = srcs assert s1.name == 'explicit name' assert s2.name == 'another name' assert s3.name == 'demo' assert s4.name == 'demo' assert s5.name == 'demo' assert s55.name == 'demo' assert s6.name == 'demo' index(cfg)
def it(): vit = (throw(v) if isinstance(v, Exception) else v for v in visits) for k, g in groupby(sorted(vit, key=key), key=key): yield k, list(sorted(g))