Пример #1
0
def create_settings():
    try:
        open(appdata(Files.settings), 'x')
    except FileExistsError:
        pass
    with open(appdata(Files.settings), 'w') as f:
        dump(default_settings, f)
Пример #2
0
def check_alpha(password: bytes):
    with open(appdata(Files.beta_key), 'rb') as f:
        kdf = PBKDF2HMAC(algorithm=hashes.SHA256(),
                         length=32,
                         salt=f.read(),
                         iterations=100000,
                         backend=default_backend())
    with open(appdata(Files.alpha_key), 'rb') as f:
        return urlsafe_b64encode(kdf.derive(password)) == f.read()
Пример #3
0
def generate_alpha(preset: bytes):
    """Generates Alpha.key file (master password)."""
    create_in_appdata(Files.alpha_key)
    with open(appdata(Files.beta_key), 'rb') as f:
        kdf = PBKDF2HMAC(algorithm=hashes.SHA256(),
                         length=32,
                         salt=f.read(),
                         iterations=100000,
                         backend=default_backend())
    with open(appdata(Files.alpha_key), 'wb') as f:
        f.write(urlsafe_b64encode(kdf.derive(preset)))
Пример #4
0
def set_setting(name: str, value):
    try:
        open(appdata(Files.settings))
    except FileNotFoundError:
        create_settings()
    finally:
        with open(appdata(Files.settings)) as f:
            settings = load(f)

    settings[name] = value

    with open(appdata(Files.settings), 'w') as f:
        dump(settings, f)
Пример #5
0
def get_setting(name: str, second_try=False):
    try:
        open(appdata(Files.settings))
    except FileNotFoundError:
        create_settings()
    finally:
        with open(appdata(Files.settings)) as f:
            settings = load(f)
    try:
        result = settings[name]
    except KeyError:
        if not second_try:
            reset_settings()
            return get_setting(name, second_try=True)
        else:
            return KeyError('Missing setting')
    else:
        return result
Пример #6
0
def query(q_input: str, q_args=None):
    """
    Executes query in SQLite database.
    :param q_input: SQL query.
    :param q_args: Query arguments (replaces ``?`` symbols in query).
    :return: All records or ``None`` if nothing is matching query.
    """
    if q_args is None:
        q_args = []
    create_in_appdata(Files.sqlite)
    with connect(appdata(Files.sqlite)) as conn:
        my_cursor = conn.cursor()
        my_cursor.execute(q_input, q_args)
        records = my_cursor.fetchall()
        conn.commit()
        return records
Пример #7
0
def generate_beta(preset: bytes):
    """Generates Beta.key file (salt)."""
    create_in_appdata(Files.beta_key)
    with open(appdata(Files.beta_key), 'wb') as f:
        f.write(preset)
Пример #8
0
def _key():
    with open(appdata(Files.alpha_key), 'rb') as f:
        return Fernet(f.read())
Пример #9
0
def create_locales_directory():
    try:
        mkdir(appdata('locales'))
    except FileExistsError:
        pass
Пример #10
0
def json_appdata(lang: str):
    return appdata(f'locales{sep}{lang}.json')