thomasv
12 years ago
13 changed files with 389 additions and 369 deletions
@ -1,4 +1,3 @@ |
|||||
from wallet import Wallet, format_satoshis, prompt_password |
from wallet import Wallet, format_satoshis, prompt_password |
||||
from interface import WalletSynchronizer, parse_proxy_options |
from interface import WalletSynchronizer, Interface, pick_random_server |
||||
from interface import TcpStratumInterface |
|
||||
from simple_config import SimpleConfig |
from simple_config import SimpleConfig |
||||
|
@ -1,66 +1,176 @@ |
|||||
import json |
import json, ast |
||||
import os |
import os, ast |
||||
from util import user_dir |
from util import user_dir |
||||
|
|
||||
|
from version import ELECTRUM_VERSION, SEED_VERSION |
||||
|
from interface import parse_proxy_options |
||||
|
|
||||
|
|
||||
|
# old stuff.. should be removed at some point |
||||
|
def replace_keys(obj, old_key, new_key): |
||||
|
if isinstance(obj, dict): |
||||
|
if old_key in obj: |
||||
|
obj[new_key] = obj[old_key] |
||||
|
del obj[old_key] |
||||
|
for elem in obj.itervalues(): |
||||
|
replace_keys(elem, old_key, new_key) |
||||
|
elif isinstance(obj, list): |
||||
|
for elem in obj: |
||||
|
replace_keys(elem, old_key, new_key) |
||||
|
|
||||
|
def old_to_new(d): |
||||
|
replace_keys(d, 'blk_hash', 'block_hash') |
||||
|
replace_keys(d, 'pos', 'index') |
||||
|
replace_keys(d, 'nTime', 'timestamp') |
||||
|
replace_keys(d, 'is_in', 'is_input') |
||||
|
replace_keys(d, 'raw_scriptPubKey', 'raw_output_script') |
||||
|
|
||||
|
|
||||
|
|
||||
class SimpleConfig: |
class SimpleConfig: |
||||
|
|
||||
default_options = { |
def __init__(self, options): |
||||
"gui": "lite", |
|
||||
"proxy": None, |
self.wallet_config = {} |
||||
"winpos-qt": [100, 100, 840, 400], |
self.read_wallet_config(options.wallet_path) |
||||
"winpos-lite": [4, 25, 351, 149], |
|
||||
"history": False |
self.common_config = {} |
||||
} |
self.read_common_config() |
||||
|
|
||||
def __init__(self): |
self.options_config = {} |
||||
# Find electrum data folder |
|
||||
self.config_folder = user_dir() |
if options.server: self.options_config['server'] = options.server |
||||
# Read the file |
if options.proxy: self.options_config['proxy'] = parse_proxy_options(options.proxy) |
||||
if os.path.exists(self.config_file_path()): |
if options.gui: self.options_config['gui'] = options.gui |
||||
self.load_config() |
|
||||
|
|
||||
|
|
||||
|
def set_key(self, key, value, save = False): |
||||
|
# find where a setting comes from and save it there |
||||
|
if self.options_config.get(key): |
||||
|
return |
||||
|
|
||||
|
elif self.wallet_config.get(key): |
||||
|
self.wallet_config[key] = value |
||||
|
if save: self.save_wallet_config() |
||||
|
|
||||
|
elif self.common_config.get(key): |
||||
|
self.common_config[key] = value |
||||
|
if save: self.save_common_config() |
||||
|
|
||||
|
else: |
||||
|
# add key to wallet config |
||||
|
self.wallet_config[key] = value |
||||
|
if save: self.save_wallet_config() |
||||
|
|
||||
|
|
||||
|
def get(self, key, default=None): |
||||
|
# 1. command-line options always override everything |
||||
|
if self.options_config.has_key(key): |
||||
|
# print "found", key, "in options" |
||||
|
out = self.options_config.get(key) |
||||
|
|
||||
|
# 2. configuration file overrides wallet file |
||||
|
elif self.common_config.has_key(key): |
||||
|
out = self.common_config.get(key) |
||||
|
|
||||
else: |
else: |
||||
self.config = self.default_options |
out = self.wallet_config.get(key) |
||||
# Make config directory if it does not yet exist. |
|
||||
if not os.path.exists(self.config_folder): |
if out is None and default is not None: |
||||
os.mkdir(self.config_folder) |
out = default |
||||
self.save_config() |
return out |
||||
|
|
||||
# This is a friendly fallback to the old style default proxy options |
|
||||
if(self.config.get("proxy") is not None and self.config["proxy"]["mode"] == "none"): |
def is_modifiable(self, key): |
||||
self.set_key("proxy", None, True) |
if self.options_config.has_key(key) or self.common_config.has_key(key): |
||||
|
return False |
||||
def set_key(self, key, value, save = True): |
|
||||
self.config[key] = value |
|
||||
if save == True: |
|
||||
self.save_config() |
|
||||
|
|
||||
def save_config(self): |
|
||||
if not os.path.exists(self.config_folder): |
|
||||
os.mkdir(self.config_folder) |
|
||||
f = open(self.config_file_path(), "w+") |
|
||||
f.write(json.dumps(self.config)) |
|
||||
|
|
||||
def load_config(self): |
|
||||
f = open(self.config_file_path(), "r") |
|
||||
file_contents = f.read() |
|
||||
if file_contents: |
|
||||
user_config = json.loads(file_contents) |
|
||||
for i in user_config: |
|
||||
self.config[i] = user_config[i] |
|
||||
else: |
else: |
||||
self.config = self.default_options |
return True |
||||
self.save_config() |
|
||||
|
|
||||
def config_file_path(self): |
def read_common_config(self): |
||||
return "%s" % (self.config_folder + "/config.json") |
for name in [ os.path.join( user_dir(), 'electrum.conf') , '/etc/electrum.conf']: |
||||
|
if os.path.exists(name): |
||||
def __init__(self): |
from interface import parse_proxy_options |
||||
# Find electrum data folder |
try: |
||||
self.config_folder = user_dir() |
import ConfigParser |
||||
self.config = self.default_options |
except: |
||||
# Read the file |
print "cannot parse electrum.conf. please install ConfigParser" |
||||
if os.path.exists(self.config_file_path()): |
return |
||||
self.load_config() |
|
||||
self.save_config() |
p = ConfigParser.ConfigParser() |
||||
|
p.read(name) |
||||
|
try: |
||||
|
self.common_config['server'] = p.get('interface','server') |
||||
|
except: |
||||
|
pass |
||||
|
try: |
||||
|
self.common_config['proxy'] = parse_proxy_options(p.get('interface','proxy')) |
||||
|
except: |
||||
|
pass |
||||
|
break |
||||
|
|
||||
|
|
||||
|
|
||||
|
def init_path(self, wallet_path): |
||||
|
"""Set the path of the wallet.""" |
||||
|
if wallet_path is not None: |
||||
|
self.path = wallet_path |
||||
|
return |
||||
|
|
||||
|
# Look for wallet file in the default data directory. |
||||
|
# Keeps backwards compatibility. |
||||
|
wallet_dir = user_dir() |
||||
|
|
||||
|
# Make wallet directory if it does not yet exist. |
||||
|
if not os.path.exists(wallet_dir): |
||||
|
os.mkdir(wallet_dir) |
||||
|
self.path = os.path.join(wallet_dir, "electrum.dat") |
||||
|
|
||||
|
|
||||
|
|
||||
|
def save_common_config(self): |
||||
|
s = repr(self.common_config) |
||||
|
# todo: decide what to do |
||||
|
print "not saving settings in common config:", s |
||||
|
|
||||
|
|
||||
|
|
||||
|
def read_wallet_config(self, path): |
||||
|
"""Read the contents of the wallet file.""" |
||||
|
self.wallet_file_exists = False |
||||
|
self.init_path(path) |
||||
|
try: |
||||
|
with open(self.path, "r") as f: |
||||
|
data = f.read() |
||||
|
except IOError: |
||||
|
return |
||||
|
try: |
||||
|
d = ast.literal_eval( data ) #parse raw data from reading wallet file |
||||
|
old_to_new(d) |
||||
|
except: |
||||
|
raise IOError("Cannot read wallet file.") |
||||
|
|
||||
|
self.wallet_config = d |
||||
|
self.wallet_file_exists = True |
||||
|
|
||||
|
|
||||
|
def set_interface(self, interface): |
||||
|
pass |
||||
|
|
||||
|
def set_gui(self, gui): |
||||
|
pass |
||||
|
|
||||
|
def save(self): |
||||
|
self.save_wallet_config() |
||||
|
|
||||
|
|
||||
|
def save_wallet_config(self): |
||||
|
s = repr(self.wallet_config) |
||||
|
f = open(self.path,"w") |
||||
|
f.write( s ) |
||||
|
f.close() |
||||
|
import stat |
||||
|
os.chmod(self.path,stat.S_IREAD | stat.S_IWRITE) |
||||
|
|
||||
|
Loading…
Reference in new issue