def read_config(self,configFilePath=defaultconfigFilePath): """ read config variables from file """ configParser = ConfigParserExtended(allow_no_value=True,interpolation=configparser.BasicInterpolation()) dataset = configParser.read(configFilePath) if len(dataset) != 1: raise ValueError("Config file {} not found!".format(configFilePath)) self.config = configParser
def get_config(): cfg = configparser.ConfigParser( interpolation=configparser.BasicInterpolation()) cfg_path = (os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.ini")) cfg.read(cfg_path) return cfg
def fuzz(): data = open(sys.argv[1], errors="surrogateescape").read() parser_basic = configparser.ConfigParser( interpolation=configparser.BasicInterpolation()) try: parser_basic.read_string(data) for name, proxy in parser_basic.items(): for option, value in parser_basic.items(name): pass repr(proxy) repr(parser_basic) except configparser.Error: pass parser_extended = configparser.ConfigParser( interpolation=configparser.ExtendedInterpolation()) try: parser_extended.read_string(data) for name, proxy in parser_extended.items(): for option, value in parser_extended.items(name): pass repr(proxy) repr(parser_extended) except configparser.Error: pass
def _makeParser(self, defaultValues=None): ret = configparser.ConfigParser( empty_lines_in_values=False, interpolation=configparser.BasicInterpolation()) ret.optionxform = lambda key: key if defaultValues is not None: ret.read_dict({DefaultSec: defaultValues}) return ret
def __init__(self, csvfile_path, config_path): self.reader = csv.reader(open(csvfile_path, "r")) self.config = configparser.ConfigParser(interpolation=configparser.BasicInterpolation()) self.config.read(config_path, encoding=None) self.colname_list = {} # pop out the first line (colname line) for index, colname in enumerate(next(self.reader)): self.colname_list[colname] = index
def setConfig(key, value): config = configparser.ConfigParser() config = configparser.ConfigParser( interpolation=configparser.BasicInterpolation()) config.read("cfg.ini") qwq = config.set('127.0.0.1:5000', key, value) config.write(open('cfg.ini', "r+")) return True # print(getConfig('uploadPermission'))
def load(f, puppet_account_id): manifest_name = f.name manifest = { "schema": "puppet-2019-04-01", "parameters": {}, "accounts": [], "launches": {}, "spoke-local-portfolios": {}, } manifest.update(yaml.safe_load(f.read())) d = os.path.dirname(os.path.abspath(f.name)) extendable = ["parameters", "launches", "spoke-local-portfolios"] for t in extendable: t_path = f"{d}{os.path.sep}{t}" if os.path.exists(t_path): for f in os.listdir(t_path): with open(f"{t_path}{os.path.sep}{f}", "r") as file: manifest[t].update(yaml.safe_load(file.read())) if os.path.exists(f"{d}{os.path.sep}manifests"): for f in os.listdir(f"{d}{os.path.sep}manifests"): with open(f"{d}{os.path.sep}manifests{os.path.sep}{f}", "r") as file: ext = yaml.safe_load(file.read()) for t in extendable: manifest[t].update(ext.get(t, {})) for config_file in [ manifest_name.replace(".yaml", ".properties"), manifest_name.replace(".yaml", f"-{puppet_account_id}.properties"), ]: parser = configparser.ConfigParser( interpolation=configparser.BasicInterpolation()) parser.optionxform = str if os.path.exists(config_file): logger.info(f"reading {config_file}") parser.read(config_file) for name, value in parser["launches"].items(): launch_name, property_name = name.split(".") if property_name != "version": raise Exception( "You can only specify a version in the properties file" ) manifest["launches"][launch_name][property_name] = value for launch_name, launch_details in manifest.get('launches').items(): if launch_details.get('execution') is None: launch_details['execution'] = constants.EXECUTION_MODE_DEFAULT return manifest
def __init__(self, config_file=config_file_path): """ :param config_file: pathlib.Path the absolute path of the file config.ini """ try: config_file = config_file.resolve() except FileNotFoundError: print("config.ini not found under config directory.") parser_obj = configparser.ConfigParser( interpolation=configparser.BasicInterpolation()) parser_obj.read(config_file) self.cx_config = parser_obj["checkmarx"] CxConfig.config = self
def __init__(self, filePath=None): if not filePath: filePath = f"{root_path}{sep}conf{sep}data.ini" # 简单配置文件使用该方法即可 self.rfg = RawConfigParser() self.rfg.read(filePath, encoding="utf-8") # 当配置文件中数据存在变量, 且以 %(variable)s 的格式使用时用此种方法 self.cfgB = ConfigParser( interpolation=configparser.BasicInterpolation()) self.cfgB.read(filePath, encoding="utf-8") # 当配置文件中数据存在参数, 且以 ${variable} 的格式使用时用此种方法 self.cfgE = ConfigParser( interpolation=configparser.ExtendedInterpolation()) self.cfgE.read(filePath, encoding="utf-8")
def get_config_info_from_config_file(): """ Returns: dictionary """ config_info = {} # the absolute path of the file config.ini config_file_path = pathlib.Path.home() / ".Checkmarx/config.ini" try: config_file = config_file_path.resolve(strict=True) parser_obj = configparser.ConfigParser( interpolation=configparser.BasicInterpolation()) parser_obj.read(config_file) config_info = dict(parser_obj["checkmarx"]) except FileNotFoundError: print("config.ini not found under ~/.Checkmarx/ directory.") return config_info
def configparse(configfile): ''' Configuration File ''' global CONFIG global CONFIGPARSER CONFIGPARSER = configparser.ConfigParser( defaults=CONFIG_DEFAULTS, interpolation=configparser.BasicInterpolation()) print('Reading Config: %s' % (configfile)) CONFIGPARSER.read(configfile if os.path.exists(configfile) else os.path. join(env.CONFIG_DIR, 'wisk_parser.cfg')) CONFIG = {} for secname, secproxy in CONFIGPARSER.items(): CONFIG[secname] = {} for k, v in secproxy.items(): CONFIG[secname][k] = v.strip() dtype = CONFIG_DATATYPES.get(secname, {}).get(k, None) if dtype is None: continue for do_op in dtype[0]: CONFIG[secname][k] = do_op(CONFIG[secname][k])
def get_settings_from_config(section_name, conf_file, interpolation=configparser.BasicInterpolation()): """ Returns dict from section_name, and ini based conf_file * section_name ..: Section to read map of properties from (ex: [auth_provider]) * conf_file .....: Ini based config file to read. Will return empty dict if None. * interpolation .: Interpolation to use. If section is not found, or conf_file is None, function will return an empty dictionary. """ conf = configparser.ConfigParser(interpolation=interpolation) if conf_file is None: return {} conf.read(conf_file) if section_name in conf.sections(): return dict(conf.items(section_name)) return {}
configparser.ConfigParser.__init__(self, defaults=None) def optionxform(self, optionstr): return optionstr if __name__ == "__main__": """ Test """ import os, sys if os.path.abspath(os.curdir) not in sys.path: sys.path.append(os.path.abspath(os.curdir)) conf = configparser.ConfigParser( interpolation=configparser.BasicInterpolation()) conf.read("./configs/crmls.ini", encoding=None) print(conf.sections()) for i in conf.sections(): print(conf.options(i)) for option in conf.options(i): print(option, conf.get(i, option)) mconf = myconf() mconf.read("./configs/crmls.ini", encoding=None) print(mconf.sections()) for i in mconf.sections(): print(mconf.options(i)) for option in mconf.options(i): print(option, mconf.get(i, option))
def load(f, puppet_account_id): manifest_name = f.name manifest = { "schema": "puppet-2019-04-01", "parameters": {}, "accounts": [], constants.LAUNCHES: {}, constants.STACKS: {}, constants.SPOKE_LOCAL_PORTFOLIOS: {}, constants.ASSERTIONS: {}, constants.CODE_BUILD_RUNS: {}, constants.LAMBDA_INVOCATIONS: {}, constants.APPS: {}, constants.WORKSPACES: {}, } contents = f.read() contents = contents.replace("${AWS::PuppetAccountId}", puppet_account_id) manifest.update(yaml.safe_load(contents)) d = os.path.dirname(os.path.abspath(f.name)) extendable = constants.ALL_SECTION_NAMES + ["parameters"] for t in extendable: t_path = f"{d}{os.path.sep}{t}" if os.path.exists(t_path): for f in os.listdir(t_path): source = f"{t_path}{os.path.sep}{f}" with open(source, "r") as file: contents = file.read() contents = contents.replace("${AWS::PuppetAccountId}", puppet_account_id) new = yaml.safe_load(contents) for n, v in new.items(): if manifest[t].get(n): raise Exception( f"{source} declares a duplicate {t}: {n}") manifest[t].update(new) if os.path.exists(f"{d}{os.path.sep}manifests"): for f in os.listdir(f"{d}{os.path.sep}manifests"): with open(f"{d}{os.path.sep}manifests{os.path.sep}{f}", "r") as file: contents = file.read() contents = contents.replace("${AWS::PuppetAccountId}", puppet_account_id) ext = yaml.safe_load(contents) for t in extendable: manifest[t].update(ext.get(t, {})) if os.path.exists(f"{d}{os.path.sep}capabilities"): for f in os.listdir(f"{d}{os.path.sep}capabilities"): with open(f"{d}{os.path.sep}capabilities{os.path.sep}{f}", "r") as file: contents = file.read() contents = contents.replace("${AWS::PuppetAccountId}", puppet_account_id) ext = yaml.safe_load(contents) always_merger.merge(manifest, ext) for config_file in [ manifest_name.replace(".yaml", ".properties"), manifest_name.replace(".yaml", f"-{puppet_account_id}.properties"), ]: parser = configparser.ConfigParser( interpolation=configparser.BasicInterpolation()) parser.optionxform = str if os.path.exists(config_file): logger.info(f"reading {config_file}") parser.read(config_file) for section_name, section_values in parser.items(): if section_name == "DEFAULT": continue for item_name, item_value in section_values.items(): name, property_name = item_name.split(".") if property_name != "version": raise Exception( "You can only specify a version in the properties file" ) if manifest.get(section_name, {}).get(name): manifest[section_name][name][ property_name] = item_value else: logger.warning( f"Could not find manifest[{section_name}][{name}]") for section in constants.ALL_SPOKE_EXECUTABLE_SECTION_NAMES: for name, details in manifest.get(section, {}).items(): if details.get("execution") is None: details["execution"] = constants.EXECUTION_MODE_DEFAULT return manifest
from utils import Extractor, MyFunction, Analysis, Specific import configparser as ConfigParser confit_fp = './yang.conf' config = ConfigParser.ConfigParser() config._interpolation = ConfigParser.BasicInterpolation() config.read(confit_fp) # Flag # 1 gainAccOfTest # 2 supplyimage # 3 computeOffLineAcc # 4 gain PCAforWordEmbeeding # 5 testResultAnalysis # 6 saveImageFeature # 7 makeUpTestImageFeature # 8 basedOnTestImageFeatureToPredictTestResult Flag = 9 if Flag == 8: pre_fp = './data/20traindelete_model/' model_key = 'gcn0' Flag_test = False if Flag_test: pre_fc = pre_fp + 'fc_vector_test.txt' Image_feature_fp = pre_fp + model_key + 'TestImageFeaturePredicted.txt' image_fp = config.get("FILE_PATH", 'image') save_final_resut_fp = pre_fp + config.get('SAVE_FILE_PATH', 'final_result') MyFunction.basedOnTestImageFeatureToPredictTestResult( pre_fc, Image_feature_fp, image_fp, save_final_resut_fp)
def configure(self, path_default_config, config_section=None, positional_required_args=[], capture_warnings=True, **kwargs): """ Configure the application. :param str path_default_config: Path to the default configuration file :param str config_section: Name of the configuration section in the configuration file. :param list positional_required_args: List of :py:mod:`argparse` :code:`dest` values of positional required commandline arguments. If such a parameter was found in the configuration file it is **always** appended to the parameters parsed :param bool capture_warnings: Capture warnings :param interpolation: Interpolation behaviour. :code:`None` can be used to turn off interpolation completely. If enabled values are interpolated using environment variables, too. :type interpolation: None or :py:class:`configparser.Interpolation` :param kwargs: Additional keyword value parameters passed to the unterlying configuration file parser """ c_parser = self._build_configfile_parser(path_default_config) args, remaining_argv = c_parser.parse_known_args() defaults = {} if path_default_config and config_section: config_parser = configparser.ConfigParser(**kwargs) config_parser.read(args.config_file) env_dict = None interpolation = kwargs.get('interpolation', configparser.BasicInterpolation()) if (interpolation and isinstance(interpolation, configparser.BasicInterpolation)): # filter out variables containing a '%'; else interpolation # fails env_dict = {k: v for k, v in os.environ.items() if v.find('%') == -1} try: defaults = dict(config_parser.items( config_section, vars=env_dict)) except Exception as err: import warnings warnings.warn( "Exception while parsing config file: {}.".format(err)) self.parser = self.build_parser(parents=[c_parser]) # XXX(damb): Make sure that the default logger has an argument # path_logging_conf try: self.parser.add_argument('--logging-conf', dest='path_logging_conf', metavar='LOGGING_CONF', type=utils.real_file_path, help="path to a logging configuration " "file") except argparse.ArgumentError as err: raise LoggingConfOptionAlreadyAvailable(err) # fetch positional arguments from config file and keep them ordered positional_required = OrderedDict() for dest in positional_required_args: try: positional_required[dest] = defaults[dest] defaults.pop(dest) except KeyError: pass # set defaults taken from configuration file self.parser.set_defaults(**defaults) # set the config_file default explicitly since adding the c_parser as a # parent would change the args.config_file to # default=PATH_EIDANGWS_CONF within the child parser try: self.parser.set_defaults(config_file=args.config_file) except AttributeError: pass try: def _error(msg): sys.exit(ExitCodes.EXIT_ERROR) error_func = self.parser.error self.parser.error = _error self.args = self.parser.parse_args(remaining_argv) except SystemExit as err: # append positional required arguments if parsing at a first glance # failed if err.code == 0: # terminate normally (for e.g. [-h|--help]) sys.exit(ExitCodes.EXIT_SUCCESS) for v in positional_required.values(): remaining_argv.append(v) self.parser.error = error_func self.args = self.parser.parse_args(remaining_argv) self._setup_logger() if not self.logger_configured: self.logger = logging.getLogger() self.logger.addHandler(logging.NullHandler()) logging.captureWarnings(capture_warnings) return self.args
def getConfig(key): config = configparser.ConfigParser() config = configparser.ConfigParser( interpolation=configparser.BasicInterpolation()) config.read("cfg.ini") return int(config['127.0.0.1:5000'][key])
def __init__(self, config_path): self.config = configparser.ConfigParser( interpolation=configparser.BasicInterpolation()) self.config.read(config_path, encoding=None)
def getManager(interpolation=configparser.BasicInterpolation()): return manager(SETTING_FILE_NAME, interpolation)
def __init__(self, default_filePath=None, *, default_values=None, default_section=None, forceExists=False, forceCondition=None, allowNone=True, interpolation=True, valid_section=None, readOnly=False, defaultFileExtension=None, backup_filePath=None, knownTypes=None, knownTypesSection="knownTypes", knownTypeDefault=None, version=None): """ allowNone (bool) - Determines what happens if a setting does not have a set value - If True: Will use None - If False: Will raise an error during load() interpolation (bool) - Determines what kind of interpolation can be done in get() - If True: Extended Interpolation - If False: Basic Interpolation - If None: No Interpolation valid_section (list) - Which sections (excluding DEFAULT) to load - If str: Will load only that section - If None: Will load all sections ~ Optionally, variables can be defined in the section given to 'knownTypesSection' knownTypesSection (str) - Which section is used to store knownTypes - If None: Will not use a section to get knownTypes from version (str) - What version the config file must have - If None: Will not do a version check - If different: Will replace the config file with the one from *default_filePath* Example Input: Configuration(self) Example Input: Configuration(self, source_directory = "database") Example Input: Configuration(self, defaults = {"startup_user": "******"}) """ self.defaultFileExtension = defaultFileExtension or "ini" self.default_section = default_section or "main" self.default_filePath = default_filePath or f"settings.{self.defaultFileExtension}" self.backup_filePath = backup_filePath self.version = version if (interpolation): interpolation = self.MyExtendedInterpolation() elif (interpolation is not None): interpolation = configparser.BasicInterpolation() self.setReset(converters=self.converters, allow_no_value=allowNone, defaults=default_values or {}, interpolation=interpolation) self.reset() # self.config.optionxform = str self.knownTypeDefault = knownTypeDefault or "_default_" self.knownTypesSection = knownTypesSection or None self.knownTypes = knownTypes or {} self.readOnly = readOnly self.set_validSection(valid_section) if (default_filePath): self.load(forceExists=forceExists, forceCondition=forceCondition)