跳转到主要内容

一些核心transmogrifier蓝图和基类

项目描述

提供transmogrifier蓝图基类和几个有用的蓝图。

蓝图基类

collective.blueprint.base包提供了清晰的、易于使用的transmogrifier蓝图基类。

更改键

对于添加或更改项目键的蓝图,使其使用的键可配置非常有用。

getKeys函数将作为关键字参数传递的键和值转换为字典,键为选项中指定的键(如果存在)。

>>> from collective.blueprint.base import keys
>>> options = {'foo-key': 'baz'}
>>> sorted(keys.getKeys(
...     options, foo='bar', bah='qux').iteritems())
[('bah', 'qux'), ('baz', 'bar')]

提供了一个基类,可以轻松创建易于阅读的蓝图,该蓝图将键添加到项目或更改遵守选项中任何键名的键。

>>> from collective.blueprint.base import blueprint
>>> class FooBlueprint(blueprint.KeyChanger):
...     keys = ('foo', 'bah')
...     def processItem(self, item):
...         return dict(foo='bar', bah='qux')
>>> from collective.blueprint.base import testing
>>> transmogrifier = testing.Transmogrifier()
>>> previous = ({'other': 'stuff'},)
>>> foo_blueprint = FooBlueprint(
...     transmogrifier, 'foosection', options, previous)
>>> item, = foo_blueprint
>>> sorted(item.iteritems())
[('bah', 'qux'), ('baz', 'bar'), ('other', 'stuff')]

源蓝图本质上就是键更改器。它们不是将键添加到管道更高处的项目,而是生成项目。源基类将上述行为扩展到这种用法模式。

>>> class BarBlueprint(blueprint.Source):
...     keys = ('foo', 'bah')
...     def getItems(self):
...         yield dict(foo='bar', bah='qux')
...         yield dict(foo='bar2', bah='qux2')
>>> previous = ({'other': 'stuff'},)
>>> bar_blueprint = BarBlueprint(
...     transmogrifier, 'barsection', options, previous)
>>> other, first, second = bar_blueprint
>>> other
{'other': 'stuff'}
>>> sorted(first.iteritems())
[('bah', 'qux'), ('baz', 'bar')]
>>> sorted(second.iteritems())
[('bah', 'qux2'), ('baz', 'bar2')]

使用键

对于访问项目中的键并对其进行某种处理的蓝图,transmogrifier提供支持匹配器,该匹配器将根据优先级策略访问项目上的键。

makeMatchers函数一次处理多个键。

>>> options = {'blueprint': 'foo.blueprint', 'bah-key': 'bar'}
>>> matchers = keys.makeMatchers(
...     options, 'barsection', 'baz', 'bah', blah=('qux', 'quux'))
>>> sorted(matchers.iteritems())
[('bah', <collective.transmogrifier.utils.Matcher object at ...>),
 ('baz', <collective.transmogrifier.utils.Matcher object at ...>),
 ('blah', <collective.transmogrifier.utils.Matcher object at ...>)]
>>> item = {'_baz': 'baz-value',
...         'bar': 'bah-value',
...         'quux': 'blah-value'}
>>> matchers['baz'](*item)
('_baz', True)
>>> matchers['bah'](*item)
('bar', True)
>>> matchers['blah'](*item)
('quux', True)

为了使实现蓝图更容易、更清晰,提供了一个基类,允许蓝图作者只关注他们需要的项目中的键。

>>> class BazKeyUser(blueprint.KeyUser):
...     keys = ('baz', 'bah')
...     extras = dict(blah=('qux', 'quux'))
...     def processItem(self, item, baz, bah, blah):
...         print baz, bah, blah
>>> previous = (item,)
>>> baz_blueprint = BazKeyUser(
...     transmogrifier, 'bazsection', options, previous)
>>> only, = baz_blueprint
baz-value bah-value blah-value
>>> sorted(only.iteritems())
[('_baz', 'baz-value'),
 ('bar', 'bah-value'),
 ('quux', 'blah-value')]

如果所需的选项不为True,则KeyUser可以跳过没有所有匹配器键的项目。

>>> class QuxKeyUser(blueprint.KeyUser):
...     keys = ('baz', 'bah', 'foo')
...     extras = dict(blah=('qux', 'quux'))
...     def processItem(self, item, baz, bah, blah, foo):
...         print baz, bah, blah
>>> options['required'] = 'False'
>>> previous = (item,)
>>> qux_blueprint = QuxKeyUser(
...     transmogrifier, 'quxsection', options, previous)
>>> only, = qux_blueprint
>>> sorted(only.iteritems())
[('_baz', 'baz-value'),
 ('bar', 'bah-value'),
 ('quux', 'blah-value')]

删除蓝图

collective.blueprint.base.delete transmogrifier蓝图可以用来创建删除项目路径上现有对象的管道部分。

某些对象在运行transmogrifier之前存在,而另一些则不存在。

