def parseToolsConfFile(self, file): """ Parse a given settings file """ #self.output.printInfo('Parsing configuration file "{0}" ...'.format(file)) full_path = FileUtils.concat_path(self.settings_dir, file) conf_filename = FileUtils.remove_ext(file).lower().strip() self.config_parsers[conf_filename] = DefaultConfigParser() self.config_parsers[conf_filename].read(full_path) # Add the entry into general settings for the service self.general_settings[conf_filename] = {} if conf_filename == MULTI_SERVICES_CONF_FILE: self.general_settings[conf_filename]['tools_categories'] = ['all'] else: # General settings - [general] in .conf file tools_cats = self.config_parsers[conf_filename].safeGetList( 'general', 'tools_categories', ',', []) self.general_settings[conf_filename]['tools_categories'] = [ StringUtils.cleanSpecialChars(e).lower() for e in tools_cats ] # General settings - Optional/Specific settings (depends on the targeted service) if conf_filename in SPECIFIC_TOOL_OPTIONS.keys(): for option in SPECIFIC_TOOL_OPTIONS[conf_filename]: setting_name = SPECIFIC_TOOL_OPTIONS[conf_filename][option] if setting_name: self.general_settings[conf_filename][setting_name] = \ [ e.lower() for e in self.config_parsers[conf_filename].safeGetList('general', setting_name, ',', []) ] # Check general settings for the current service self.checkGeneralSettings(conf_filename) # Add service as new toolbox section self.toolbox.addService(conf_filename) # Add tools in current config file into the toolbox, under the correct service section for section in self.config_parsers[conf_filename].sections(): if section.startswith(PREFIX_TOOL_SECTIONNAME): if conf_filename != MULTI_SERVICES_CONF_FILE: newtool = self.createToolFromConfiguration( section, conf_filename, tooltype=ToolType.STANDARD) else: newtool = self.createToolFromConfiguration( section, conf_filename, tooltype=ToolType.MULTI_SERVICES) elif section.startswith(PREFIX_TOOL_USEMULTI_SECTIONNAME): newtool = self.createToolFromConfiguration( section, conf_filename, tooltype=ToolType.USE_MULTI) else: continue if newtool: if not self.toolbox.addTool(newtool, conf_filename): self.output.printWarning( 'Unable to add tool "{0}" into the toolbox'.format( newtool.name)) else: #self.output.printSettings('Tool "{0}" added into the toolbox (category "{1}")'.format(newtool.name, # newtool.category)) pass
def parseToolGeneralOptions(self, section, service_name, tooltype=ToolType.STANDARD): """ Parse the general options inside a tool section in settings file. General options include: - Mandatory options (depends on the tooltype), defined inside Constants.py - Optional options: install, update - Install status: extracted from INSTALL_STATUS_CONF_FILE @Args section: Section from config file corresponding to a tool service_name: Service targeted by the tool tooltype: ToolType @Returns If success: Dictionary options_general If error: None """ options_general = { 'name': '', 'tool_ref_name': '', 'category': '', 'description': '', 'command': '', 'install': '', 'update': '', 'installed': False, 'last_update': '' } # ---- # Check presence of mandatory options for o in MANDATORY_TOOL_OPTIONS[tooltype]: if o not in self.config_parsers[service_name].options(section): self.output.printWarning( '[{0}{1}] Section "{2}" > missing mandatory option "{3}", skipped' .format(service_name, CONF_EXT, section, o)) return None # ---- # Parse mandatory options try: for o in MANDATORY_TOOL_OPTIONS[tooltype]: options_general[o] = self.config_parsers[service_name].safeGet( section, o, '', None).strip() if o == 'name' or o == 'tool_ref_name': options_general[o] = StringUtils.cleanSpecialChars( options_general[o]) if o == 'category': options_general[o] = StringUtils.cleanSpecialChars( options_general[o]).lower() except: self.output.printWarning( '[{0}{1}] Section "{2}" > syntax error with mandatory options'. format(service_name, CONF_EXT, section)) return None if tooltype == ToolType.MULTI_SERVICES: options_general['category'] = 'all' # ---- # Check mandatory options for o in MANDATORY_TOOL_OPTIONS[tooltype]: if not options_general[o]: self.output.printWarning( '[{0}{1}] Section "{2}" > option "{3}" is empty, section skipped' .format(service_name, CONF_EXT, section, o)) return None if options_general['category'] not in self.general_settings[ service_name]['tools_categories']: self.output.printWarning( '[{0}{1}] Section "{2}" > option "category" ("{3}") not in "tools_categories", section skipped' .format(service_name, CONF_EXT, section, category)) return None # ---- # Parse general+optional options try: options_general['install'] = self.config_parsers[ service_name].safeGet(section, 'install', '', None).strip() options_general['update'] = self.config_parsers[ service_name].safeGet(section, 'update', '', None).strip() except: pass # ---- # Retrieve install status # By default: not installed, no last update date # If the tool entry is actually a reference to a multi-services tool, extract the install status # from [multi] section inside INSTALL_STATUS_CONF_FILE if tooltype == ToolType.USE_MULTI: tool_installed = self.config_parsers[ INSTALL_STATUS_CONF_FILE].safeGet( MULTI_SERVICES_CONF_FILE, options_general['tool_ref_name'], 'false', None).lower().strip() else: tool_installed = self.config_parsers[ INSTALL_STATUS_CONF_FILE].safeGet(service_name, options_general['name'], 'false', None).lower().strip() if tool_installed == 'false': options_general['installed'] = False options_general['last_update'] = '' elif tool_installed == 'true': options_general['installed'] = True options_general['last_update'] = '' else: options_general['installed'] = True options_general['last_update'] = tool_installed return options_general