Commit 56448d82 by Piotr Mitros

Clean up of djobject

parent 881f3413
...@@ -10,6 +10,16 @@ import json ...@@ -10,6 +10,16 @@ import json
schema = None schema = None
def find_in_schema(cls = None, name = None):
items = []
for item in schema:
if cls and item['class'] != cls:
continue
if name and item['name'] != name:
continue
items.append(item)
return items
def http_rpc_helper(baseurl, view_or_query, function, headers = {}): def http_rpc_helper(baseurl, view_or_query, function, headers = {}):
if baseurl: if baseurl:
baseembedurl = baseurl+view_or_query+"/" baseembedurl = baseurl+view_or_query+"/"
...@@ -26,7 +36,7 @@ def http_rpc_helper(baseurl, view_or_query, function, headers = {}): ...@@ -26,7 +36,7 @@ def http_rpc_helper(baseurl, view_or_query, function, headers = {}):
raise AttributeError(function) raise AttributeError(function)
error = "Error calling {func} {status}".format(func=function, status=response.status_code) error = "Error calling {func} {status}".format(func=function, status=response.status_code)
raise Exception(error) raise Exception(error)
rpc_call.__doc__ = "rpc call" rpc_call.__doc__ = find_in_schema(cls = view_or_query, name = function)[0]['doc']
return rpc_call return rpc_call
def local_call_helper(view_or_query, function): def local_call_helper(view_or_query, function):
...@@ -37,17 +47,20 @@ def local_call_helper(view_or_query, function): ...@@ -37,17 +47,20 @@ def local_call_helper(view_or_query, function):
class embed(): class embed():
def __init__(self, view_or_query, baseurl = None, headers = {}): def __init__(self, view_or_query, baseurl = None, headers = {}):
global schema
self._baseurl = baseurl self._baseurl = baseurl
self._view_or_query = view_or_query
self._headers = headers
self._refresh_schema()
def _refresh_schema(self):
global schema
if not schema: if not schema:
if baseurl: if self._baseurl:
url = baseurl+"schema" url = self._baseurl+"schema"
schema = json.loads(requests.get(url).content) schema = json.loads(requests.get(url).content)
else: else:
import djobject.views import djobject.views
schema = djobject.views.schema_helper() schema = djobject.views.schema_helper()
self._view_or_query = view_or_query
self._headers = headers
def __getattr__(self, attr): def __getattr__(self, attr):
## Disallow internal. This is necessary both for analytics, ## Disallow internal. This is necessary both for analytics,
...@@ -56,19 +69,19 @@ class embed(): ...@@ -56,19 +69,19 @@ class embed():
if attr[0] == '_': if attr[0] == '_':
return return
if self._baseurl: if self._baseurl:
return http_rpc_helper(self._baseurl, self._view_or_query, attr) helper = http_rpc_helper(self._baseurl, self._view_or_query, attr)
else: else:
return local_call_helper(self._view_or_query, attr) helper = local_call_helper(self._view_or_query, attr)
# Set the docstring
helper.__doc__ = find_in_schema(cls = self._view_or_query, name = attr)[0]['doc']
helper.__name__ = "remote_"+attr
return helper
## TODO: Use probe/schema to populate this ## TODO: Use probe/schema to populate this
def __dir__(self): def __dir__(self):
results = [] self._refresh_schema()
probeurl = self._baseurl+"probe/"+self._view_or_query return [i["name"] for i in find_in_schema(cls = self._view_or_query)]
classes = requests.get(probeurl, headers = self._headers).content.split('\n')
for param_set in classes:
items = requests.get(probeurl+"/"+param_set, headers = self._headers).content.split('\n')
results = results + items
return results
def __repr__(self): def __repr__(self):
return self._view_or_query+" object host: ["+self._baseurl+"]" return self._view_or_query+" object host: ["+self._baseurl+"]"
...@@ -82,4 +95,4 @@ if __name__ == "__main__": ...@@ -82,4 +95,4 @@ if __name__ == "__main__":
djo = djobject(baseurl = "http://127.0.0.1:8000/") djo = djobject(baseurl = "http://127.0.0.1:8000/")
print djo.query.djt_event_count() print djo.query.djt_event_count()
print djo.query.djt_user_event_count(user = "bob") print djo.query.djt_user_event_count(user = "bob")
#print djo.query.__dir__() print djo.query.__dir__()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment