From 1e1a672143482d9cc13495110da95748c2bdf9fd Mon Sep 17 00:00:00 2001 From: andrei Date: Wed, 23 Feb 2022 13:37:17 +0200 Subject: [PATCH] rewrited config + updated tarantooldb class --- planetmint/__init__.py | 15 ++++++-- planetmint/backend/connection_tarantool.py | 41 ++++++++++++---------- planetmint/backend/tarantool/drop_db.txt | 23 ++++++++++++ 3 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 planetmint/backend/tarantool/drop_db.txt diff --git a/planetmint/__init__.py b/planetmint/__init__.py index 73b10ae..60978c6 100644 --- a/planetmint/__init__.py +++ b/planetmint/__init__.py @@ -46,6 +46,16 @@ _database_map = { 'tarantool_db': _database_tarantool, } +init_config = { + "init_file": "init_db.txt", + "relative_path": "backend/tarantool/" +} + +drop_config = { + "drop_file": "drop_db.txt", + "relative_path": "backend/tarantool/" +} + config = { 'server': { # Note: this section supports all the Gunicorn settings: @@ -59,7 +69,8 @@ config = { "login": "admin", "host": "admin:pass@127.0.0.1:3301", "service": "tarantoolctl connect", - "init_file": "init_db.txt" + "init_config": init_config, + "drop_config": drop_config }, 'wsserver': { 'scheme': 'ws', @@ -96,7 +107,7 @@ config = { # for more info. _config = copy.deepcopy(config) # TODO Check what to do with those imports from planetmint.common.transaction import Transaction # noqa -from planetmint import models # noqa +from planetmint import models # noqa from planetmint.upsert_validator import ValidatorElection # noqa from planetmint.elections.vote import Vote # noqa diff --git a/planetmint/backend/connection_tarantool.py b/planetmint/backend/connection_tarantool.py index cf87ed0..bf58a53 100644 --- a/planetmint/backend/connection_tarantool.py +++ b/planetmint/backend/connection_tarantool.py @@ -8,13 +8,8 @@ from importlib import import_module from itertools import repeat import tarantool - -import os -import pathlib - -from planetmint.backend.tarantool.utils import run - import planetmint + from planetmint.backend.exceptions import ConnectionError from planetmint.backend.utils import get_planetmint_config_value, get_planetmint_config_value_or_key_error from planetmint.common.exceptions import ConfigurationError @@ -29,29 +24,37 @@ logger = logging.getLogger(__name__) class TarantoolDB: def __init__(self, host: str, port: int, user: str, password: str, reset_database: bool = False): if reset_database: + self.drop_database() self.init_database() self.db_connect = None self.db_connect = tarantool.connect(host=host, port=port, user=user, password=password) - def __init_tarantool(self): + def get_connection(self, space_name: str = None): + return self.db_connect if space_name is None else self.db_connect.space(space_name) + + def __read_commands(self, file_path): + with open(file_path, "r") as cmd_file: + commands = [line + '\n' for line in cmd_file.readlines() if len(str(line)) > 1] + cmd_file.close() + return commands + + def drop_database(self): from planetmint.backend.tarantool.utils import run - config = get_planetmint_config_value_or_key_error("ctl_config") - with open(config["init_file"], 'r') as file: - commands = [line + '\n' for line in file.readlines() if len(str(line)) > 1] - file.close() + config = get_planetmint_config_value_or_key_error("ctl_config")["drop_config"] + f_path = "%s%s" % (config["relative_path"], config["drop_file"]) + commands = self.__read_commands(file_path=f_path) run(commands=commands, config=config) - def __drop_tarantool(self): - pass - - def get_connection(self): - return self.db_connect - def init_database(self): - pass + from planetmint.backend.tarantool.utils import run + config = get_planetmint_config_value_or_key_error("ctl_config")["init_config"] + f_path = "%s%s" % (config["relative_path"], config["init_file"]) + commands = self.__read_commands(file_path=f_path) + run(commands=commands, config=config) -def connect(host: str = None, port: int = None, username: str = "admin", password: str = "pass", backend: str = None): +def connect(host: str = None, port: int = None, username: str = "admin", password: str = "pass", + backend: str = None): backend = backend or get_planetmint_config_value_or_key_error('backend') # TODO Rewrite Configs host = host or get_planetmint_config_value_or_key_error('host') port = port or get_planetmint_config_value_or_key_error('port') diff --git a/planetmint/backend/tarantool/drop_db.txt b/planetmint/backend/tarantool/drop_db.txt new file mode 100644 index 0000000..14f513b --- /dev/null +++ b/planetmint/backend/tarantool/drop_db.txt @@ -0,0 +1,23 @@ +abci_chains:drop() + +assets:drop() + +blocks:drop() + +blocks_tx:drop() + +elections:drop() + +meta_datas:drop() + +pre_commits:drop() + +validators:drop() + +transactions:drop() + +inputs:drop() + +outputs:drop() + +keys:drop() \ No newline at end of file