Refactored user_dir to utils and replaced it in wallet and config
[electrum-nvc.git] / lib / util.py
1 import os
2 import platform
3 import sys
4
5 def print_error(*args):
6     # Stringify args
7     args = [str(item) for item in args]
8     sys.stderr.write(" ".join(args) + "\n")
9     sys.stderr.flush()
10
11 def user_dir():
12     if "HOME" in os.environ:
13       return os.path.join(os.environ["HOME"], ".electrum")
14     elif "LOCALAPPDATA" in os.environ:
15       return os.path.join(os.environ["LOCALAPPDATA"], "Electrum")
16     elif "APPDATA" in os.environ:
17       return os.path.join(os.environ["APPDATA"], "Electrum")
18     else:
19       raise BaseException("No home directory found in environment variables.")
20
21 def appdata_dir():
22     """Find the path to the application data directory; add an electrum folder and return path."""
23     if platform.system() == "Windows":
24         return os.path.join(os.environ["APPDATA"], "Electrum")
25     elif platform.system() == "Linux":
26         return os.path.join(sys.prefix, "share", "electrum")
27     elif (platform.system() == "Darwin" or
28           platform.system() == "DragonFly"):
29         return "/Library/Application Support/Electrum"
30     else:
31         raise Exception("Unknown system")
32
33 def get_resource_path(*args):
34     return os.path.join(".", *args)
35
36 def local_data_dir():
37     """Return path to the data folder."""
38     assert sys.argv
39     prefix_path = os.path.dirname(sys.argv[0])
40     local_data = os.path.join(prefix_path, "data")
41     return local_data
42
43 def load_theme_name(theme_path):
44     try:
45         with open(os.path.join(theme_path, "name.cfg")) as name_cfg_file:
46             return name_cfg_file.read().rstrip("\n").strip()
47     except IOError:
48         return None
49
50 def theme_dirs_from_prefix(prefix):
51     if not os.path.exists(prefix):
52         return []
53     theme_paths = {}
54     for potential_theme in os.listdir(prefix):
55         theme_full_path = os.path.join(prefix, potential_theme)
56         theme_css = os.path.join(theme_full_path, "style.css")
57         if not os.path.exists(theme_css):
58             continue
59         theme_name = load_theme_name(theme_full_path)
60         if theme_name is None:
61             continue
62         theme_paths[theme_name] = prefix, potential_theme
63     return theme_paths
64
65 def load_theme_paths():
66     theme_paths = {}
67     prefixes = (local_data_dir(), appdata_dir())
68     for prefix in prefixes:
69         theme_paths.update(theme_dirs_from_prefix(prefix))
70     return theme_paths
71