#1223: notifications for suggestions
[wolnelektury.git] / apps / piston / doc.py
index 63f89ec..fc14add 100644 (file)
@@ -15,14 +15,14 @@ def generate_doc(handler_cls):
     """
     if not type(handler_cls) is handler.HandlerMetaClass:
         raise ValueError("Give me handler, not %s" % type(handler_cls))
-        
+
     return HandlerDocumentation(handler_cls)
-    
+
 class HandlerMethod(object):
     def __init__(self, method, stale=False):
         self.method = method
         self.stale = stale
-        
+
     def iter_args(self):
         args, _, _, defaults = inspect.getargspec(self.method)
 
@@ -36,34 +36,34 @@ class HandlerMethod(object):
                 yield (arg, str(defaults[-didx]))
             else:
                 yield (arg, None)
-        
+
     @property
     def signature(self, parse_optional=True):
         spec = ""
 
         for argn, argdef in self.iter_args():
             spec += argn
-            
+
             if argdef:
                 spec += '=%s' % argdef
-            
+
             spec += ', '
-            
+
         spec = spec.rstrip(", ")
-        
+
         if parse_optional:
             return spec.replace("=None", "=<optional>")
-            
+
         return spec
-        
+
     @property
     def doc(self):
         return inspect.getdoc(self.method)
-    
+
     @property
     def name(self):
         return self.method.__name__
-    
+
     @property
     def http_name(self):
         if self.name == 'read':
@@ -74,21 +74,21 @@ class HandlerMethod(object):
             return 'DELETE'
         elif self.name == 'update':
             return 'PUT'
-    
+
     def __repr__(self):
         return "<Method: %s>" % self.name
-    
+
 class HandlerDocumentation(object):
     def __init__(self, handler):
         self.handler = handler
-        
+
     def get_methods(self, include_default=False):
         for method in "read create update delete".split():
             met = getattr(self.handler, method, None)
 
             if not met:
                 continue
-                
+
             stale = inspect.getmodule(met) is handler
 
             if not self.handler.is_anonymous:
@@ -97,64 +97,64 @@ class HandlerDocumentation(object):
             else:
                 if not stale or met.__name__ == "read" \
                     and 'GET' in self.allowed_methods:
-                    
+
                     yield HandlerMethod(met, stale)
-        
+
     def get_all_methods(self):
         return self.get_methods(include_default=True)
-        
+
     @property
     def is_anonymous(self):
         return handler.is_anonymous
 
     def get_model(self):
         return getattr(self, 'model', None)
-            
+
     @property
     def has_anonymous(self):
         return self.handler.anonymous
-            
+
     @property
     def anonymous(self):
         if self.has_anonymous:
             return HandlerDocumentation(self.handler.anonymous)
-            
+
     @property
     def doc(self):
         return self.handler.__doc__
-    
+
     @property
     def name(self):
         return self.handler.__name__
-    
+
     @property
     def allowed_methods(self):
         return self.handler.allowed_methods
-    
+
     def get_resource_uri_template(self):
         """
         URI template processor.
-        
+
         See http://bitworking.org/projects/URI-Templates/
         """
         def _convert(template, params=[]):
             """URI template converter"""
             paths = template % dict([p, "{%s}" % p] for p in params)
             return u'%s%s' % (get_script_prefix(), paths)
-        
+
         try:
             resource_uri = self.handler.resource_uri()
-            
+
             components = [None, [], {}]
 
             for i, value in enumerate(resource_uri):
                 components[i] = value
-        
+
             lookup_view, args, kwargs = components
             lookup_view = get_callable(lookup_view, True)
 
             possibilities = get_resolver(None).reverse_dict.getlist(lookup_view)
-            
+
             for possibility, pattern in possibilities:
                 for result, params in possibility:
                     if args:
@@ -167,9 +167,9 @@ class HandlerDocumentation(object):
                         return _convert(result, params)
         except:
             return None
-        
+
     resource_uri_template = property(get_resource_uri_template)
-    
+
     def __repr__(self):
         return u'<Documentation for "%s">' % self.name
 
@@ -180,16 +180,16 @@ def documentation_view(request):
     """
     docs = [ ]
 
-    for handler in handler_tracker: 
+    for handler in handler_tracker:
         docs.append(generate_doc(handler))
 
-    def _compare(doc1, doc2): 
+    def _compare(doc1, doc2):
        #handlers and their anonymous counterparts are put next to each other.
        name1 = doc1.name.replace("Anonymous", "")
        name2 = doc2.name.replace("Anonymous", "")
-       return cmp(name1, name2)    
+       return cmp(name1, name2)
+
     docs.sort(_compare)
-       
-    return render_to_response('documentation.html', 
+
+    return render_to_response('documentation.html',
         { 'docs': docs }, RequestContext(request))