moves settings into json format

This commit is contained in:
Oleg Silkin 2019-05-20 05:56:20 -04:00
parent 75fb6fd9ba
commit 861ea1ea8c
8 changed files with 19 additions and 27 deletions

View file

@ -1,9 +1,10 @@
{ {
"files": { "path": {
"schema": "schema/comments_ddl.sql", "schema": "schema/comments_ddl.sql",
"main": "database/comments.db", "main": "database/comments.db",
"backup": "database/comments.backup.db", "backup": "database/comments.backup.db",
"dev": "database/example.db" "default": "database/default.db",
"test": "tests/test.db"
}, },
"anonymous": { "anonymous": {
"channel_id": "9cb713f01bf247a0e03170b5ed00d5161340c486", "channel_id": "9cb713f01bf247a0e03170b5ed00d5161340c486",

View file

@ -1 +0,0 @@

View file

@ -1,5 +1,8 @@
from lbry_comment_server.database import obtain_connection, generate_schema from lbry_comment_server.settings import config
from lbry_comment_server.database import get_comments_by_id, get_comment_ids, get_claim_comments, create_comment from lbry_comment_server.database import obtain_connection, validate_input, get_claim_comments
from lbry_comment_server.database import create_backup, validate_input from lbry_comment_server.database import get_comments_by_id, get_comment_ids, create_comment
from lbry_comment_server.conf import database_dir, anonymous, schema_dir, backup_dir, project_dir schema = config['path']['schema']
database_fp = config['path']['dev']
backup = config['path']['backup']
anonymous = config['anonymous']

View file

@ -3,29 +3,17 @@ import typing
import re import re
import nacl.hash import nacl.hash
import time 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): 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) connection = sqlite3.connect(filepath)
if row_factory: if row_factory:
connection.row_factory = sqlite3.Row connection.row_factory = sqlite3.Row
return connection 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, def get_claim_comments(conn: sqlite3.Connection, claim_id: str, parent_id: str = None,
page: int = 1, page_size: int = 50, top_level=False): page: int = 1, page_size: int = 50, top_level=False):
if top_level: if top_level:

View file

@ -4,7 +4,7 @@ import json
from aiohttp import web from aiohttp import web
from settings import config 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 from lbry_comment_server.database import obtain_connection
ERRORS = { ERRORS = {

View file

@ -8,8 +8,8 @@ config_path = root_dir / 'config' / 'conf.json'
def get_config(filepath): def get_config(filepath):
with open(filepath, 'r') as cfile: with open(filepath, 'r') as cfile:
conf = json.load(cfile) conf = json.load(cfile)
for key, path in conf['files'].items(): for key, path in conf['path'].items():
conf['files'][key] = str(root_dir / path) conf['path'][key] = str(root_dir / path)
return conf return conf

View file

@ -4,7 +4,8 @@ from faker.providers import internet
from faker.providers import lorem from faker.providers import lorem
from faker.providers import misc 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 lbry_comment_server.database as db
import faker import faker
from random import randint from random import randint
@ -18,8 +19,8 @@ fake.add_provider(misc)
class DatabaseTestCase(unittest.TestCase): class DatabaseTestCase(unittest.TestCase):
def setUp(self) -> None: def setUp(self) -> None:
super().setUp() super().setUp()
self.conn = db.obtain_connection('test.db') schema.setup_database(config['path']['test'])
db.generate_schema(self.conn, conf.schema_dir) self.conn = db.obtain_connection(config['path']['test'])
def tearDown(self) -> None: def tearDown(self) -> None:
curs = self.conn.execute('SELECT * FROM COMMENT') curs = self.conn.execute('SELECT * FROM COMMENT')