>>> hasattr(folder, 'foo')
True
>>> folder.foo
<File at /test_folder_1_/foo>
>>> hasattr(folder, 'bar')
False

组装并注册一个带有删除部分的transmogrifier。

>>> deleter = """
... [transmogrifier]
... pipeline =
...     foosource
...     barsource
...     deleter
...     printer
...
... [foosource]
... blueprint = collective.blueprint.base.configsource
... _path = /foo
...
... [barsource]
... blueprint = collective.blueprint.base.configsource
... _path = /bar
...
... [deleter]
... blueprint = collective.blueprint.base.deleter
...
... [printer]
... blueprint = collective.transmogrifier.sections.tests.pprinter
... """
>>> registerConfig(
...     u'collective.blueprint.base.delete.testing.deleter',
...     deleter)

运行转换器。蓝图会忽略指向不存在对象的路径项。

>>> from collective.transmogrifier import transmogrifier
>>> transmogrifier.Transmogrifier(folder)(
...     u'collective.blueprint.base.delete.testing.deleter')
{'_path': '/foo'}
{'_path': '/bar'}

对象已被删除。

>>> hasattr(folder, 'foo')
False
>>> hasattr(folder, 'bar')
False

部分配置源

可以使用集体蓝图.base.configsource转换器蓝本来将配置部分中的键值注入到管道中。

组装并注册一个带有删除部分的transmogrifier。

>>> configsource = """
... [transmogrifier]
... pipeline =
...     configsource
...     printer
...
... [configsource]
... blueprint = collective.blueprint.base.configsource
... configsource-lists = baz qux
... foo =
...     bar blah
... baz =
...     bah
... qux =
...     quux
...     foo bar
...     baz blah
...
... [printer]
... blueprint = collective.transmogrifier.sections.tests.pprinter
... """
>>> registerConfig(
...     u'collective.blueprint.base.delete.testing.configsource',
...     configsource)

运行转换器。注入与配置部分内容对应的项。所有值都会去除空白。在configsource-lists变量中列出的变量名将在换行符处拆分成列表。

>>> transmogrifier(
...     u'collective.blueprint.base.delete.testing.configsource')
{'qux': ['quux', 'foo bar', 'baz blah'],
 'foo': 'bar blah',
 'baz': ['bah']}

键分割器

集体蓝图.base.keysplitter转换器蓝本可以用来从原始项的一个键中插入任意数量的项到管道中。例如,当需要根据上游项中的键对任意数量的对象进行约束(或其他管道操作)时,这可能很有用。

用于迭代和插入新项的值将按顺序从以下位置检索:_collective.blueprint.base.keysplitter_[sectionname]_keysplitter_collective.blueprint.base.keysplitter_keysplitter_[sectionname]_keysplitter_keysplitter,其中[sectionname]将被当前部分的名称替换。如果需要,这允许您精确地定位正确的部分。或者,您可以通过指定keysplitter-key选项来指定用于keysplitter的键,该选项应是一个要尝试的键列表(每行一个键,使用re:regexp:前缀来指定正则表达式)。

可以使用“pipeline”选项指定仅对新项运行的章节列表。原始项将不会通过此管道。除非设置了“include”选项,否则新项将不会通过起源管道的其余部分。

默认情况下,新项将插入到管道中,在原始项产出之前。当需要使用新项的处理结果来完成原始项的处理时,这很有用。例如,可能需要根据键值创建几个对象,以便为原始项创建的对象可以设置对这些对象的引用。然而,如果原始项必须在处理新项之前处理,则设置keysplitter部分中的“after”选项会导致新项在原始项产出和处理后插入。

组装并注册一个带有关键拆分器的转换器。

>>> keysplitter = """
... [transmogrifier]
... pipeline =
...     initsource
...     keysplitter
...     qux
...     printer
...
... [initsource]
... blueprint = collective.blueprint.base.configsource
... configsource-lists = _keysplitter
... _keysplitter =
...     bar
...     baz
...
... [keysplitter]
... blueprint = collective.blueprint.base.keysplitter
... pipeline =
...     foo
...     printer
...
... [foo]
... blueprint = collective.transmogrifier.sections.inserter
... key = string:foo
... value = item/_keysplitter
...
... [qux]
... blueprint = collective.transmogrifier.sections.inserter
... key = string:qux
... value = item/_keysplitter
...
... [printer]
... blueprint = collective.transmogrifier.sections.tests.pprinter
... """
>>> registerConfig(
...     u'collective.blueprint.base.keysplitter.testing',
...     keysplitter)

运行转换器。一个带有两个针对keysplitter部分的值的项被插入到管道中。当此项到达keysplitter部分时,将迭代‘_keysplitter’键中的值以插入新项。

>>> transmogrifier(
...     u'collective.blueprint.base.keysplitter.testing')
{'_keysplitter': 'bar', 'foo': 'bar'}
{'_keysplitter': 'baz', 'foo': 'baz'}
{'_keysplitter': ['bar', 'baz'], 'qux': ['bar', 'baz']}

