forked from rotki/rotki
/
config.py
60 lines (46 loc) · 1.89 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import logging
import os
import platform
import shutil
from pathlib import Path
log = logging.getLogger(__name__)
def get_xdg_data_home() -> Path:
directory = os.environ.get('XDG_DATA_HOME', None)
if directory is None:
home = os.path.expanduser("~")
directory = os.path.join(home, '.local', 'share')
return Path(directory)
def get_win32_appdata() -> Path:
directory = os.environ.get('LOCALAPPDATA', None)
if not directory:
# In windows XP there is no localappdata
directory = os.environ.get('APPDATA', None)
if not directory:
raise AssertionError('Could not detect an APPDATA directory')
return Path(directory)
def old_data_directory() -> Path:
home = os.path.expanduser("~")
directory = os.path.join(home, '.rotkehlchen')
return Path(directory)
def default_data_directory() -> Path:
"""Find the default data directory for Rotki for each different OS
An interesting lirary that finds the data directories per OS is this:
https://github.com/ActiveState/appdirs/blob/master/appdirs.py
"""
if platform.system() == 'Linux':
xdgconfig = get_xdg_data_home()
datadir = xdgconfig / 'rotki' / 'data'
elif platform.system() == 'Windows':
appdata = get_win32_appdata()
datadir = appdata / 'rotki' / 'data'
elif platform.system() == 'Darwin':
datadir = Path(os.path.expanduser('~/Library/Application Support/rotki/data'))
else:
raise AssertionError(f'Rotki running in unknown system: {platform.system()}')
# If old data directory exists and new does not exist copy stuff
old_dir = old_data_directory()
if old_dir.exists() and not datadir.exists():
log.info(f'First time using standard data directory. Copying from {old_dir} to {datadir}')
shutil.copytree(old_dir, datadir)
datadir.mkdir(parents=True, exist_ok=True)
return datadir