Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ venv/
.idea/
*.pyc
*.swp
.mypy_cache/
.mypy_cache/
*.db
15 changes: 6 additions & 9 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@
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))


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()
Expand All @@ -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',
Expand All @@ -47,4 +44,4 @@ def init_argparser():
action='store',
help='text for formatting',
nargs='?')
handle_arguments(parser)
handle_arguments(parser, db_config)
74 changes: 74 additions & 0 deletions db.py
Original file line number Diff line number Diff line change
@@ -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}'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better use combined DATABASE_URL and make it possible to override from os.environ (https://12factor.net/backing-services).



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()
6 changes: 5 additions & 1 deletion main
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
Module for launching formater
"""
from cli import init_argparser
from db import init_styles_db


if __name__ == '__main__':
init_argparser()
db_config = init_styles_db()
init_argparser(db_config)

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SQLAlchemy==1.3.12