No project description provided
Project description
A library of resource tree helps targeted at the pyramid framework.
Constructing a resource tree
Pyramid when seeking to find an context object in a resource tree takes a URL such as /books/bible and atempts to traverse a root object treating the root object as tree of mappings. So the above URL resolves in the context object of root["books"]["bible"]
ctq aids in the construction of these a resource trees by being able to decorate factory methods mapping them to item looksup:
>>> from ctq import Resourceful, resource >>> class Root(Resourceful): ... ... @resource('books') ... def get_books(self): ... return Shelf() >>> class Shelf(Resourceful): ... ... @resource("bible") ... def get_bible(self): ... return Bible() >>> class Bible(object): ... pass
Resourceful is a class that is intended to be mixed into resource objects with gives a mapping behavior for methods that are decorated with @resource:
>>> root = Root() >>> root["books"]["bible"] <Bible object at ...>
Resourceful implements caching so subsiquent look ups of the same object result in the same object being returned.:
>>> a = root["books"] >>> b = root["books"] >>> a is b True
The resource decorator adds __parent__ and __name__ attributes which pyramid expects for its traversal operations.:
>>> books = root["books"] >>> books.__name__ 'books' >>> books.__parent__ <Root object at ...>
Additionally if the factory fucntion returns an object with the parent and name attributes already defined then it will not override them. This allows sym-links within the traversal tree.:
>>> class LinkExample(Resourceful): ... ... @resource('shelf-1') ... def get_shelf1(self): ... return Shelf() ... ... @resource('shelf-2') ... def get_shelf2(self): ... return Shelf() ... ... @resource('shelf-default') ... def get_shelf_default(self): ... return self['shelf-1'] >>> root2 = LinkExample() >>> root2['shelf-1'].__name__ 'shelf-1' >>> root2['shelf-2'].__name__ 'shelf-2' >>> root2['shelf-default'].__name__ 'shelf-1'
Event Handling
cqt provides a function emit(target: Any, name: str, data: Optional[dict]) which creates an event object, searches and calls event handlers starting with the target and bubbeling up to the root of a resource tree.
Event handlers can be declared using the @handle(*event_names, priority: Optional[int]) decorator on an instance method which accepts the paramiter event.
For example:
>>> from ctq import handle >>> from ctq import emit >>> class EventTreeRoot(object): ... ... @handle('after-edit') ... def on_after_edit(self, event): ... print("Was very edited.") >>> class Foo(object): ... ... @handle('after-edit') ... def on_after_edit(self, event): ... print("Was edited.") >>> foo = Foo() >>> foo.__parent__ = EventTreeRoot() >>> emit(foo, 'after-edit') Was edited. Was very edited.
Workflow behavior
cqt provides a Workflowable class to add workflow behavior to objects this adds some methods that are handy in performing workflow state transitions on objects including emmiting events and guarding agenst illegal transitions.
The current workflow is get/set on the property self.workflow_state and transitions are defined in the property self.workflow_transitions. Events emitted that the form workflow-before-ACTION and workflow-after-ACTION
For example:
>>> from ctq import Workflowable >>> class Document(Workflowable): ... ... workflow_state = "new" ... ... workflow_transitions = { ... "submit-for-review": {"from": ["new"], "to": "pending-review"}, ... "publish": {"from": ["new", "pending-review"], "to": "public"}, ... "retract": {"from": ["public"], "to": "retracted"}, ... } ... ... @handle("workflow-before-publish") ... def on_before_publish(self, event): ... print("about to publish") ... ... @handle("workflow-after-publish") ... def on_after_publish(self, event): ... print("published!")
To action a workflow transition calle the self.workflow_action(action) method:
>>> doc = Document() >>> doc.workflow_state 'new' >>> doc.workflow_action("publish") about to publish published! >>> doc.workflow_state 'public' >>> doc.workflow_action("publish") Traceback (most recent call last): ... ctq.workflow.WorkflowIllegalTransitionError: Can not publish on an instance of Document in the state public
Helper methods
Additionally there are some functions that enable performing verious tasks around the tree
Traversing back up the tree with traverse_up(obj):
>>> from ctq import traverse_up >>> context = root["books"]["bible"] >>> list(traverse_up(context)) [<Bible object at ...>, <Shelf object at ...>, <Root object at ...>]
Getting the root object with get_root(obj):
>>> from ctq import get_root >>> get_root(context) <Root object at ...>
Getting the path names of an object with resource_path_names(obj):
>>> from ctq import resource_path_names >>> resource_path_names(context) ('', 'books', 'bible')
Use acquisition using with acquire(obj):
>>> from ctq import acquire >>> root.site_name = "Small room with lots of books" >>> acquire(context).site_name 'Small room with lots of books'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ctq-0.1.tar.gz
.
File metadata
- Download URL: ctq-0.1.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e1460f39f2f5bd9b3bc310a6fddcad300b973814bb2c87682203b7df0ba91a5 |
|
MD5 | 20e9a251e1b1afa7d82acd99c74edfb8 |
|
BLAKE2b-256 | 3093b551908db85b3cd6ae66016ec4d67bcbdd51e43b3c409ce64159bfab742f |
File details
Details for the file ctq-0.1-py3-none-any.whl
.
File metadata
- Download URL: ctq-0.1-py3-none-any.whl
- Upload date:
- Size: 16.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 592abfc27583f5d8d989d3ad57c9a3b8c69ed7a49ec868de68f132429c22963a |
|
MD5 | a5137c4f30a1f028e5a916939a2910c4 |
|
BLAKE2b-256 | 19c497527abcab99120688f8c235d17c2a1e542f86bbba0ca31db1dfaec5c8a4 |