kopia lustrzana https://github.com/dsblank/activitypub
Reorganize app, clean up
rodzic
4f46f616a1
commit
7bb08a8a22
|
@ -1,2 +1,3 @@
|
|||
from .classes import *
|
||||
from .manager import Manager, app
|
||||
from ._version import __version__, VERSION
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
from .base import Manager
|
||||
from .base import Manager, app
|
||||
from .flaskman import FlaskManager
|
||||
from .tornadoman import TornadoManager
|
||||
|
|
|
@ -1,18 +1,16 @@
|
|||
import inspect
|
||||
import binascii
|
||||
import os
|
||||
import uuid
|
||||
|
||||
def wrap_method(manager, f):
|
||||
def function(*args, **kwargs):
|
||||
print(f.__name__, "called with:", args, kwargs)
|
||||
return f(manager, *args, **kwargs)
|
||||
function.__name__ = f.__name__
|
||||
return function
|
||||
|
||||
class Data():
|
||||
routes = []
|
||||
filters = []
|
||||
|
||||
def clear(self):
|
||||
self.routes.clear()
|
||||
self.filters.clear()
|
||||
|
||||
class DataWrapper():
|
||||
"""
|
||||
Instance for saving routes, filters, etc. for app.
|
||||
|
@ -55,16 +53,26 @@ class DataWrapper():
|
|||
return f
|
||||
return decorator
|
||||
|
||||
class Routes():
|
||||
routes = []
|
||||
def get_routes(self):
|
||||
return self._data.routes
|
||||
|
||||
def __call__(self, path, methods=["GET"]):
|
||||
print("call Routes!")
|
||||
def decorator(function):
|
||||
print("call decorator!")
|
||||
Routes.routes.append((path, function))
|
||||
print("return")
|
||||
return decorator
|
||||
def get_filters(self):
|
||||
return self._data.filters
|
||||
|
||||
def wrap_function(manager, f):
|
||||
"""
|
||||
General Function/Method Wrapper
|
||||
"""
|
||||
## Check the signature:
|
||||
params = [x.name for x in inspect.signature(f).parameters.values()]
|
||||
if len(params) == 0 or params[0] != "self":
|
||||
raise Exception("route function %s needs 'self' as first parameter"
|
||||
% f.__name__)
|
||||
def function(*args, **kwargs):
|
||||
results = f(manager, *args, **kwargs)
|
||||
return results
|
||||
function.__name__ = f.__name__
|
||||
return function
|
||||
|
||||
class Manager():
|
||||
"""
|
||||
|
@ -80,7 +88,6 @@ class Manager():
|
|||
app_name = "activitypub"
|
||||
version = "1.0.0"
|
||||
key_path = "./keys"
|
||||
route = Routes()
|
||||
|
||||
def __init__(self, context=None, defaults=None, database=None):
|
||||
from ..classes import ActivityPubBase
|
||||
|
|
|
@ -7,9 +7,7 @@ try:
|
|||
except:
|
||||
pass # flask not available
|
||||
|
||||
import inspect
|
||||
|
||||
from .base import Manager, app, wrap_method
|
||||
from .base import Manager, wrap_function, app
|
||||
|
||||
class FlaskManager(Manager):
|
||||
"""
|
||||
|
@ -23,6 +21,9 @@ class FlaskManager(Manager):
|
|||
def url_for(self, name):
|
||||
return url_for(name)
|
||||
|
||||
def write(self, item):
|
||||
self._write_data += str(item)
|
||||
|
||||
@property
|
||||
def request(self):
|
||||
return request
|
||||
|
@ -44,20 +45,12 @@ class FlaskManager(Manager):
|
|||
self.app.config["ID"] = "http://localhost:5000"
|
||||
## Add routes:
|
||||
for path, methods, f in app._data.routes:
|
||||
params = [x.name for x in inspect.signature(f).parameters.values()]
|
||||
print(f.__name__, params)
|
||||
if len(params) > 0 and params[0] == "self":
|
||||
self.app.route(path)(wrap_method(self, f))
|
||||
else:
|
||||
self.app.route(path)(f)
|
||||
## Add the route:
|
||||
self.app.route(path)(wrap_function(self, f))
|
||||
## Add filters:
|
||||
for f in app._data.filters:
|
||||
params = [x.name for x in inspect.signature(f).parameters.values()]
|
||||
print(f.__name__, params)
|
||||
if len(params) > 0 and params[0] == "self":
|
||||
self.app.template_filter()(wrap_method(self, f))
|
||||
else:
|
||||
self.app.template_filter()(f)
|
||||
## Add the template filter function:
|
||||
self.app.template_filter()(wrap_function(self, f))
|
||||
self.app.run(debug=1)
|
||||
|
||||
def load_secret_key(self, name):
|
||||
|
|
|
@ -7,9 +7,12 @@ except:
|
|||
import inspect
|
||||
import jinja2
|
||||
|
||||
from .base import Manager, app, wrap_method
|
||||
from .base import Manager, wrap_function, app
|
||||
|
||||
def make_handler(f, manager):
|
||||
"""
|
||||
Make a Tornado Handler
|
||||
"""
|
||||
class Handler(RequestHandler):
|
||||
|
||||
def get(self):
|
||||
|
@ -24,19 +27,15 @@ def make_handler(f, manager):
|
|||
class TornadoManager(Manager):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self._filters = None
|
||||
self.template_env = jinja2.Environment(
|
||||
loader=jinja2.FileSystemLoader(self.get_template_folder()))
|
||||
|
||||
def get_filters(self):
|
||||
filters = []
|
||||
for f in app._data.filters:
|
||||
params = [x.name for x in inspect.signature(f).parameters.values()]
|
||||
if len(params) > 0 and params[0] == "self":
|
||||
filters.append(wrap_method(self, f))
|
||||
else:
|
||||
filters.append(f)
|
||||
filters = {f.__name__: f for f in filters}
|
||||
return filters
|
||||
if self._filters is None:
|
||||
self._filters = {f.__name__: wrap_function(self, f)
|
||||
for f in app._data.filters}
|
||||
return self._filters
|
||||
|
||||
def render_template(self, name, **kwargs):
|
||||
filters = self.get_filters()
|
||||
|
|
Ładowanie…
Reference in New Issue