def __init__(self, name, prop, intermediate_class, parent_name): self.parent_name = parent_name function = prop.fget function.command = self # make command def accessible from function, just like functions gen'd from server info json_schema = { } # make empty, since this command didn't come from json, and there is no need to generate it full_name = self._generate_full_name(parent_name, name) params = [] params.append( Parameter(name='self', data_type='object', use_self=True, optional=False, default=None, doc=None)) return_info = ReturnInfo(intermediate_class, use_self=False, doc='%s object' % intermediate_class.__name__) maturity = function.maturity if hasattr(function, "maturity") else None super(IntermediatePropertyCommandDefinition, self).__init__(json_schema, full_name, params, return_info, is_property=True, doc=prop.__doc__, maturity=maturity) function.__doc__ = get_spa_docstring(self)
def get_doc_stub_init_text(command_def, override_rtype=None): args_text=command_def.get_function_args_text() return ''' def __init__({args_text}): """ {doc} """ raise {error}("{name}") '''.format(args_text=args_text, doc=get_spa_docstring(command_def, override_rtype=override_rtype), error=DocStubCalledError.__name__, name=command_def.full_name)
def get_doc_stub_init_text(command_def, override_rtype=None): args_text = command_def.get_function_args_text() return ''' def __init__({args_text}): """ {doc} """ raise {error}("{name}") '''.format(args_text=args_text, doc=get_spa_docstring(command_def, override_rtype=override_rtype), error=DocStubCalledError.__name__, name=command_def.full_name)
def __init__(self, name, prop, intermediate_class, parent_name): self.parent_name = parent_name function = prop.fget function.command = self # make command def accessible from function, just like functions gen'd from server info json_schema = {} # make empty, since this command didn't come from json, and there is no need to generate it full_name = self._generate_full_name(parent_name, name) params = [] params.append(Parameter(name='self', data_type='object', use_self=True, optional=False, default=None, doc=None)) return_info = ReturnInfo(intermediate_class, use_self=False, doc='%s object' % intermediate_class.__name__) maturity = function.maturity if hasattr(function, "maturity") else None super(IntermediatePropertyCommandDefinition, self).__init__(json_schema, full_name, params, return_info, is_property=True, doc=prop.__doc__, maturity=maturity) function.__doc__ = get_spa_docstring(self)
def create_function(loadable_class, command_def, execute_command_function=None): """Creates the function which will appropriately call execute_command for this command""" execute_command = create_execute_command_function( command_def, execute_command_function) api_decorator = get_api_context_decorator( logging.getLogger(loadable_class.__module__)) if command_def.is_constructor: func_text = get_function_text( command_def, body_text=_get_init_body_text(command_def), decorator_text='@api') #print "func_text for %s = %s" % (command_def.full_name, func_text) dependencies = { 'api': api_decorator, 'base_class': _installable_classes_store.get( entity_type_to_baseclass_name(command_def.install_path.full), CommandInstallable), EXECUTE_COMMAND_FUNCTION_NAME: execute_command } else: func_text = get_function_text( command_def, body_text='return ' + get_call_execute_command_text(command_def), decorator_text='@api') dependencies = { 'api': api_decorator, EXECUTE_COMMAND_FUNCTION_NAME: execute_command } try: function = _compile_function(command_def.name, func_text, dependencies) except: sys.stderr.write( "Metaprogramming problem compiling %s for class %s in code: %s" % (command_def.full_name, loadable_class.__name__, func_text)) raise function.command = command_def function.__doc__ = get_spa_docstring(command_def) return function
def create_function(loadable_class, command_def, execute_command_function=None): """Creates the function which will appropriately call execute_command for this command""" execute_command = create_execute_command_function(command_def, execute_command_function) api_decorator = get_api_context_decorator(logging.getLogger(loadable_class.__module__)) if command_def.is_constructor: func_text = get_function_text(command_def, body_text=_get_init_body_text(command_def), decorator_text='@api') #print "func_text for %s = %s" % (command_def.full_name, func_text) dependencies = {'api': api_decorator, 'base_class': _installable_classes_store.get(entity_type_to_baseclass_name(command_def.install_path.full), CommandInstallable), EXECUTE_COMMAND_FUNCTION_NAME: execute_command} else: func_text = get_function_text(command_def, body_text='return ' + get_call_execute_command_text(command_def), decorator_text='@api') dependencies = {'api': api_decorator, EXECUTE_COMMAND_FUNCTION_NAME: execute_command} try: function = _compile_function(command_def.name, func_text, dependencies) except: sys.stderr.write("Metaprogramming problem compiling %s for class %s in code: %s" % (command_def.full_name, loadable_class.__name__, func_text)) raise function.command = command_def function.__doc__ = get_spa_docstring(command_def) return function
def get_function_text(command_def, body_text='return None', decorator_text=''): """Produces python code text for a command to be inserted into python modules""" args_text = command_def.get_function_args_text() if command_def.is_constructor: if args_text: args_text += ", _info=None" else: args_text = "_info=None" elif command_def.is_property: decorator_text = "@property\n" + decorator_text return '''{decorator} def {name}({args_text}): """ {doc} """ {body_text} '''.format(decorator=decorator_text, name=command_def.name, args_text=args_text, doc=get_spa_docstring(command_def), body_text=body_text)
def get_function_text(command_def, body_text='return None', decorator_text=''): """Produces python code text for a command to be inserted into python modules""" args_text=command_def.get_function_args_text() if command_def.is_constructor: if args_text: args_text += ", _info=None" else: args_text = "_info=None" elif command_def.is_property: decorator_text = "@property\n" + decorator_text return '''{decorator} def {name}({args_text}): """ {doc} """ {body_text} '''.format(decorator=decorator_text, name=command_def.name, args_text=args_text, doc=get_spa_docstring(command_def), body_text=body_text)
def __init__(self, class_name, member, is_property, override_name=None): # Note: this code runs during package init (before connect) self.client_member = member self.parent_name = class_name function = member.fget if is_property else member function.command = self # make command def accessible from function, just like functions gen'd from server info json_schema = {} # make empty, since this command didn't come from json, and there is no need to generate it full_name = self._generate_full_name(class_name, override_name or function.__name__) params = [] return_info = None args, kwargs, varargs, varkwargs = get_args_spec_from_function(function, ignore_private_args=True) num_args = len(args) + len(kwargs) + (1 if varargs else 0) + (1 if varkwargs else 0) if hasattr(function, "arg_docs"): arg_docs = function.arg_docs num_arg_docs = len(arg_docs) if num_arg_docs > num_args: # only check for greater than, the code after will give a better exception message for less than case raise ValueError("function received %d @arg decorators, expected %d for function %s." % (num_arg_docs, num_args, function.__name__)) def _get_arg_doc(name): try: arg_doc = filter(lambda d: d.name == name, arg_docs)[0] except IndexError: raise ValueError("Function missing @arg decorator for argument '%s' in function %s" % (name, function.__name__)) if not isinstance(arg_doc, ArgDoc): raise TypeError("InternalError - @api decorator expected an ArgDoc for argument '%s' in function %s. Received type %s" % (name, function.__name__, type(arg_doc))) return arg_doc else: def _get_arg_doc(name): return ArgDoc(name, '', '') if args and args[0] == "self": params.append(Parameter(name='self', data_type='object', use_self=True, optional=False, default=None, doc='')) args.pop(0) for arg_name in args: arg_doc = _get_arg_doc(arg_name) params.append(Parameter(name=arg_doc.name, data_type=arg_doc.data_type, use_self=False, optional=False, default=None, doc=arg_doc.description)) for arg_name, default in kwargs: arg_doc = _get_arg_doc(arg_name) params.append(Parameter(name=arg_doc.name, data_type=arg_doc.data_type, use_self=False, optional=True, default=default, doc=arg_doc.description)) for arg_name in [varargs, varkwargs]: if arg_name: arg_doc = _get_arg_doc(arg_name) params.append(Parameter(name=arg_doc.name, data_type=arg_doc.data_type, use_self=False, optional=True, default=None, doc=arg_doc.description)) if hasattr(function, "return_doc"): return_doc = function.return_doc if not isinstance(return_doc, ReturnDoc): raise TypeError("InternalError - @returns decorator expected an ReturnDoc in function %s. Received type %s." % (function.__name__, type(return_doc))) return_info = ReturnInfo(return_doc.data_type, use_self=False, doc=return_doc.description) # todo: remove use_self from ReturnInfo maturity = function.maturity if hasattr(function, "maturity") else None doc_string = parse_for_doc(function.__doc__) super(ClientCommandDefinition, self).__init__(json_schema, full_name, params, return_info, is_property, doc_string, maturity=maturity) spa_doc = get_spa_docstring(self) # todo: make this numpydoc/googledoc function.__doc__ = spa_doc
def __init__(self, class_name, member, is_property, override_name=None): # Note: this code runs during package init (before connect) self.client_member = member self.parent_name = class_name function = member.fget if is_property else member function.command = self # make command def accessible from function, just like functions gen'd from server info json_schema = { } # make empty, since this command didn't come from json, and there is no need to generate it full_name = self._generate_full_name( class_name, override_name or function.__name__) params = [] return_info = None args, kwargs, varargs, varkwargs = get_args_spec_from_function( function, ignore_private_args=True) num_args = len(args) + len(kwargs) + (1 if varargs else 0) + (1 if varkwargs else 0) if hasattr(function, "arg_docs"): arg_docs = function.arg_docs num_arg_docs = len(arg_docs) if num_arg_docs > num_args: # only check for greater than, the code after will give a better exception message for less than case raise ValueError( "function received %d @arg decorators, expected %d for function %s." % (num_arg_docs, num_args, function.__name__)) def _get_arg_doc(name): arg_name = name def name_matches(arg_doc): doc_name = arg_doc.name while doc_name.startswith('*'): doc_name = doc_name[1:] return doc_name == arg_name try: arg_doc = filter(name_matches, arg_docs)[0] except IndexError: raise ValueError( "Function missing @arg decorator for argument '%s' in function %s" % (name, function.__name__)) if not isinstance(arg_doc, ArgDoc): raise TypeError( "InternalError - @api decorator expected an ArgDoc for argument '%s' in function %s. Received type %s" % (name, function.__name__, type(arg_doc))) return arg_doc else: def _get_arg_doc(name): return ArgDoc(name, '', '') if args and args[0] == "self": params.append( Parameter(name='self', data_type='object', use_self=True, optional=False, default=None, doc='')) args.pop(0) for arg_name in args: arg_doc = _get_arg_doc(arg_name) params.append( Parameter(name=arg_doc.name, data_type=arg_doc.data_type, use_self=False, optional=False, default=None, doc=arg_doc.description)) for arg_name, default in kwargs: arg_doc = _get_arg_doc(arg_name) params.append( Parameter(name=arg_doc.name, data_type=arg_doc.data_type, use_self=False, optional=True, default=default, doc=arg_doc.description)) for arg_name in [varargs, varkwargs]: if arg_name: arg_doc = _get_arg_doc(arg_name) params.append( Parameter(name=arg_doc.name, data_type=arg_doc.data_type, use_self=False, optional=True, default=None, doc=arg_doc.description)) if hasattr(function, "return_doc"): return_doc = function.return_doc if not isinstance(return_doc, ReturnDoc): raise TypeError( "InternalError - @returns decorator expected an ReturnDoc in function %s. Received type %s." % (function.__name__, type(return_doc))) return_info = ReturnInfo(return_doc.data_type, use_self=False, doc=return_doc.description ) # todo: remove use_self from ReturnInfo maturity = function.maturity if hasattr(function, "maturity") else None doc_string = parse_for_doc(function.__doc__) super(ClientCommandDefinition, self).__init__(json_schema, full_name, params, return_info, is_property, doc_string, maturity=maturity) spa_doc = get_spa_docstring(self) # todo: make this numpydoc/googledoc function.__doc__ = spa_doc