Skip to content

Commit 43cffb4

Browse files
committedJul 15, 2013
first commit
0 parents  commit 43cffb4

10 files changed

+665
-0
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

‎.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>appier</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.python.pydev.PyDevBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.python.pydev.pythonNature</nature>
16+
</natures>
17+
</projectDescription>

‎.pydevproject

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?eclipse-pydev version="1.0"?><pydev_project>
3+
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
4+
<path>/appier/src</path>
5+
</pydev_pathproperty>
6+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.6</pydev_property>
7+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
8+
</pydev_project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=utf-8

‎src/appier/__init__.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
import base
5+
import exceptions
6+
import request
7+
import settings
8+
import util
9+
10+
from base import *
11+
from exceptions import *
12+
from request import *
13+
from settings import *
14+
from util import *

‎src/appier/base.py

+483
Large diffs are not rendered by default.

‎src/appier/exceptions.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
class BotException(Exception):
5+
"""
6+
Top level exception to be used as the root of
7+
all the exceptions to be raised by the bot infra-
8+
structure. Should be compatible with http status
9+
codes for proper http serialization.
10+
"""
11+
12+
def __init__(self, *args, **kwargs):
13+
Exception.__init__(self, *args)
14+
self.error_code = kwargs.get("error_code", 500)

‎src/appier/request.py

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
import types
5+
6+
import util
7+
8+
class Request(object):
9+
10+
ALIAS = ("token",)
11+
SESSIONS = {}
12+
13+
def __init__(self, method, path, params = {}, data_j = {}, environ = {}):
14+
self.method = method
15+
self.path = path
16+
self.params = params
17+
self.data_j = data_j
18+
self.environ = environ
19+
self.session = {}
20+
self.cookies = {}
21+
self.in_headers = {}
22+
self.out_headers = {}
23+
self.warnings = []
24+
25+
def warning(self, message):
26+
message_t = type(message)
27+
28+
if message_t in types.StringTypes:
29+
message = dict(message = message)
30+
elif not message_t == types.DictType:
31+
raise RuntimeError("Invalid message type '%s'", message_t)
32+
33+
self.warnings.append(message)
34+
35+
def get_param(self, name, default = None):
36+
if not name in self.params: return default
37+
value = self.params[name]
38+
return value[0] if value else default
39+
40+
def set_params(self, params):
41+
self.params = params
42+
43+
def set_json(self, data_j):
44+
self.data_j = data_j
45+
46+
def set_header(self, name, value):
47+
self.out_headers[name] = value
48+
49+
def resolve_params(self):
50+
self.params = self._resolve_p(self.params)
51+
52+
def load_session(self):
53+
self.load_cookies()
54+
self.set_alias()
55+
self.set_session()
56+
57+
def load_cookies(self):
58+
cookie_s = self.environ.get("HTTP_COOKIE", "")
59+
60+
cookies = [cookie.strip() for cookie in cookie_s.split(";")]
61+
for cookie in cookies:
62+
if not "=" in cookie: cookie += "="
63+
name, value = cookie.split("=", 1)
64+
self.cookies[name] = value
65+
66+
def set_alias(self):
67+
for alias in Request.ALIAS:
68+
if not alias in self.params: continue
69+
self.params["sid"] = self.params[alias]
70+
71+
def set_session(self, create = False):
72+
sid = self.cookies.get("sid", None)
73+
sid = self.params.get("sid", (None,))[0] or sid
74+
self.session = Request.SESSIONS.get(sid, {})
75+
76+
if not self.session and create:
77+
sid = util.gen_token()
78+
self.session = dict(sid = sid)
79+
Request.SESSIONS[sid] = self.session
80+
81+
return self.session
82+
83+
def get_warnings(self):
84+
return self.warnings
85+
86+
def get_headers(self):
87+
return self.out_headers.items()

‎src/appier/settings.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
DEBUG = True
5+
USERNAME = "admin"
6+
PASSWORD = "admin"

‎src/appier/util.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
import uuid
5+
import hashlib
6+
import inspect
7+
8+
import exceptions
9+
10+
def gen_token():
11+
token_s = str(uuid.uuid4())
12+
token = hashlib.sha256(token_s).hexdigest()
13+
return token
14+
15+
def private(function):
16+
def _private(self, *args, **kwargs):
17+
is_auth = self.request.session and "username" in self.request.session
18+
if not is_auth: raise exceptions.BotException(
19+
"Method '%s' requires authentication" % function.__name__,
20+
error_code = 403
21+
)
22+
23+
sanitize(function, kwargs)
24+
return function(self, *args, **kwargs)
25+
return _private
26+
27+
def sanitize(function, kwargs):
28+
removal = []
29+
method_a = inspect.getargspec(function)[0]
30+
for name in kwargs:
31+
if name in method_a: continue
32+
removal.append(name)
33+
for name in removal: del kwargs[name]

0 commit comments

Comments
 (0)
Please sign in to comment.