forked from OpenBazaar/OpenBazaar-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
206 lines (157 loc) · 6.22 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
'''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 platform import platform
from os.path import expanduser, join, isfile
from ConfigParser import ConfigParser
from urlparse import urlparse
PROTOCOL_VERSION = 12
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': 'OpenBazaar', # FIXME change to 'None' when issue #163 is resolved
'ksize': '20',
'alpha': '3',
'transaction_fee': '10000',
'libbitcoin_server': 'tcp://libbitcoin1.openbazaar.org:9091',
'libbitcoin_server_testnet': 'tcp://libbitcoin2.openbazaar.org:9091',
'resolver': 'http://resolver.onename.com/',
'ssl_cert': None,
'ssl_key': None,
'username': None,
'password': None,
'seed': 'seed.openbazaar.org:8080,5b44be5c18ced1bc9400fe5e79c8ab90204f06bebacc04dd9c70a95eaca6e117',
}
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: # TODO add clauses for Windows, and BSD
name = '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_seed_tuple(tup):
if isinstance(tup, tuple):
return 'seed' in tup[0]
return False
def _tuple_from_seed_string(string):
'''
Accepts well formed seed string, returns tuple (url:port, key)
'''
return tuple(string.split(','))
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'))
LIBBITCOIN_SERVER = cfg.get('CONSTANTS', 'LIBBITCOIN_SERVER')
LIBBITCOIN_SERVER_TESTNET = cfg.get('CONSTANTS', 'LIBBITCOIN_SERVER_TESTNET')
RESOLVER = cfg.get('CONSTANTS', 'RESOLVER')
SSL_CERT = cfg.get('AUTHENTICATION', 'SSL_CERT')
SSL_KEY = cfg.get('AUTHENTICATION', 'SSL_KEY')
USERNAME = cfg.get('AUTHENTICATION', 'USERNAME')
PASSWORD = cfg.get('AUTHENTICATION', 'PASSWORD')
SEEDS = []
items = cfg.items('SEEDS') # this also includes items in DEFAULTS
for item in items:
if _is_seed_tuple(item):
seed = item[1]
if _is_well_formed_seed_string(seed):
SEEDS.append(_tuple_from_seed_string(seed))
else:
print 'Warning: please check your configuration file: %s' % seed
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_seed_tuple(good)
assert not _is_seed_tuple(bad_not_tuple)
assert not _is_seed_tuple(bad_not_seed_tuple)
_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()