def discover_api(self): """ Discovers the remote API using the special method '0'. After this the client will have a `vim` attribute containing an object that implements an interface similar to the one found in the python-vim module(legacy python->vim bridge) """ if self.vim: # Only need to do this once return channel_id, api = self.rpc_request("vim_get_api_info", []) # The 'Vim' class is the main entry point of the api types = {'vim': type('Vim', (), {})} setattr(types['vim'], 'loop_start', lambda s, *args, **kwargs: self.loop_start(*args, **kwargs)) setattr(types['vim'], 'loop_stop', lambda s, *args, **kwargs: self.loop_stop(*args, **kwargs)) setattr(types['vim'], 'next_message', lambda s, *args, **kwargs: self.next_message(*args, **kwargs)) setattr(types['vim'], 'post', lambda s, *args, **kwargs: self.post(*args, **kwargs)) # Build types for manipulating the remote structures, assigning to a # dict using lower case names as keys, so we can easily match methods # in the API. for cls in api['types']: klass = type(cls + 'Base', (Remote,), {}) # Methods of this class will pass an integer representing the # remote object as first argument types[cls.lower()] = klass # now build function wrappers for function in api['functions']: # Split the name on underscores, the first part is the class name, # the remaining is the function name class_name, method_name = function['name'].split('_', 1) generate_wrapper(self, types[class_name], method_name, function['name'], function['return_type'], function['parameters']) if self.vim_compatible: make_vim_compatible(types['vim']) # Now apply all available mixins to the generated types for name, mixin in mixins.items(): types[name] = type(mixin.__name__, (types[name], mixin,), {}) # Create the 'vim object', which is a singleton of the 'Vim' class self.vim = types['vim']() # Initialize with some useful attributes types['vim'].initialize(self.vim, types, channel_id, api) # Add attributes for each other class for name, klass in types.items(): if name != 'vim': setattr(self.vim, klass.__name__, klass) # Configure the rpc stream with type information self.stream.configure(self.vim) self.vim
def discover_api(self): """ Discovers the remote API using the special method '0'. After this the client will have a `vim` attribute containing an object that implements an interface similar to the one found in the python-vim module(legacy python->vim bridge) """ if self.vim: # Only need to do this once return channel_id, api = self.msgpack_rpc_request(0, [])[3] api = msgpack.unpackb(api) # The 'Vim' class is the main entry point of the api classes = {'vim': type('Vim', (), {})} setattr(classes['vim'], 'next_event', lambda s, *args, **kwargs: self.next_event(*args, **kwargs)) setattr(classes['vim'], 'expect', lambda s, *args, **kwargs: self.expect(*args, **kwargs)) # Build classes for manipulating the remote structures, assigning to a # dict using lower case names as keys, so we can easily match methods # in the API. for cls in api['classes']: klass = type(cls + 'Base', (Remote, ), {}) # Methods of this class will pass an integer representing the # remote object as first argument classes[cls.lower()] = klass # now build function wrappers for function in api['functions']: # Split the name on underscores, the first part is the class name, # the remaining is the function name class_name, method_name = function['name'].split('_', 1) generate_wrapper(self, classes[class_name], method_name, function['id'], function['return_type'], function['parameters']) # Now apply all available mixins to the generated classes for name, mixin in mixins.items(): classes[name] = type(mixin.__name__, ( classes[name], mixin, ), {}) # Create the 'vim object', which is a singleton of the 'Vim' class self.vim = classes['vim']() self.vim.channel_id = channel_id # Add attributes for each other class for name, klass in classes.items(): if name != 'vim': setattr(self.vim, klass.__name__, klass)
def discover_api(self): """ Discovers the remote API using the special method '0'. After this the client will have a `vim` attribute containing an object that implements an interface similar to the one found in the python-vim module(legacy python->vim bridge) """ if self.vim: # Only need to do this once return channel_id, api = self.msgpack_rpc_request(0, [])[3] api = msgpack.unpackb(api) # The 'Vim' class is the main entry point of the api classes = {"vim": type("Vim", (), {})} setattr(classes["vim"], "next_event", lambda s, *args, **kwargs: self.next_event(*args, **kwargs)) setattr(classes["vim"], "expect", lambda s, *args, **kwargs: self.expect(*args, **kwargs)) # Build classes for manipulating the remote structures, assigning to a # dict using lower case names as keys, so we can easily match methods # in the API. for cls in api["classes"]: klass = type(cls + "Base", (Remote,), {}) # Methods of this class will pass an integer representing the # remote object as first argument classes[cls.lower()] = klass # now build function wrappers for function in api["functions"]: # Split the name on underscores, the first part is the class name, # the remaining is the function name class_name, method_name = function["name"].split("_", 1) generate_wrapper( self, classes[class_name], method_name, function["id"], function["return_type"], function["parameters"] ) # Now apply all available mixins to the generated classes for name, mixin in mixins.items(): classes[name] = type(mixin.__name__, (classes[name], mixin), {}) # Create the 'vim object', which is a singleton of the 'Vim' class self.vim = classes["vim"]() self.vim.channel_id = channel_id # Add attributes for each other class for name, klass in classes.items(): if name != "vim": setattr(self.vim, klass.__name__, klass)