Skip to content

Commit

Permalink
adding some code
Browse files Browse the repository at this point in the history
  • Loading branch information
MagnusKnutas committed Mar 10, 2015
1 parent bae1300 commit 8fd8211
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 0 deletions.
43 changes: 43 additions & 0 deletions restedorm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import inspect

__author__ = 'magnusknutas'

BASE_URL = ''


def caller_name(skip=2):
"""Get a name of a caller in the format module.class.method
`skip` specifies how many levels of stack to skip while getting caller
name. skip=1 means "who calls me", skip=2 "who calls my caller" etc.
An empty string is returned if skipped levels exceed stack height
"""
stack = inspect.stack()
start = 0 + skip
if len(stack) < start + 1:
return ''
parentframe = stack[start][0]

name = []
module = inspect.getmodule(parentframe)

# `modname` can be None when frame is executed directly in console
# TODO(techtonik): consider using __main__
if module:
name.append(module.__name__)
# detect classname

if 'self' in parentframe.f_locals:
# I don't know any way to detect call from the object method
# XXX: there seems to be no way to detect static method call - it will
# be just a function call
name.append(parentframe.f_locals['self'].__class__.__name__)
codename = parentframe.f_code.co_name
if codename != '<module>': # top level usually
name.append( codename ) # function or a method
del parentframe

return ".".join(name)
23 changes: 23 additions & 0 deletions restedorm/abstract_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import json
import requests
from restedorm.exeptions import NotFoundException
from restedorm.managers import BaseManager

__author__ = 'magnusknutas'
import logging

logger = logging.getLogger(__name__)


class RestObject(object):
uri = str
objects = BaseManager

class Meta:
endpoint = None

def __init__(self, *args, **kwargs):
for key in kwargs.keys():
setattr(self, key, kwargs[key])
3 changes: 3 additions & 0 deletions restedorm/annotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
__author__ = 'magnusknutas'
19 changes: 19 additions & 0 deletions restedorm/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
from restedorm.managers import BaseManager

__author__ = 'magnusknutas'


class Endpoint(object):

def __init__(self, endpoint):
self.endpoint = endpoint

def __call__(self, *args):
if args[0].Meta.endpoint:
args[0].objects = BaseManager(args[0], args[0].Meta.endpoint)
else:
args[0].objects = BaseManager(args[0], self.endpoint)

return args[0]
21 changes: 21 additions & 0 deletions restedorm/exeptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
__author__ = 'magnusknutas'


class NotFoundException(Exception):
def __init__(self, value):
self.value = value
super(NotFoundException, self).__init__()

def __str__(self):
return self.value


class ApiError(Exception):
def __init__(self, value):
self.value = value
super(ApiError, self).__init__()

def __str__(self):
return self.value
72 changes: 72 additions & 0 deletions restedorm/managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import json
import requests
from restedorm import BASE_URL, caller_name
from restedorm.exeptions import NotFoundException, ApiError

__author__ = 'magnusknutas'
from restedorm.statics import HTTP_200, HTTP_404


class BaseManager(object):
next = None
previous = None
count = 0

def __init__(self, *args, **kwargs):
if len(args) > 0:
self.endpoint = args[1]
self.cls = args[0]
self.module = caller_name(skip=4)

def filter(self, **kwargs):
uri = self._get_uri() + ('?' if len(kwargs) > 0 else '')
for key in kwargs.keys():
uri += str(key)+"="+str(kwargs[key])
print uri

r = requests.get(uri, auth=('name', 'pass')) # TODO: Fix auth
if r.status_code == 404:
raise NotFoundException("endpoint %s is not found" % self.endpoint)
objs = json.loads(r.text)
ret = []
for user in objs.get('results', {}):
user.pop('id')
ret.append(self.cls(**user))
self.next = objs.get('next', None)
self.previous = objs.get('previous', None)
self.count = objs.get('count', None)
return ret

def all(self):
page = 1
ret = self.filter(page=page)
while self.next:
page += 1
ret += self.filter(page=page)
return ret

def get(self, ident):
uri = self._get_uri() + "%i/" % ident

r = requests.get(uri, auth=('name', 'pass')) # TODO: Fix auth

if r.status_code == HTTP_404:
raise NotFoundException("id %i not found in endpoint /%s/" % (ident, self.endpoint))

obj = json.loads(r.text)

if r.status_code == HTTP_200:
return self.cls(**obj)
else:
ApiError("%s, %s" % (r.status_code, r.text))

def create(self):
pass

def _get_uri(self):
if self.endpoint:
return BASE_URL + self.endpoint + '/'
else:
print "oh herro"
6 changes: 6 additions & 0 deletions restedorm/statics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
__author__ = 'magnusknutas'

HTTP_404 = 404
HTTP_200 = 200

0 comments on commit 8fd8211

Please sign in to comment.