create a class for transaction dialog
[electrum-nvc.git] / lib / util.py
index 99908ae..9503b88 100644 (file)
@@ -1,4 +1,4 @@
-import os, sys
+import os, sys, re
 import platform
 import shutil
 from datetime import datetime
@@ -22,17 +22,27 @@ def print_msg(*args):
     sys.stdout.write(" ".join(args) + "\n")
     sys.stdout.flush()
 
+def print_json(obj):
+    import json
+    try:
+        s = json.dumps(obj,sort_keys = True, indent = 4)
+    except TypeError:
+        s = repr(obj)
+    sys.stdout.write(s + "\n")
+    sys.stdout.flush()
+
 
 def check_windows_wallet_migration():
-    if os.path.exists(os.path.join(os.environ["LOCALAPPDATA"], "Electrum")):
-        if os.path.exists(os.path.join(os.environ["APPDATA"], "Electrum")):
-            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"]))
-            sys.exit()
-        try:
-            shutil.move(os.path.join(os.environ["LOCALAPPDATA"], "Electrum"), os.path.join(os.environ["APPDATA"]))
-            print_msg("Your wallet has been moved from %s to %s."% (os.environ["LOCALAPPDATA"], os.environ["APPDATA"]))
-        except:
-            print_msg("Failed to move your wallet.")
+    if platform.release() != "XP":
+        if os.path.exists(os.path.join(os.environ["LOCALAPPDATA"], "Electrum")):
+            if os.path.exists(os.path.join(os.environ["APPDATA"], "Electrum")):
+                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"]))
+                sys.exit()
+            try:
+                shutil.move(os.path.join(os.environ["LOCALAPPDATA"], "Electrum"), os.path.join(os.environ["APPDATA"]))
+                print_msg("Your wallet has been moved from %s to %s."% (os.environ["LOCALAPPDATA"], os.environ["APPDATA"]))
+            except:
+                print_msg("Failed to move your wallet.")
     
 
 def user_dir():
@@ -72,14 +82,14 @@ def local_data_dir():
     return local_data
 
 
-def format_satoshis(x, is_diff=False, num_zeros = 0):
+def format_satoshis(x, is_diff=False, num_zeros = 0, decimal_point = 8, whitespaces=False):
     from decimal import Decimal
     s = Decimal(x)
     sign, digits, exp = s.as_tuple()
     digits = map(str, digits)
-    while len(digits) < 9:
+    while len(digits) < decimal_point + 1:
         digits.insert(0,'0')
-    digits.insert(-8,'.')
+    digits.insert(-decimal_point,'.')
     s = ''.join(digits).rstrip('0')
     if sign: 
         s = '-' + s
@@ -88,8 +98,9 @@ def format_satoshis(x, is_diff=False, num_zeros = 0):
 
     p = s.find('.')
     s += "0"*( 1 + num_zeros - ( len(s) - p ))
-    s += " "*( 9 - ( len(s) - p ))
-    s = " "*( 5 - ( p )) + s
+    if whitespaces:
+        s += " "*( 1 + decimal_point - ( len(s) - p ))
+        s = " "*( 13 - decimal_point - ( p )) + s 
     return s
 
 
@@ -140,3 +151,36 @@ def age(from_date, since_date = None, target_tz=None, include_seconds=False):
         return "about 1 year ago"
     else:
         return "over %d years ago" % (round(distance_in_minutes / 525600))
+
+
+
+
+# URL decode
+_ud = re.compile('%([0-9a-hA-H]{2})', re.MULTILINE)
+urldecode = lambda x: _ud.sub(lambda m: chr(int(m.group(1), 16)), x)
+
+def parse_url(url):
+    o = url[8:].split('?')
+    address = o[0]
+    if len(o)>1:
+        params = o[1].split('&')
+    else:
+        params = []
+
+    amount = label = message = signature = identity = ''
+    for p in params:
+        k,v = p.split('=')
+        uv = urldecode(v)
+        if k == 'amount': amount = uv
+        elif k == 'message': message = uv
+        elif k == 'label': label = uv
+        elif k == 'signature':
+            identity, signature = uv.split(':')
+            url = url.replace('&%s=%s'%(k,v),'')
+        else: 
+            print k,v
+
+    return address, amount, label, message, signature, identity, url
+
+
+