more changes
This commit is contained in:
parent
c0c489b719
commit
695d8dc50f
|
@ -400,7 +400,6 @@ class Nodeinfo(DotDict):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def new(cls, name, version, niversion='2.1', **kwargs):
|
def new(cls, name, version, niversion='2.1', **kwargs):
|
||||||
print(name, version, niversion)
|
|
||||||
assert niversion in ['2.0', '2.1']
|
assert niversion in ['2.0', '2.1']
|
||||||
|
|
||||||
open_regs = boolean(kwargs.pop('open_regs', True))
|
open_regs = boolean(kwargs.pop('open_regs', True))
|
||||||
|
@ -430,11 +429,10 @@ class Nodeinfo(DotDict):
|
||||||
services = {
|
services = {
|
||||||
'inbound': kwargs.pop('inbound', []),
|
'inbound': kwargs.pop('inbound', []),
|
||||||
'outbound': kwargs.pop('outbound', [])
|
'outbound': kwargs.pop('outbound', [])
|
||||||
},
|
}
|
||||||
metadata = kwargs
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if data.version == '2.1':
|
if niversion == '2.1':
|
||||||
if repository:
|
if repository:
|
||||||
data.software.repository = repository
|
data.software.repository = repository
|
||||||
|
|
||||||
|
@ -453,6 +451,9 @@ class Nodeinfo(DotDict):
|
||||||
if comments:
|
if comments:
|
||||||
data.usage.localComments = comments
|
data.usage.localComments = comments
|
||||||
|
|
||||||
|
if kwargs:
|
||||||
|
data.metadata = kwargs
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,21 @@
|
||||||
http_methods = ['CONNECT', 'DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT', 'TRACE']
|
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
|
from .application import Application, Blueprint
|
||||||
|
|
|
@ -20,13 +20,15 @@ from ..template import Template
|
||||||
try: from ..sql import Database
|
try: from ..sql import Database
|
||||||
except ImportError: Database = None
|
except ImportError: Database = None
|
||||||
|
|
||||||
|
|
||||||
frontend = Path(__file__).resolve().parent.parent.join('http_frontend')
|
frontend = Path(__file__).resolve().parent.parent.join('http_frontend')
|
||||||
|
|
||||||
|
|
||||||
class ApplicationBase:
|
class ApplicationBase:
|
||||||
ctx = DotDict()
|
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.cfg = Config(**kwargs)
|
||||||
self.db = None
|
self.db = None
|
||||||
self.router = Router(trim_last_slash=True)
|
self.router = Router(trim_last_slash=True)
|
||||||
|
@ -302,7 +304,9 @@ class Application(ApplicationBase):
|
||||||
|
|
||||||
except:
|
except:
|
||||||
traceback.print_exc()
|
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:
|
try:
|
||||||
response.headers.update(self.cfg.default_headers)
|
response.headers.update(self.cfg.default_headers)
|
||||||
|
@ -324,3 +328,12 @@ class Blueprint(ApplicationBase):
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
self.prefix = prefix
|
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:
|
class Request:
|
||||||
__slots__ = [
|
__slots__ = [
|
||||||
'_body', '_form', '_reader', '_method', 'app', 'address',
|
'_body', '_form', '_reader', '_method', '_app', 'address',
|
||||||
'path', 'version', 'headers', 'cookies',
|
'path', 'version', 'headers', 'cookies',
|
||||||
'query', 'raw_query'
|
'query', 'raw_query'
|
||||||
]
|
]
|
||||||
|
@ -24,6 +24,7 @@ class Request:
|
||||||
def __init__(self, app, reader, address):
|
def __init__(self, app, reader, address):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
|
self._app = app
|
||||||
self._reader = reader
|
self._reader = reader
|
||||||
self._body = b''
|
self._body = b''
|
||||||
self._form = DotDict()
|
self._form = DotDict()
|
||||||
|
@ -33,7 +34,6 @@ class Request:
|
||||||
self.cookies = Cookies()
|
self.cookies = Cookies()
|
||||||
self.query = DotDict()
|
self.query = DotDict()
|
||||||
|
|
||||||
self.app = app
|
|
||||||
self.address = address
|
self.address = address
|
||||||
self.path = None
|
self.path = None
|
||||||
self.version = None
|
self.version = None
|
||||||
|
@ -66,6 +66,16 @@ class Request:
|
||||||
self.ctx[key] = value
|
self.ctx[key] = value
|
||||||
|
|
||||||
|
|
||||||
|
@property
|
||||||
|
def app(self):
|
||||||
|
return self._app or get_app()
|
||||||
|
|
||||||
|
|
||||||
|
@app.setter
|
||||||
|
def app(self, app):
|
||||||
|
self._app = app
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def agent(self):
|
def agent(self):
|
||||||
return self.headers.getone('User-Agent', 'no agent')
|
return self.headers.getone('User-Agent', 'no agent')
|
||||||
|
|
|
@ -2,16 +2,18 @@ import json, traceback
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
from . import get_app
|
||||||
from .misc import Cookies, Headers, CookieItem
|
from .misc import Cookies, Headers, CookieItem
|
||||||
|
|
||||||
from ..dotdict import MultiDotDict
|
from ..dotdict import MultiDotDict
|
||||||
|
|
||||||
|
|
||||||
class Response:
|
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):
|
def __init__(self, body=b'', status=200, headers={}, cookies={}, content_type='text/plain', request=None):
|
||||||
|
self._app = None
|
||||||
self._body = b''
|
self._body = b''
|
||||||
|
|
||||||
self.headers = Headers(headers)
|
self.headers = Headers(headers)
|
||||||
|
@ -23,6 +25,16 @@ class Response:
|
||||||
self.request = request
|
self.request = request
|
||||||
|
|
||||||
|
|
||||||
|
@property
|
||||||
|
def app(self):
|
||||||
|
return self._app or get_app()
|
||||||
|
|
||||||
|
|
||||||
|
@app.setter
|
||||||
|
def app(self, app):
|
||||||
|
self._app = app
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def body(self):
|
def body(self):
|
||||||
return self._body
|
return self._body
|
||||||
|
|
|
@ -595,6 +595,16 @@ class DateString(str):
|
||||||
return getattr(self.dt, key)
|
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
|
@classmethod
|
||||||
def from_datetime(cls, date, format):
|
def from_datetime(cls, date, format):
|
||||||
assert format in datetime_formats
|
assert format in datetime_formats
|
||||||
|
|
Loading…
Reference in a new issue