def _sortModuleNames(self, moduleList): """ Returns a module list, sorted by inheritance order. Called from _generateViewRoster(). """ output = [] for moduleName in moduleMapper.getInstalledModules(): if moduleName in moduleList: output.append(moduleName) return output
def _sortModuleNames(self, moduleList): ''' Returns a module list, sorted by inheritance order. Called from _generateViewRoster(). ''' output = [] for moduleName in moduleMapper.getInstalledModules(): if moduleName in moduleList: output.append(moduleName) return output
def _moduleGenerator( self, moduleConfig, position=0, depth=0, parentLayout=None, parentId=None, panelName=None, usageCounter=None, isHidden=False, ): """ Yields every module that is listed in moduleConfig. The dict yielded for each module looks like: { 'parentmodule': '#AsciiTimeline_0_0', 'inheritance': [ 'Splunk.Module', 'Splunk.Module.SearchModule', 'Splunk.Module.SimpleFieldViewer' ], 'className': 'SimpleFieldViewer', 'layoutPanel': 'fullWidthControls', 'id': 'SimpleFieldViewer_1_1', 'templatePath': '../../modules/field_viewer/field_viewer.html', 'params': { 'paramName1': { 'default': 'defaultValue1', 'required': True, 'values': ['acceptableValue1','acceptableValue2','acceptableValue3'] } }, 'stickyParams': ['key1','key2','key3'], 'persistableParams': ['key1','key2','key3'], // custom param fields... 'count': 10, 'field': 'twikiuser' } 'id' is of the form: <module_name>_<module_usage_count>_<tree_depth>_<sibling_position> 'templatePath' is the relative URL (mako seems to need it) of the template 'parentmodule' is the DOM id of parent; not present if a top level module """ # first lookup the module definition # TODO: the module prefix is patched in here moduleName = "Splunk.Module." + moduleConfig["className"] moduleDefinition = moduleMapper.getInstalledModules().get(moduleName, {}) # generate module config for current module output = moduleConfig.copy() # add the module persistence information output["stickyParams"] = moduleDefinition.get("stickyParams", []) output["persistableParams"] = moduleDefinition.get("persistableParams", []) output.setdefault("params", {}) # merge default params supplied by the module's manifest file with the view's params if moduleDefinition: # check that required params defined in .conf are present for param_name, param in moduleDefinition["params"].items(): if not output["params"].has_key(param_name): if param["default"] is not None: output["params"][param_name] = param["default"] elif param["required"]: message.send_client_message( "error", _( "Misconfigured view '%(panel)s' - Parameter '%(param)s' is required for module %(module)s" ) % {"panel": panelName, "param": param_name, "module": moduleConfig["className"]}, ) if param["values"] and param["required"] and output["params"].get(param_name) not in param["values"]: message.send_client_message( "error", _( "Misconfigured view '%(panel)s' - Parameter '%(param)s' must be set to one of %(values)s for module %(module)s" ) % { "panel": panelName, "param": param_name, "values": param["values"], "module": moduleConfig["className"], }, ) # check if the module config defines wildcarded parameters wildcardPrefixes = [x[0:-1] for x in moduleDefinition["params"] if x.endswith("*")] # now remove the wildcard placeholder from param list for wc in wildcardPrefixes: output["params"].pop(wc + "*", None) # the reverse check - enforce that all configured params are documented in the conf. for param_name in output["params"]: # ensure that params that match wildcards are allowed through if any(map(param_name.startswith, wildcardPrefixes)): continue if not moduleDefinition["params"].has_key(param_name) and param_name not in { "group": 1, "groupLabel": 1, "autoRun": 1, "altTitle": 1, }: message.send_client_message( "error", _( "Misconfigured view '%(panel)s' - Unknown parameter '%(param)s' is defined for module %(module)s. Make sure the parameter is specified in %(module)s.conf." ) % {"panel": panelName, "param": param_name, "module": moduleConfig["className"]}, ) if ( moduleDefinition["params"].has_key(param_name) and moduleDefinition["params"][param_name]["translate"] ): translate = moduleDefinition["params"][param_name]["translate"] if translate == "string": output["params"][param_name] = _(output["params"][param_name]) else: i18n.translate_view_params(translate, output["params"][param_name]) # assign a unique DOM ID to this module usageCounter.setdefault(moduleConfig["className"], -1) usageCounter[moduleConfig["className"]] += 1 output["id"] = "%s_%s_%s_%s" % ( moduleConfig["className"], usageCounter[moduleConfig["className"]], depth, position, ) output["isHidden"] = isHidden if moduleConfig.get("intersect"): output["intersect"] = moduleConfig.get("intersect") output["layoutPanel"] = moduleConfig.get("layoutPanel") or parentLayout if not output["layoutPanel"]: raise Exception, _( "Splunk cannot load the specified view because a layoutPanel is not defined for module %s." ) % output["id"] if parentId: output["parentmodule"] = "#%s" % parentId if "html" in moduleDefinition: output["templatePath"] = "=" + moduleDefinition["html"] # use '=' prefix to denote absolute template path if "inheritance" in moduleDefinition: output["inheritance"] = moduleDefinition["inheritance"] if "include" in moduleDefinition: output["include"] = moduleDefinition["include"] if "children" in output: del output["children"] yield output # process children if "children" in moduleConfig: for i, child in enumerate(moduleConfig["children"]): hideChild = ( True if (isHidden or splunk.util.normalizeBoolean(output["params"].get("hideChildrenOnLoad"))) else False ) childOutput = self._moduleGenerator( child, i, depth + 1, output["layoutPanel"], output["id"], panelName=panelName, usageCounter=usageCounter, isHidden=hideChild, ) if isinstance(childOutput, dict): yield childOutput else: for x in childOutput: yield x
def _moduleGenerator(self, moduleConfig, position=0, depth=0, parentLayout=None, parentId=None, panelName=None, usageCounter=None, isHidden=False): ''' Yields every module that is listed in moduleConfig. The dict yielded for each module looks like: { 'parentmodule': '#AsciiTimeline_0_0', 'inheritance': [ 'Splunk.Module', 'Splunk.Module.SearchModule', 'Splunk.Module.SimpleFieldViewer' ], 'className': 'SimpleFieldViewer', 'layoutPanel': 'fullWidthControls', 'id': 'SimpleFieldViewer_1_1', 'templatePath': '../../modules/field_viewer/field_viewer.html', 'params': { 'paramName1': { 'default': 'defaultValue1', 'required': True, 'values': ['acceptableValue1','acceptableValue2','acceptableValue3'] } }, 'stickyParams': ['key1','key2','key3'], 'persistableParams': ['key1','key2','key3'], // custom param fields... 'count': 10, 'field': 'twikiuser' } 'id' is of the form: <module_name>_<module_usage_count>_<tree_depth>_<sibling_position> 'templatePath' is the relative URL (mako seems to need it) of the template 'parentmodule' is the DOM id of parent; not present if a top level module ''' # first lookup the module definition # TODO: the module prefix is patched in here moduleName = 'Splunk.Module.' + moduleConfig['className'] moduleDefinition = moduleMapper.getInstalledModules().get(moduleName, {}) # generate module config for current module output = moduleConfig.copy() # add the module persistence information output['stickyParams'] = moduleDefinition.get('stickyParams', []) output['persistableParams'] = moduleDefinition.get('persistableParams', []) output.setdefault('params', {}) # merge default params supplied by the module's manifest file with the view's params if moduleDefinition: # check that required params defined in .conf are present for param_name, param in moduleDefinition['params'].items(): if not output['params'].has_key(param_name): if param['default'] is not None: output['params'][param_name] = param['default'] elif param['required']: lib.message.send_client_message( 'error', _("Misconfigured view '%(panel)s' - Parameter '%(param)s' is required for module %(module)s") % { 'panel':panelName, 'param':param_name, 'module':moduleConfig['className'] } ) if param['values'] and param['required'] and output['params'].get(param_name) not in param['values']: lib.message.send_client_message( 'error', _("Misconfigured view '%(panel)s' - Parameter '%(param)s' must be set to one of %(values)s for module %(module)s") % { 'panel':panelName, 'param':param_name, 'values': param['values'], 'module': moduleConfig['className'] } ) # check if the module config defines wildcarded parameters wildcardPrefixes = [x[0:-1] for x in moduleDefinition['params'] if x.endswith('*')] # now remove the wildcard placeholder from param list for wc in wildcardPrefixes: output['params'].pop(wc + '*', None) # the reverse check - enforce that all configured params are documented in the conf. for param_name in output['params'] : # ensure that params that match wildcards are allowed through if any(map(param_name.startswith, wildcardPrefixes)): continue if not moduleDefinition['params'].has_key(param_name) and param_name not in {"group":1, "groupLabel": 1, "autoRun":1, "altTitle":1} : lib.message.send_client_message( 'error', _("Misconfigured view '%(panel)s' - Unknown parameter '%(param)s' is defined for module %(module)s. Make sure the parameter is specified in %(module)s.conf.") % { 'panel':panelName, 'param':param_name, 'module':moduleConfig['className'] } ) if moduleDefinition['params'].has_key(param_name) and moduleDefinition['params'][param_name]['translate']: translate = moduleDefinition['params'][param_name]['translate'] if translate == 'string': output['params'][param_name] = _(output['params'][param_name]) else: i18n.translate_view_params(translate, output['params'][param_name]) # assign a unique DOM ID to this module usageCounter.setdefault(moduleConfig['className'], -1) usageCounter[moduleConfig['className']] += 1 output['id'] = '%s_%s_%s_%s' % ( moduleConfig['className'], usageCounter[moduleConfig['className']], depth, position) output['isHidden'] = isHidden if moduleConfig.get('intersect'): output['intersect'] = moduleConfig.get('intersect') output['layoutPanel'] = moduleConfig.get('layoutPanel') or parentLayout if not output['layoutPanel']: raise Exception, _('Splunk cannot load the specified view because a layoutPanel is not defined for module %s.') % output['id'] if parentId: output['parentmodule'] = '#%s' % parentId if 'html' in moduleDefinition: output["templatePath"] = '=' + moduleDefinition['html'] # use '=' prefix to denote absolute template path if 'inheritance' in moduleDefinition: output['inheritance'] = moduleDefinition['inheritance'] if 'include' in moduleDefinition: output['include'] = moduleDefinition['include'] if 'children' in output: del output['children'] yield output # process children if 'children' in moduleConfig: for i, child in enumerate(moduleConfig['children']): hideChild = True if (isHidden or splunk.util.normalizeBoolean(output['params'].get('hideChildrenOnLoad'))) else False childOutput = self._moduleGenerator(child, i, depth+1, output['layoutPanel'], output['id'], panelName=panelName, usageCounter=usageCounter, isHidden=hideChild) if isinstance(childOutput, dict): yield childOutput else: for x in childOutput: yield x