4848 hexlify = lambda b : binascii .hexlify (b ).decode ('utf8' )
4949
5050
51- class JSONRPCException (Exception ):
51+ class JSONRPCError (Exception ):
52+ """JSON-RPC protocol error"""
53+
5254 def __init__ (self , rpc_error ):
53- super (JSONRPCException , self ).__init__ ('msg: %r code: %r' %
54- (rpc_error ['message' ], rpc_error ['code' ]))
55+ super (JSONRPCException , self ).__init__ (
56+ 'msg: %r code: %r' %
57+ (rpc_error ['message' ], rpc_error ['code' ]))
5558 self .error = rpc_error
5659
5760
61+ # 0.4.0 compatibility
62+ JSONRPCException = JSONRPCError
63+
64+
5865class BaseProxy (object ):
5966 """Base JSON-RPC proxy class. Contains only private methods; do not use
6067 directly."""
6168
62- def __init__ (self , service_url = None ,
63- service_port = None ,
64- btc_conf_file = None ,
65- timeout = DEFAULT_HTTP_TIMEOUT ):
69+ def __init__ (self ,
70+ service_url = None ,
71+ service_port = None ,
72+ btc_conf_file = None ,
73+ timeout = DEFAULT_HTTP_TIMEOUT ):
6674
6775 if service_url is None :
6876 # Figure out the path to the bitcoin.conf file
@@ -166,9 +174,9 @@ def _call(self, service_name, *args):
166174
167175 response = self ._get_response ()
168176 if response ['error' ] is not None :
169- raise JSONRPCException (response ['error' ])
177+ raise JSONRPCError (response ['error' ])
170178 elif 'result' not in response :
171- raise JSONRPCException ({
179+ raise JSONRPCError ({
172180 'code' : - 343 , 'message' : 'missing JSON-RPC result' })
173181 else :
174182 return response ['result' ]
@@ -187,7 +195,7 @@ def _batch(self, rpc_call_list):
187195 def _get_response (self ):
188196 http_response = self .__conn .getresponse ()
189197 if http_response is None :
190- raise JSONRPCException ({
198+ raise JSONRPCError ({
191199 'code' : - 342 , 'message' : 'missing HTTP response from server' })
192200
193201 return json .loads (http_response .read ().decode ('utf8' ),
@@ -201,14 +209,15 @@ class RawProxy(BaseProxy):
201209 """Low-level proxy to a bitcoin JSON-RPC service
202210
203211 Unlike ``Proxy``, no conversion is done besides parsing JSON. As far as
204- Python is concerned, you can call any method; ``JSONRPCException `` will be
212+ Python is concerned, you can call any method; ``JSONRPCError `` will be
205213 raised if the server does not recognize it.
206214 """
207- def __init__ (self , service_url = None ,
208- service_port = None ,
209- btc_conf_file = None ,
210- timeout = DEFAULT_HTTP_TIMEOUT ,
211- ** kwargs ):
215+ def __init__ (self ,
216+ service_url = None ,
217+ service_port = None ,
218+ btc_conf_file = None ,
219+ timeout = DEFAULT_HTTP_TIMEOUT ,
220+ ** kwargs ):
212221 super (RawProxy , self ).__init__ (service_url = service_url ,
213222 service_port = service_port ,
214223 btc_conf_file = btc_conf_file ,
@@ -239,11 +248,12 @@ class Proxy(BaseProxy):
239248 there are a few incompatibilities.
240249 """
241250
242- def __init__ (self , service_url = None ,
243- service_port = None ,
244- btc_conf_file = None ,
245- timeout = DEFAULT_HTTP_TIMEOUT ,
246- ** kwargs ):
251+ def __init__ (self ,
252+ service_url = None ,
253+ service_port = None ,
254+ btc_conf_file = None ,
255+ timeout = DEFAULT_HTTP_TIMEOUT ,
256+ ** kwargs ):
247257 """Create a proxy object
248258
249259 If ``service_url`` is not specified, the username and password are read
@@ -258,7 +268,9 @@ def __init__(self, service_url=None,
258268 ``timeout`` - timeout in seconds before the HTTP interface times out
259269 """
260270
261- super (Proxy , self ).__init__ (service_url = service_url , service_port = service_port , btc_conf_file = btc_conf_file ,
271+ super (Proxy , self ).__init__ (service_url = service_url ,
272+ service_port = service_port ,
273+ btc_conf_file = btc_conf_file ,
262274 timeout = timeout ,
263275 ** kwargs )
264276
@@ -274,15 +286,18 @@ def dumpprivkey(self, addr):
274286 return CBitcoinSecret (r )
275287
276288 def getaccountaddress (self , account = None ):
277- """Return the current Bitcoin address for receiving payments to this account."""
289+ """Return the current Bitcoin address for receiving payments to this
290+ account."""
278291 r = self ._call ('getaccountaddress' , account )
279292 return CBitcoinAddress (r )
280293
281294 def getbalance (self , account = '*' , minconf = 1 ):
282295 """Get the balance
283296
284- account - The selected account. Defaults to "*" for entire wallet. It may be the default account using "".
285- minconf - Only include transactions confirmed at least this many times. (default=1)
297+ account - The selected account. Defaults to "*" for entire wallet. It
298+ may be the default account using "".
299+ minconf - Only include transactions confirmed at least this many times.
300+ (default=1)
286301 """
287302 r = self ._call ('getbalance' , account , minconf )
288303 return int (r * COIN )
@@ -299,7 +314,7 @@ def getblock(self, block_hash):
299314 (self .__class__ .__name__ , block_hash .__class__ ))
300315 try :
301316 r = self ._call ('getblock' , block_hash , False )
302- except JSONRPCException as ex :
317+ except JSONRPCError as ex :
303318 raise IndexError ('%s.getblock(): %s (%d)' %
304319 (self .__class__ .__name__ , ex .error ['message' ], ex .error ['code' ]))
305320 return CBlock .deserialize (unhexlify (r ))
@@ -311,7 +326,7 @@ def getblockhash(self, height):
311326 """
312327 try :
313328 return lx (self ._call ('getblockhash' , height ))
314- except JSONRPCException as ex :
329+ except JSONRPCError as ex :
315330 raise IndexError ('%s.getblockhash(): %s (%d)' %
316331 (self .__class__ .__name__ , ex .error ['message' ], ex .error ['code' ]))
317332
@@ -368,7 +383,7 @@ def getrawtransaction(self, txid, verbose=False):
368383 """
369384 try :
370385 r = self ._call ('getrawtransaction' , b2lx (txid ), 1 if verbose else 0 )
371- except JSONRPCException as ex :
386+ except JSONRPCError as ex :
372387 raise IndexError ('%s.getrawtransaction(): %s (%d)' %
373388 (self .__class__ .__name__ , ex .error ['message' ], ex .error ['code' ]))
374389 if verbose :
@@ -395,7 +410,8 @@ def getreceivedbyaddress(self, addr, minconf=1):
395410 always show zero.
396411
397412 addr - The address. (CBitcoinAddress instance)
398- minconf - Only include transactions confirmed at least this many times. (default=1)
413+ minconf - Only include transactions confirmed at least this many times.
414+ (default=1)
399415 """
400416 r = self ._call ('getreceivedbyaddress' , str (addr ), minconf )
401417 return int (r * COIN )
@@ -409,7 +425,7 @@ def gettransaction(self, txid):
409425 """
410426 try :
411427 r = self ._call ('gettransaction' , b2lx (txid ))
412- except JSONRPCException as ex :
428+ except JSONRPCError as ex :
413429 raise IndexError ('%s.getrawtransaction(): %s (%d)' %
414430 (self .__class__ .__name__ , ex .error ['message' ], ex .error ['code' ]))
415431 return r
@@ -468,7 +484,8 @@ def listunspent(self, minconf=0, maxconf=9999999, addrs=None):
468484
469485 def lockunspent (self , unlock , outpoints ):
470486 """Lock or unlock outpoints"""
471- json_outpoints = [{'txid' :b2lx (outpoint .hash ),'vout' :outpoint .n } for outpoint in outpoints ]
487+ json_outpoints = [{'txid' :b2lx (outpoint .hash ), 'vout' :outpoint .n }
488+ for outpoint in outpoints ]
472489 return self ._call ('lockunspent' , unlock , json_outpoints )
473490
474491 def sendrawtransaction (self , tx , allowhighfees = False ):
@@ -486,7 +503,8 @@ def sendrawtransaction(self, tx, allowhighfees=False):
486503
487504 def sendmany (self , fromaccount , payments , minconf = 1 , comment = '' ):
488505 """Sent amount to a given address"""
489- json_payments = {str (addr ):float (amount )/ COIN for addr ,amount in payments .items ()}
506+ json_payments = {str (addr ):float (amount )/ COIN
507+ for addr , amount in payments .items ()}
490508 r = self ._call ('sendmany' , fromaccount , json_payments , minconf , comment )
491509 return lx (r )
492510
@@ -543,7 +561,8 @@ def removenode(self, node):
543561 return self ._addnode (node , 'remove' )
544562
545563__all__ = (
546- 'JSONRPCException' ,
547- 'RawProxy' ,
548- 'Proxy' ,
564+ 'JSONRPCError' ,
565+ 'JSONRPCException' ,
566+ 'RawProxy' ,
567+ 'Proxy' ,
549568)
0 commit comments