Skip to content

Commit 9eaef82

Browse files
committed
rpc: split Proxy from RawProxy (**BREAKS API**)
The magic __getattr__ is now only provided by RawProxy; methods not yet explicitly wrapped in Proxy are available through "call". This allows future wrappers to be written without further breakage.
1 parent 0bc631e commit 9eaef82

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

bitcoin/rpc.py

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,11 @@ def __init__(self, rpc_error):
5555
self.error = rpc_error
5656

5757

58-
class RawProxy(object):
58+
class BaseProxy(object):
5959
def __init__(self, service_url=None,
6060
service_port=None,
6161
btc_conf_file=None,
6262
timeout=DEFAULT_HTTP_TIMEOUT):
63-
"""Low-level JSON-RPC proxy
64-
65-
Unlike Proxy no conversion is done from the raw JSON objects.
66-
"""
6763

6864
if service_url is None:
6965
# Figure out the path to the bitcoin.conf file
@@ -175,20 +171,6 @@ def _call(self, service_name, *args):
175171
return response['result']
176172

177173

178-
def __getattr__(self, name):
179-
if name.startswith('__') and name.endswith('__'):
180-
# Python internal stuff
181-
raise AttributeError
182-
183-
# Create a callable to do the actual call
184-
f = lambda *args: self._call(name, *args)
185-
186-
# Make debuggers show <function bitcoin.rpc.name> rather than <function
187-
# bitcoin.rpc.<lambda>>
188-
f.__name__ = name
189-
return f
190-
191-
192174
def _batch(self, rpc_call_list):
193175
postdata = json.dumps(list(rpc_call_list))
194176
self.__conn.request('POST', self.__url.path, postdata,
@@ -212,7 +194,38 @@ def __del__(self):
212194
self.__conn.close()
213195

214196

215-
class Proxy(RawProxy):
197+
class RawProxy(BaseProxy):
198+
199+
def __init__(self, service_url=None,
200+
service_port=None,
201+
btc_conf_file=None,
202+
timeout=DEFAULT_HTTP_TIMEOUT,
203+
**kwargs):
204+
"""Low-level JSON-RPC proxy
205+
206+
Unlike Proxy no conversion is done from the raw JSON objects.
207+
"""
208+
super(RawProxy, self).__init__(service_url=service_url,
209+
service_port=service_port,
210+
btc_conf_file=btc_conf_file,
211+
timeout=timeout,
212+
**kwargs)
213+
214+
def __getattr__(self, name):
215+
if name.startswith('__') and name.endswith('__'):
216+
# Python internal stuff
217+
raise AttributeError
218+
219+
# Create a callable to do the actual call
220+
f = lambda *args: self._call(name, *args)
221+
222+
# Make debuggers show <function bitcoin.rpc.name> rather than <function
223+
# bitcoin.rpc.<lambda>>
224+
f.__name__ = name
225+
return f
226+
227+
228+
class Proxy(BaseProxy):
216229
def __init__(self, service_url=None,
217230
service_port=None,
218231
btc_conf_file=None,
@@ -239,6 +252,10 @@ def __init__(self, service_url=None,
239252
timeout=timeout,
240253
**kwargs)
241254

255+
def call(self, service_name, *args):
256+
"""Call an RPC method by name and raw (JSON encodable) arguments"""
257+
return self._call(service_name, *args)
258+
242259
def dumpprivkey(self, addr):
243260
"""Return the private key matching an address
244261
"""

0 commit comments

Comments
 (0)