more changes
This commit is contained in:
parent
c0c489b719
commit
695d8dc50f
|
@ -400,7 +400,6 @@ class Nodeinfo(DotDict):
|
|||
|
||||
@classmethod
|
||||
def new(cls, name, version, niversion='2.1', **kwargs):
|
||||
print(name, version, niversion)
|
||||
assert niversion in ['2.0', '2.1']
|
||||
|
||||
open_regs = boolean(kwargs.pop('open_regs', True))
|
||||
|
@ -430,11 +429,10 @@ class Nodeinfo(DotDict):
|
|||
services = {
|
||||
'inbound': kwargs.pop('inbound', []),
|
||||
'outbound': kwargs.pop('outbound', [])
|
||||
},
|
||||
metadata = kwargs
|
||||
}
|
||||
)
|
||||
|
||||
if data.version == '2.1':
|
||||
if niversion == '2.1':
|
||||
if repository:
|
||||
data.software.repository = repository
|
||||
|
||||
|
@ -453,6 +451,9 @@ class Nodeinfo(DotDict):
|
|||
if comments:
|
||||
data.usage.localComments = comments
|
||||
|
||||
if kwargs:
|
||||
data.metadata = kwargs
|
||||
|
||||
return data
|
||||
|
||||
|
||||
|
|
|
@ -1,4 +1,21 @@
|
|||
http_methods = ['CONNECT', 'DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT', 'TRACE']
|
||||
applications = {}
|
||||
|
||||
def get_app(name='default'):
|
||||
try:
|
||||
return applications.get(name)
|
||||
|
||||
except KeyError:
|
||||
return set_app(Application(appname=name))
|
||||
|
||||
|
||||
def set_app(app):
|
||||
applications[app.name] = app
|
||||
return app
|
||||
|
||||
|
||||
def create_app(appname, **kwargs):
|
||||
return set_app(Application(appname=appname, **kwargs))
|
||||
|
||||
|
||||
from .application import Application, Blueprint
|
||||
|
|
|
@ -20,13 +20,15 @@ from ..template import Template
|
|||
try: from ..sql import Database
|
||||
except ImportError: Database = None
|
||||
|
||||
|
||||
frontend = Path(__file__).resolve().parent.parent.join('http_frontend')
|
||||
|
||||
|
||||
class ApplicationBase:
|
||||
ctx = DotDict()
|
||||
|
||||
def __init__(self, views=[], middleware=[], dbtype=None, dbargs={}, **kwargs):
|
||||
def __init__(self, appname='default', views=[], middleware=[], dbtype=None, dbargs={}, **kwargs):
|
||||
self.name = appname
|
||||
self.cfg = Config(**kwargs)
|
||||
self.db = None
|
||||
self.router = Router(trim_last_slash=True)
|
||||
|
@ -302,7 +304,9 @@ class Application(ApplicationBase):
|
|||
|
||||
except:
|
||||
traceback.print_exc()
|
||||
response = self.cfg.response_class(request=request).set_error('Server Error', 500)
|
||||
|
||||
## Don't use a custom response class here just in case it caused the error
|
||||
response = Response(request=request).set_error('Server Error', 500)
|
||||
|
||||
try:
|
||||
response.headers.update(self.cfg.default_headers)
|
||||
|
@ -324,3 +328,12 @@ class Blueprint(ApplicationBase):
|
|||
super().__init__(**kwargs)
|
||||
|
||||
self.prefix = prefix
|
||||
|
||||
|
||||
## might keep this
|
||||
def set_response(request, resp_class, func, *args, **kwargs):
|
||||
try:
|
||||
return getattr(resp_class, func)(*args, **kwargs)
|
||||
except:
|
||||
traceback.print_exc()
|
||||
return Response(request=request).set_error('Server Error', 500)
|
||||
|
|
|
@ -14,7 +14,7 @@ LocalTime = datetime.now(UtcTime).astimezone().tzinfo
|
|||
|
||||
class Request:
|
||||
__slots__ = [
|
||||
'_body', '_form', '_reader', '_method', 'app', 'address',
|
||||
'_body', '_form', '_reader', '_method', '_app', 'address',
|
||||
'path', 'version', 'headers', 'cookies',
|
||||
'query', 'raw_query'
|
||||
]
|
||||
|
@ -24,6 +24,7 @@ class Request:
|
|||
def __init__(self, app, reader, address):
|
||||
super().__init__()
|
||||
|
||||
self._app = app
|
||||
self._reader = reader
|
||||
self._body = b''
|
||||
self._form = DotDict()
|
||||
|
@ -33,7 +34,6 @@ class Request:
|
|||
self.cookies = Cookies()
|
||||
self.query = DotDict()
|
||||
|
||||
self.app = app
|
||||
self.address = address
|
||||
self.path = None
|
||||
self.version = None
|
||||
|
@ -66,6 +66,16 @@ class Request:
|
|||
self.ctx[key] = value
|
||||
|
||||
|
||||
@property
|
||||
def app(self):
|
||||
return self._app or get_app()
|
||||
|
||||
|
||||
@app.setter
|
||||
def app(self, app):
|
||||
self._app = app
|
||||
|
||||
|
||||
@property
|
||||
def agent(self):
|
||||
return self.headers.getone('User-Agent', 'no agent')
|
||||
|
|
|
@ -2,16 +2,18 @@ import json, traceback
|
|||
|
||||
from datetime import datetime
|
||||
|
||||
from . import get_app
|
||||
from .misc import Cookies, Headers, CookieItem
|
||||
|
||||
from ..dotdict import MultiDotDict
|
||||
|
||||
|
||||
class Response:
|
||||
__slots__ = ['_body', 'headers', 'cookies', 'status', 'request']
|
||||
__slots__ = ['_app', '_body', 'headers', 'cookies', 'status', 'request']
|
||||
|
||||
|
||||
def __init__(self, body=b'', status=200, headers={}, cookies={}, content_type='text/plain', request=None):
|
||||
self._app = None
|
||||
self._body = b''
|
||||
|
||||
self.headers = Headers(headers)
|
||||
|
@ -23,6 +25,16 @@ class Response:
|
|||
self.request = request
|
||||
|
||||
|
||||
@property
|
||||
def app(self):
|
||||
return self._app or get_app()
|
||||
|
||||
|
||||
@app.setter
|
||||
def app(self, app):
|
||||
self._app = app
|
||||
|
||||
|
||||
@property
|
||||
def body(self):
|
||||
return self._body
|
||||
|
|
|
@ -595,6 +595,16 @@ class DateString(str):
|
|||
return getattr(self.dt, key)
|
||||
|
||||
|
||||
@classmethod
|
||||
def new_activitypub(cls, date):
|
||||
return cls(date, 'activitypub')
|
||||
|
||||
|
||||
@classmethod
|
||||
def new_http(cls, date):
|
||||
return cls(date, 'http')
|
||||
|
||||
|
||||
@classmethod
|
||||
def from_datetime(cls, date, format):
|
||||
assert format in datetime_formats
|
||||
|
|
Loading…
Reference in a new issue