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