cablegates/pandora/api/actions.py

105 lines
3.2 KiB
Python
Raw Normal View History

2010-12-22 15:17:38 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import sys
2010-12-22 15:17:38 +00:00
from django.conf import settings
from ox.django.shortcuts import render_to_json_response, json_response
2011-01-14 10:49:48 +00:00
from ox.utils import json
2010-12-22 15:17:38 +00:00
def autodiscover():
#register api actions from all installed apps
from django.utils.importlib import import_module
from django.utils.module_loading import module_has_submodule
for app in settings.INSTALLED_APPS:
if app != 'api':
mod = import_module(app)
try:
import_module('%s.views'%app)
except:
if module_has_submodule(mod, 'views'):
2011-01-01 11:44:42 +00:00
raise
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)
2011-01-01 11:44:42 +00:00
class ApiActions(dict):
2011-01-13 08:33:14 +00:00
properties = {}
def __init__(self):
2011-01-01 11:44:42 +00:00
def api(request):
'''
2011-01-14 10:49:48 +00:00
returns list of all known api actions
param data {
docs: bool
}
if docs is true, action properties contain docstrings
return {
status: {'code': int, 'text': string},
data: {
actions: {
'api': {
cache: true,
doc: 'recursion'
},
'hello': {
cache: true,
..
}
...
}
}
}
'''
2011-01-14 10:51:50 +00:00
data = json.loads(request.POST.get('data', '{}'))
2011-01-14 10:49:48 +00:00
docs = data.get('docs', False)
2011-01-13 08:33:14 +00:00
_actions = self.keys()
_actions.sort()
actions = {}
for a in _actions:
actions[a] = self.properties[a]
2011-01-14 10:49:48 +00:00
if docs:
actions[a]['doc'] = self.doc(a)
2011-01-01 11:44:42 +00:00
response = json_response({'actions': actions})
return render_to_json_response(response)
2011-01-13 08:33:14 +00:00
self.register(api)
def doc(self, f):
return trim(self[f].__doc__)
2011-01-13 08:33:14 +00:00
def register(self, method, action=None, cache=True):
if not action:
action = method.func_name
self[action] = method
2011-01-13 08:33:14 +00:00
self.properties[action] = {'cache': cache}
def unregister(self, action):
if action in self:
del self[action]
actions = ApiActions()