def model_name_to_filename(name, tmp=False): assert isinstance(name, basestring) assert is_valid_identifier(name), "Invalid model name" suffix = '.tmp' if tmp else '' return '__mlspl_' + name + '.csv' + suffix
def handle(self, in_string): """ The base handler method, inheriting from PersistentServerConnectionApplication Args: in_string (str): a JSON string containing information about the request Returns: The return value, as decided by the handler method that this method delegates to """ request = json.loads(in_string) method = request['method'] path_parts = self.get_path_info_parts(request) try: handler_name = path_parts[0] if not is_valid_identifier(handler_name): raise Exception('REST handlers can only start with letters and underscores and can contain only letters, numbers, and underscores') try: handler_package = importlib.import_module("rest_handlers.%s" % handler_name) handler_class = getattr(handler_package, handler_name.title().replace('_', '')) except (AttributeError, ImportError) as e: logger.exception(e) return { 'payload': 'Unknown REST endpoint: %s' % handler_name, 'status': 404 } try: handler_method = getattr(handler_class, "handle_%s" % method.lower()) reply = handler_method(request, path_parts) except AttributeError as e: logger.debug(e) return { 'payload': 'Unsupported method: %s' % method, 'status': 405 } except Exception as e: logger.debug(e) return { 'payload': 'Internal REST handler error', 'status': 500 } return reply
def clone_experiment_models(self, experiment_fetch_reply, request, url_parts): """ the function performs the "clone models" operation for an experiment, the experiment info is from 'experiment_fetch_reply' Args: experiment_fetch_reply (dict) : the reply from a mltk/experiments/<guid> POST request request (dict) : the request object url_parts (list) : a subset of the url, here is a list of length 1 which contains experiment id. Returns: (dict) a dictionary of `status` and `payload` """ target_info = json.loads(request.get('payload', {})) if target_info.viewkeys() == {'app', 'name'}: target_model_name = target_info.get('name') if not is_valid_identifier(target_model_name): raise SplunkRestProxyException( 'Invalid model name "%s"' % target_model_name, logging.ERROR, httplib.BAD_REQUEST) source_searchinfo = searchinfo_from_request(request) target_searchinfo = copy.deepcopy(source_searchinfo) target_searchinfo['app'] = target_info.get('app') clone_experiment_model_callback = self._clone_experiment_model_callback( source_searchinfo, target_searchinfo, target_model_name, url_parts[0], reply_handler=self._add_model_name_to_reply) reply_list = self._handle_all_experiment_models( experiment_fetch_reply, clone_experiment_model_callback) formatted_reply = self._handle_clone_reply(reply_list) return self._handle_reply(formatted_reply, {}, request, url_parts, 'POST') else: raise SplunkRestProxyException( 'This handler only supports "app" and "name" as arguments', logging.ERROR, httplib.BAD_REQUEST)
def experiment_name_to_filename(experiment_name, experiment_type): if not is_valid_identifier(experiment_name): raise Exception("Invalid experiment name '%s'" % experiment_name) return '_'.join( (EXPERIMENT_PREFIX, experiment_type, experiment_name)) + ".csv"