~intellectronica/lazr.restful/unicode-bug-438802

« back to all changes in this revision

Viewing changes to src/lazr/restful/simple.py

  • Committer: Leonard Richardson
  • Date: 2009-09-02 19:37:43 UTC
  • mfrom: (66.2.14 grok)
  • Revision ID: leonard.richardson@canonical.com-20090902193743-n9dijkeifk6ojhc4
[r=gary] Use grok to define web service configuration classes more idiomatically.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
__metaclass__ = type
4
4
__all__ = [
 
5
    'BaseWebServiceConfiguration',
5
6
    'IMultiplePathPartLocation',
6
 
    'make_configuration',
7
 
    'make_configuration_superclass',
8
7
    'MultiplePathPartAbsoluteURL',
9
8
    'Publication',
10
9
    'PublicationMixin',
349
348
    __call__ = __str__
350
349
 
351
350
 
352
 
def make_simple_configuration(
353
 
    attributes, request_class=Request, publication_class=Publication,
354
 
    class_name="WebServiceConfiguration"):
355
 
    """Return a class that implements all of IWebServiceConfiguration.
356
 
 
357
 
    The generated class will define all the attributes and have simple
358
 
    implementations of the methods specified in
359
 
    IWebServiceConfiguration. If you need different implementations of
360
 
    the methods, define your own class and pass it into
361
 
    make_configuration as 'superclass'.
362
 
    """
363
 
    attributes = dict(attributes)
364
 
    if (not 'createRequest' in attributes and request_class is not None
365
 
        and publication_class is not None):
366
 
        def createRequest(self, body_instream, environ):
367
 
            """See `IWebServiceConfiguration`."""
368
 
            request = request_class(body_instream, environ)
369
 
            service_root_object = getUtility(IServiceRootResource)
370
 
            request.setPublication(publication_class(service_root_object))
371
 
            return request
372
 
        attributes['createRequest'] = createRequest
373
 
 
374
 
    if not 'get_request_user' in attributes:
375
 
        def get_request_user(self):
376
 
            return None
377
 
        attributes['get_request_user'] = attributes
378
 
 
379
 
    return make_configuration(attributes, class_name=class_name)
380
 
 
381
 
 
382
 
def make_configuration(attributes, superclass=object,
383
 
                       class_name="WebServiceConfiguration"):
384
 
    """Return an IWebServiceConfiguration implementation.
385
 
 
386
 
    :param superclass: The superclass to use.
387
 
 
388
 
    :param attributes: A dict of attribute values and method
389
 
    definitions. This will be combined with the default values for
390
 
    attributes defined in IWebServiceConfiguration.
391
 
 
392
 
    Either 'superclass' must implement the methods defined in
393
 
    IWebServiceConfiguration, or they must be provided in
394
 
    'attributes'. Otherwise, your class won't work.
395
 
    """
396
 
    return implement_from_dict(
397
 
        class_name, IWebServiceConfiguration, attributes, superclass)
 
351
BaseWebServiceConfiguration = implement_from_dict(
 
352
    "BaseWebServiceConfiguration", IWebServiceConfiguration, {}, object)