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")
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"]))
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"]))
36 print_msg("Failed to move your wallet.")
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")
47 #raise BaseException("No home directory found in environment variables.")
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"
61 raise Exception("Unknown system")
64 def get_resource_path(*args):
65 return os.path.join(".", *args)
69 """Return path to the data folder."""
71 prefix_path = os.path.dirname(sys.argv[0])
72 local_data = os.path.join(prefix_path, "data")
76 def format_satoshis(x, is_diff=False, num_zeros = 0):
77 from decimal import Decimal
79 sign, digits, exp = s.as_tuple()
80 digits = map(str, digits)
81 while len(digits) < 9:
84 s = ''.join(digits).rstrip('0')
91 s += "0"*( 1 + num_zeros - ( len(s) - p ))
92 s += " "*( 9 - ( len(s) - p ))
93 s = " "*( 5 - ( p )) + s
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):
102 from_date = datetime.fromtimestamp(from_date)
103 if since_date is None:
104 since_date = datetime.now(target_tz)
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))
110 if distance_in_minutes <= 1:
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"
120 return "1 minute ago"
122 if distance_in_minutes == 0:
123 return "less than a minute ago"
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:
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"
143 return "over %d years ago" % (round(distance_in_minutes / 525600))