def create_name_property(self): obj_term = self._term obj_class = self._class http = self._http execute_command = self._execute_command module_logger = self._module_logger def get_name(self): r = http.get(self.uri) payload = r.json() return payload.get('name', None) get_name.__name__ = '__name' doc = """ Set or get the name of the {term} object. Change or retrieve {term} object identification. Identification names must start with a letter and are limited to alphanumeric characters and the ``_`` character. Examples -------- .. code:: >>> my_{term}.name "csv_data" >>> my_{term}.name = "cleaned_data" >>> my_{term}.name "cleaned_data" """.format(term=self._term) get_name.__doc__ = doc def set_name(self, value): arguments = {obj_term: self.uri, "new_name": value} execute_command(obj_term + "/rename", self, **arguments) set_name.__name__ = '__name' from trustedanalytics.meta.context import get_api_context_decorator api_set_name = get_api_context_decorator(module_logger)(set_name) name_prop = property(fget=get_name, fset=api_set_name, fdel=None, doc=doc) return get_api_decorator( module_logger, parent_class_name=obj_class.__name__)(name_prop)
def create_name_property(self): obj_term = self._term obj_class = self._class http = self._http execute_command = self._execute_command module_logger = self._module_logger def get_name(self): r = http.get(self.uri) payload = r.json() return payload.get("name", None) get_name.__name__ = "__name" doc = """ Set or get the name of the {term} object. Change or retrieve {term} object identification. Identification names must start with a letter and are limited to alphanumeric characters and the ``_`` character. Examples -------- .. code:: >>> my_{term}.name "csv_data" >>> my_{term}.name = "cleaned_data" >>> my_{term}.name "cleaned_data" """.format( term=self._term ) get_name.__doc__ = doc def set_name(self, value): arguments = {obj_term: self.uri, "new_name": value} execute_command(obj_term + "/rename", self, **arguments) set_name.__name__ = "__name" from trustedanalytics.meta.context import get_api_context_decorator api_set_name = get_api_context_decorator(module_logger)(set_name) name_prop = property(fget=get_name, fset=api_set_name, fdel=None, doc=doc) return get_api_decorator(module_logger, parent_class_name=obj_class.__name__)(name_prop)
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 api_decorator(item): """ Decorator for API objects For a class, it registers it with api_globals For a method, it "swallows" it by synthesizing and storing a client-side command def object for it and then returning a canned method in its place, which raises an error if actually called. The API installation process will install (or restore) the method with a public name. Meanwhile, its metadata is available in the general meta-programming data structures for the API. Note: this @api decorator must be the first decorator when combined with other decorators from this package. The python @property decorator would come before this one. Example: @api @beta @arg('n', int, 'number of bananas') def feed_apes(n): ''' One line summary to say feed the apes. Extended summary to describe the side-effects of feeding of the apes. ''' """ if inspect.isclass(item): return decorate_api_class(item) is_property = isinstance(item, property) attr = item.fget if is_property else item mark_item_as_api(attr) # for a method, we need the name of its class if parent_class_name: class_name = parent_class_name else: try: # http://stackoverflow.com/questions/306130/python-decorator-makes-function-forget-that-it-belongs-to-a-class outerframes = inspect.getouterframes(inspect.currentframe()) call_depth_to_class = 1 class_name = outerframes[call_depth_to_class][3] #print "classname=%s" % class_name except: raise RuntimeError("Internal Error: @api decoration cannot resolve class name for item %s" % item) _patch_member_name(class_name, attr) # wrap the function with API logging and error handling function = get_api_context_decorator(execution_logger)(attr) if function.__name__ == "__init__": command_def = InitClientCommandDefinition(class_name, function) else: member = property(fget=function, fset=item.fset) if is_property else function command_def = ClientCommandDefinition(class_name, member, is_property) client_commands.append((class_name, command_def)) return get_clientside_api_stub(command_def.full_name)
def api_decorator(item): """ Decorator for API objects For a class, it registers it with api_globals For a method, it "swallows" it by synthesizing and storing a client-side command def object for it and then returning a canned method in its place, which raises an error if actually called. The API installation process will install (or restore) the method with a public name. Meanwhile, its metadata is available in the general meta-programming data structures for the API. Note: this @api decorator must be the first decorator when combined with other decorators from this package. The python @property decorator would come before this one. Example: @api @beta @arg('n', int, 'number of bananas') def feed_apes(n): ''' One line summary to say feed the apes. Extended summary to describe the side-effects of feeding of the apes. ''' """ if inspect.isclass(item): return decorate_api_class(item) is_property = isinstance(item, property) attr = item.fget if is_property else item mark_item_as_api(attr) # for a method, we need the name of its class if parent_class_name: class_name = parent_class_name else: try: # http://stackoverflow.com/questions/306130/python-decorator-makes-function-forget-that-it-belongs-to-a-class outerframes = inspect.getouterframes(inspect.currentframe()) call_depth_to_class = 1 class_name = outerframes[call_depth_to_class][3] #print "classname=%s" % class_name except: raise RuntimeError( "Internal Error: @api decoration cannot resolve class name for item %s" % item) _patch_member_name(class_name, attr) # wrap the function with API logging and error handling function = get_api_context_decorator(execution_logger)(attr) if function.__name__ == "__init__": command_def = InitClientCommandDefinition(class_name, function) else: member = property(fget=function, fset=item.fset) if is_property else function command_def = ClientCommandDefinition(class_name, member, is_property) attr.command = command_def client_commands.append((class_name, command_def)) return get_clientside_api_stub(command_def.full_name)