izzylib-http-async/barkshark_http_async/server/config.py
2022-02-18 16:45:32 -05:00

102 lines
2.8 KiB
Python

from .request import Request
from .response import Response
from .. import __version__
from ..config import BaseConfig
from ..dotdict import LowerDotDict
from ..http_client_async import HttpClient
from ..misc import boolean
class Config(BaseConfig):
_startup = True
def __init__(self, **kwargs):
super().__init__(
name = 'IzzyLib Http Server',
title = None,
version = '0.0.1',
git_repo = 'https://git.barkshark.xyz/izaliamae/izzylib',
socket = None,
listen = 'localhost',
host = None,
web_host = None,
alt_hosts = [],
port = 8080,
proto = 'http',
access_log = True,
timeout = 60,
default_headers = LowerDotDict(),
request_class = Request,
response_class = Response,
sig_handler = None,
sig_handler_args = [],
sig_handler_kwargs = {},
tpl_search = [],
tpl_globals = {},
tpl_context = None,
tpl_autoescape = True,
tpl_default = True,
tpl_favicon_path = '/framework/static/icon64.png',
client_class = HttpClient,
client_args = [],
client_kwargs = {}
)
self._startup = False
self.default_headers.update(kwargs.pop('default_headers', {}))
self.set_data(kwargs)
if not self.default_headers.get('server'):
self.default_headers['server'] = f'{self.name}/{__version__}'
if not self.client_kwargs.get('appagent') and not self.client_kwargs.get('useragent'):
self.client_kwargs['appagent'] = f'{self.name}/{__version__}'
@property
def hosts(self):
return (f'{self.listen}:{self.port}', self.host, self.web_host, *self.alt_hosts)
def parse_value(self, key, value):
if self._startup:
return value
if key == 'listen':
if not self.host:
self.host = value
if not self.web_host:
self.web_host = value
elif key == 'host':
if not self.web_host or self.web_host == self.listen:
self.web_host = value
elif key == 'port' and not isinstance(value, int):
raise TypeError(f'{key} must be an integer')
elif key == 'socket':
value = Path(value)
elif key in ['access_log', 'tpl_autoescape', 'tpl_default'] and not isinstance(value, bool):
raise TypeError(f'{key} must be a boolean')
elif key in ['alt_hosts', 'sig_handler_args', 'tpl_search'] and not isinstance(value, list):
raise TypeError(f'{key} must be a list')
elif key in ['sig_handler_kwargs', 'tpl_globals'] and not isinstance(value, dict):
raise TypeError(f'{key} must be a dict')
elif key == 'tpl_context' and not getattr(value, '__call__', None):
raise TypeError(f'{key} must be a callable')
elif key == 'request_class' and not issubclass(value, Request):
raise TypeError(f'{key} must be a subclass of izzylib.http_server_async.Request')
elif key == 'response_class' and not issubclass(value, Response):
raise TypeError(f'{key} must be a subclass of izzylib.http_server_async.Response')
return value