Dont migrate wallets for xp
[electrum-nvc.git] / lib / util.py
1 import os, sys
2 import platform
3 import shutil
4 from datetime import datetime
5 is_verbose = True
6
7
8
9 def set_verbosity(b):
10     global is_verbose
11     is_verbose = b
12
13 def print_error(*args):
14     if not is_verbose: return
15     args = [str(item) for item in args]
16     sys.stderr.write(" ".join(args) + "\n")
17     sys.stderr.flush()
18
19 def print_msg(*args):
20     # Stringify args
21     args = [str(item) for item in args]
22     sys.stdout.write(" ".join(args) + "\n")
23     sys.stdout.flush()
24
25
26 def check_windows_wallet_migration():
27     if platform.release() != "XP":
28         if os.path.exists(os.path.join(os.environ["LOCALAPPDATA"], "Electrum")):
29             if os.path.exists(os.path.join(os.environ["APPDATA"], "Electrum")):
30                 print_msg("Two Electrum folders have been found, the default Electrum location for Windows has changed from %s to %s since Electrum 1.7, please check your wallets and fix the problem manually." % (os.environ["LOCALAPPDATA"], os.environ["APPDATA"]))
31                 sys.exit()
32             try:
33                 shutil.move(os.path.join(os.environ["LOCALAPPDATA"], "Electrum"), os.path.join(os.environ["APPDATA"]))
34                 print_msg("Your wallet has been moved from %s to %s."% (os.environ["LOCALAPPDATA"], os.environ["APPDATA"]))
35             except:
36                 print_msg("Failed to move your wallet.")
37     
38
39 def user_dir():
40     if "HOME" in os.environ:
41         return os.path.join(os.environ["HOME"], ".electrum")
42     elif "APPDATA" in os.environ:
43         return os.path.join(os.environ["APPDATA"], "Electrum")
44     elif "LOCALAPPDATA" in os.environ:
45         return os.path.join(os.environ["LOCALAPPDATA"], "Electrum")
46     else:
47         #raise BaseException("No home directory found in environment variables.")
48         return 
49
50 def appdata_dir():
51     """Find the path to the application data directory; add an electrum folder and return path."""
52     if platform.system() == "Windows":
53         return os.path.join(os.environ["APPDATA"], "Electrum")
54     elif platform.system() == "Linux":
55         return os.path.join(sys.prefix, "share", "electrum")
56     elif (platform.system() == "Darwin" or
57           platform.system() == "DragonFly" or
58           platform.system() == "NetBSD"):
59         return "/Library/Application Support/Electrum"
60     else:
61         raise Exception("Unknown system")
62
63
64 def get_resource_path(*args):
65     return os.path.join(".", *args)
66
67
68 def local_data_dir():
69     """Return path to the data folder."""
70     assert sys.argv
71     prefix_path = os.path.dirname(sys.argv[0])
72     local_data = os.path.join(prefix_path, "data")
73     return local_data
74
75
76 def format_satoshis(x, is_diff=False, num_zeros = 0):
77     from decimal import Decimal
78     s = Decimal(x)
79     sign, digits, exp = s.as_tuple()
80     digits = map(str, digits)
81     while len(digits) < 9:
82         digits.insert(0,'0')
83     digits.insert(-8,'.')
84     s = ''.join(digits).rstrip('0')
85     if sign: 
86         s = '-' + s
87     elif is_diff:
88         s = "+" + s
89
90     p = s.find('.')
91     s += "0"*( 1 + num_zeros - ( len(s) - p ))
92     s += " "*( 9 - ( len(s) - p ))
93     s = " "*( 5 - ( p )) + s
94     return s
95
96
97 # Takes a timestamp and returns a string with the approximation of the age
98 def age(from_date, since_date = None, target_tz=None, include_seconds=False):
99     if from_date is None:
100         return "Unknown"
101
102     from_date = datetime.fromtimestamp(from_date)
103     if since_date is None:
104         since_date = datetime.now(target_tz)
105
106     distance_in_time = since_date - from_date
107     distance_in_seconds = int(round(abs(distance_in_time.days * 86400 + distance_in_time.seconds)))
108     distance_in_minutes = int(round(distance_in_seconds/60))
109
110     if distance_in_minutes <= 1:
111         if include_seconds:
112             for remainder in [5, 10, 20]:
113                 if distance_in_seconds < remainder:
114                     return "less than %s seconds ago" % remainder
115             if distance_in_seconds < 40:
116                 return "half a minute ago"
117             elif distance_in_seconds < 60:
118                 return "less than a minute ago"
119             else:
120                 return "1 minute ago"
121         else:
122             if distance_in_minutes == 0:
123                 return "less than a minute ago"
124             else:
125                 return "1 minute ago"
126     elif distance_in_minutes < 45:
127         return "%s minutes ago" % distance_in_minutes
128     elif distance_in_minutes < 90:
129         return "about 1 hour ago"
130     elif distance_in_minutes < 1440:
131         return "about %d hours ago" % (round(distance_in_minutes / 60.0))
132     elif distance_in_minutes < 2880:
133         return "1 day ago"
134     elif distance_in_minutes < 43220:
135         return "%d days ago" % (round(distance_in_minutes / 1440))
136     elif distance_in_minutes < 86400:
137         return "about 1 month ago"
138     elif distance_in_minutes < 525600:
139         return "%d months ago" % (round(distance_in_minutes / 43200))
140     elif distance_in_minutes < 1051200:
141         return "about 1 year ago"
142     else:
143         return "over %d years ago" % (round(distance_in_minutes / 525600))