|
5 | 5 | APP_NAME = 'Vorta' |
6 | 6 | APP_AUTHOR = 'BorgBase' |
7 | 7 | APP_ID_DARWIN = 'com.borgbase.client.macos' |
8 | | -dirs = platformdirs.PlatformDirs(APP_NAME, APP_AUTHOR) |
9 | | -SETTINGS_DIR = dirs.user_data_path |
10 | | -LOG_DIR = dirs.user_log_path |
11 | | -CACHE_DIR = dirs.user_cache_path |
12 | | -TEMP_DIR = CACHE_DIR / "tmp" |
13 | | -PROFILE_BOOTSTRAP_FILE = Path.home() / '.vorta-init.json' |
| 8 | +SETTINGS_DIR = None |
| 9 | +LOG_DIR = None |
| 10 | +CACHE_DIR = None |
| 11 | +TEMP_DIR = None |
| 12 | +PROFILE_BOOTSTRAP_FILE = None |
14 | 13 |
|
15 | 14 |
|
16 | | -# ensure directories exist |
17 | | -for dir in (SETTINGS_DIR, LOG_DIR, CACHE_DIR, TEMP_DIR): |
18 | | - dir.mkdir(parents=True, exist_ok=True) |
| 15 | +def default_dev_dir() -> Path: |
| 16 | + """Returns a default dir for config files in the project's main folder""" |
| 17 | + return Path(__file__).parent.parent.parent / '.dev_config' |
| 18 | + |
| 19 | + |
| 20 | +def init_from_platformdirs(): |
| 21 | + """Initializes config dirs for system-wide use""" |
| 22 | + dirs = platformdirs.PlatformDirs(APP_NAME, APP_AUTHOR) |
| 23 | + init(dirs.user_data_path, dirs.user_log_path, dirs.user_cache_path, dirs.user_cache_path / 'tmp', Path.home()) |
| 24 | + |
| 25 | + |
| 26 | +def init_dev_mode(dir: Path): |
| 27 | + """Initializes config dirs for local use inside provided dir""" |
| 28 | + dir_full_path = Path(dir).resolve() |
| 29 | + init( |
| 30 | + dir_full_path / 'settings', |
| 31 | + dir_full_path / 'logs', |
| 32 | + dir_full_path / 'cache', |
| 33 | + dir_full_path / 'tmp', |
| 34 | + dir_full_path, |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +def init(settings: Path, logs: Path, cache: Path, tmp: Path, bootstrap: Path): |
| 39 | + """Initializes config directories with provided paths""" |
| 40 | + global SETTINGS_DIR |
| 41 | + global LOG_DIR |
| 42 | + global CACHE_DIR |
| 43 | + global TEMP_DIR |
| 44 | + global PROFILE_BOOTSTRAP_FILE |
| 45 | + SETTINGS_DIR = settings |
| 46 | + LOG_DIR = logs |
| 47 | + CACHE_DIR = cache |
| 48 | + TEMP_DIR = tmp |
| 49 | + PROFILE_BOOTSTRAP_FILE = bootstrap / '.vorta-init.json' |
| 50 | + ensure_dirs() |
| 51 | + |
| 52 | + |
| 53 | +def ensure_dirs(): |
| 54 | + """Creates config dirs and parent dirs if they don't exist""" |
| 55 | + # ensure directories exist |
| 56 | + for dir in (SETTINGS_DIR, LOG_DIR, CACHE_DIR, TEMP_DIR): |
| 57 | + dir.mkdir(parents=True, exist_ok=True) |
| 58 | + |
| 59 | + |
| 60 | +# Make sure that the config values are valid |
| 61 | +init_from_platformdirs() |
0 commit comments