Skip to content

Instantly share code, notes, and snippets.

@cdent
Created July 21, 2009 16:20
Show Gist options
  • Save cdent/151440 to your computer and use it in GitHub Desktop.
Save cdent/151440 to your computer and use it in GitHub Desktop.
"""
Play around with getting stuff out of the store.
This is probably best described as "the new way"
since much of my code doesn't do it this way.
I'm trying to toe a line here with the correct
use of a control, which tries to help remove
some duplication and centralize the controlling of
bag and recipe contents and processing somewhere
other than bag and recipe objects (so they can be
close to being pure data).
"""
from tiddlyweb.config import config
from tiddlywebplugins import get_store
from tiddlyweb import control
from tiddlyweb.model.bag import Bag
from tiddlyweb.model.recipe import Recipe
from tiddlyweb.model.tiddler import Tiddler
# Include because it makes testing the recipe output
# easier. Ordering of tiddlers in a recipe is not guaranteed.
from tiddlyweb.filters.sort import sort_by_attribute
def get_bags_tiddlers_gen(bag_name):
store = get_store(config)
bag = Bag(bag_name)
bag = store.get(bag)
# It's frequently the case that you want
# the list of tiddlers in a bag by title,
# but not any other info. If bag.store is
# a Store, then the below gets tiddler data,
# otherwise it just returns Tiddler's with
# bag and title set.
return control.get_tiddlers_from_bag(bag)
def get_recipes_tiddlers_gen(recipe_name):
store = get_store(config)
recipe = Recipe(recipe_name)
recipe = store.get(recipe)
# Tiddlers from a recipe have not made it to
# generator land yet, because they do some
# uniquifying, so get_tiddlers_from_recipe is a list for now.
# We make a generator so it is like above (but see below).
# XXX would like to fix this stuff.
return (tiddler for tiddler in control.get_tiddlers_from_recipe(recipe))
def make_data():
store = get_store(config)
for bag_id in [1,2,3,4]:
bag_name = 'bag%s' % bag_id
bag = Bag(bag_name)
store.put(bag)
for tiddler_id in [1,2,3,4]:
tiddler = Tiddler('tiddler%s.%s' % (bag_id, tiddler_id), bag_name)
store.put(tiddler)
recipe = Recipe('recipe')
recipe.set_recipe([
['bag1', ''],
['bag3', ''],
])
store.put(recipe)
if __name__ == '__main__':
make_data()
x = get_bags_tiddlers_gen('bag1')
assert x.next().title == 'tiddler1.1'
assert x.next().title == 'tiddler1.2'
assert x.next().title == 'tiddler1.3'
assert x.next().title == 'tiddler1.4'
# XXX sort_by_attribute still returning a list
tiddlers = sort_by_attribute('title', get_recipes_tiddlers_gen('recipe'))
assert tiddlers[0].title == 'tiddler1.1'
assert tiddlers[-1].title == 'tiddler3.4'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment