Skip to content
Merged
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
7 changes: 6 additions & 1 deletion croco_cli/cli/change.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import click
from croco_cli.database import database
from croco_cli.database import Database
from croco_cli.types import Option, Wallet, CustomAccount
from croco_cli.utils import show_key_mode, sort_wallets, echo_error, make_screen_option

Expand All @@ -12,6 +12,7 @@ def change():
def _make_wallet_option(wallet: Wallet) -> Option:
"""Create a new wallet option for keyboard interactive mode"""
label = wallet['label']
database = Database()

def _handler():
database.set_wallet(wallet['private_key'], label)
Expand All @@ -35,6 +36,7 @@ def _deleting_handler():
def _make_custom_option(account: CustomAccount) -> Option:
"""Create a new custom account option for keyboard interactive mode"""
label = account["email"]
database = Database()

def _handler():
account.pop('current')
Expand All @@ -59,6 +61,8 @@ def _deleting_handler():
def _wallet():
"""Change the current wallet for unit tests"""
wallets = []
database = Database()

if database.wallets.table_exists():
wallets = database.get_wallets()

Expand All @@ -76,6 +80,7 @@ def _wallet():
def custom():
"""Change the custom user account"""
accounts_map = dict()
database = Database()

custom_accounts = database.get_custom_accounts()
if not len(custom_accounts):
Expand Down
15 changes: 10 additions & 5 deletions croco_cli/cli/init.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""
This module contains functions to initialize python packages and projects
"""

import datetime
import os
import click
from croco_cli.utils import snake_case, require_github
from croco_cli.database import database
from croco_cli.database import Database


@click.group()
Expand All @@ -25,6 +25,8 @@ def _add_poetry(
:param is_package: Whether project should be configured as Python package
:return: None
"""
database = Database()

snaked_name = snake_case(project_name)
github_user = database.get_github_user()

