# Path: lib/wallet.py
# class WalletStorage(object):
# 
#     def __init__(self, config):
#         self.lock = threading.RLock()
#         self.config = config
#         self.data = {}
#         self.file_exists = False
#         self.path = self.init_path(config)
#         print_error( "wallet path", self.path )
#         if self.path:
#             self.read(self.path)
# 
#     def init_path(self, config):
#         """Set the path of the wallet."""
# 
#         # command line -w option
#         path = config.get('wallet_path')
#         if path:
#             return path
# 
#         # path in config file
#         path = config.get('default_wallet_path')
#         if path:
#             return path
# 
#         # default path
#         dirpath = os.path.join(config.path, "wallets")
#         if not os.path.exists(dirpath):
#             os.mkdir(dirpath)
# 
#         new_path = os.path.join(config.path, "wallets", "default_wallet")
# 
#         # default path in pre 1.9 versions
#         old_path = os.path.join(config.path, "electrum-myr.dat")
#         if os.path.exists(old_path) and not os.path.exists(new_path):
#             os.rename(old_path, new_path)
# 
#         return new_path
# 
#     def read(self, path):
#         """Read the contents of the wallet file."""
#         try:
#             with open(self.path, "r") as f:
#                 data = f.read()
#         except IOError:
#             return
#         try:
#             self.data = json.loads(data)
#         except:
#             try:
#                 d = ast.literal_eval(data)  #parse raw data from reading wallet file
#             except Exception:
#                 raise IOError("Cannot read wallet file.")
#             self.data = {}
#             for key, value in d.items():
#                 try:
#                     json.dumps(key)
#                     json.dumps(value)
#                 except:
#                     continue
#                 self.data[key] = value
#         self.file_exists = True
# 
#     def get(self, key, default=None):
#         with self.lock:
#             v = self.data.get(key)
#             if v is None:
#                 v = default
#             else:
#                 v = copy.deepcopy(v)
#             return v
# 
#     def put(self, key, value, save = True):
#         try:
#             json.dumps(key)
#             json.dumps(value)
#         except:
#             print_error("json error: cannot save", key)
#             return
#         with self.lock:
#             if value is not None:
#                 self.data[key] = copy.deepcopy(value)
#             elif key in self.data:
#                 self.data.pop(key)
#             if save:
#                 self.write()
# 
#     def write(self):
#         s = json.dumps(self.data, indent=4, sort_keys=True)
#         f = open(self.path,"w")
#         f.write(s)
#         f.close()
#         if 'ANDROID_DATA' not in os.environ:
#             import stat
#             os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
# 
# class NewWallet(BIP32_HD_Wallet, Mnemonic):
#     # bip 44
#     root_name = 'x/'
#     root_derivation = "m/44'/50'"
#     wallet_type = 'standard'


import shutil
import tempfile
import sys
import unittest
import os
import json
from StringIO import StringIO
from lib.wallet import WalletStorage, NewWallet
    def test_write_dictionnary_to_file(self):
        path = os.path.join(self.user_dir, "somewallet")
        self.fake_config.set("wallet_path", path)

        storage = WalletStorage(self.fake_config)

        some_dict = {"a":"b", "c":"d"}
        storage.data = some_dict

        storage.write()

        contents = ""
        with open(path, "r") as f:
            contents = f.read()
        self.assertEqual(some_dict, json.loads(contents))


class TestNewWallet(WalletTestCase):

    seed_text = "travel nowhere air position hill peace suffer parent beautiful rise blood power home crumble teach"
    password = "secret"

    first_account_name = "account1"

    import_private_key = "TAroS5Knm8GZcnpPycBgzjwwDLWMyQjDrcuGPPoArgrbW7Ln22qp"
    import_key_address = "LPzGaoLUtXFkmNo3u1chDxGxDnSaBQTTxm"

    def setUp(self):
        super(TestNewWallet, self).setUp()
        self.storage = WalletStorage(self.fake_config)

