@@ -136,6 +136,36 @@ def default_btc_dir():
136
136
return os .path .expanduser ('~/.bitcoin' )
137
137
138
138
139
+ def parse_conf_file (file_object ):
140
+ conf = {}
141
+ for line in file_object .readlines ():
142
+ if '#' in line :
143
+ line = line [:line .index ('#' )]
144
+ if '=' not in line :
145
+ continue
146
+ k , v = line .split ('=' , 1 )
147
+ conf [k .strip ()] = v .strip ()
148
+ return conf
149
+
150
+
151
+ def get_authpair (conf , network , btc_conf_file ):
152
+ cookie_dir = conf .get ('datadir' , os .path .dirname (btc_conf_file ))
153
+ if network != "mainnet" :
154
+ cookie_dir = os .path .join (cookie_dir , network )
155
+ cookie_file = os .path .join (cookie_dir , ".cookie" )
156
+
157
+ try :
158
+ with open (cookie_file , 'r' ) as fd :
159
+ return fd .read ()
160
+ except IOError as err :
161
+ if 'rpcpassword' in conf :
162
+ return "%s:%s" % (conf ['rpcuser' ], conf ['rpcpassword' ])
163
+
164
+ raise ValueError ('Cookie file unusable (%s) and rpcpassword '
165
+ 'not specified in the configuration file: %r'
166
+ % (err , btc_conf_file ))
167
+
168
+
139
169
class BaseProxy (object ):
140
170
"""Base JSON-RPC proxy class. Contains only private methods; do not use
141
171
directly."""
@@ -164,14 +194,7 @@ def __init__(self,
164
194
# Extract contents of bitcoin.conf to build service_url
165
195
try :
166
196
with open (btc_conf_file , 'r' ) as fd :
167
- for line in fd .readlines ():
168
- if '#' in line :
169
- line = line [:line .index ('#' )]
170
- if '=' not in line :
171
- continue
172
- k , v = line .split ('=' , 1 )
173
- conf [k .strip ()] = v .strip ()
174
-
197
+ conf .update (parse_conf_file (fd ))
175
198
# Treat a missing bitcoin.conf as though it were empty
176
199
except FileNotFoundError :
177
200
pass
@@ -184,19 +207,7 @@ def __init__(self,
184
207
service_url = ('%s://%s:%d' %
185
208
('http' , conf ['rpchost' ], conf ['rpcport' ]))
186
209
187
- cookie_dir = conf .get ('datadir' , os .path .dirname (btc_conf_file ))
188
- if bitcoin .params .NAME != "mainnet" :
189
- cookie_dir = os .path .join (cookie_dir , bitcoin .params .NAME )
190
- cookie_file = os .path .join (cookie_dir , ".cookie" )
191
- try :
192
- with open (cookie_file , 'r' ) as fd :
193
- authpair = fd .read ()
194
- except IOError as err :
195
- if 'rpcpassword' in conf :
196
- authpair = "%s:%s" % (conf ['rpcuser' ], conf ['rpcpassword' ])
197
-
198
- else :
199
- raise ValueError ('Cookie file unusable (%s) and rpcpassword not specified in the configuration file: %r' % (err , btc_conf_file ))
210
+ authpair = get_authpair (conf , bitcoin .params .NAME , btc_conf_file )
200
211
201
212
else :
202
213
url = urlparse .urlparse (service_url )
0 commit comments