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