<s> import re <EOL> import sys <EOL> import inspect <EOL> import piston3 . handler as handler <EOL> from types import ModuleType <EOL> from resource import CsrfExemptResource <EOL> from handlers import ObjectHandlerMetaClass <EOL> from django . core . urlresolvers import get_resolver , get_callable , get_script_prefix <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . shortcuts import render_to_response <EOL> from django . template import RequestContext <EOL> from django . db import models <EOL> def get_module ( mdl ) : <EOL> if isinstance ( mdl , basestring ) : <EOL> __import__ ( mdl ) <EOL> return sys . modules [ mdl ] <EOL> elif isinstance ( mdl , ModuleType ) : <EOL> return mdl <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def generate_doc ( handler_cls ) : <EOL> """<STR_LIT>""" <EOL> if not ( type ( handler_cls ) is ObjectHandlerMetaClass <EOL> or type ( handler_cls ) is handler . HandlerMetaClass ) : <EOL> raise ValueError ( "<STR_LIT>" % type ( handler_cls ) ) <EOL> return HandlerDocumentation ( handler_cls ) <EOL> def get_field_data_type ( field ) : <EOL> """<STR_LIT>""" <EOL> return field . description % field . __dict__ <EOL> class HandlerMethod ( object ) : <EOL> def __init__ ( self , method , stale = False ) : <EOL> self . method = method <EOL> self . stale = stale <EOL> def iter_args ( self ) : <EOL> args , _ , _ , defaults = inspect . getargspec ( self . method ) <EOL> for idx , arg in enumerate ( args ) : <EOL> if arg in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> continue <EOL> didx = len ( args ) - idx <EOL> if defaults and len ( defaults ) >= didx : <EOL> yield ( arg , str ( defaults [ - didx ] ) ) <EOL> else : <EOL> yield ( arg , None ) <EOL> def get_signature ( self , parse_optional = True ) : <EOL> spec = "<STR_LIT>" <EOL> for argn , argdef in self . iter_args ( ) : <EOL> spec += argn <EOL> if argdef : <EOL> spec += '<STR_LIT>' % argdef <EOL> spec += '<STR_LIT:U+002CU+0020>' <EOL> spec = spec . rstrip ( "<STR_LIT:U+002CU+0020>" ) <EOL> if parse_optional : <EOL> return spec . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return spec <EOL> signature = property ( get_signature ) <EOL> def get_fields ( self ) : <EOL> to_update = False <EOL> doc = inspect . getdoc ( self . method ) <EOL> if not doc and issubclass ( self . method . im_class . model , models . Model ) : <EOL> fields = None <EOL> if self . method . __name__ == '<STR_LIT>' : <EOL> fields = self . method . im_class . fields if self . method . im_class . fields else tuple ( <EOL> attr . name for attr in self . method . im_class . model . _meta . local_fields ) <EOL> elif self . method . __name__ in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> to_update = True <EOL> if hasattr ( self . method . im_class , '<STR_LIT>' ) and hasattr ( self . method . im_class . form , '<STR_LIT>' ) : <EOL> fields = self . method . im_class . form . _meta . fields <EOL> else : <EOL> fields = self . method . im_class . fields <EOL> if fields : <EOL> for field in fields : <EOL> for mfield in self . method . im_class . model . _meta . fields : <EOL> if mfield . name == field : <EOL> yield { '<STR_LIT:name>' : mfield . name , <EOL> '<STR_LIT>' : not mfield . blank if to_update else False , <EOL> '<STR_LIT:type>' : get_field_data_type ( mfield ) , <EOL> '<STR_LIT>' : mfield . verbose_name , <EOL> '<STR_LIT>' : mfield . help_text , } <EOL> break <EOL> def get_doc ( self ) : <EOL> doc = inspect . getdoc ( self . method ) <EOL> if not doc and issubclass ( self . method . im_class . model , models . Model ) : <EOL> if self . method . __name__ == '<STR_LIT>' : <EOL> doc = _ ( <EOL> '<STR_LIT>' ) <EOL> elif self . method . __name__ == '<STR_LIT>' : <EOL> doc = _ ( <EOL> '<STR_LIT>' ) <EOL> elif hasattr ( self . method . im_class , '<STR_LIT>' ) : <EOL> if self . method . __name__ == '<STR_LIT>' : <EOL> doc = _ ( '<STR_LIT>' ) <EOL> elif self . method . __name__ == '<STR_LIT>' : <EOL> doc = _ ( '<STR_LIT>' ) <EOL> return doc <EOL> doc = property ( get_doc ) <EOL> def get_name ( self ) : <EOL> return self . method . __name__ <EOL> name = property ( get_name ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . name <EOL> def _convert ( template , params = None ) : <EOL> """<STR_LIT>""" <EOL> if params is None : <EOL> params = [ ] <EOL> paths = template % dict ( [ p , "<STR_LIT>" % p ] for p in params ) <EOL> return u'<STR_LIT>' % ( get_script_prefix ( ) , paths ) <EOL> class HandlerDocumentation ( object ) : <EOL> def __init__ ( self , handler ) : <EOL> self . handler = handler <EOL> def get_methods ( self , include_default = False ) : <EOL> for method in self . handler . allowed_methods : <EOL> met = getattr ( self . handler , CsrfExemptResource . callmap . get ( method ) ) <EOL> stale = inspect . getmodule ( met ) is handler <EOL> if not self . handler . is_anonymous : <EOL> if met and ( not stale or include_default ) : <EOL> yield HandlerMethod ( met , stale ) <EOL> else : <EOL> if not stale or met . __name__ == "<STR_LIT>" and '<STR_LIT:GET>' in self . allowed_methods : <EOL> yield HandlerMethod ( met , stale ) <EOL> def get_all_methods ( self ) : <EOL> return self . get_methods ( include_default = True ) <EOL> @ property <EOL> def is_anonymous ( self ) : <EOL> return handler . is_anonymous <EOL> def get_model ( self ) : <EOL> return getattr ( self , '<STR_LIT>' , None ) <EOL> def get_doc ( self ) : <EOL> return self . handler . __doc__ <EOL> doc = property ( get_doc ) <EOL> @ property <EOL> def name ( self ) : <EOL> return self . handler . __name__ <EOL> @ property <EOL> def display_name ( self ) : <EOL> name = self . handler . __name__ . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> try : <EOL> pattern = re . compile ( '<STR_LIT>' ) <EOL> name = pattern . sub ( <EOL> lambda m : m . group ( ) [ : <NUM_LIT:1> ] + "<STR_LIT:U+0020>" + m . group ( ) [ <NUM_LIT:1> : ] , name ) <EOL> except : <EOL> pass <EOL> return name <EOL> @ property <EOL> def allowed_methods ( self ) : <EOL> return self . handler . allowed_methods <EOL> def get_resource_uri_template ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> resource_uri = self . handler . resource_uri ( ) <EOL> components = [ None , [ ] , { } ] <EOL> for i , value in enumerate ( resource_uri ) : <EOL> components [ i ] = value <EOL> lookup_view , args , kwargs = components <EOL> lookup_view = get_callable ( lookup_view , True ) <EOL> possibilities = get_resolver ( <EOL> '<STR_LIT>' ) . reverse_dict . getlist ( lookup_view ) <EOL> for possibility , pattern in possibilities : <EOL> for result , params in possibility : <EOL> if args : <EOL> if len ( args ) != len ( params ) : <EOL> continue <EOL> return _convert ( result , params ) <EOL> else : <EOL> if set ( kwargs . keys ( ) ) != set ( params ) : <EOL> continue <EOL> return _convert ( result , params ) <EOL> except : <EOL> return None <EOL> def get_resource_uri_index ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> resource_uri = self . handler . resource_uri ( ) <EOL> components = [ None , [ ] , { } ] <EOL> for i , value in enumerate ( resource_uri ) : <EOL> components [ i ] = value <EOL> lookup_view , args , kwargs = components <EOL> if args or kwargs : <EOL> lookup_view = get_callable ( lookup_view , True ) <EOL> possibilities = get_resolver ( <EOL> '<STR_LIT>' ) . reverse_dict . getlist ( lookup_view ) <EOL> for possibility , pattern in possibilities : <EOL> for result , params in possibility : <EOL> if not params : <EOL> return _convert ( result ) <EOL> except : <EOL> return None <EOL> resource_uri_template = property ( get_resource_uri_template ) <EOL> def __repr__ ( self ) : <EOL> return u'<STR_LIT>' % self . name <EOL> def documentation_view ( request , module ) : <EOL> docs = [ ] <EOL> for name , clsmember in inspect . getmembers ( get_module ( module ) , inspect . isclass ) : <EOL> if issubclass ( clsmember , handler . BaseHandler ) and getattr ( clsmember , '<STR_LIT>' , None ) : <EOL> docs . append ( generate_doc ( clsmember ) ) <EOL> return render_to_response ( '<STR_LIT>' , { '<STR_LIT>' : docs } , RequestContext ( request ) ) </s>
