def test_annotation_transformation(): ext = AnnotationExtension() ctx = Context() result = ext.transform(ctx, endpoint, 1134) assert result == ('int', 1134)
def test_annotation_method(): ext = AnnotationExtension() ctx = Context() args = [] kwargs = {'a': '27', 'b': '42'} ext.mutate(ctx, Endpoint().endpoint, args, kwargs) assert kwargs == {'a': 27, 'b': 42}
def test_annotation_positional(): ext = AnnotationExtension() ctx = Context() args = ['27', '42'] kwargs = {} ext.mutate(ctx, endpoint, args, kwargs) assert args == [27, 42] assert kwargs == {}
def test_annotation_bare(): ext = AnnotationExtension() ctx = Context() args = [] kwargs = {'a': '27', 'b': '42'} ext.mutate(ctx, bare_endpoint, args, kwargs) assert kwargs == {'a': '27', 'b': '42'} assert ext.transform(ctx, bare_endpoint, None) is None
from web.ext.annotation import AnnotationExtension # Built-in to WebCore. from web.ext.debug import DebugExtension from web.ext.serialize import SerializationExtension # New in 2.0.3! from web.ext.db import DatabaseExtension # From external dependency: web.db # Get a reference to our database connection adapter. from web.db.mongo import MongoDBConnection # From extenral dependency: marrow.mongo # Get a reference to our Wiki root object. from web.app.wiki.root import Wiki # This is our WSGI application instance. app = Application( Wiki, extensions=[ # Extensions that are always enabled. AnnotationExtension( ), # Allows us to use Python 3 function annotations. SerializationExtension( ), # Allows the return of mappings from endpoints, transformed to JSON. DatabaseExtension(MongoDBConnection("mongodb://localhost/test")), ] + ([ # Extensions that are only enabled in development or testing environments. DebugExtension( ) # Interactive traceback debugger, but gives remote code execution access. ] if __debug__ else [])) # If we're run as the "main script", serve our application over HTTP. if __name__ == "__main__": app.serve('wsgiref')
# Enable the use of cinje templates. __import__('cinje') # Doing it this way prevents an "imported but unused" warning. from web.core import Application # Get references to web framework extensions. from web.ext.annotation import AnnotationExtension from web.ext.debug import DebugExtension from web.ext.serialize import SerializationExtension from web.ext.db import DatabaseExtension # Get a reference to our database connection adapter. from web.db.mongo import MongoDBConnection # Get a reference to our WCMDB root object. from web.app.wcmdb.root import Wcmdb app = Application(Wcmdb, extensions=[ AnnotationExtension(), DebugExtension(), SerializationExtension(), DatabaseExtension(MongoDBConnection("mongodb://localhost/test")), ]) if __name__ == "__main__": app.serve('wsgiref')
return dict(answer=a * b) class SampleExtension: """Here's an example of how to catch an annotation like this as a view handler.""" def start(self, context): context.view.register(tuple, self.render_json) def render_json(self, context, result): # Bail out if this isn't a 2-tuple, or isn't intended for JSON serialization. # This is an informal protocol shared with the more complete `web.template` package and possibly others. if len(result) != 2 or result[0] != 'json': return resp = context.response resp.content_type = 'application/json' resp.encoding = 'utf-8' resp.text = dumps(result[1]) return True if __name__ == '__main__': from web.core import Application from web.ext.annotation import AnnotationExtension Application(Root, extensions=[SampleExtension(), AnnotationExtension()]).serve('wsgiref')