separate bitcoin related functions from wallet.py
[electrum-nvc.git] / lib / util.py
1 import os, sys
2 import platform
3
4 def print_error(*args):
5     # Stringify args
6     args = [str(item) for item in args]
7     sys.stderr.write(" ".join(args) + "\n")
8     sys.stderr.flush()
9
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
22 def appdata_dir():
23     """Find the path to the application data directory; add an electrum folder and return path."""
24     if platform.system() == "Windows":
25         return os.path.join(os.environ["APPDATA"], "Electrum")
26     elif platform.system() == "Linux":
27         return os.path.join(sys.prefix, "share", "electrum")
28     elif (platform.system() == "Darwin" or
29           platform.system() == "DragonFly"):
30         return "/Library/Application Support/Electrum"
31     else:
32         raise Exception("Unknown system")
33
34
35 def get_resource_path(*args):
36     return os.path.join(".", *args)
37
38
39 def local_data_dir():
40     """Return path to the data folder."""
41     assert sys.argv
42     prefix_path = os.path.dirname(sys.argv[0])
43     local_data = os.path.join(prefix_path, "data")
44     return local_data
45
46
47 def format_satoshis(x, is_diff=False, num_zeros = 0):
48     from decimal import Decimal
49     s = Decimal(x)
50     sign, digits, exp = s.as_tuple()
51     digits = map(str, digits)
52     while len(digits) < 9:
53         digits.insert(0,'0')
54     digits.insert(-8,'.')
55     s = ''.join(digits).rstrip('0')
56     if sign: 
57         s = '-' + s
58     elif is_diff:
59         s = "+" + s
60
61     p = s.find('.')
62     s += "0"*( 1 + num_zeros - ( len(s) - p ))
63     s += " "*( 9 - ( len(s) - p ))
64     s = " "*( 5 - ( p )) + s
65     return s