From 861ea1ea8c15f4db284a81a16799396ab460e226 Mon Sep 17 00:00:00 2001 From: Oleg Silkin Date: Mon, 20 May 2019 05:56:20 -0400 Subject: [PATCH] moves settings into json format --- config/conf.json | 5 +++-- lbry-comment-server/__init__.py | 1 - lbry_comment_server/__init__.py | 11 +++++++---- lbry_comment_server/database.py | 16 ++-------------- lbry_comment_server/main.py | 2 +- lbry_comment_server/{conf.py => routes.py} | 0 lbry_comment_server/settings.py | 4 ++-- tests/database_test.py | 7 ++++--- 8 files changed, 19 insertions(+), 27 deletions(-) delete mode 100644 lbry-comment-server/__init__.py rename lbry_comment_server/{conf.py => routes.py} (100%) diff --git a/config/conf.json b/config/conf.json index 102e246..7375879 100644 --- a/config/conf.json +++ b/config/conf.json @@ -1,9 +1,10 @@ { - "files": { + "path": { "schema": "schema/comments_ddl.sql", "main": "database/comments.db", "backup": "database/comments.backup.db", - "dev": "database/example.db" + "default": "database/default.db", + "test": "tests/test.db" }, "anonymous": { "channel_id": "9cb713f01bf247a0e03170b5ed00d5161340c486", diff --git a/lbry-comment-server/__init__.py b/lbry-comment-server/__init__.py deleted file mode 100644 index 8b13789..0000000 --- a/lbry-comment-server/__init__.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/lbry_comment_server/__init__.py b/lbry_comment_server/__init__.py index 75dad5e..f4f8efa 100644 --- a/lbry_comment_server/__init__.py +++ b/lbry_comment_server/__init__.py @@ -1,5 +1,8 @@ -from lbry_comment_server.database import obtain_connection, generate_schema -from lbry_comment_server.database import get_comments_by_id, get_comment_ids, get_claim_comments, create_comment -from lbry_comment_server.database import create_backup, validate_input -from lbry_comment_server.conf import database_dir, anonymous, schema_dir, backup_dir, project_dir +from lbry_comment_server.settings import config +from lbry_comment_server.database import obtain_connection, validate_input, get_claim_comments +from lbry_comment_server.database import get_comments_by_id, get_comment_ids, create_comment +schema = config['path']['schema'] +database_fp = config['path']['dev'] +backup = config['path']['backup'] +anonymous = config['anonymous'] diff --git a/lbry_comment_server/database.py b/lbry_comment_server/database.py index 0430cc4..998a8e8 100644 --- a/lbry_comment_server/database.py +++ b/lbry_comment_server/database.py @@ -3,29 +3,17 @@ import typing import re import nacl.hash import time -from lbry_comment_server.conf import * +from lbry_comment_server import anonymous, database_fp def obtain_connection(filepath: str = None, row_factory: bool = True): - filepath = filepath if filepath else database_dir + filepath = filepath if filepath else database_fp connection = sqlite3.connect(filepath) if row_factory: connection.row_factory = sqlite3.Row return connection -def generate_schema(conn: sqlite3.Connection, filepath: str = None): - filepath = filepath if filepath else schema_dir - with open(filepath, 'r') as ddl_file: - conn.executescript(ddl_file.read()) - - -def create_backup(conn: sqlite3.Connection, filepath: str = None): - filepath = filepath if filepath else backup_dir - with sqlite3.connect(filepath) as back: - conn.backup(back) - - def get_claim_comments(conn: sqlite3.Connection, claim_id: str, parent_id: str = None, page: int = 1, page_size: int = 50, top_level=False): if top_level: diff --git a/lbry_comment_server/main.py b/lbry_comment_server/main.py index 77b25f5..07ced75 100644 --- a/lbry_comment_server/main.py +++ b/lbry_comment_server/main.py @@ -4,7 +4,7 @@ import json from aiohttp import web from settings import config -from lbry_comment_server import database_dir +from lbry_comment_server import database_fp from lbry_comment_server.database import obtain_connection ERRORS = { diff --git a/lbry_comment_server/conf.py b/lbry_comment_server/routes.py similarity index 100% rename from lbry_comment_server/conf.py rename to lbry_comment_server/routes.py diff --git a/lbry_comment_server/settings.py b/lbry_comment_server/settings.py index 541da69..b515a8c 100644 --- a/lbry_comment_server/settings.py +++ b/lbry_comment_server/settings.py @@ -8,8 +8,8 @@ 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['files'].items(): - conf['files'][key] = str(root_dir / path) + for key, path in conf['path'].items(): + conf['path'][key] = str(root_dir / path) return conf diff --git a/tests/database_test.py b/tests/database_test.py index 8e658a1..26d433c 100644 --- a/tests/database_test.py +++ b/tests/database_test.py @@ -4,7 +4,8 @@ from faker.providers import internet from faker.providers import lorem from faker.providers import misc -import lbry_comment_server.conf as conf +import schema.db_helpers as schema +from lbry_comment_server.settings import config import lbry_comment_server.database as db import faker from random import randint @@ -18,8 +19,8 @@ fake.add_provider(misc) class DatabaseTestCase(unittest.TestCase): def setUp(self) -> None: super().setUp() - self.conn = db.obtain_connection('test.db') - db.generate_schema(self.conn, conf.schema_dir) + schema.setup_database(config['path']['test']) + self.conn = db.obtain_connection(config['path']['test']) def tearDown(self) -> None: curs = self.conn.execute('SELECT * FROM COMMENT')