Replace database methods with peewee ORM #39

Merged
osilkin98 merged 26 commits from orm-rewrite into master 2020-04-03 23:40:43 +02:00
6 changed files with 52 additions and 31 deletions
Showing only changes of commit a581425a64 - Show all commits

View file

@ -14,6 +14,8 @@ setup(
data_files=[('config', ['config/conf.json',])],
include_package_data=True,
install_requires=[
'mysql-connector-python',
'pyyaml',
'Faker>=1.0.7',
'asyncio>=3.4.3',
'aiohttp==3.5.4',
@ -24,5 +26,6 @@ setup(
'PyNaCl>=1.3.0',
'requests',
'cython',
'peewee'
]
)

7
src/definitions.py Normal file
View file

@ -0,0 +1,7 @@
import os
SRC_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = os.path.dirname(SRC_DIR)
CONFIG_FILE = os.path.join(ROOT_DIR, 'config', 'conf.json')
LOGGING_DIR = os.path.join(ROOT_DIR, 'logs')
DATABASE_DIR = os.path.join(ROOT_DIR, 'database')

View file

@ -1,13 +1,20 @@
import argparse
import json
import yaml
import logging
import logging.config
import os
import sys
from src.server.app import run_app
from src.settings import config
from src.definitions import LOGGING_DIR, CONFIG_FILE, DATABASE_DIR
def config_logging_from_settings(conf):
def setup_logging_from_config(conf: dict):
# set the logging directory here from the settings file
if not os.path.exists(LOGGING_DIR):
os.mkdir(LOGGING_DIR)
_config = {
"version": 1,
"disable_existing_loggers": False,
@ -32,7 +39,7 @@ def config_logging_from_settings(conf):
"level": "DEBUG",
"formatter": "standard",
"class": "logging.handlers.RotatingFileHandler",
"filename": conf['path']['debug_log'],
"filename": os.path.join(LOGGING_DIR, 'debug.log'),
"maxBytes": 10485760,
"backupCount": 5
},
@ -40,7 +47,7 @@ def config_logging_from_settings(conf):
"level": "ERROR",
"formatter": "standard",
"class": "logging.handlers.RotatingFileHandler",
"filename": conf['path']['error_log'],
"filename": os.path.join(LOGGING_DIR, 'error.log'),
"maxBytes": 10485760,
"backupCount": 5
},
@ -48,7 +55,7 @@ def config_logging_from_settings(conf):
"level": "NOTSET",
"formatter": "aiohttp",
"class": "logging.handlers.RotatingFileHandler",
"filename": conf['path']['server_log'],
"filename": os.path.join(LOGGING_DIR, 'server.log'),
"maxBytes": 10485760,
"backupCount": 5
}
@ -70,15 +77,36 @@ def config_logging_from_settings(conf):
logging.config.dictConfig(_config)
def get_config(filepath):
with open(filepath, 'r') as cfile:
config = yaml.load(cfile, Loader=yaml.FullLoader)
return config
def setup_db_from_config(config: dict):
if 'sqlite' in config['database']:
if not os.path.exists(DATABASE_DIR):
os.mkdir(DATABASE_DIR)
config['db_path'] = os.path.join(
DATABASE_DIR, config['database']['sqlite']
)
def main(argv=None):
argv = argv or sys.argv[1:]
parser = argparse.ArgumentParser(description='LBRY Comment Server')
parser.add_argument('--port', type=int)
parser.add_argument('--config', type=str)
args = parser.parse_args(argv)
config_logging_from_settings(config)
config = get_config(CONFIG_FILE) if not args.config else args.config
setup_logging_from_config(config)
setup_db_from_config(config)
if args.port:
config['port'] = args.port
config_logging_from_settings(config)
run_app(config)

View file

@ -75,12 +75,9 @@ class CommentDaemon:
self.config = app['config']
# configure the db file
if db_file:
app['db_path'] = db_file
app['backup'] = backup
else:
app['db_path'] = config['path']['database']
app['backup'] = backup or (app['db_path'] + '.backup')
app['db_path'] = db_file or config.get('db_path')
if app['db_path']:
app['backup'] = backup or '.'.join((app['db_path'], 'backup'))
# configure the order of tasks to run during app lifetime
app.on_startup.append(setup_db_schema)

View file

@ -1,17 +0,0 @@
# cython: language_level=3
import json
import pathlib
root_dir = pathlib.Path(__file__).parent.parent
config_path = root_dir / 'config' / 'conf.json'
def get_config(filepath):
with open(filepath, 'r') as cfile:
conf = json.load(cfile)
for key, path in conf['path'].items():
conf['path'][key] = str(root_dir / path)
return conf
config = get_config(config_path)

View file

@ -9,13 +9,16 @@ from faker.providers import internet
from faker.providers import lorem
from faker.providers import misc
from src.settings import config
from src.main import get_config, CONFIG_FILE
from src.server import app
from src.server.validation import is_valid_base_comment
from test.testcase import AsyncioTestCase
config = get_config(CONFIG_FILE)
if 'slack_webhook' in config:
config.pop('slack_webhook')