此转换器使用after和include选项,并且不包含子管道。

>>> keysplitter = """
... [transmogrifier]
... pipeline =
...     initsource
...     keysplitter
...     printer
...
... [initsource]
... blueprint = collective.blueprint.base.configsource
... configsource-lists = _keysplitter
... _keysplitter =
...     bar
...     baz
...
... [keysplitter]
... blueprint = collective.blueprint.base.keysplitter
... after = True
... include = True
...
... [printer]
... blueprint = collective.transmogrifier.sections.tests.pprinter
... """
>>> registerConfig(
...     u'collective.blueprint.base.keysplitter.testing2',
...     keysplitter)
>>> transmogrifier(
...     u'collective.blueprint.base.keysplitter.testing2')
{'_keysplitter': ['bar', 'baz']}
{'_keysplitter': 'bar'}
{'_keysplitter': 'baz'}

递归分割器

可以使用集体蓝图.base.recurser转换器蓝本向管道添加递归。

要递归的值将按以下顺序从以下位置检索:_collective.blueprint.base.recurser_[sectionname]_recurser_collective.blueprint.base.recurser_recurser_[sectionname]_recurser_recurser,其中[sectionname]将被当前部分的名称替换。如果需要,这允许您精确地定位正确的部分。或者,您可以通过指定recurser-key选项来指定用于recurser的键,该选项应是一个要尝试的键列表(每行一个键,使用re:regexp:前缀来指定正则表达式)。

“管道”选项用于指定一个将递归运行的章节列表。默认情况下,递归项目将在原始项目产生后插入到管道中。但是,如果设置了“before”选项,递归项目将在原始项目产生和处理后插入。

当递归到一个值时,该值将被插入到“key”选项指定的键下的项中。递归管道负责处理“key”值,并在适合递归时插入“_recurser”键。

使用列表拆分部分组装并注册一个转换器。

>>> recurser = """
... [transmogrifier]
... pipeline =
...     initsource
...     inserter
...     recurser
...     printer
...
... [initsource]
... blueprint = collective.blueprint.base.configsource
...
... [inserter]
... blueprint = collective.transmogrifier.sections.inserter
... key = string:foo
... value = python:[['bar', 'baz'], ['qux', 'quux']]
...
... [recurser]
... blueprint = collective.blueprint.base.recurser
... key = foo
... pipeline = foo
...
... [foo]
... blueprint = collective.transmogrifier.sections.inserter
... condition = python:isinstance(item['foo'], list)
... key = string:_recurser
... value = item/foo
...
... [printer]
... blueprint = collective.transmogrifier.sections.tests.pprinter
... """
>>> registerConfig(
...     u'collective.blueprint.base.recurser.testing',
...     recurser)

运行转换器。

>>> transmogrifier(
...     u'collective.blueprint.base.recurser.testing')
{'_recurser': [['bar', 'baz'], ['qux', 'quux']],
 'foo': [['bar', 'baz'], ['qux', 'quux']]}
{'_recurser': ['bar', 'baz'], 'foo': ['bar', 'baz']}
{'foo': 'bar'}
{'foo': 'baz'}
{'_recurser': ['qux', 'quux'], 'foo': ['qux', 'quux']}
{'foo': 'qux'}
{'foo': 'quux'}

此转换器使用“before”选项。

>>> recurser = """
... [transmogrifier]
... pipeline =
...     initsource
...     inserter
...     recurser
...     printer
...
... [initsource]
... blueprint = collective.blueprint.base.configsource
...
... [inserter]
... blueprint = collective.transmogrifier.sections.inserter
... key = string:foo
... value = python:[['bar', 'baz'], ['qux', 'quux']]
...
... [recurser]
... blueprint = collective.blueprint.base.recurser
... key = foo
... pipeline = foo
... before = True
...
... [foo]
... blueprint = collective.transmogrifier.sections.inserter
... condition = python:isinstance(item['foo'], list)
... key = string:_recurser
... value = item/foo
...
... [printer]
... blueprint = collective.transmogrifier.sections.tests.pprinter
... """
>>> registerConfig(
...     u'collective.blueprint.base.recurser.testing2',
...     recurser)
>>> transmogrifier(
...     u'collective.blueprint.base.recurser.testing2')
{'foo': 'bar'}
{'foo': 'baz'}
{'_recurser': ['bar', 'baz'], 'foo': ['bar', 'baz']}
{'foo': 'qux'}
{'foo': 'quux'}
{'_recurser': ['qux', 'quux'], 'foo': ['qux', 'quux']}
{'_recurser': [['bar', 'baz'], ['qux', 'quux']],
 'foo': [['bar', 'baz'], ['qux', 'quux']]}

变更日志

1.0 - 2010-04-23

  • 初始版本

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分发

collective.blueprint.base-1.0.tar.gz (13.2 kB 查看散列值)

上传时间

由以下支持