Expand Down Expand Up @@ -93,6 +95,8 @@ def _initialize_folders(
:param description: The description of the project
:return: None
"""
database = Database()

github_user = database.get_github_user()
snaked_name = snake_case(project_name)
os.mkdir(snaked_name)
Expand Down Expand Up @@ -132,7 +136,7 @@ def _initialize_folders(
with open('LICENSE', 'w') as license_file:
license_file.write(f"""MIT License

Copyright (c) 2023 {github_user['name']}
Copyright (c) {datetime.datetime.now().year} {github_user['name']}

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -168,8 +172,7 @@ async def main():
if __name__ == '__main__':
asyncio.run(main())""")
with open('globals.py', 'w') as global_file:
global_file.write(f"""
import os
global_file.write(f"""import os
import tomllib

PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
Expand Down Expand Up @@ -197,6 +200,8 @@ def _add_readme(
:param is_package: Whether the readme should be configured for the Python package
:return: None
"""
database = Database()

github_user = database.get_github_user()
content = (f"""# {project_name}

Expand Down
4 changes: 3 additions & 1 deletion croco_cli/cli/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import os
import click
from functools import partial
from croco_cli.database import Database
from croco_cli.types import Option, Package, GithubPackage, PackageSet
from croco_cli.utils import show_key_mode, require_github, is_github_package
from croco_cli.globals import PYPI_PACKAGES, GITHUB_PACKAGES, PACKAGE_SETS
from croco_cli.database import database

_DESCRIPTION = "Install Croco Factory packages"

Expand Down Expand Up @@ -47,6 +47,8 @@ def _make_install_option(
:param package: package to install
:return: An installing option
"""
database = Database()

github_user = database.get_github_user()
package_name = package['name']
description = package['description']
Expand Down
4 changes: 3 additions & 1 deletion croco_cli/cli/make.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import click
from croco_cli.database import Database
from croco_cli.utils import require_wallet, constant_case
from croco_cli.database import database


@click.group()
Expand All @@ -13,6 +13,8 @@ def make():
@require_wallet
def dotenv():
"""Make file with environment variables. Use with python-dotenv"""
database = Database()

wallets = database.get_wallets()
custom_accounts = database.get_custom_accounts(current=True)
env_variables = database.get_env_variables()
Expand Down
4 changes: 3 additions & 1 deletion croco_cli/cli/reset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import click
from croco_cli.database import database
from croco_cli.database import Database


@click.command()
Expand Down Expand Up @@ -45,6 +45,8 @@
)
def reset(user: bool, git: bool, wallets: bool, custom: bool, envar: bool):
"""Reset user accounts"""
database = Database()

if git or wallets or custom or envar:
if git:
database.github_users.drop_table()
Expand Down
12 changes: 10 additions & 2 deletions croco_cli/cli/set.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import click
from typing import Optional
from croco_cli.database import database
from croco_cli.types import CustomAccount, Wallet
from .user import _show_github, _show_custom_account
from croco_cli.utils import show_wallet, show_detail, constant_case
from croco_cli.database import Database


@click.group(name='set')
Expand All @@ -17,6 +17,8 @@ def _set():
@click.argument('mnemonic', default=None, required=False, type=click.STRING)
def wallet(private_key: str, label: Optional[str] = None, mnemonic: Optional[str] = None) -> None:
"""Set wallet for unit tests using its private key"""
database = Database()

database.set_wallet(private_key, label, mnemonic)
public_key = database.get_public_key(private_key)
current_wallet = Wallet(
Expand All @@ -33,10 +35,12 @@ def wallet(private_key: str, label: Optional[str] = None, mnemonic: Optional[str
@click.argument('access_token', default=None, required=False, type=click.STRING)
def git(access_token: str):
"""Set GitHub user account, using access token"""
database = Database()

if not access_token:
access_token = click.prompt('Please enter the access token of new account', hide_input=True)

database.set_github(access_token)
database.set_github_user(access_token)
_show_github()


Expand All @@ -62,6 +66,8 @@ def custom(
email_password: Optional[str] = None
) -> None:
"""Set a custom user account"""
database = Database()

email_password = email_password or password

data = {key: value for key, value in fields}
Expand All @@ -85,6 +91,8 @@ def custom(
@click.argument('value', type=click.STRING)
def envar(key: str, value: str) -> None:
"""Set an environment variable"""
database = Database()

key = constant_case(key)
database.set_env_variable(key, value)
show_detail(key, value, 0)
6 changes: 5 additions & 1 deletion croco_cli/cli/user.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import click
from croco_cli.database import database
from croco_cli.database import Database
from croco_cli.types import CustomAccount
from croco_cli.utils import require_github, show_detail, show_label, hide_value, show_account_dict, show_wallets, echo_error

Expand Down Expand Up @@ -43,6 +43,8 @@ def user(git: bool, wallets: bool, custom: bool) -> None:

def _show_github() -> None:
"""Show GitHub user account"""
database = Database()

github_user = database.get_github_user()
access_token = hide_value(github_user['access_token'], 10)
show_label('GitHub')
Expand All @@ -66,6 +68,8 @@ def _show_custom_account(custom_account: CustomAccount) -> None:

def _show_custom_accounts() -> None:
"""Show custom accounts of user"""
database = Database()

if not database.custom_accounts.table_exists():
echo_error('There are no custom accounts to show')
return
Expand Down
17 changes: 12 additions & 5 deletions croco_cli/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
from peewee import Model, CharField, BlobField, SqliteDatabase, BooleanField


class _DatabaseMeta(type):
_instance = None

def __call__(cls, *args, **kwargs):
if not isinstance(cls._instance, cls):
cls._instance = super().__call__(*args, **kwargs)

return cls._instance


def _get_cache_folder() -> str:
username = getpass.getuser()
os_name = os.name
Expand All @@ -32,7 +42,7 @@ def _get_cache_folder() -> str:
return cache_path


class Database:
class Database(metaclass=_DatabaseMeta):
_path: ClassVar[str] = os.path.join(_get_cache_folder(), 'user.db')
interface: ClassVar[SqliteDatabase] = SqliteDatabase(_path)

Expand Down Expand Up @@ -368,7 +378,7 @@ def set_env_variable(
def get_env_variables(self) -> list[EnvVariable] | None:
env_variables = self._env_variables
if not env_variables.table_exists():
return
return

query = env_variables.select()

Expand All @@ -383,6 +393,3 @@ def get_env_variables(self) -> list[EnvVariable] | None:
def delete_env_variables(self) -> None:
env_variables = self._env_variables
env_variables.delete().execute()


database = Database()
1 change: 0 additions & 1 deletion croco_cli/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

GITHUB_PACKAGES = (
_PY_OKX,
_SELDEGEN
)

PACKAGE_SETS = {
Expand Down
Loading