diff --git a/poetry.lock b/poetry.lock index 68ad98f..7564c8d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -9,6 +9,14 @@ python-versions = ">=3.5" [package.extras] dev = ["pytest", "pytest-cov"] +[[package]] +name = "appdirs" +version = "1.4.4" +description = "A small Python module for determining appropriate platform-specific dirs, e.g. a \"user data dir\"." +category = "main" +optional = false +python-versions = "*" + [[package]] name = "bleach" version = "4.1.0" @@ -610,13 +618,17 @@ testing = ["pytest (>=4.6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytes [metadata] lock-version = "1.1" python-versions = "^3.9" -content-hash = "aac971d893d4770d90abd2a2063b6f8c9bded2443752c070ebca051954048553" +content-hash = "914ae1c12a84fa2da7ce4cff679e3235e262119adbee3860d884775fbb486d4b" [metadata.files] aiorun = [ {file = "aiorun-2021.10.1-py3-none-any.whl", hash = "sha256:49c3b0ca9aa4c8ace443691d31d3114ad7a3abe7af19abb13725309dec2e3c2c"}, {file = "aiorun-2021.10.1.tar.gz", hash = "sha256:bb621493e008cd072a9b19de09a615513be2f48b594ee16ee726da5255fa4267"}, ] +appdirs = [ + {file = "appdirs-1.4.4-py2.py3-none-any.whl", hash = "sha256:a841dacd6b99318a741b166adb07e19ee71a274450e68237b4650ca1055ab128"}, + {file = "appdirs-1.4.4.tar.gz", hash = "sha256:7d5d0167b2b1ba821647616af46a749d1c653740dd0d2415100fe26e27afdf41"}, +] bleach = [ {file = "bleach-4.1.0-py2.py3-none-any.whl", hash = "sha256:4d2651ab93271d1129ac9cbc679f524565cc8a1b791909c4a51eac4446a15994"}, {file = "bleach-4.1.0.tar.gz", hash = "sha256:0900d8b37eba61a802ee40ac0061f8c2b5dee29c1927dd1d233e075ebf5a71da"}, diff --git a/pyproject.toml b/pyproject.toml index a8e9fe0..768366f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,6 +34,7 @@ PyYAML = "^6.0" pydantic = "^1.8.2" aiorun = "^2021.10.1" rich = "^10.16.1" +appdirs = "^1.4.4" [tool.poetry.dev-dependencies] python-semantic-release = "^7.23.0" diff --git a/tgpy/main.py b/tgpy/main.py index 0e86f18..d911a28 100644 --- a/tgpy/main.py +++ b/tgpy/main.py @@ -6,7 +6,7 @@ from tgpy.console import console from tgpy.handlers import add_handlers from tgpy.hooks import HookType, Hook -from tgpy.utils import migrate_from_old_versions, SESSION_FILENAME +from tgpy.utils import migrate_config, SESSION_FILENAME, create_config_dirs log = logging.getLogger(__name__) @@ -68,7 +68,8 @@ async def run_client(): async def _main(): - migrate_from_old_versions() + migrate_config() + create_config_dirs() app.config = Config.load() if not (app.config.api_id and app.config.api_hash): diff --git a/tgpy/run_code/builtin_functions.py b/tgpy/run_code/builtin_functions.py index 5bb9b7a..bcc3753 100644 --- a/tgpy/run_code/builtin_functions.py +++ b/tgpy/run_code/builtin_functions.py @@ -11,7 +11,7 @@ from tgpy.message_design import get_code from tgpy.run_code import variables from tgpy.run_code.utils import Context, filename_prefix, save_function_to_variables -from tgpy.utils import run_cmd, get_version, BASE_DIR +from tgpy.utils import run_cmd, get_version variables['ctx'] = ctx = Context() @@ -40,7 +40,6 @@ def restart(msg: Optional[str] = 'Restarted successfully'): datetime=dt.datetime.fromtimestamp(0), ) hook.save() - os.chdir(BASE_DIR) os.execl(sys.executable, sys.executable, '-m', 'tgpy', *sys.argv[1:]) diff --git a/tgpy/utils.py b/tgpy/utils.py index 6289d95..8d8699c 100644 --- a/tgpy/utils.py +++ b/tgpy/utils.py @@ -4,38 +4,55 @@ from pathlib import Path from subprocess import Popen, PIPE +import appdirs import yaml from pydantic import ValidationError from yaml.representer import SafeRepresenter -BASE_DIR = Path(__file__).parent.parent -DATA_DIR = BASE_DIR / 'data' -DATA_DIR.mkdir(exist_ok=True) +DATA_DIR = Path(appdirs.user_config_dir('tgpy')) HOOKS_DIR = DATA_DIR / 'hooks' -HOOKS_DIR.mkdir(exist_ok=True) WORKDIR = DATA_DIR / 'workdir' -WORKDIR.mkdir(exist_ok=True) CONFIG_FILENAME = DATA_DIR / 'config.yml' SESSION_FILENAME = DATA_DIR / 'TGPy.session' -def migrate_from_old_versions(): - from tgpy.app_config import Config +def create_config_dirs(): + DATA_DIR.mkdir(exist_ok=True) + HOOKS_DIR.mkdir(exist_ok=True) + WORKDIR.mkdir(exist_ok=True) + - old_session_file = BASE_DIR / 'TGPy.session' - if old_session_file.exists() and not SESSION_FILENAME.exists(): - old_session_file.rename(SESSION_FILENAME) - old_config_file = BASE_DIR / 'config.py' - if old_config_file.exists() and not CONFIG_FILENAME.exists(): +def migrate_config(): + from tgpy.app_config import Config + old_base_dir = Path(__file__).parent.parent + + old_data_dir = old_base_dir / 'data' + old_session_file = old_base_dir / 'TGPy.session' + new_session_file = old_data_dir / SESSION_FILENAME.name + if not old_session_file.exists() or DATA_DIR.exists(): + # old config file doesn't exist or new config file already exists + return + # ensure old_data_dir exists (it should, because old_session_file does) + old_data_dir.mkdir(exist_ok=True) + # migrate old session file + old_session_file.rename(new_session_file) + + # migrate old config file + old_config_file = old_base_dir / 'config.py' + new_config_file = old_data_dir / CONFIG_FILENAME.name + if old_config_file.exists(): try: config_mod = importlib.import_module('config') config = Config(api_id=config_mod.api_id, api_hash=config_mod.api_hash) - with open(CONFIG_FILENAME, 'w') as file: + with open(new_config_file, 'w') as file: yaml.safe_dump(config.dict(), file) except (ValidationError, AttributeError, ImportError): pass old_config_file.unlink() + # finally, move data dir to new location + old_data_dir.rename(DATA_DIR) + def _multiline_presenter(dumper, data): return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|') @@ -88,5 +105,5 @@ def get_version(): return 'unknown' -__all__ = ['BASE_DIR', 'DATA_DIR', 'HOOKS_DIR', 'WORKDIR', 'CONFIG_FILENAME', 'SESSION_FILENAME', - 'yaml_multiline_str', 'run_cmd', 'get_version', 'migrate_from_old_versions'] +__all__ = ['DATA_DIR', 'HOOKS_DIR', 'WORKDIR', 'CONFIG_FILENAME', 'SESSION_FILENAME', + 'yaml_multiline_str', 'run_cmd', 'get_version', 'migrate_config', 'create_config_dirs']