From 8eb60cb0f4b3bc63cb5c7119618e00d1888a0cf8 Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Tue, 20 Dec 2022 06:09:27 -0500 Subject: [PATCH] split database into sub-module --- relay/database/__init__.py | 17 +++ relay/database/base.py | 67 ++++++++++ relay/{database.py => database/connection.py} | 114 +----------------- relay/database/rows.py | 35 ++++++ 4 files changed, 125 insertions(+), 108 deletions(-) create mode 100644 relay/database/__init__.py create mode 100644 relay/database/base.py rename relay/{database.py => database/connection.py} (64%) create mode 100644 relay/database/rows.py diff --git a/relay/database/__init__.py b/relay/database/__init__.py new file mode 100644 index 0000000..0444c74 --- /dev/null +++ b/relay/database/__init__.py @@ -0,0 +1,17 @@ +import tinysql + +from .base import DEFAULT_CONFIG, RELAY_SOFTWARE, TABLES +from .connection import Connection +from .rows import ROWS + + +class Database(tinysql.Database): + def __init__(self, **config): + tinysql.Database.__init__(self, **config, + connection_class = Connection, + row_classes = ROWS + ) + + + def create(self): + self.create_database(TABLES) diff --git a/relay/database/base.py b/relay/database/base.py new file mode 100644 index 0000000..7f79d43 --- /dev/null +++ b/relay/database/base.py @@ -0,0 +1,67 @@ +from tinysql import Column, Table + + +DEFAULT_CONFIG = { + 'description': ('str', 'Make a note about your relay here'), + 'http_timeout': ('int', 10), + 'json_cache': ('int', 1024), + 'log_level': ('str', 'INFO'), + 'name': ('str', 'ActivityRelay'), + 'privkey': ('str', ''), + 'push_limit': ('int', 512), + 'require_approval': ('bool', False), + 'version': ('int', 20221211), + 'whitelist': ('bool', False), + 'workers': ('int', 8) +} + +RELAY_SOFTWARE = [ + 'activity-relay', # https://github.com/yukimochi/Activity-Relay + 'activityrelay', # https://git.pleroma.social/pleroma/relay + 'aoderelay', # https://git.asonix.dog/asonix/relay + 'feditools-relay' # https://git.ptzo.gdn/feditools/relay +] + +TABLES = [ + Table('config', + Column('key', 'text', unique=True, nullable=False, primary_key=True), + Column('value', 'text') + ), + Table('instances', + Column('id', 'serial'), + Column('domain', 'text', unique=True, nullable=False), + Column('actor', 'text'), + Column('inbox', 'text', nullable=False), + Column('followid', 'text'), + Column('software', 'text'), + Column('note', 'text'), + Column('joined', 'datetime', nullable=False), + Column('updated', 'datetime') + ), + Table('whitelist', + Column('id', 'serial'), + Column('domain', 'text', unique=True), + Column('created', 'datetime', nullable=False) + ), + Table('bans', + Column('id', 'serial'), + Column('name', 'text', unique=True), + Column('note', 'text'), + Column('type', 'text', nullable=False), + Column('created', 'datetime', nullable=False) + ), + Table('users', + Column('id', 'serial'), + Column('handle', 'text', unique=True, nullable=False), + Column('domain', 'text', nullable=False), + Column('api_token', 'text'), + Column('created', 'datetime', nullable=False), + Column('updated', 'datetime') + ), + Table('tokens', + Column('id', 'text', unique=True, nullable=False, primary_key=True), + Column('userid', 'integer', nullable=False), + Column('created', 'datetime', nullable=False), + Column('updated', 'datetime') + ) +] diff --git a/relay/database.py b/relay/database/connection.py similarity index 64% rename from relay/database.py rename to relay/database/connection.py index eee269e..d183990 100644 --- a/relay/database.py +++ b/relay/database/connection.py @@ -1,91 +1,10 @@ import tinysql from datetime import datetime -from tinysql import Column, Table from urllib.parse import urlparse -from .logger import set_level -from .misc import AppBase, DotDict, boolean - - -TABLES = [ - Table('config', - Column('key', 'text', unique=True, nullable=False, primary_key=True), - Column('value', 'text') - ), - Table('instances', - Column('id', 'serial'), - Column('domain', 'text', unique=True, nullable=False), - Column('actor', 'text'), - Column('inbox', 'text', nullable=False), - Column('followid', 'text'), - Column('software', 'text'), - Column('actor_data', 'json'), - Column('note', 'text'), - Column('joined', 'datetime', nullable=False), - Column('updated', 'datetime') - ), - Table('whitelist', - Column('id', 'serial'), - Column('domain', 'text', unique=True), - Column('created', 'datetime', nullable=False) - ), - Table('bans', - Column('id', 'serial'), - Column('name', 'text', unique=True), - Column('note', 'text'), - Column('type', 'text', nullable=False), - Column('created', 'datetime', nullable=False) - ), - Table('users', - Column('id', 'serial'), - Column('handle', 'text', unique=True, nullable=False), - Column('domain', 'text', nullable=False), - Column('api_token', 'text'), - Column('created', 'datetime', nullable=False), - Column('updated', 'datetime') - ), - Table('tokens', - Column('id', 'text', unique=True, nullable=False, primary_key=True), - Column('userid', 'integer', nullable=False), - Column('created', 'datetime', nullable=False), - Column('updated', 'datetime') - ) -] - -DEFAULT_CONFIG = { - 'description': ('str', 'Make a note about your relay here'), - 'http_timeout': ('int', 10), - 'json_cache': ('int', 1024), - 'log_level': ('str', 'INFO'), - 'name': ('str', 'ActivityRelay'), - 'privkey': ('str', ''), - 'push_limit': ('int', 512), - 'require_approval': ('bool', False), - 'version': ('int', 20221211), - 'whitelist': ('bool', False), - 'workers': ('int', 8) -} - -RELAY_SOFTWARE = [ - 'activityrelay', # https://git.pleroma.social/pleroma/relay - 'aoderelay', # https://git.asonix.dog/asonix/relay - 'feditools-relay' # https://git.ptzo.gdn/feditools/relay -] - - -class Database(AppBase, tinysql.Database): - def __init__(self, **config): - tinysql.Database.__init__(self, **config, - connection_class = Connection, - row_classes = [ - ConfigRow - ] - ) - - - def create(self): - self.create_database(TABLES) +from .base import DEFAULT_CONFIG +from ..misc import DotDict class Connection(tinysql.ConnectionMixin): @@ -163,12 +82,12 @@ class Connection(tinysql.ConnectionMixin): if not row: return DEFAULT_CONFIG[key][1] - return row.get_value() + return row.value def get_config_all(self): rows = self.select('config').all() - config = DotDict({row.key: row.get_value() for row in rows}) + config = DotDict({row.key: row.value for row in rows}) for key, data in DEFAULT_CONFIG.items(): if key not in config: @@ -247,8 +166,8 @@ class Connection(tinysql.ConnectionMixin): if value == '__DEFAULT__': value = DEFAULT_CONFIG[key][1] - if key == 'log_level': - set_level(value) + elif key == 'log_level' and not getattr(logging, value.upper(), False): + raise KeyError(value) row = self.select('config', key=key).one() @@ -318,24 +237,3 @@ class Connection(tinysql.ConnectionMixin): 'domain': domain, 'created': datetime.now() }) - - -class ConfigRow(tinysql.Row): - __table__ = 'config' - - def get_value(self): - type = DEFAULT_CONFIG[self.key][0] - - if type == 'int': - return int(self.value) - - elif type == 'bool': - return boolean(self.value.encode('utf-8')) - - elif type == 'list': - return json.loads(value) - - elif type == 'json': - return DotDict.parse(value) - - return self.value diff --git a/relay/database/rows.py b/relay/database/rows.py new file mode 100644 index 0000000..34a4313 --- /dev/null +++ b/relay/database/rows.py @@ -0,0 +1,35 @@ +import json +from tinysql import Row +from .base import DEFAULT_CONFIG +from ..misc import DotDict, boolean + + +ROWS = [] + + +def register(cls): + ROWS.append(cls) + return cls + + +@register +class ConfigRow(Row): + __table__ = 'config' + + @property + def value(self): + type = DEFAULT_CONFIG[self.key][0] + + if type == 'int': + return int(self['value']) + + elif type == 'bool': + return boolean(self['value']) + + elif type == 'list': + return json.loads(self['value']) + + elif type == 'json': + return DotDict.parse(self['value']) + + return self['value']