forked from OpenBazaar/OpenBazaar-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
290 lines (234 loc) · 8.9 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
'''Parses configuration file and sets project wide constants.
This file has intrinsic naming difficulties because it is trying to be platform
agnostic but naming variables is inherently platform specific (i.e directory vs
folder)
'''
__author__ = 'foxcarlos-TeamCreed', 'Tobin Harding'
import os
from random import shuffle
from platform import platform
from os.path import expanduser, join, isfile
from ConfigParser import ConfigParser
from urlparse import urlparse
PROTOCOL_VERSION = 1
CONFIG_FILE = join(os.getcwd(), 'ob.cfg')
# FIXME probably a better way to do this. This curretly checks two levels deep.
for i in range(2):
if not isfile(CONFIG_FILE):
paths = CONFIG_FILE.rsplit('/', 2)
CONFIG_FILE = join(paths[0], paths[2])
DEFAULTS = {
# Default project config file may now remove these items
'data_folder': None,
'ksize': '20',
'alpha': '3',
'transaction_fee': '10000',
'libbitcoin_servers': 'tcp://libbitcoin1.openbazaar.org:9091',
'libbitcoin_servers_testnet': 'tcp://libbitcoin2.openbazaar.org:9091, <Z&{.=LJSPySefIKgCu99w.L%b^6VvuVp0+pbnOM',
'resolver': 'http://resolver.onename.com/',
'ssl_cert': None,
'ssl_key': None,
'ssl': False,
'username': None,
'password': None,
'mainnet_seeds': 'seed2.openbazaar.org:8080,8b17082a57d648894a5181cb6e1b8a6f5b3b7e1c347c0671abfcd7deb6f105fe',
'testnet_seeds': 'seed.openbazaar.org:8080,5b44be5c18ced1bc9400fe5e79c8ab90204f06bebacc04dd9c70a95eaca6e117',
}
def str_to_bool(s):
if isinstance(s, bool):
return s
if s.lower() == 'true':
return True
elif s.lower() == 'false':
return False
else:
raise ValueError
def _platform_agnostic_data_path(data_folder):
'''
Create absolute path name, exported as DATA_FOLDER.
User may configure using relative path, absolute path or use default.
Relative path puts named folder in users home directory.
Absolute path uses (obviously) the named absolute path.
Default is currently to use 'OpenBazaar' in home directory.
See issue #163
'''
if data_folder:
if os.path.isabs(data_folder):
return data_folder
return join(_platform_agnostic_home_path(), _platform_agnostic_data_folder(data_folder), '')
def _platform_agnostic_home_path():
home_path = ''
if _is_windows():
home_path = os.environ['HOMEPATH'] # Does this work for versions before Windows 7?
else:
home_path = expanduser('~')
return home_path
# see issue #163
def _platform_agnostic_data_folder(data_folder):
'''
Try to fit in with platform file naming conventions.
'''
if data_folder:
return data_folder
name = ''
if _is_osx():
name = join('Library', 'Application Support', 'OpenBazaar')
elif _is_linux():
name = '.openbazaar'
else:
name = join(os.getenv('APPDATA'), 'OpenBazaar')
return name
def _is_windows():
which_os = platform(aliased=True, terse=True).lower()
return 'window' in which_os
def _is_linux():
which_os = platform(aliased=True, terse=True).lower()
return 'linux' in which_os
def _is_osx():
which_os = platform(aliased=True, terse=True).lower()
return 'darwin' in which_os
def _is_well_formed_seed_string(string):
'''
Parse string url:port,key
'''
if ',' in string:
url, key = string.split(',')
parsed = urlparse(url)
if _validate_url(parsed.geturl()):
if _validate_key(key):
return True
return False
def _validate_url(url):
# TODO (How tight should the configuration requirements for a url be?)
return True
def _validate_key(key):
# TODO (is this done elsewhere in the project?)
return True
def _is_tuple(tup, key):
if isinstance(tup, tuple):
return key in tup[0]
return False
def _tuple_from_string(string):
'''
Accepts well formed seed string, returns tuple (url:port, key)
'''
l = string.split(',')
if len(l) == 1:
l.append(None)
return tuple(l)
cfg = ConfigParser(DEFAULTS)
if isfile(CONFIG_FILE):
cfg.read(CONFIG_FILE)
else:
print 'Warning: configuration file not found: (%s), using default values' % CONFIG_FILE
DATA_FOLDER = _platform_agnostic_data_path(cfg.get('CONSTANTS', 'DATA_FOLDER'))
KSIZE = int(cfg.get('CONSTANTS', 'KSIZE'))
ALPHA = int(cfg.get('CONSTANTS', 'ALPHA'))
TRANSACTION_FEE = int(cfg.get('CONSTANTS', 'TRANSACTION_FEE'))
RESOLVER = cfg.get('CONSTANTS', 'RESOLVER')
SSL = str_to_bool(cfg.get('AUTHENTICATION', 'SSL'))
SSL_CERT = cfg.get('AUTHENTICATION', 'SSL_CERT')
SSL_KEY = cfg.get('AUTHENTICATION', 'SSL_KEY')
USERNAME = cfg.get('AUTHENTICATION', 'USERNAME')
PASSWORD = cfg.get('AUTHENTICATION', 'PASSWORD')
LIBBITCOIN_SERVERS = []
LIBBITCOIN_SERVERS_TESTNET = []
SEEDS = []
SEEDS_TESTNET = []
items = cfg.items('MAINNET_SEEDS') # this also includes items in DEFAULTS
for item in items:
if _is_tuple(item, "mainnet_seed"):
seed = item[1]
if _is_well_formed_seed_string(seed):
new_seed = _tuple_from_string(seed)
if new_seed not in SEEDS:
SEEDS.append(new_seed)
else:
print 'Warning: please check your configuration file: %s' % seed
items = cfg.items('TESTNET_SEEDS') # this also includes items in DEFAULTS
for item in items:
if _is_tuple(item, "testnet_seed"):
seed = item[1]
if _is_well_formed_seed_string(seed):
new_seed = _tuple_from_string(seed)
if new_seed not in SEEDS_TESTNET:
SEEDS_TESTNET.append(new_seed)
else:
print 'Warning: please check your configuration file: %s' % seed
items = cfg.items('LIBBITCOIN_SERVERS') # this also includes items in DEFAULTS
for item in items:
if _is_tuple(item, "mainnet_server"):
server = item[1]
new_server = _tuple_from_string(server)
if item[0] == "mainnet_server_custom":
LIBBITCOIN_SERVERS = [new_server]
break
elif new_server not in LIBBITCOIN_SERVERS:
LIBBITCOIN_SERVERS.append(new_server)
else:
print 'Warning: please check your configuration file: %s' % server
shuffle(LIBBITCOIN_SERVERS)
items = cfg.items('LIBBITCOIN_SERVERS_TESTNET') # this also includes items in DEFAULTS
for item in items:
if _is_tuple(item, "testnet_server"):
server = item[1]
new_server = _tuple_from_string(server)
if item[0] == "testnet_server_custom":
LIBBITCOIN_SERVERS_TESTNET = [new_server]
break
elif new_server not in LIBBITCOIN_SERVERS_TESTNET:
LIBBITCOIN_SERVERS_TESTNET.append(new_server)
else:
print 'Warning: please check your configuration file: %s' % server
shuffle(LIBBITCOIN_SERVERS_TESTNET)
def set_value(section, name, value):
config = ConfigParser()
if isfile(CONFIG_FILE):
config.read(CONFIG_FILE)
config.set(section, name, value)
with open(CONFIG_FILE, 'wb') as configfile:
config.write(configfile)
def get_value(section, name):
config = ConfigParser()
if isfile(CONFIG_FILE):
config.read(CONFIG_FILE)
try:
return config.get(section, name)
except Exception:
return None
def delete_value(section, name):
config = ConfigParser()
if isfile(CONFIG_FILE):
config.read(CONFIG_FILE)
config.remove_option(section, name)
with open(CONFIG_FILE, 'wb') as configfile:
config.write(configfile)
if __name__ == '__main__':
def test_is_well_formed_seed_string():
well_formed = 'seed.openbazaar.org:8080,5b44be5c18ced1bc9400fe5e79c8ab90204f06bebacc04dd9c70a95eaca6e117'
# test ill-formed url's (build fails with pylint error if we use long/descriptive names
# key too short
# bad_1 = 'seed.openbazaar.org:8080,5b44be5c18ced1bc9400fe5e79'
# no port number
# bad_2 = 'seed.openbazaar.org,5b44be5c18ced1bc9400fe5e79c8ab90204f06bebacc04dd9c70a95eaca6e117'
# no host name in url
# bad_3 = 'openbazaar.org:8080,5b44be5c18ced1bc9400fe5e79c8ab90204f06bebacc04dd9c70a95eaca6e117'
assert _is_well_formed_seed_string(well_formed)
# assert not _is_well_formed_seed_string(b1)
# assert not _is_well_formed_seed_string(b2)
# assert not _is_well_formed_seed_string(b3)
def test_is_seed_tuple():
good = ('seed.openbazaar.org:8080', '5b44be5c18ced1bc9400fe5e79c8ab90204f06bebacc04dd9c70a95eaca6e117')
bad_not_tuple = 'seed.openbazaar.org:8080,5b44be5c18ced1bc9400fe5e79c8ab90204f06bebacc04dd9c70a95eaca6e117'
bad_not_seed_tuple = ('aoioai', 'aoioai')
assert _is_tuple(good, "seed")
assert not _is_tuple(bad_not_tuple, "seed")
assert not _is_tuple(bad_not_seed_tuple, "seed")
_is_linux()
_is_windows()
_is_osx()
if _is_linux():
assert not _is_windows()
assert not _is_osx()
test_is_well_formed_seed_string()
test_is_seed_tuple()