Skip to content

Commit

Permalink
fix: store data in system config dir instead of module directory
Browse files Browse the repository at this point in the history
  • Loading branch information
vanutp committed Dec 26, 2021
1 parent 27a2b48 commit 7d92544
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
14 changes: 13 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions tgpy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions tgpy/run_code/builtin_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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:])


Expand Down
47 changes: 32 additions & 15 deletions tgpy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='|')
Expand Down Expand Up @@ -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']

0 comments on commit 7d92544

Please sign in to comment.