def load_save(save_filename, program_info): save_config = configparser.ConfigParser() try: save_config.read_file(open(save_filename)) ## Check to make sure it is well formed # if not save_config.has_option('rule_info', 'rule_name'): raise configparser.Error('Missing rule_name') if not save_config.has_option('rule_info','uuid'): raise configparser.Error('Missing rule uuid') if not save_config.has_option('rule_info','skip_brute'): raise configparser.Error('Missing the skip_brute flag for session') if not save_config.has_option('session_info','last_updated'): raise configparser.Error('Missing last_updated') # Set the ruleset info program_info['rule_name'] = save_config.get('rule_info','rule_name') # Set the skip_brute flag program_info['skip_brute'] = save_config.getboolean('rule_info','skip_brute') return save_config except IOError as msg: print("Could not open the session save file.",file=sys.stderr) print("Save File: " + save_filename,file=sys.stderr) return None except configparser.Error as msg: print("Error occured parsing the save file: " + str(msg),file=sys.stderr) return None
def check_config(conf, result): # Some error checking: for section in conf.sections(): if section not in ['APY', 'REPLACEMENTS']: raise configparser.Error("\nUnknown section [%s]" % (section,)) apy_diff = set(k.lower() for k in conf['APY'].keys()) - set(k.lower() for k in result.keys()) if apy_diff: raise configparser.Error("\nUnknown key(s) in section [APY]: %s" % (apy_diff,)) return True
def _add_option(self, optname, value=None, line=None, sep='='): if value is None and line is None: raise configparser.Error('Either value or line must be passed in') elif value and line: raise configparser.Error('value and line are mutually exclusive') if value is not None: line = '%s%s%s' % (optname, sep, value) opt = self._find(optname) if opt: opt.format(line) else: self._lines.append(OptionLine(optname, line))
def read_oms_config_file(): # Reads the oms config file # Returns: AgentID config value if os.path.isfile(OMS_ADMIN_CONFIG_FILE): try: keyvals = config_file_to_kv_pair(OMS_ADMIN_CONFIG_FILE) return keyvals[AGENT_ID].strip() except configparser.NoSectionError, exception: log(DEBUG, exception.message) raise configparser.Error(exception.message) except configparser.NoOptionError, exception: log('DEUBG', exception.message) raise configparser.Error(exception.message)
def __init__(self, *files): "Initialize the capability dictionary" configparser.RawConfigParser.__init__(self) if not files: files = ["gpscap.ini", "/usr/share/gpsd/gpscap.ini"] try: self.read(files, encoding='utf-8') except TypeError: self.read(files) # For Python 2.6 # Resolve uses= members while True: keepgoing = False for section in self.sections(): if self.has_option(section, "uses"): parent = self.get(section, "uses") if self.has_option(parent, "uses"): continue # Found a parent section without a uses = part. for heritable in self.options(parent): if not self.has_option(section, heritable): self.set(section, heritable, self.get(parent, heritable)) keepgoing = True self.remove_option(section, "uses") if not keepgoing: break # Sanity check: All items must have a type field. for section in self.sections(): if not self.has_option(section, "type"): raise configparser.Error("%s has no type" % section) if ((self.get(section, "type") not in ("engine", "vendor", "device"))): raise configparser.Error("%s has invalid type" % section) # Sanity check: All devices must point at a vendor object. # Side effect: build the lists of vendors and devices. self.vendors = [] self.devices = [] for section in self.sections(): if self.get(section, "type") == "vendor": self.vendors.append(section) if self.get(section, "type") == "device": self.devices.append(section) self.vendors.sort() for section in self.sections(): if self.get(section, "type") == "device": if not self.has_option(section, "vendor"): raise configparser.Error("%s has no vendor" % section) if self.get(section, "vendor") not in self.vendors: raise configparser.Error("%s has invalid vendor" % section)
def __init__(self, recipe_dir, filename): """ Constructor. Input: recipe_dir: str: Path to the recipe repo filename: .cfg file to read """ self.recipe_dir = recipe_dir self.filename = filename self.hardcoded_sections = [ "Main", "Global Sources", "PullRequest Dependencies", "Push Dependencies", "Manual Dependencies", "Release Dependencies", "Global Environment", ] self.config = configparser.RawConfigParser() # ConfigParser will default to having case insensitive options and # returning lower case versions of options. This is not good # for environment variables self.config.optionxform = str fname = os.path.join(recipe_dir, filename) valid_files = self.config.read([fname]) if not valid_files: raise configparser.Error("Bad filename: %s" % fname) self.recipe = {}
def __connect(configfile: str = "/etc/cobbler/mongodb.conf"): """ Reads the config file for mongodb and then connects to the mongodb. """ if not pathlib.Path(configfile).is_file(): raise FileNotFoundError( "Specified Cobbler MongoDB config file could not be found!") cp = ConfigParser() try: cp.read(configfile) except configparser.Error as cp_error: raise configparser.Error( "Could not read Cobbler MongoDB config file!") from cp_error host = cp.get("connection", "host", fallback="localhost") port = cp.getint("connection", "port", fallback=27017) # pylint: disable=global-statement global mongodb mongodb = MongoClient(host, port)["cobbler"] try: # The ismaster command is cheap and doesn't require auth. mongodb.admin.command("ismaster") except ConnectionFailure as e: # FIXME: log error raise CX( 'Unable to connect to Mongo database or get database "cobbler"' ) from e except ConfigurationError as e: raise CX( "The configuration of the MongoDB connection isn't correct, please check the Cobbler settings." ) from e
def parse_file(self, fp): if os.path.exists(fp): parser = configparser.ConfigParser() parser.read(fp) for selector_user in parser.sections(): selector = selector_user.lower() if selector not in self.VALID_SELECTORS: self._build_error(selector_user, self.VALID_SELECTORS, 'valid selector') for styling_user in parser[selector]: styling = styling_user.lower() if styling not in self.VALID_STYLINGS: self._build_error(styling_user, self.VALID_STYLINGS, 'valid styling') val_user = parser[selector][styling] val = val_user.lower() if styling == 'fg' or styling == 'bg': if val not in self.VALID_COLORS: self._build_error(val_user, self.VALID_COLORS, 'valid color') else: if val not in self.VALID_BOOLEANS: self._build_error(val_user, self.VALID_BOOLEANS, 'valid boolean') val = self.VALID_BOOLEANS[val] self.styles[selector][styling] = val else: raise configparser.Error(f'{fp!r} is not a valid filepath.')
def read_config(config_file): try: config = configparser.ConfigParser() config.read(config_file) return config except: configparser.Error('error')
def __init__(self, cfg_file_path): self.cfg_file_path = cfg_file_path self.cfg_folder_path = os.path.dirname(cfg_file_path) self.configuration = configparser.ConfigParser() if not self.configuration.read(self.cfg_file_path): raise configparser.Error('{} file not found'.format( self.cfg_file_path)) self.book_infodata_log_file = self._get_config_ebook_extrainfo_log_filename( ) self.anticaptcha_clientkey = self.configuration.get( "ANTICAPTCHA_DATA", 'key') self.packtpub_url = "https://www.packtpub.com" self.my_books_url = "https://www.packtpub.com/account/my-ebooks" self.login_url = "https://www.packtpub.com/register" self.freelearning_url = "https://www.packtpub.com/packt/offers/free-learning" self.req_headers = { 'Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 ' '(KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36' } self.my_packt_email, self.my_packt_password = self._get_config_login_data( ) self.download_folder_path, self.download_formats = self._get_config_download_data( ) if not os.path.exists(self.download_folder_path): message = "Download folder path: '{}' doesn't exist".format( self.download_folder_path) logger.error(message) raise ValueError(message)
def __init__(self, cfgFilePath): self.cfgFilePath = cfgFilePath self.configuration = configparser.ConfigParser() if not self.configuration.read(self.cfgFilePath): raise configparser.Error('{} file not found'.format( self.cfgFilePath)) self.bookInfoDataLogFile = self.__getEbookExtraInfoLogFilename() self.packtPubUrl = "https://www.packtpub.com" self.myBooksUrl = "https://www.packtpub.com/account/my-ebooks" self.loginUrl = "https://www.packtpub.com/register" self.freeLearningUrl = "https://www.packtpub.com/packt/offers/free-learning" self.reqHeaders = { 'Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 ' '(KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36' } self.myPacktEmail, self.myPacktPassword = self.__getLoginData() self.downloadFolderPath, self.downloadFormats, self.downloadBookTitles = self.__getDownloadData( ) if not os.path.exists(self.downloadFolderPath): message = "Download folder path: '{}' doesn't exist".format( self.downloadFolderPath) logger.error(message) raise ValueError(message) self.session = self.createSession()
def ishash_same(in_file, in_name=None, in_config_loc='default'): _cfg = configparser.ConfigParser() _cfg_loc = pathmaker( 'cwd', 'config', 'solid_config.ini') if in_config_loc == 'default' else in_config_loc _bin_file = readbin(in_file) _name = splitoff(in_file)[1].replace('.', '') if in_name is None else in_name _cfg.read(_cfg_loc) _hash = hashlib.md5(_bin_file).hexdigest() if _cfg.has_section('hashes') is True: if _cfg.has_option('hashes', _name): if _cfg.get('hashes', _name) != _hash: _out = False log.info("hashes are !NOT! the same") elif _cfg.get('hashes', _name) == _hash: _out = True log.info("hashes are the same") else: _out = False log.info('missing option') else: log.critical( "section ['hashes'] is missing in solid_config.ini, it is absolutely needed" ) raise configparser.Error("section ['hashes'] does not exist!!") return _out
def load_config(raw): """Read in string data; return a "peeled" config A peeled config structure contains only JSON-like objects (dicts) or primitives, as shown in ``default_config``. Objects returned are "assumed peeled" but may be far from it. raw data in string form or path to a file JSON dedupes automatically by updating existing items. The IniParser should buck with some native exception. """ stripped = raw.strip() if stripped.startswith("{") and stripped.endswith("}"): import json return json.loads(stripped) elif "\n" in stripped: from .iniquitous import subdivide_ini, parse_ini sections = subdivide_ini(raw) return parse_ini(sections) else: path = PurePosixPath(raw) # Raises FileNotFoundError if missing with open(path) as flo: if path.suffix == ".json": import json return json.load(flo) else: raw = flo.read() if "\n" not in raw.strip(): raise configparser.Error("Unrecognized config format") return load_config(raw)
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 __set_config_data(self, cfg_file_path): """Sets all the config data for Google drive manager""" configuration = configparser.ConfigParser() if not configuration.read(cfg_file_path): raise configparser.Error('{} file not found'.format(cfg_file_path)) self.app_name = configuration.get("GOOGLE_DRIVE_DATA", 'gdAppName') self.folder_name = configuration.get("GOOGLE_DRIVE_DATA", 'gdFolderName')
def param(self, section, name, dtype=str, default=None): try: if dtype is bool: data = self.getboolean(section, name) elif dtype is int: data = self.getint(section, name) elif dtype is float: data = self.getfloat(section, name) elif dtype is str: data = self.get(section, name) else: raise configparser.Error('Not found type {}'.format( type(dtype))) except ValueError: return default except configparser.NoSectionError: data = default self.add_section(section) self.set(section, name, str(data)) self._write_file() except configparser.NoOptionError: data = default self.set(section, name, str(data)) self._write_file() return data
def hash_to_solidcfg(in_file, in_name=None, in_config_loc='default'): _cfg = configparser.ConfigParser() _cfg_loc = pathmaker( 'cwd', 'config', 'solid_config.ini') if in_config_loc == 'default' else in_config_loc _bin_file = readbin(in_file) _name = splitoff(in_file)[1].replace('.', '') if in_name is None else in_name _cfg.read(_cfg_loc) _hash = hashlib.md5(_bin_file).hexdigest() if _cfg.has_section('hashes') is False: log.info( "section ['hashes'] does not exist in solid_config.ini, creating it now!" ) _cfg.add_section('hashes') log.info("section ['hashes'] added to solid_config.ini") _cfg.set('hashes', _name, _hash) log.info(f"added hash [{_hash}] to section ['hashes'] in solid_config.ini") with open(_cfg_loc, 'w') as configfile: _cfg.write(configfile) log.debug("saved new solid_config.ini at '{_cfg_loc}'") _cfg.read(_cfg_loc) if _cfg.get('hashes', _name) != _hash: raise configparser.Error( "recently saved hash does not match the file hash")
def update(cls, value: str) -> str: """Update value from configfile with external resources. If the value contains a variable with a valid converter prefix, the variable is replaced with the value of the corresponding converter function. Args: value: Value of config option possibly containing external variables. Returns: The value with any external variables replaced by their value. """ match = VARIABLE_RE.match(value) if match is None: return value full_match, variable = match.group(0), match.group(1) _logger.debug("Updating config value '%s' using '%s'", value, full_match) for prefix, converter in ExternalInterpolation.CONVERTERS.items(): if variable.lower().startswith(prefix): variable_value = converter(variable[len(prefix) :]) _logger.debug( "Updated '%s' = '%s' using '%s' converter", full_match, variable_value, prefix.rstrip(":"), ) return value.replace(full_match, variable_value) raise configparser.Error( f"Invalid variable name '{variable}', " f"must start with one of: {quotedjoin(cls.CONVERTERS)}" )
def validate(option, section, key): if option == '--INVALID--': raise configparser.Error( 'Found default invalid option in \'{}\' key in \'{}\' section!'. format(key, section)) return option
def read_worker_state(): # Reads the state.config file and returns the values of pid, workspace_id, resource_running_version if os.path.isfile(WORKER_STATE_FILE_PATH): state = configparser.configparser() try: state.read(WORKER_STATE_FILE_PATH) pid = state.get(STATE_SECTION, PID) workspace_id = state.get(STATE_SECTION, WORKSPACE_ID) resource_running_version = state.get(STATE_SECTION, DSC_RESOURCE_VERSION) except configparser.NoSectionError, exception: log(DEBUG, exception.message) raise configparser.Error(exception.message) except configparser.NoOptionError, exception: log(DEBUG, exception.message) raise configparser.Error(exception.message)
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 sendmail(text, config_filename): """Send email message using Python SMTP library""" # Read config file from disk to get SMTP server host, port, username # FIXME: move config stuff out of this function? if not hasattr(sendmail, "host"): config = configparser.RawConfigParser() config.read(config_filename) sendmail.host = config.get("smtp_server", "host") sendmail.port = config.getint("smtp_server", "port") sendmail.username = config.get("smtp_server", "username") sendmail.security = config.get("smtp_server", "security") print(">>> Read SMTP server configuration from {}".format( config_filename)) print(">>> host = {}".format(sendmail.host)) print(">>> port = {}".format(sendmail.port)) print(">>> username = {}".format(sendmail.username)) print(">>> security = {}".format(sendmail.security)) # Prompt for password if not hasattr(sendmail, "password"): prompt = ">>> password for {} on {}: ".format(sendmail.username, sendmail.host) sendmail.password = getpass.getpass(prompt) # Parse message headers message = email.parser.Parser().parsestr(text) # Connect to SMTP server if sendmail.security == "SSL/TLS": smtp = smtplib.SMTP_SSL(sendmail.host, sendmail.port) elif sendmail.security == "STARTTLS": smtp = smtplib.SMTP(sendmail.host, sendmail.port) smtp.ehlo() smtp.starttls() smtp.ehlo() elif sendmail.security == "Never": smtp = smtplib.SMTP(sendmail.host, sendmail.port) else: raise configparser.Error("Unrecognized security type: {}".format( sendmail.security)) # Send credentials smtp.login(sendmail.username, sendmail.password) # Send message try: # Python 3.x smtp.send_message(message) except AttributeError: # Python 2.7.x smtp.sendmail( message["from"], message["to"], message.as_string(), ) smtp.close()
def validate_booleans(config): """ Ensures that the Predict options that should be boolean, are. """ try: config.getboolean("WHITELIST", "WHITELIST_ENABLED") except ValueError: # TODO: List suitable boolean values as specified by the get_boolean method of configparser. raise configparser.Error( "The value of " + config["WHITELIST"]["WHITELIST_ENABLED"] + " from section WHITELIST, option WHITELIST_ENABLED is not a suitable boolean value!" ) try: config.getboolean("SECURITY", "LOGIN_REQUIRED") except ValueError: raise configparser.Error( "The value of " + config["SECURITY"]["LOGIN_REQUIRED"] + " from section SECURITY option LOGIN_REQUIRED is not a suitable boolean value!" )
def sendmail(text, config_filename): """Send email message using Python SMTP library.""" # Read config file from disk to get SMTP server host, port, username if not hasattr(sendmail, "host"): config = configparser.RawConfigParser() config.read(config_filename) sendmail.host = config.get("smtp_server", "host") sendmail.port = config.getint("smtp_server", "port") sendmail.username = config.get("smtp_server", "username") sendmail.security = config.get("smtp_server", "security") print(">>> Read SMTP server configuration from {}".format( config_filename)) print(">>> host = {}".format(sendmail.host)) print(">>> port = {}".format(sendmail.port)) print(">>> username = {}".format(sendmail.username)) print(">>> security = {}".format(sendmail.security)) # Parse message headers (message, recipients) = parsemail(text) # Prompt for password if not hasattr(sendmail, "password"): if sendmail.security == "Dummy": sendmail.password = None else: prompt = ">>> password for {} on {}: ".format( sendmail.username, sendmail.host) sendmail.password = getpass.getpass(prompt) # Connect to SMTP server if sendmail.security == "SSL/TLS": smtp = smtplib.SMTP_SSL(sendmail.host, sendmail.port) elif sendmail.security == "STARTTLS": smtp = smtplib.SMTP(sendmail.host, sendmail.port) smtp.ehlo() smtp.starttls() smtp.ehlo() elif sendmail.security == "Never": smtp = smtplib.SMTP(sendmail.host, sendmail.port) elif sendmail.security == "Dummy": smtp = smtp_dummy.SMTP_dummy() else: raise configparser.Error("Unrecognized security type: {}".format( sendmail.security)) # Send credentials smtp.login(sendmail.username, sendmail.password) # Send message. Note that we can't use the elegant # "smtp.send_message(message)" because that's python3 only smtp.sendmail( message["from"], recipients, message.as_string(), ) smtp.close()
def getenv(variable: str) -> str: """Return the value of an environment variable. Used as converter function for parser and therefore raises configparser.Error on failure. """ try: return os.environ[variable] except KeyError: raise configparser.Error(f"Variable '{variable}' not found in environment")
def read_oms_primary_workspace_config_file(): # Reads the oms config file # Returns: AgentID config value if os.path.isfile(OMS_ADMIN_CONFIG_FILE): # the above path always points to the oms configuration file of the primary workspace try: keyvals = config_file_to_kv_pair(OMS_ADMIN_CONFIG_FILE) return keyvals[OMS_WORKSPACE_ID_KEY].strip( ), keyvals[AGENT_ID].strip() except configparser.NoSectionError as exception: log(DEBUG, exception.message) raise configparser.Error(exception.message) except configparser.NoOptionError as exception: log(DEBUG, exception.message) raise configparser.Error(exception.message) else: error_string = "could not find file " + OMS_ADMIN_CONFIG_FILE log(DEBUG, error_string) raise configparser.Error(error_string)
def headlightsSaveCfgTest(): configfile = configparser.ConfigParser() configfile['testsect'] = {} configfile['testsect']['test'] = 'true' with open('config/test.cfg', 'w') as fileman: configfile.write(fileman) configfile = configparser.ConfigParser() configfile.read("config/test.cfg") if configfile['testsect']['test'] != 'true': raise configparser.Error("Could not write to test.cfg successfully")
def manage_config(files): """Update the __configparser__ using the passed files. :param files: List of strings representing paths or filenames :type files: [strings] :rtype: None """ if not files or not all(os_isfile(x) for x in files): raise configparser.Error("One or more of the files passed as argument for config do not exist.\n") __configparser__.read(files)
def __enter__(self): config = configparser.ConfigParser() success = config.read(self.config_file) if not success: raise configparser.Error("Could not read file {}".format( self.config_file)) self.cnx = mysql.connector.connect(user=config['SQL']['user'], password=config['SQL']['password'], database=config['SQL']['database']) return self.cnx
def getLoginData(self, cfgFilePath): config =configparser.ConfigParser() try: if(not config.read(cfgFilePath)): raise configparser.Error(cfgFilePath+ ' file not found') email= config.get("LOGIN_DATA",'email') password= config.get("LOGIN_DATA",'password') return (email,password) except configparser.Error as e: print("[ERROR] "+cfgFilePath+ " file incorrect or doesn't exist! : "+str(e)) sys.exit(1)