4 from datetime import datetime
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")
21 args = [str(item) for item in args]
22 sys.stdout.write(" ".join(args) + "\n")
27 s = json.dumps(obj,sort_keys = True, indent = 4)
28 sys.stdout.write(s + "\n")
32 def check_windows_wallet_migration():
33 if platform.release() != "XP":
34 if os.path.exists(os.path.join(os.environ["LOCALAPPDATA"], "Electrum")):
35 if os.path.exists(os.path.join(os.environ["APPDATA"], "Electrum")):
36 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"]))
39 shutil.move(os.path.join(os.environ["LOCALAPPDATA"], "Electrum"), os.path.join(os.environ["APPDATA"]))
40 print_msg("Your wallet has been moved from %s to %s."% (os.environ["LOCALAPPDATA"], os.environ["APPDATA"]))
42 print_msg("Failed to move your wallet.")
46 if "HOME" in os.environ:
47 return os.path.join(os.environ["HOME"], ".electrum")
48 elif "APPDATA" in os.environ:
49 return os.path.join(os.environ["APPDATA"], "Electrum")
50 elif "LOCALAPPDATA" in os.environ:
51 return os.path.join(os.environ["LOCALAPPDATA"], "Electrum")
53 #raise BaseException("No home directory found in environment variables.")
57 """Find the path to the application data directory; add an electrum folder and return path."""
58 if platform.system() == "Windows":
59 return os.path.join(os.environ["APPDATA"], "Electrum")
60 elif platform.system() == "Linux":
61 return os.path.join(sys.prefix, "share", "electrum")
62 elif (platform.system() == "Darwin" or
63 platform.system() == "DragonFly" or
64 platform.system() == "NetBSD"):
65 return "/Library/Application Support/Electrum"
67 raise Exception("Unknown system")
70 def get_resource_path(*args):
71 return os.path.join(".", *args)
75 """Return path to the data folder."""
77 prefix_path = os.path.dirname(sys.argv[0])
78 local_data = os.path.join(prefix_path, "data")
82 def format_satoshis(x, is_diff=False, num_zeros = 0, decimal_point = 8, whitespaces=False):
83 from decimal import Decimal
85 sign, digits, exp = s.as_tuple()
86 digits = map(str, digits)
87 while len(digits) < decimal_point + 1:
89 digits.insert(-decimal_point,'.')
90 s = ''.join(digits).rstrip('0')
97 s += "0"*( 1 + num_zeros - ( len(s) - p ))
99 s += " "*( 1 + decimal_point - ( len(s) - p ))
100 s = " "*( 13 - decimal_point - ( p )) + s
104 # Takes a timestamp and returns a string with the approximation of the age
105 def age(from_date, since_date = None, target_tz=None, include_seconds=False):
106 if from_date is None:
109 from_date = datetime.fromtimestamp(from_date)
110 if since_date is None:
111 since_date = datetime.now(target_tz)
113 distance_in_time = since_date - from_date
114 distance_in_seconds = int(round(abs(distance_in_time.days * 86400 + distance_in_time.seconds)))
115 distance_in_minutes = int(round(distance_in_seconds/60))
117 if distance_in_minutes <= 1:
119 for remainder in [5, 10, 20]:
120 if distance_in_seconds < remainder:
121 return "less than %s seconds ago" % remainder
122 if distance_in_seconds < 40:
123 return "half a minute ago"
124 elif distance_in_seconds < 60:
125 return "less than a minute ago"
127 return "1 minute ago"
129 if distance_in_minutes == 0:
130 return "less than a minute ago"
132 return "1 minute ago"
133 elif distance_in_minutes < 45:
134 return "%s minutes ago" % distance_in_minutes
135 elif distance_in_minutes < 90:
136 return "about 1 hour ago"
137 elif distance_in_minutes < 1440:
138 return "about %d hours ago" % (round(distance_in_minutes / 60.0))
139 elif distance_in_minutes < 2880:
141 elif distance_in_minutes < 43220:
142 return "%d days ago" % (round(distance_in_minutes / 1440))
143 elif distance_in_minutes < 86400:
144 return "about 1 month ago"
145 elif distance_in_minutes < 525600:
146 return "%d months ago" % (round(distance_in_minutes / 43200))
147 elif distance_in_minutes < 1051200:
148 return "about 1 year ago"
150 return "over %d years ago" % (round(distance_in_minutes / 525600))
156 _ud = re.compile('%([0-9a-hA-H]{2})', re.MULTILINE)
157 urldecode = lambda x: _ud.sub(lambda m: chr(int(m.group(1), 16)), x)
160 o = url[8:].split('?')
163 params = o[1].split('&')
167 amount = label = message = signature = identity = ''
171 if k == 'amount': amount = uv
172 elif k == 'message': message = uv
173 elif k == 'label': label = uv
174 elif k == 'signature':
175 identity, signature = uv.split(':')
176 url = url.replace('&%s=%s'%(k,v),'')
180 return address, amount, label, message, signature, identity, url