diff --git a/.gitignore b/.gitignore index 2f9c3cf..b29f4fe 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ venv/ .idea/ *.pyc *.swp -.mypy_cache/ \ No newline at end of file +.mypy_cache/ +*.db diff --git a/cli.py b/cli.py index abbc72c..26bbe44 100644 --- a/cli.py +++ b/cli.py @@ -4,11 +4,8 @@ from formatter import Styles, show_style -def show_all_styles(): - all_styles: List[str] = [ - f'{style.value}. {style.name}'.lower() - for style in Styles.STYLE_MAPPING.values() - ] +def show_all_styles(db_config): + all_styles: List[str] = db_config.list() print('\n'.join(all_styles)) @@ -16,10 +13,10 @@ def chosen_style_exists(chosen_style: str) -> bool: return chosen_style in Styles.STYLE_MAPPING -def handle_arguments(parser: argparse.ArgumentParser): +def handle_arguments(parser: argparse.ArgumentParser, db_config): arguments = parser.parse_args() if arguments.show_styles: - show_all_styles() + show_all_styles(db_config) return if not arguments.chosen_style: parser.print_help() @@ -31,7 +28,7 @@ def handle_arguments(parser: argparse.ArgumentParser): arguments.text) -def init_argparser(): +def init_argparser(db_config): parser = argparse.ArgumentParser( description='This is parser for the formatter flags') parser.add_argument('-a', @@ -47,4 +44,4 @@ def init_argparser(): action='store', help='text for formatting', nargs='?') - handle_arguments(parser) + handle_arguments(parser, db_config) diff --git a/db.py b/db.py new file mode 100644 index 0000000..993e386 --- /dev/null +++ b/db.py @@ -0,0 +1,74 @@ +import os + +from typing import List + +from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String +from sqlalchemy.sql import select +import sqlalchemy_utils + +SQLITE_DB_PATH = 'styles.db' +SQLITE_URL = f'sqlite:///{SQLITE_DB_PATH}' + + +class DBConfig(): + + STYLE_TABLE_NAME = 'styles' + + def __init__(self, db_url: str): + self.__engine = None + self.meta = None + self.db_url = db_url + self.__styles = None + + def create_styles_table(self): + self.meta = MetaData() + if not self.styles_table_exists(): + self.__styles = Table( + self.STYLE_TABLE_NAME, + self.meta, + Column('id', Integer, primary_key=True), + Column('name', String), + Column('body', String), + ) + self.meta.create_all(self.__engine) + + conn = self.__engine.connect() + conn.execute(self.__styles.insert().values(name='hash_border', + body='')) + conn.execute(self.__styles.insert().values(name='plain_text', + body='')) + conn.execute(self.__styles.insert().values(name='at_sign', + body='')) + else: + self.__styles = Table( + self.STYLE_TABLE_NAME, + self.meta, + Column('id', Integer, primary_key=True), + Column('name', String), + Column('body', String), + ) + + def connect(self): + self.__engine = create_engine(self.db_url, echo=True) + + def styles_table_exists(self) -> bool: + return self.__engine.dialect.has_table(self.__engine, + self.STYLE_TABLE_NAME) + + def list(self) -> List[str]: + conn = self.__engine.connect() + select_query = select([self.__styles]) + styles = conn.execute(select_query) + all_styles: List[str] = [style.name for style in styles] + return all_styles + + +def get_db_object(): + db_config = DBConfig(SQLITE_URL) + db_config.connect() + db_config.create_styles_table() + return db_config + + +def init_styles_db(): + return get_db_object() diff --git a/main b/main index f470afc..7bad62f 100755 --- a/main +++ b/main @@ -3,6 +3,10 @@ Module for launching formater """ from cli import init_argparser +from db import init_styles_db + if __name__ == '__main__': - init_argparser() \ No newline at end of file + db_config = init_styles_db() + init_argparser(db_config) + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8cbd72e --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +SQLAlchemy==1.3.12