import sys from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response def trim(docstring): if not docstring: return '' # Convert tabs to spaces (following the normal Python rules) # and split into a list of lines: lines = docstring.expandtabs().splitlines() # Determine minimum indentation (first line doesn't count): indent = sys.maxint for line in lines[1:]: stripped = line.lstrip() if stripped: indent = min(indent, len(line) - len(stripped)) # Remove indentation (first line is special): trimmed = [lines[0].strip()] if indent < sys.maxint: for line in lines[1:]: trimmed.append(line[indent:].rstrip()) # Strip off trailing and leading blank lines: while trimmed and not trimmed[-1]: trimmed.pop() while trimmed and not trimmed[0]: trimmed.pop(0) # Return a single string: return '\n'.join(trimmed) class ApiActions(dict): def __init__(self): def api(request): ''' returns list of all known api action return {'status': {'code': int, 'text': string}, 'data': {actions: ['api', 'hello', ...]}} ''' actions = self.keys() actions.sort() return render_to_json_response(json_response({'actions': actions})) self['api'] = api def apidoc(request): ''' returns array of actions with documentation ''' actions = self.keys() actions.sort() docs = {} for f in actions: docs[f] = self.doc(f) return render_to_json_response(json_response({'actions': docs})) self['apidoc'] = apidoc def doc(self, f): return trim(self[f].__doc__) def register(self, method, action=None): if not action: action = method.func_name self[action] = method def unregister(self, action): if action in self: del self[action] actions = ApiActions()