Created print_error docstring
[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     if platform.system() == "Windows":
14         return os.path.join(os.environ["APPDATA"], "Electrum")
15     elif platform.system() == "Linux":
16         return os.path.join(sys.prefix, "share", "electrum")
17     elif (platform.system() == "Darwin" or
18           platform.system() == "DragonFly"):
19         return "/Library/Application Support/Electrum"
20     else:
21         raise Exception("Unknown system")
22
23 def get_resource_path(*args):
24     return os.path.join(".", *args)
25
26 def local_data_dir():
27     assert sys.argv
28     prefix_path = os.path.dirname(sys.argv[0])
29     local_data = os.path.join(prefix_path, "data")
30     return local_data
31
32 def load_theme_name(theme_path):
33     try:
34         with open(os.path.join(theme_path, "name.cfg")) as name_cfg_file:
35             return name_cfg_file.read().rstrip("\n").strip()
36     except IOError:
37         return None
38
39 def theme_dirs_from_prefix(prefix):
40     if not os.path.exists(prefix):
41         return []
42     theme_paths = {}
43     for potential_theme in os.listdir(prefix):
44         theme_full_path = os.path.join(prefix, potential_theme)
45         theme_css = os.path.join(theme_full_path, "style.css")
46         if not os.path.exists(theme_css):
47             continue
48         theme_name = load_theme_name(theme_full_path)
49         if theme_name is None:
50             continue
51         theme_paths[theme_name] = prefix, potential_theme
52     return theme_paths
53
54 def load_theme_paths():
55     theme_paths = {}
56     prefixes = (local_data_dir(), appdata_dir())
57     for prefix in prefixes:
58         theme_paths.update(theme_dirs_from_prefix(prefix))
59     return theme_paths
60