def get_opt_tags(self, section, option, tags): """ Supplement to ConfigParser.ConfigParser.get(). This will search for an option in [section] and if it doesn't find it will also try in [section-tag] for every value of tag in tags. Will raise a ConfigParser.Error if it cannot find a value. Parameters ----------- self : ConfigParser object The ConfigParser object (automatically passed when this is appended to the ConfigParser class) section : string The section of the ConfigParser object to read option : string The ConfigParser option to look for tags : list of strings The name of subsections to look in, if not found in [section] Returns -------- string The value of the options being searched for """ # Need lower case tag name; also exclude cases with tag=None if tags: tags = [tag.lower() for tag in tags if tag is not None] try: return self.get(section, option) except ConfigParser.Error: err_string = "No option '%s' in section [%s] " % (option, section) if not tags: raise ConfigParser.Error(err_string + ".") return_vals = [] sub_section_list = [] for sec_len in range(1, len(tags) + 1): for tag_permutation in itertools.permutations(tags, sec_len): joined_name = "-".join(tag_permutation) sub_section_list.append(joined_name) section_list = ["%s-%s" % (section, sb) for sb in sub_section_list] err_section_list = [] for sub in sub_section_list: if self.has_section("%s-%s" % (section, sub)): if self.has_option("%s-%s" % (section, sub), option): err_section_list.append("%s-%s" % (section, sub)) return_vals.append( self.get("%s-%s" % (section, sub), option)) # We also want to recursively go into sections if not return_vals: err_string += "or in sections [%s]." % ( "] [".join(section_list)) raise ConfigParser.Error(err_string) if len(return_vals) > 1: err_string += ("and multiple entries found in sections [%s]." % ("] [".join(err_section_list))) raise ConfigParser.Error(err_string) return return_vals[0]
def check_config(config): sane = True apihost = [ "api.cloudpassage.com", "api.lichi.cloudpassage.com", "api.bass.cloudpassage.com", "api.zink.cloudpassage.com" ] try: # make sure all the values are there and the lenghts match if not config.get("halo", "api_host") in apihost or \ len(config.get('halo', 'api_key')) != 8 or\ len(config.get('halo', 'api_secret')) != 32: raise configparser.Error( 'Ensure you have an 8 digit api_key, 32 digit api_secret ' 'and a valid api_host entry in your config file.') # grab the configured path or use the current working directory repo_path = config.get("halo", "repo_base_path") or "." if not os.path.isdir(repo_path): print("Repo path does not exist or you do not have " "rights to access it.") sane = False except configparser.NoOptionError as noe: sane = False print('The configuration file is malformed or missing a value.') print(noe._get_message()) except configparser.Error as e: sane = False print(e) return sane
def _set_config_file_value(cls, section, option, value, name=None, filename=None): """ Write a config value to a file creating it if needed On errors a ConfigParserError is raised """ if not name and not filename: raise configparser.Error( "Either 'name' or 'filename' must be given") if not filename: filename = os.path.expanduser(cls._name_to_filename(name)) # Create e new config parser since we only operate on a single file cfg = configparser.RawConfigParser() cfg.read(filename) if not cfg.has_section(section): cfg.add_section(section) cfg.set(section, option, value) with open(filename, 'w') as fp: cfg.write(fp)
def parse_lists(self): """ Parse options that can be given as lists Since they take multiple arguments they can also be given in plural form e.g. components instead of component. """ for opt in self.list_opts: try: plural_opt = opt + 's' valp = self._listify(self.config.get(plural_opt, None)) vals = self._listify(self.config[opt]) if valp and vals: raise configparser.Error("Found %s and %s - use only one" % (valp, vals)) self.config[opt] = valp or vals except ValueError: raise configparser.Error("Failed to parse %s: %s" % (opt, self.config[opt]))
def _listify(value): """ >>> GbpOptionParser._listify(None) [] >>> GbpOptionParser._listify('string') ['string'] >>> GbpOptionParser._listify('["q", "e", "d"] ') ['q', 'e', 'd'] >>> GbpOptionParser._listify('[') Traceback (most recent call last): ... configparser.Error: [ is not a proper list """ # filter can be either a list or a string, always build a list: if value: if value.startswith('['): try: return eval(value) except SyntaxError: raise configparser.Error("%s is not a proper list" % value) else: return [value] else: return []
def setup_tmpltbank_pregenerated(workflow, tags=None): ''' Setup CBC workflow to use a pregenerated template bank. The bank given in cp.get('workflow','pregenerated-template-bank') will be used as the input file for all matched-filtering jobs. If this option is present, workflow will assume that it should be used and not generate template banks within the workflow. Parameters ---------- workflow: pycbc.workflow.core.Workflow An instanced class that manages the constructed workflow. tags : list of strings If given these tags are used to uniquely name and identify output files that would be produced in multiple calls to this function. Returns -------- tmplt_banks : pycbc.workflow.core.FileList The FileList holding the details of the template bank. ''' if tags is None: tags = [] # Currently this uses the *same* fixed bank for all ifos. # Maybe we want to add capability to analyse separate banks in all ifos? # Set up class for holding the banks tmplt_banks = FileList([]) cp = workflow.cp global_seg = workflow.analysis_time file_attrs = {'segs': global_seg, 'tags': tags} try: # First check if we have a bank for all ifos pre_gen_bank = cp.get_opt_tags('workflow-tmpltbank', 'tmpltbank-pregenerated-bank', tags) file_attrs['ifos'] = workflow.ifos curr_file = resolve_url_to_file(pre_gen_bank, attrs=file_attrs) tmplt_banks.append(curr_file) except ConfigParser.Error: # Okay then I must have banks for each ifo for ifo in workflow.ifos: try: pre_gen_bank = cp.get_opt_tags( 'workflow-tmpltbank', 'tmpltbank-pregenerated-bank-%s' % ifo.lower(), tags) file_attrs['ifos'] = [ifo] curr_file = resolve_url_to_file(pre_gen_bank, attrs=file_attrs) tmplt_banks.append(curr_file) except ConfigParser.Error: err_msg = "Cannot find pregerated template bank in section " err_msg += "[workflow-tmpltbank] or any tagged sections. " if tags: tagged_secs = " ".join("[workflow-tmpltbank-%s]" \ %(ifo,) for ifo in workflow.ifos) err_msg += "Tagged sections are %s. " % (tagged_secs, ) err_msg += "I looked for 'tmpltbank-pregenerated-bank' option " err_msg += "and 'tmpltbank-pregenerated-bank-%s'." % (ifo, ) raise ConfigParser.Error(err_msg) return tmplt_banks