feat: save password to sqlite

This commit is contained in:
Ruidy 2021-08-02 19:45:29 +02:00
parent 30583a981f
commit 4216801998
8 changed files with 42 additions and 5 deletions

3
.gitignore vendored
View file

@ -90,3 +90,6 @@ Icon
Network Trash Folder Network Trash Folder
Temporary Items Temporary Items
.apdisk .apdisk
.idea
pg.db
test.txt

0
app/data/__init__.py Normal file
View file

14
app/data/sqlite.py Normal file
View file

@ -0,0 +1,14 @@
import sqlite3
class DB:
def __init__(self, db_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)")
def commit(self) -> None:
self.connection.commit()
def execute(self, query: str, *args) -> None:
self.cursor.execute(query, *args)

View file

@ -3,8 +3,10 @@ from typing import Optional
import typer import typer
import app.data.sqlite as sqlite
import app.usecases.pass_gen as pass_gen import app.usecases.pass_gen as pass_gen
import app.usecases.utils as utils import app.usecases.utils as utils
from app.repositories.sqlite import PasswordRepository
app = typer.Typer() app = typer.Typer()
@ -44,8 +46,9 @@ def main(
symbols=symbols, symbols=symbols,
numbers=numbers, numbers=numbers,
) )
db = sqlite.DB()
password = pass_gen.generate_password(options) sqlite_repo = PasswordRepository(db)
password = pass_gen.generate_password(sqlite_repo, options)
typer.echo(typer.style(f"🔐 {password}", fg=typer.colors.GREEN, bold=True)) typer.echo(typer.style(f"🔐 {password}", fg=typer.colors.GREEN, bold=True))

View file

View file

@ -0,0 +1,13 @@
from typing import Any
class PasswordRepository:
def __init__(self, db: Any) -> None:
self.db = db
def save(self, password: str) -> None:
try:
self.db.execute(f"INSERT INTO passwords VALUES (:password)", {"password": password})
self.db.commit()
except Exception as e:
print(e)

View file

@ -4,6 +4,8 @@ from typing import Protocol
from pydantic import BaseModel from pydantic import BaseModel
from app.repositories.sqlite import PasswordRepository
class PassGenOptions(BaseModel): class PassGenOptions(BaseModel):
seed: int seed: int
@ -12,10 +14,12 @@ class PassGenOptions(BaseModel):
numbers: bool = True numbers: bool = True
def generate_password(options: PassGenOptions) -> str: def generate_password(repo: PasswordRepository, options: PassGenOptions) -> str:
characters = _build_characters(symbols=options.symbols, numbers=options.numbers) characters = _build_characters(symbols=options.symbols, numbers=options.numbers)
random_generator = _new_random_generator(options.seed) random_generator = _new_random_generator(options.seed)
return "".join(random_generator.sample(characters, options.length)) password = "".join(random_generator.sample(characters, options.length))
repo.save(password)
return password
class RandomSampler(Protocol): class RandomSampler(Protocol):

View file

@ -1 +1 @@
2yW4AcqG _B'-*/U|devSqHh@