From 97a21ae09e5cc8098841a212ab5a995329a7793f Mon Sep 17 00:00:00 2001 From: Ruidy Date: Tue, 3 Aug 2021 10:20:40 +0200 Subject: [PATCH] chore: explicit types --- README.md | 2 +- app/data/sqlite.py | 5 +++-- app/data/type.py | 9 +++++++++ app/main.py | 7 +++---- app/repositories/sqlite.py | 12 +++++++++--- 5 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 app/data/type.py diff --git a/README.md b/README.md index ebd63ac..b1f15e1 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ A simple password generator command line interface. - [x] Include numbers - [x] As a user I want the generated password to be copied to the clipboard - [x] As a user I want the generated password to be saved to a file -- [ ] As a user I want to store generated passwords in a database for later usage +- [x] As a user I want to store generated passwords in a database for later usage - [ ] Passwords must not be associated directly with the service they were generated for - [ ] I can verify if the password is associated to a given service diff --git a/app/data/sqlite.py b/app/data/sqlite.py index 1fd146a..d44b986 100644 --- a/app/data/sqlite.py +++ b/app/data/sqlite.py @@ -1,8 +1,9 @@ import sqlite3 +from typing import Any class DB: - def __init__(self, db_str="pg.db") -> None: + def __init__(self, db_str: str = "pg.db") -> None: self.connection = sqlite3.connect(db_str) self.cursor = self.connection.cursor() self.execute("CREATE TABLE IF NOT EXISTS passwords (password text)") @@ -10,5 +11,5 @@ class DB: def commit(self) -> None: self.connection.commit() - def execute(self, query: str, *args) -> None: + def execute(self, query: str, *args: Any) -> None: self.cursor.execute(query, *args) diff --git a/app/data/type.py b/app/data/type.py new file mode 100644 index 0000000..9648bd8 --- /dev/null +++ b/app/data/type.py @@ -0,0 +1,9 @@ +from typing import Any, Protocol + + +class DBConnector(Protocol): + def commit(self) -> None: + ... + + def execute(self, query: str, *args: Any) -> None: + ... diff --git a/app/main.py b/app/main.py index 8b392bb..db08e7d 100644 --- a/app/main.py +++ b/app/main.py @@ -3,10 +3,9 @@ from typing import Optional import typer -import app.data.sqlite as sqlite +import app.repositories.sqlite as sqlite import app.usecases.pass_gen as pass_gen import app.usecases.utils as utils -from app.repositories.sqlite import PasswordRepository app = typer.Typer() @@ -39,6 +38,7 @@ def main( ), random: bool = True, ) -> None: + sqlite_repo = sqlite.get_instance() seed = r.randint(0, 100) if random else 0 options = pass_gen.PassGenOptions( seed=seed, @@ -46,8 +46,7 @@ def main( symbols=symbols, numbers=numbers, ) - db = sqlite.DB() - sqlite_repo = PasswordRepository(db) + password = pass_gen.generate_password(sqlite_repo, options) typer.echo(typer.style(f"🔐 {password}", fg=typer.colors.GREEN, bold=True)) diff --git a/app/repositories/sqlite.py b/app/repositories/sqlite.py index 3c72753..edd8077 100644 --- a/app/repositories/sqlite.py +++ b/app/repositories/sqlite.py @@ -1,13 +1,19 @@ -from typing import Any +import app.data.sqlite as sqlite +from app.data.type import DBConnector class PasswordRepository: - def __init__(self, db: Any) -> None: + def __init__(self, db: DBConnector) -> None: self.db = db def save(self, password: str) -> None: try: - self.db.execute(f"INSERT INTO passwords VALUES (:password)", {"password": password}) + self.db.execute("INSERT INTO passwords VALUES (:password)", {"password": password}) self.db.commit() except Exception as e: print(e) + + +def get_instance() -> PasswordRepository: + db = sqlite.DB() + return PasswordRepository(db)