chore: explicit types

This commit is contained in:
Ruidy 2021-08-03 10:20:40 +02:00
parent 80776e8be4
commit 97a21ae09e
5 changed files with 25 additions and 10 deletions

View file

@ -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

View file

@ -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)

9
app/data/type.py Normal file
View file

@ -0,0 +1,9 @@
from typing import Any, Protocol
class DBConnector(Protocol):
def commit(self) -> None:
...
def execute(self, query: str, *args: Any) -> None:
...

View file

@ -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))

View file

@ -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)