# Path: lib/wallet.py
# class WalletStorage(object):
# 
#     def __init__(self, config):
#         self.lock = threading.RLock()
#         self.config = config
#         self.data = {}
#         self.file_exists = False
#         self.path = self.init_path(config)
#         print_error( "wallet path", self.path )
#         if self.path:
#             self.read(self.path)
# 
#     def init_path(self, config):
#         """Set the path of the wallet."""
# 
#         # command line -w option
#         path = config.get('wallet_path')
#         if path:
#             return path
# 
#         # path in config file
#         path = config.get('default_wallet_path')
#         if path:
#             return path
# 
#         # default path
#         dirpath = os.path.join(config.path, "wallets")
#         if not os.path.exists(dirpath):
#             os.mkdir(dirpath)
# 
#         new_path = os.path.join(config.path, "wallets", "default_wallet")
# 
#         # default path in pre 1.9 versions
#         old_path = os.path.join(config.path, "electrum-myr.dat")
#         if os.path.exists(old_path) and not os.path.exists(new_path):
#             os.rename(old_path, new_path)
# 
#         return new_path
# 
#     def read(self, path):
#         """Read the contents of the wallet file."""
#         try:
#             with open(self.path, "r") as f:
#                 data = f.read()
#         except IOError:
#             return
#         try:
#             self.data = json.loads(data)
#         except:
#             try:
#                 d = ast.literal_eval(data)  #parse raw data from reading wallet file
#             except Exception:
#                 raise IOError("Cannot read wallet file.")
#             self.data = {}
#             for key, value in d.items():
#                 try:
#                     json.dumps(key)
#                     json.dumps(value)
#                 except:
#                     continue
#                 self.data[key] = value
#         self.file_exists = True
# 
#     def get(self, key, default=None):
#         with self.lock:
#             v = self.data.get(key)
#             if v is None:
#                 v = default
#             else:
#                 v = copy.deepcopy(v)
#             return v
# 
#     def put(self, key, value, save = True):
#         try:
#             json.dumps(key)
#             json.dumps(value)
#         except:
#             print_error("json error: cannot save", key)
#             return
#         with self.lock:
#             if value is not None:
#                 self.data[key] = copy.deepcopy(value)
#             elif key in self.data:
#                 self.data.pop(key)
#             if save:
#                 self.write()
# 
#     def write(self):
#         s = json.dumps(self.data, indent=4, sort_keys=True)
#         f = open(self.path,"w")
#         f.write(s)
#         f.close()
#         if 'ANDROID_DATA' not in os.environ:
#             import stat
#             os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE)
# 
# class NewWallet(BIP32_HD_Wallet, Mnemonic):
#     # bip 44
#     root_name = 'x/'
#     root_derivation = "m/44'/50'"
#     wallet_type = 'standard'


# Path: lib/tests/test_wallet.py
import shutil
import tempfile
import sys
import unittest
import os
import json
from StringIO import StringIO
from lib.wallet import WalletStorage, NewWallet

    def test_write_dictionnary_to_file(self):
        path = os.path.join(self.user_dir, "somewallet")
        self.fake_config.set("wallet_path", path)

        storage = WalletStorage(self.fake_config)

        some_dict = {"a":"b", "c":"d"}
        storage.data = some_dict

        storage.write()

        contents = ""
        with open(path, "r") as f:
            contents = f.read()
        self.assertEqual(some_dict, json.loads(contents))


class TestNewWallet(WalletTestCase):

    seed_text = "travel nowhere air position hill peace suffer parent beautiful rise blood power home crumble teach"
    password = "secret"

    first_account_name = "account1"

    import_private_key = "TAroS5Knm8GZcnpPycBgzjwwDLWMyQjDrcuGPPoArgrbW7Ln22qp"
    import_key_address = "LPzGaoLUtXFkmNo3u1chDxGxDnSaBQTTxm"

    def setUp(self):
        super(TestNewWallet, self).setUp()
        self.storage = WalletStorage(self.fake_config)

        self.wallet = NewWallet(self.storage)