11"""Code shared between the API classes."""
2+ import functools
23
34
45class Remote (object ):
@@ -10,6 +11,20 @@ class Remote(object):
1011 object handle into consideration.
1112 """
1213
14+ def __init__ (self , session , code_data ):
15+ """Initialize from session and code_data immutable object.
16+
17+ The `code_data` contains serialization information required for
18+ msgpack-rpc calls. It must be immutable for Buffer equality to work.
19+ """
20+ self ._session = session
21+ self .code_data = code_data
22+ self .api = RemoteApi (self , self ._api_prefix )
23+ self .vars = RemoteMap (self , self ._api_prefix + 'get_var' ,
24+ self ._api_prefix + 'set_var' )
25+ self .options = RemoteMap (self , self ._api_prefix + 'get_option' ,
26+ self ._api_prefix + 'set_option' )
27+
1328 def __eq__ (self , other ):
1429 """Return True if `self` and `other` are the same object."""
1530 return (hasattr (other , 'code_data' ) and
@@ -19,6 +34,24 @@ def __hash__(self):
1934 """Return hash based on remote object id."""
2035 return self .code_data .__hash__ ()
2136
37+ def request (self , name , * args , ** kwargs ):
38+ """Wrapper for nvim.request."""
39+ return self ._session .request (name , self , * args , ** kwargs )
40+
41+
42+ class RemoteApi (object ):
43+
44+ """Wrapper to allow api methods to be called like python methods."""
45+
46+ def __init__ (self , obj , api_prefix ):
47+ """Initialize a RemoteApi with object and api prefix."""
48+ self ._obj = obj
49+ self ._api_prefix = api_prefix
50+
51+ def __getattr__ (self , name ):
52+ """Return wrapper to named api method."""
53+ return functools .partial (self ._obj .request , self ._api_prefix + name )
54+
2255
2356class RemoteMap (object ):
2457
@@ -31,12 +64,12 @@ class RemoteMap(object):
3164 It is used to provide a dict-like API to vim variables and options.
3265 """
3366
34- def __init__ (self , session , get_method , set_method , self_obj = None ):
67+ def __init__ (self , obj , get_method , set_method = None , self_obj = None ):
3568 """Initialize a RemoteMap with session, getter/setter and self_obj."""
36- self ._get = _wrap ( session , get_method , self_obj )
69+ self ._get = functools . partial ( obj . request , get_method )
3770 self ._set = None
3871 if set_method :
39- self ._set = _wrap ( session , set_method , self_obj )
72+ self ._set = functools . partial ( obj . request , set_method )
4073
4174 def __getitem__ (self , key ):
4275 """Return a map value by key."""
@@ -91,9 +124,9 @@ class RemoteSequence(object):
91124 locally(iteration, indexing, counting, etc).
92125 """
93126
94- def __init__ (self , session , method , self_obj = None ):
127+ def __init__ (self , session , method ):
95128 """Initialize a RemoteSequence with session, method and self_obj."""
96- self ._fetch = _wrap (session , method , self_obj )
129+ self ._fetch = functools . partial (session . request , method )
97130
98131 def __len__ (self ):
99132 """Return the length of the remote sequence."""
@@ -155,10 +188,3 @@ def walk(fn, obj, *args):
155188 return dict ((walk (fn , k , * args ), walk (fn , v , * args )) for k , v in
156189 obj .items ())
157190 return fn (obj , * args )
158-
159-
160- def _wrap (session , method , self_obj ):
161- if self_obj is not None :
162- return lambda * args : session .request (method , self_obj , * args )
163- else :
164- return lambda * args : session .request (method , * args )
0 commit comments