def getInfosDictFromPlugin(self, dirpath, filename): """ Returns the extracted plugin informations as a dictionary. This function ensures that "name" and "path" are provided. :param filename: :param dirpath: """ # use the filename alone to extract minimal informations. # do not import the file, but it okay to read infos = {} module_name = os.path.splitext(filename)[0] plugin_filename = os.path.join(dirpath, filename) if module_name == "__init__": module_name = os.path.basename(dirpath) plugin_filename = dirpath infos["name"] = "%s" % module_name infos["path"] = plugin_filename cf_parser = ConfigParser() cf_parser.add_section("Core") cf_parser.set("Core", "Name", infos["name"]) cf_parser.set("Core", "Module", infos["path"]) # must return these at minimum # return None, cf_parser if plugin doesn't validate return infos, cf_parser
def login(): # Do we have the credentials stored? c = ConfigParser() c.read(CONFIG_FILE) if c.has_section('credentials'): # Get the token from the config file token = c.get('credentials', 'token').strip() else: # Ask for credentials print("Please insert your GitHub credentials below:") user = input("Username: ").strip() password = getpass() auth = authorize(user, password, SCOPES, DESCRIPTION) token = auth.token # Save credentials to the config file c.add_section('credentials') c.set('credentials', 'token', str(token)) c.set('credentials', 'id', str(auth.id)) with open(CONFIG_FILE, 'w') as f: c.write(f) return github_login(token=token)
def collect_mangled_addrs(self): addrs = {} for top in self.topics: for art in top.articles: msg_str = art.raw_message # see http://stackoverflow.com/questions/201323 msg_matches = MANGLED_ADDR_RE.findall(msg_str) if msg_matches is not None: for mtch in msg_matches: if isinstance(mtch, tuple): mtch = mtch[0] if mtch in addrs: addrs[mtch] += 1 else: addrs[mtch] = 1 addrs = OrderedDict(sorted(addrs.items(), key=operator.itemgetter(1), reverse=True)) with open("{0}.cnf".format(self.name), "w") as cnf_f: cnf_p = ConfigParser(dict_type=OrderedDict) cnf_p.add_section(ADDR_SEC_LABEL) for addr in addrs: try: cnf_p.set(ADDR_SEC_LABEL, addr, "") except TypeError: # Failed with addr = ('*****@*****.**', '.') logging.info("Failed with addr = {0}".format(addr)) raise cnf_p.write(cnf_f)
def persist(self): config = ConfigParser() config.add_section('codic') for key, value in self.config.items(): config.set('codic', key, value) with open(Config.CODIC_SETTINGS_FILE, 'w') as f: config.write(f)
def check_user_aws_credential(AWS_CREDENTIAL_FILE_NAME, AWS_FOLDER): ''' get user credential in ~/.aws/credential folder. if there is not any, update that. ''' # check credential file config = ConfigParser() config.read(AWS_CREDENTIAL_FILE_NAME) if 'default' not in config or \ 'aws_access_key_id' not in config['default'] or \ 'aws_secret_access_key' not in config['default']: # create the ~/.aws folder try: os.makedirs(AWS_FOLDER) except OSError as e: if e.errno != errno.EEXIST: raise aws_access_key_id = input('AWS ACCESS KEY ID: ') aws_secret_access_key = input('AWS SECRET ACCESS KEY: ') config.add_section('default') config.set('default', 'aws_access_key_id', aws_access_key_id) config.set('default', 'aws_secret_access_key', aws_secret_access_key) with open(AWS_CREDENTIAL_FILE_NAME, 'a+') as tmpfile: config.write(tmpfile) else: aws_access_key_id = config['default']['aws_access_key_id'] aws_secret_access_key = config['default']['aws_secret_access_key'] return aws_access_key_id, aws_secret_access_key
def get_user_aws_config(AWS_CONFIG_FILE_NAME, AWS_FOLDER): ''' get user credential in ~/.aws/config folder. if there is not any, update that. ''' config = ConfigParser() config.read(AWS_CONFIG_FILE_NAME) if 'default' not in config or \ 'region' not in config['default'] or \ 'output' not in config['default']: # create the ~/.aws folder try: os.makedirs(AWS_FOLDER) except OSError as e: if e.errno != errno.EEXIST: raise region = input( 'Default region name [us-east-1]: ') output = input('Default output format [json]: ') config.add_section('default') config.set('default', 'region', region) config.set('default', 'output', output) with open(AWS_CONFIG_FILE_NAME, 'a+') as tmpfile: config.write(tmpfile) else: region = config['default']['region'] output = config['default']['output'] return region, output
def save(master, args): parser = ConfigParser() parser.add_section(GENERAL_SECTION) parser.set(GENERAL_SECTION, 'user', master.user) parser.set(GENERAL_SECTION, 'password', master.password) parser.set(GENERAL_SECTION, 'pin', str(master.pin)) parser.set(GENERAL_SECTION, 'pout', str(master.pout)) parser.set(GENERAL_SECTION, 'iface', str(master.iface)) for device in master.devices: parser.add_section(device.address) parser.set(device.address, 'name', device.name) for plug in device.plugs: key = PLUG_PREFIX + str(plug.index) parser.set(device.address, key, plug.name) with open(CONFIG_FILE, 'w') as out: parser.write(out) device_count, plug_count = print_master(master) if device_count: print("Saved config with {} device(s) and {} plugs".format(device_count, plug_count)) else: print("Saved config without any devices")
def __init__(self, args=None, configuration=None): if configuration: super().__init__(configuration) return parser = ConfigParser() paths = CONFIG_PATHS if args and args.config: paths = [args.config] + CONFIG_PATHS # Make sure all sections are present parser.read([expand(path) for path in paths]) for section in self._fields: if not parser.has_section(section): parser.add_section(section) # Override from CLI data = self._override_config(parser, args) # Parse config without interpolation first, then do interpolation afterward. This prevents # fields with defaults from not being interpolated properly first_pass_conf = Config(configuration=data) first_pass = first_pass_conf.to_dict() # Do string interpolation for section, settings in first_pass.items(): nointerpolate = getattr(getattr(first_pass_conf, section), '__nointerpolate__', []) for key, value in settings.items(): settings[key] = value if key in nointerpolate else interpolate(value, settings) super().__init__(first_pass)
def create_config(values, key='General'): config = ConfigParser() config.add_section('General') for option, value in values.items(): config.set('General', option, value) with open(configpath, 'w') as configfile: config.write(configfile)
def create_header(self, data_only): ''' Create the backup file header that contains the meta data about this particular backup. ''' config = SafeConfigParser() config.add_section("ipa") if data_only: config.set('ipa', 'type', 'DATA') else: config.set('ipa', 'type', 'FULL') config.set('ipa', 'time', time.strftime(ISO8601_DATETIME_FMT, time.gmtime())) config.set('ipa', 'host', api.env.host) config.set('ipa', 'ipa_version', str(version.VERSION)) config.set('ipa', 'version', '1') dn = DN(('cn', api.env.host), api.env.container_masters, api.env.basedn) services_cns = [] try: conn = self.get_connection() services = conn.get_entries(dn, conn.SCOPE_ONELEVEL) except errors.NetworkError: logger.critical( "Unable to obtain list of master services, continuing anyway") except Exception as e: logger.error("Failed to read services from '%s': %s", conn.ldap_uri, e) else: services_cns = [s.single_value['cn'] for s in services] config.set('ipa', 'services', ','.join(services_cns)) with open(self.header, 'w') as fd: config.write(fd)
def moodle_config_skeleton(course_name, moodle_id): from configparser import ConfigParser config = ConfigParser() config.add_section("course") config['course']['course_name'] = course_name config['course']['moodle_id'] = str(moodle_id) return config
def _install_hg_hook(path): getenv = os.environ.get if not os.path.isfile(path): # Make the file so we can avoid IOError's open(path, 'w').close() c = ConfigParser() c.readfp(open(path, 'r')) if not c.has_section('hooks'): c.add_section('hooks') if not c.has_option('hooks', 'commit'): c.set('hooks', 'commit', 'python:flake8.hooks.hg_hook') if not c.has_option('hooks', 'qrefresh'): c.set('hooks', 'qrefresh', 'python:flake8.hooks.hg_hook') if not c.has_section('flake8'): c.add_section('flake8') if not c.has_option('flake8', 'complexity'): c.set('flake8', 'complexity', str(getenv('FLAKE8_COMPLEXITY', 10))) if not c.has_option('flake8', 'strict'): c.set('flake8', 'strict', getenv('FLAKE8_STRICT', False)) if not c.has_option('flake8', 'ignore'): c.set('flake8', 'ignore', getenv('FLAKE8_IGNORE', '')) if not c.has_option('flake8', 'lazy'): c.set('flake8', 'lazy', getenv('FLAKE8_LAZY', False)) with open(path, 'w') as fd: c.write(fd)
def _save_auth(self): if "auth" in self.config_dict: parser = ConfigParser() parser.add_section("auth") parser.set("auth", "token", self.config_dict["auth"]["token"]) with open(self.auth_config_file, mode="w+", encoding="utf-8") as auth_config_file: parser.write(auth_config_file)
def save(self): """Save the modules to @_path. If @modules is an empty dict, then @_path should be removed. """ root_logger.debug("Saving StateFile to '%s'", self._path) for module in list(self.modules): if len(self.modules[module]) == 0: del self.modules[module] if len(self.modules) == 0: root_logger.debug(" -> no modules, removing file") if os.path.exists(self._path): os.remove(self._path) return p = SafeConfigParser() p.optionxform = str for module in self.modules: p.add_section(module) for (key, value) in self.modules[module].items(): p.set(module, key, str(value)) with open(self._path, "w") as f: p.write(f)
def add_repo(self, name, uri, repo_type='rpm-md', prio=None): """ Add yum repository :param string name: repository base file name :param string uri: repository URI :param repo_type: repostory type name :param int prio: yum repostory priority """ repo_file = self.shared_yum_dir['reposd-dir'] + '/' + name + '.repo' self.repo_names.append(name + '.repo') if 'iso-mount' in uri: # iso mount point is a tmpdir, thus different each time we build Path.wipe(repo_file) if os.path.exists(uri): # yum requires local paths to take the file: type uri = 'file://' + uri if not os.path.exists(repo_file): repo_config = ConfigParser() repo_config.add_section(name) repo_config.set( name, 'name', name ) repo_config.set( name, 'baseurl', uri ) if prio: repo_config.set( name, 'priority', format(prio) ) with open(repo_file, 'w') as repo: repo_config.write(repo)
def check_task_credential(): ''' get the credential and configures stored in ~/.cloud_pipe/task, if there is not any, collect those inforamtions and saved in ~/.cloud_pipe ''' folder = get_full_path('~/.cloud_pipe') path = get_full_path('~/.cloud_pipe/task') config = ConfigParser() config.read(path) if 'default' not in config: aws_access_key_id = input('run task AWS ACCESS KEY ID: ') aws_secret_access_key = input('run task AWS SECRET ACCESS KEY: ') region = input( 'Default region name [us-east-1]: ') output = input('Default output format [json]: ') config.add_section('default') config.set('default', 'aws_access_key_id', aws_access_key_id) config.set('default', 'aws_secret_access_key', aws_secret_access_key) config.set('default', 'region', region) config.set('default', 'output', output) # try: os.makedirs(folder) except OSError as e: if e.errno != errno.EEXIST: raise with open(path, 'w+') as tmpfile: config.write(tmpfile) return config
def dummy_config_parser() -> ConfigParser: cp = ConfigParser() cp.add_section('Core') cp.set('Core', 'Name', 'dummy') cp.set('Core', 'Module', 'dummy') cp.add_section('Errbot') return cp
def check_update(): """Check if there is a new version""" import requests config = ConfigParser() try: config.read(CONFIG) try: section = config["update"] except Exception: config.add_section("update") section = config["update"] except Exception: section = dict() check = float(section.get("check", 0)) ver = section.get("version", __version__) url = section.get("url", None) now = time.time() if check + 86400 < now: ver = requests.get(UPDATE_INFO).json()[0] url = ver["zipball_url"] ver = ver["name"] section["check"] = str(now) section["version"] = ver section["url"] = url with open(CONFIG, "w") as configfile: config.write(configfile) if ([int(i) for i in ver.replace("v", "").split(".")] > [int(i) for i in __version__.split(".")] and url): print("New version {} available:\nInstall: pip3 install -U {}". format(ver, url), flush=True)
def gen_build_version(): builddate = time.asctime() gitloc = "/usr/bin/git" gitdate = "?" gitstamp = "?" if not os.path.isfile(gitloc): print("warning: " + gitloc + " not found") else: cmd = subprocess.Popen([gitloc, "log", "--format=%h%n%ad", "-1"], stdout=subprocess.PIPE) data = cmd.communicate()[0].strip() if cmd.returncode == 0: gitstamp, gitdate = data.split(b"\n") fd = open(os.path.join(OUTPUT_DIR, "version"), "w+") config = ConfigParser() config.add_section("cobbler") config.set("cobbler", "gitdate", str(gitdate)) config.set("cobbler", "gitstamp", str(gitstamp)) config.set("cobbler", "builddate", builddate) config.set("cobbler", "version", VERSION) config.set("cobbler", "version_tuple", str([int(x) for x in VERSION.split(".")])) config.write(fd) fd.close()
def __init__(self, rubik_config=None, rubik_dir=None): rubik_dir, rubik_config = self.get_config_rubik_dir_and_config(rubik_config=rubik_config, rubik_dir=rubik_dir) self.rubik_dir = rubik_dir self.rubik_config = rubik_config self.create_missing_files() config = ConfigParser(defaults=self.CONFIG_DEFAULTS) if os.path.exists(self.rubik_config): try: config.read(self.rubik_config) except Exception as err: raise RubikError("cannot read config file {}: {}: {}".format(self.rubik_config, type(err).__name__, err)) if not config.has_section("general"): config.add_section("general") try: self.preferred_output_action = config.get("general", "preferred_output_action") self.preferred_controller = config.get("general", "preferred_controller") self.preferred_visualizer = config.get("general", "preferred_visualizer") self.default_options = shlex.split(config.get("general", "default_options")) self.default_data_type = config.get("general", "default_data_type") self.default_file_format = config.get("general", "default_file_format") self.default_memory_limit = Memory(config.get("general", "default_memory_limit")) self.default_read_threshold_size = Memory(config.get("general", "default_read_threshold_size")) self.default_clobber = config.getboolean("general", "default_clobber") self.default_warnings = shlex.split(config.get("general", "default_warnings")) self.default_trace_errors = config.getboolean("general", "default_trace_errors") self.default_verbose_level = config.getint("general", "default_verbose_level") self.default_report_level = config.getint("general", "default_report_level") except Exception as err: raise RubikError("invalid config file {}: {}: {}".format(self.rubik_config, type(err).__name__, err)) self.visualizer_attributes = { self.VISUALIZER_VolumeSlicer: self.read_attribute_file(self.get_visualizer_defaults_filename(self.VISUALIZER_VolumeSlicer)), } self.controller_attributes = { self.CONTROLLER_Controller: self.read_attribute_file(self.get_controller_defaults_filename(self.CONTROLLER_Controller)), }
def migrate_config(config_file: str, config_obj: configparser.ConfigParser, send: Callable[[str], None]) -> None: example_obj = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation()) example_obj.read_string(resources.read_text('cslbot.static', 'config.example')) modified = False # Check for new sections/options for section in example_obj.sections(): if not config_obj.has_section(section): send("Adding config section %s" % section) config_obj.add_section(section) modified = True for option in example_obj.options(section): if not config_obj.has_option(section, option): send("Adding default value for config option %s.%s" % (section, option)) config_obj[section][option] = example_obj[section][option] modified = True # Check for removed sections/options for section in config_obj.sections(): if example_obj.has_section(section): for option in config_obj.options(section): if not example_obj.has_option(section, option): send("Obsolete config option %s.%s, consider removing." % (section, option)) else: send("Obsolete config section %s, consider removing." % section) if modified: send("Config file automatically migrated, please review.") with open(config_file, 'w') as f: config_obj.write(f)
def _install_hg_hook(path): if not os.path.isfile(path): # Make the file so we can avoid IOError's open(path, 'w+').close() c = ConfigParser() c.readfp(open(path, 'r')) if not c.has_section('hooks'): c.add_section('hooks') if not c.has_option('hooks', 'commit'): c.set('hooks', 'commit', 'python:flake8.hooks.hg_hook') if not c.has_option('hooks', 'qrefresh'): c.set('hooks', 'qrefresh', 'python:flake8.hooks.hg_hook') if not c.has_section('flake8'): c.add_section('flake8') if not c.has_option('flake8', 'complexity'): c.set('flake8', 'complexity', str(os.getenv('FLAKE8_COMPLEXITY', 10))) if not c.has_option('flake8', 'strict'): c.set('flake8', 'strict', os.getenv('FLAKE8_STRICT', False)) c.write(open(path, 'w+'))
def setup(formula, cl_args): config = ConfigParser() config.add_section('Project') config.set('Project', 'type', cl_args.type) # Run environment setup make_virtualenv(cl_args) pip_install('uwsgi') # Formula-specific setup formula.setup(cl_args) current_virtualenv = os.path.join(env.virtualenv_root, cl_args.name) # Secretly put the config file inside virtualenv to store project info with chdir(current_virtualenv): with open(env.project_config_file_name, 'w+') as f: config.write(f) # Setup nginx add_nginx_conf(cl_args.name, formula.get_nginx_conf(cl_args)) # Setup uWSGI ini_file = add_uwsgi_ini(cl_args, formula) uwsgi = os.path.join(current_virtualenv, 'bin', 'uwsgi') cmd = '{uwsgi} --ini {ini_file}'.format(uwsgi=uwsgi, ini_file=ini_file) print('Starting daemon...') run(cmd, quiet=True) add_startup_conf(env.startup_script_prefix + cl_args.name, cmd) reload_nginx() # Fix permission fix_permission(current_virtualenv)
def loadSystemConfig (): global configlog configdir = findProgramDirectory () gameconfigdir = joinpath (configdir, "games") if not exists (gameconfigdir): mkdir (gameconfigdir) configname = joinpath (configdir, "pydoom.ini") config = ConfigParser () config.add_section ('video') config.set ('video', 'fullscreen', 'no') config.set ('video', 'width', '640') config.set ('video', 'height', '480') if not exists (configname): configlog.info ("{} doesn't exist! Creating it.".format (configname)) config.write (open (configname, 'wt')) else: configlog.info ("Read settings from {}.".format (configname)) configfile = open (configname, 'rt') config.read_file (configfile) configfile.close ()
def load(self, config: ConfigParser, modulelist: Dict[str, BaseManager]) -> None: if not isinstance(config, ConfigParser): raise TypeError("config") if not config.has_section("MAIN"): config.add_section("MAIN") if not config.has_section("TIMELINE"): config.add_section("TIMELINE") self._currentName = None for sectionName in self._sectionlist.keys(): sectionBlock = self._sectionlist[sectionName] sectionBlock.destroy() self._sectionlist.clear() self._listBox.delete(0, "end") section = config["MAIN"] idle = section.getint("idletime", 1) self._idleVariable.set(idle) backColor = self._getTuple(section.get("backgroundcolor", "(0, 0, 0)")) foreColor = self._getTuple(section.get("foregroundcolor", "(255, 255, 255)")) self._colorFrame.load(backColor, foreColor) selection = section.get("BlockList", "") selection = [item.strip(" '") for item in selection.split(",") if item.strip() in modulelist] self._frame.load(selection, modulelist) section = config["TIMELINE"] csvValue = section.get("sections") if csvValue: sectionSchemas = [item.strip(" '") for item in csvValue.split(",") if item.strip()] sectionSchemas = [str(item) for item in sectionSchemas if config.has_section(item)] for item in sectionSchemas: section = config[item] sectionBlock = MainSetting(self._sectionFrame, item) sectionBlock.load(config, item) self._sectionlist[item] = sectionBlock self._listBox.insert("end", item)
def load_environment_file(envfile, key_length=64): config = None lock = FileLock(os.path.abspath(envfile) + ".lock") with lock: if not os.path.exists(envfile): # Create empty file if it doesn't exists old_umask = os.umask(0o177) # Use '0600' file permissions config = ConfigParser() config.add_section('django') config['django']['secret_key'] = get_random_string(key_length, VALID_KEY_CHARS) with open(envfile, 'w') as configfile: config.write(configfile) os.umask(old_umask) if (os.stat(envfile).st_mode & 0o777) != 0o600: raise FilePermissionError("Insecure environment file permissions for %s!" % envfile) if not config: config = ConfigParser() config.read_file(open(envfile)) if not config.has_section('django'): raise ImproperlyConfigured('Missing `django` section in the environment file.') if not config.get('django', 'secret_key', fallback=None): raise ImproperlyConfigured('Missing `secret_key` in django section in the environment file.') # Register all keys as environment variables for key, value in config.items('django'): envname = 'DJANGO_%s' % key.upper() # Prefix to avoid collisions with existing env variables if envname not in os.environ: # Don't replace existing defined variables os.environ[envname] = value
def main(): config_path = sys.argv[1] set_to = sys.argv[2] if len(sys.argv) > 2 else None config = ConfigParser() config.read(env.config_file_path) try: section, option = config_path.split('.') except ValueError: # Cannot expand the path to exactly two items raise ConfigArgumentError( 'Config name needs to consist a two-part string, seperated by a ' 'dot (.).' ) if set_to is None: # Get config try: print(config.get(section, option)) except (NoSectionError, NoOptionError): print() else: # Set config if set_to: if not config.has_section(section): config.add_section(section) config.set(section, option, set_to) else: try: config.remove_option(section, option) except (NoSectionError, NoOptionError): pass if not config.items(section): config.remove_section(section) with open(env.config_file_path, 'w') as f: config.write(f)
def getConfig(self, section="DEFAULT", option=None, getType=None): """Return the product's ConfigParser, which will be empty if the file doesn't exist""" cfgFile = os.path.join(self.dir, "ups", "%s.cfg" % self.name) config = ConfigParser({"filename" : cfgFile}) config.read(cfgFile) # cfgFile need not exist # # Add default values # for s, o, val in [("distrib", "binary", True),]: if not config.has_option(s, o): if not config.has_section(s): config.add_section(s) config.set(s, o, str(val)) if option: try: if getType == bool: return config.getboolean(section, option) elif getType == float: return config.getfloat(section, option) elif getType == int: return config.getint(section, option) else: return config.get(section, option) except Exception as e: raise RuntimeError("Processing %s: %s" % (cfgFile, e)) return config
def save(self, data): cp = ConfigParser() cp.add_section("auth") for k, v in data.items(): cp.set("auth", k, v) with open(self.filename, "wb") as f: cp.write(f)
def generate_config(self, servers): parser = ConfigParser() config_dir = self.__config.get_config_dir() parser.add_section('unix_http_server') parser.set('unix_http_server', 'file', config_dir + '/supervisor.sock') parser.set('unix_http_server', 'chmod', '0700') parser.add_section('supervisord') parser.set('supervisord', 'logfile', config_dir + '/supervisor_error.log') parser.set('supervisord', 'pidfile', config_dir + '/supervisor.pid') parser.add_section('rpcinterface:supervisor') parser.set('rpcinterface:supervisor', 'supervisor.rpcinterface_factory', 'supervisor.rpcinterface:make_main_rpcinterface') parser.add_section('supervisorctl') parser.set('supervisorctl', 'serverurl', 'unix://' + config_dir + '/supervisor.sock') ql_executable = self.get_ql_executable() for sid,data in servers.items(): name = self.process_prefix + sid section = 'program:' + name parser.add_section(section) parser.set(section, 'command', self.build_command_line(sid, data, ql_executable)) parser.set(section, 'process_name', name) parser.set(section, 'autorestart', 'true') if os.path.isfile(self.__config_file) and not os.access(self.__config_file, os.W_OK): raise IOError('Cannot write to file ' + self.__config_file) with (open(self.__config_file, 'w+')) as config_fp: parser.write(config_fp)
class MyConfigParser: def __init__(self, filename): self.filename=filename self.config=ConfigParser() if path.exists(self.filename): self.config.read(self.filename) else: print("Configuration file {} doesn't exist".format(self.filename)) self.__generate_id() self.id=self.get("MyConfigParser","id")[:16] def cset(self, section, option, value): a=AESCipher(self.id.encode("utf8")); ci=a.encrypt(value.encode("utf8")) self.set(section,option,ci.decode("utf8")) def cget(self, section, option, default=None): a=AESCipher(self.id.encode("utf8")); value=self.get(section,option,default).encode("utf8") deci=a.decrypt(value) return deci.decode("utf8") def get(self, section, option, default=None): if self.config.has_option(section, option)==True: return self.config.get(section, option) else: self.set(section, option, default) return self.get(section, option) def getDecimal(self, section,option, default=None): try: value=self.get(section, option, default) return Decimal(value) except: debug("I couldn't convert to Decimal {} ({})".format(value, value.__class__)) def getFloat(self, section,option, default=None): try: value=self.get(section, option, default) return float(value) except: debug("I couldn't convert to float {} ({})".format(value, value.__class__)) def getInteger(self, section,option, default=None): try: value=self.get(section, option, default) return int(value) except: debug("I couldn't convert to int {} ({})".format(value, value.__class__)) def getBoolean(self, section,option, default=None): try: value=self.get(section, option, default) return str2bool(value) except: debug("I couldn't convert to boolean {} ({})".format(value, value.__class__)) ## Example: self.value_datetime_naive("Version", "197001010000", "%Y%m%d%H%M") def getDatetimeNaive(self, section, option, default=None, format="%Y%m%d%H%M"): try: value=self.get(section, option, default) return string2dtnaive(value, format) except: debug("I couldn't convert to datetime naive {} ({})".format(value, value.__class__)) def getList(self, section, option, default): try: value=self.get(section, option, default) return string2list_of_strings(value) except: debug("I couldn't convert to list of strings {} ({})".format(value, value.__class__)) def getListOfIntegers(self, section, option, default): try: value=self.get(section, option, default) return string2list_of_integers(value) except: debug("I couldn't convert to list of integers {} ({})".format(value, value.__class__)) def set(self, section, option, value): if isinstance(value, list): value=list2string(value) if section not in self.config: self.config.add_section(section) self.config[section]={} self.config.set(section, option, str(value)) def save(self): dirname=path.dirname(self.filename) if dirname != "": makedirs(dirname, exist_ok=True) with open(self.filename, 'w') as f: self.config.write(f) ## Generate a [MyConfigParser] -> id if it's not created yet def __generate_id(self): if self.config.has_option("MyConfigParser","id") is False: h = SHA256.new() h.update(str(datetime.now()).encode("utf8")) self.set("MyConfigParser","id", h.hexdigest())
class IniFile: """ Use with care: - addParam and setParam are 'auto-write' - addSection isn't - removeSection isn't - removeParam isn't """ __configParser = None # ConfigParser __inifilepath = None # string __file = None # File-like object __removeWhenDereferenced = False # bool def __init__(self, iniFile, create=False, removeWhenDereferenced=False): """ Initialize IniFile. If the file already exists, read it and parse the structure. If the file did not yet exist. Don't do anything yet. @param iniFile: The file to write to. This can be either a string representing a file-path or a file-like object @type iniFile: string or file-like object @param create: Whether or not to create a new file (Ignored if iniFile is a file-like object) @type create: bool @param removeWhenDereferenced: Whether or not to remove the file when this object is dereferenced @type removeWhenDereferenced: bool """ self.logger = j.logger.get("j.tools.inifile") self.__configParser = ConfigParser() self.__removeWhenDereferenced = removeWhenDereferenced if isinstance(iniFile, str): # iniFile is a filepath self.__inifilepath = iniFile if create: j.sal.fs.createDir(j.sal.fs.getDirName(iniFile)) self.logger.info("Create config file: " + iniFile) j.sal.fs.writeFile(iniFile, '') if not j.sal.fs.isFile(iniFile): raise j.exceptions.RuntimeError( "Inifile could not be found on location %s" % iniFile) else: # iniFile is a file-like object self.__file = iniFile self.__readFile() def __str__(self): """Returns string representation of the IniFile""" return '<IniFile> filepath: %s ' % self.__inifilepath __repr__ = __str__ def __del__(self): if self.__inifilepath and self.__removeWhenDereferenced: j.sal.fs.remove(self.__inifilepath) def __readFile(self): # content=j.sal.fs.fileGetContents(self.__inifilepath) fp = None try: if self.__inifilepath: fp = open(self.__inifilepath, "r") else: fp = self.__file return self.__configParser.readfp(fp) except Exception as err: print(err) if fp and not fp.closed: fp.close() raise j.exceptions.RuntimeError( "Failed to read the inifile \nERROR: %s" % (str(err))) def getSections(self): """ Return list of sections from this IniFile""" try: return self.__configParser.sections() except Exception as err: raise LookupError("Failed to get sections \nERROR: %s" % str(err)) def getParams(self, sectionName): """ Return list of params in a certain section of this IniFile @param sectionName: Name of the section for which you wish the param""" if not self.checkSection(sectionName): return try: return self.__configParser.options(sectionName) except Exception as err: raise LookupError( "Failed to get parameters under the specified section: %s \nERROR: %s" % (sectionName, str(err))) def checkSection(self, sectionName): """ Boolean indicating whether section exists in this IniFile @param sectionName: name of the section""" try: return self.__configParser.has_section(sectionName) except Exception as err: raise ValueError( 'Failed to check if the specified section: %s exists \nERROR: %s' % (sectionName, str(err))) def checkParam(self, sectionName, paramName): """Boolean indicating whether parameter exists under this section in the IniFile @param sectionName: name of the section where the param should be located @param paramName: name of the parameter you wish to check""" try: return self.__configParser.has_option(sectionName, paramName) except Exception as e: raise ValueError( 'Failed to check if the parameter: %s under section: %s exists \nERROR: %s' % (paramName, sectionName, str(e))) def getValue(self, sectionName, paramName, raw=False, default=None): """ Get value of the parameter from this IniFile @param sectionName: name of the section @param paramName: name of the parameter @param raw: boolean specifying whether you wish the value to be returned raw @param default: if given and the value does not exist the default value will be given @return: The value""" if default is not None and not self.checkParam(sectionName, paramName): return default try: result = self.__configParser.get(sectionName, paramName, raw=raw) self.logger.info( "Inifile: get %s:%s from %s, result:%s" % (sectionName, paramName, self.__inifilepath, result)) return result except Exception as err: raise LookupError( 'Failed to get value of the parameter: %s under section: %s in file %s.\nERROR: %s' % (paramName, sectionName, self.__inifilepath, str(err))) def getBooleanValue(self, sectionName, paramName): """Get boolean value of the specified parameter @param sectionName: name of the section @param paramName: name of the parameter""" try: result = self.__configParser.getboolean(sectionName, paramName) self.logger.info( "Inifile: get boolean %s:%s from %s, result:%s" % (sectionName, paramName, self.__inifilepath, result)) return result except Exception as e: raise LookupError( 'Inifile: Failed to get boolean value of parameter:%s under section:%s \nERROR: %s' % (paramName, sectionName, e)) def getIntValue(self, sectionName, paramName): """Get an integer value of the specified parameter @param sectionName: name of the section @param paramName: name of the parameter""" try: result = self.__configParser.getint(sectionName, paramName) self.logger.info( "Inifile: get integer %s:%s from %s, result:%s" % (sectionName, paramName, self.__inifilepath, result)) return result except Exception as e: raise LookupError( 'Failed to get integer value of parameter: %s under section: %s\nERROR: %s' % (paramName, sectionName, e)) def getFloatValue(self, sectionName, paramName): """Get float value of the specified parameter @param sectionName: name of the section @param paramName: name of the parameter""" try: result = self.__configParser.getfloat(sectionName, paramName) self.logger.info( "Inifile: get integer %s:%s from %s, result:%s" % (sectionName, paramName, self.__inifilepath, result)) return result except Exception as e: raise LookupError( 'Failed to get float value of parameter:%s under section:%s \nERROR: %' % (paramName, sectionName, e)) def addSection(self, sectionName): """ Add a new section to this Inifile. If it already existed, silently pass @param sectionName: name of the section""" try: if (self.checkSection(sectionName)): return self.logger.info("Inifile: add section %s to %s" % (sectionName, self.__inifilepath)) self.__configParser.add_section(sectionName) if self.checkSection(sectionName): return True except Exception as err: raise j.exceptions.RuntimeError( 'Failed to add section with sectionName: %s \nERROR: %s' % (sectionName, str(err))) def addParam(self, sectionName, paramName, newvalue): """ Add name-value pair to section of IniFile @param sectionName: name of the section @param paramName: name of the parameter @param newValue: value you wish to assign to the parameter""" try: if str(newvalue) == "none": newvalue == "*NONE*" self.__configParser.set(sectionName, paramName, str(newvalue)) self.logger.info( "Inifile: set %s:%s=%s on %s" % (sectionName, paramName, str(newvalue), self.__inifilepath)) # if self.checkParam(sectionName, paramName): # return True self.write() return False except Exception as err: raise j.exceptions.RuntimeError( 'Failed to add parameter with sectionName: %s, parameterName: %s, value: %s \nERROR: %s' % (sectionName, paramName, newvalue, str(err))) def setParam(self, sectionName, paramName, newvalue): """ Add name-value pair to section of IniFile @param sectionName: name of the section @param paramName: name of the parameter @param newValue: value you wish to assign to the parameter""" self.addParam(sectionName, paramName, newvalue) def removeSection(self, sectionName): """ Remove a section from this IniFile @param sectionName: name of the section""" if not self.checkSection(sectionName): return False try: self.__configParser.remove_section(sectionName) self.logger.info("inifile: remove section %s on %s" % (sectionName, self.__inifilepath)) if self.checkSection(sectionName): return False return True except Exception as err: raise j.exceptions.RuntimeError( 'Failed to remove section %s with \nERROR: %s' % (sectionName, str(err))) def removeParam(self, sectionName, paramName): """ Remove a param from this IniFile @param sectionName: name of the section @param paramName: name of the parameter""" if not self.checkParam(sectionName, paramName): return False try: self.__configParser.remove_option(sectionName, paramName) self.logger.info("Inifile:remove %s:%s from %s" % (sectionName, paramName, self.__inifilepath)) return True except Exception as err: raise j.exceptions.RuntimeError( 'Failed to remove parameter: %s under section: %s \nERROR: %s' % (paramName, sectionName, str(err))) def write(self, filePath=None): """ Write the IniFile content to disk This completely overwrites the file @param filePath: location where the file will be written """ closeFileHandler = True fp = None self.logger.info("Inifile: Write configfile %s to disk" % (self.__inifilepath)) if not filePath: if self.__inifilepath: # Use the inifilepath that was set in the constructor filePath = self.__inifilepath elif self.__file: # write to the file-like object that was set in the constructor closeFileHandler = False # We don't want to close this object fp = self.__file fp.seek(0) fp.truncate( ) # Clear the file-like object before writing to it else: # Nothing to write to raise Exception("No filepath to write to") try: if not fp: j.sal.fs.lock(filePath) fp = open(filePath, 'w') # Completely overwrite the file. self.__configParser.write(fp) fp.flush() if closeFileHandler: fp.close() j.sal.fs.unlock_(filePath) except Exception as err: if fp and closeFileHandler and not fp.closed: fp.close() j.sal.fs.unlock_(filePath) raise j.exceptions.RuntimeError( "Failed to update the inifile at '%s'\nERROR: %s\n" % (filePath, str(err))) def getContent(self): """ Get the Inifile content to a string """ # TODO: jumpscale primitives should be used (no fp...) fp = None if self.__file and not self.__file.closed: fp = self.__file fp.seek(0) fp.truncate() else: try: from io import StringIO except ImportError: from io import StringIO fp = StringIO() self.__configParser.write(fp) fp.seek(0) return fp.read() def getSectionAsDict(self, section): retval = {} for key in self.getParams(section): retval[key] = self.getValue(section, key) return retval def getFileAsDict(self): retval = {} for section in self.getSections(): retval[section] = self.getSectionAsDict(section) return retval
class AppGUI: # languages languageChoices = ["English", "Polski"] #,"Русский" languageCodes = ["EN", "PL"] openedFileNames = [] #actually opened filename paddingNSWE = 20 # default padding on all sides configSettings = { 'configIniFile': 'appconfig.ini', 'defaultSection': 'default', 'langDefault': 'EN', # default language is English 'useMiddleEndianDefault': True, # default is True } # fills variables with proper texts def LoadTexts(self): # button texts lang = self.GetIniParam('lang') # get language if (lang == 'PL'): self.text_openButton = "Otwórz plik" self.text_addButton = "Dodaj plik do istniejącego trendu" #settings texts self.text_settings = "Ustawienia:" self.text_swapWords32bitVars = "Użyj Middle-endian dla zmiennych 32-bitowych" self.text_language = "Język:" self.text_alreadyOpened = "Ten plik został już otwarty!" self.text_alert = "Ostrzeżenie" else: # button texts self.text_openButton = "Open file" self.text_addButton = "Add file to existing trend" #settings texts self.text_settings = "Settings:" self.text_swapWords32bitVars = "Use Middle-endian for 32-bit variables" self.text_language = "Language:" self.text_alreadyOpened = "This file has already been opened!" self.text_alert = "Warning" # vymeni texty def ChangeTexts(self): self.openButton['text'] = self.text_openButton self.addButton['text'] = self.text_addButton self.labelSettings['text'] = self.text_settings self.useMiddleEndianCheckBox['text'] = self.text_swapWords32bitVars self.langLabel['text'] = self.text_language self.copyrightInfoLabel['text'] = vs.appCopyrightInfo # constructor, draws the window def __init__(self, master=None): # creating basic window self.window = tk.Tk() #self.window.geometry("312x324") # size of the window width:- 500, height:- 375 self.window.resizable(0, 0) # this prevents from resizing the window self.window.title(vs.appTitle) #icon file iconFile = "images/vonsch.ico" if (os.path.exists(iconFile)): self.window.iconbitmap(iconFile) # ikona else: # icon for PyInstaller package fullIconPath = os.path.join(sys._MEIPASS, iconFile) if (os.path.exists(fullIconPath)): self.window.iconbitmap(fullIconPath) self.window.protocol("WM_DELETE_WINDOW", self.on_closing) # create the application print("Starting GUI...") # load configuration self.LoadIni() # load texts self.LoadTexts() # loads dictionary of tag names tag_names.LoadTagNames(self.GetIniParam('lang')) # buttony self.openButton = tk.Button(self.window, text=self.text_openButton, command=self.openButton_click, fg="black") self.openButton.grid( row=0, column=0, padx=self.paddingNSWE, pady=self.paddingNSWE, sticky=tk.E + tk.N) # 'fg - foreground' is used to color the contents self.addButton = tk.Button(self.window, text=self.text_addButton, command=self.addButton_click, fg="red", state=tk.DISABLED) self.addButton.grid( row=0, column=1, padx=self.paddingNSWE, pady=self.paddingNSWE, sticky=tk.W + tk.N) # 'text' is used to write the text on the Button ## settings self.labelSettings = tk.Label(self.window, text=self.text_settings) self.labelSettings.grid(row=2, column=0, columnspan=2, padx=self.paddingNSWE, pady=self.paddingNSWE, sticky=tk.W + tk.N) # use Middle-Endian (Weintek block read) self.useMiddleEndianCheckBox = tk.Checkbutton( self.window, text=self.text_swapWords32bitVars, variable=self.useMiddleEndianIntVar) # default je ON self.useMiddleEndianCheckBox.grid(row=4, column=0, columnspan=2) # language label self.langLabel = tk.Label(self.window, text=self.text_language) self.langLabel.grid( row=3, column=0 ) # , padx=self.paddingNSWE, pady=self.paddingNSWE, sticky=tk.E+tk.N # language combo box lang = self.GetIniParam('lang') langIndex = self.languageCodes.index(lang) #nacitanie indexu self.languageComboBox = ttk.Combobox(self.window, values=self.languageChoices, width=20) self.languageComboBox.grid(row=3, column=1, padx=self.paddingNSWE, pady=self.paddingNSWE, sticky=tk.E + tk.N) self.languageComboBox.current(langIndex) self.languageComboBox.bind('<<ComboboxSelected>>', self.languageComboBox_change) # copyright info self.copyrightInfoLabel = tk.Label(self.window, text=vs.appCopyrightInfo) self.copyrightInfoLabel.grid(row=5, column=0, columnspan=2, pady=self.paddingNSWE, sticky=tk.S) def EnableOrDisableAddFile(self): if ('.dtl' in self.openedFileNames[0]): self.addButton.configure(state=tk.NORMAL) else: self.addButton.configure(state=tk.DISABLED) # kliknutie na openButton def openButton_click(self): common_vars.ClearVars() # erases all data before opening new file self.openedFileNames.clear() # clear list of opened files self.UpdateUseMiddleEndianVars( ) # updates the middle endian variable, no bind function openFile = self.OpenFileDialogGetFileName() self.openedFileNames.append( openFile) # add file to list of opened files self.EnableOrDisableAddFile( ) # can another file be appended?, digable/enable button ppt.OpenTrendFile(openFile) #opens file # kliknutie na addButton def addButton_click(self): addFile = self.AddFileDialogGetFileName() if addFile in self.openedFileNames: tk.messagebox.showinfo(self.text_alert, self.text_alreadyOpened ) # warning that it has been already opened else: # OK self.openedFileNames.append(addFile) # add file ppt.AddTrendFile(addFile) #opens file and draws everything # zmena jazyka def languageComboBox_change(self, event=None): #tk.messagebox.showinfo("Info","Zmenil si jazyk!") langIndex = self.languageComboBox.current() lang = self.languageCodes[langIndex] self.configObj.set(self.configSettings['defaultSection'], 'lang', lang) # load and change the texts self.LoadTexts() self.ChangeTexts() # loads dictionary of tag names tag_names.LoadTagNames(lang) # debug info to console if event: # <-- this works only with bind because `command=` doesn't send event print("Language changed to:", event.widget.get()) # otvori dialog a vrati nazov suboru def OpenFileDialogGetFileName(self): file_path = filedialog.askopenfilename( filetypes=(("Supported HMI panel logs", ".txt .dtl"), ("All files", "*.*"))) # samotny dialog na vyber okna return file_path # doplnenie suboru DTL def AddFileDialogGetFileName(self): baseFileName = os.path.basename( self.openedFileNames[0]) #first file opened is taken file_path = filedialog.askopenfilename( filetypes=(("Weintek DTL file for same day", baseFileName), ("All files", "*.*"))) # samotny dialog na vyber okna return file_path # beh programu def run(self): self.window.mainloop() # Load ini file def LoadIni(self): self.configObj = ConfigParser() self.configObj.read(self.configSettings['configIniFile']) section = self.configSettings['defaultSection'] # when ini file is empty or sections do not exist if section not in self.configObj: self.configObj.add_section(section) self.configObj.set(section, 'lang', self.configSettings['langDefault']) self.configObj.set( section, 'useMiddleEndian', str(self.configSettings['useMiddleEndianDefault'])) useMiddleEndianLocal = True # default ON else: # ini file looks OK useMiddleEndianLocal = (self.configObj.getboolean( section, 'useMiddleEndian')) # naplnime useMiddleEndian, pouziva sa neskor pri inicializacii checkbuttonu self.useMiddleEndianIntVar = tk.IntVar(value=int(useMiddleEndianLocal)) self.UpdateUseMiddleEndianVars() # update global variable def UpdateUseMiddleEndianVars(self): common_vars.SetMiddleEndianUsage(bool( self.useMiddleEndianIntVar.get())) # setting the global variable # save ini file def SaveIni(self): # deal with useMiddleEndian, different formats section = self.configSettings['defaultSection'] useMiddleEndianBool = bool(self.useMiddleEndianIntVar.get()) self.configObj.set( section, 'useMiddleEndian', str(useMiddleEndianBool) ) # double type conversion, wants boolean in form of string # write all configuration to file with open(self.configSettings['configIniFile'], 'w') as output_file: self.configObj.write(output_file) # reads ini parameter def GetIniParam(self, paramName, section=None): if (section == None): section = self.configSettings['defaultSection'] return self.configObj[section][paramName] # when closing the window def on_closing(self): # save ini self.SaveIni() print('') print('Closing application...') self.window.destroy()
def createThemeFromTemplate(title, description, baseOn='template'): """Create a new theme from the given title and description based on another theme resource directory """ source = queryResourceDirectory(THEME_RESOURCE_NAME, baseOn) if source is None: raise KeyError("Theme {0:s} not found".format(baseOn)) themeName = getUtility(IURLNormalizer).normalize(title) if six.PY2 and isinstance(themeName, six.text_type): themeName = themeName.encode('utf-8') resources = getOrCreatePersistentResourceDirectory() resources.makeDirectory(themeName) target = resources[themeName] cloneResourceDirectory(source, target) manifest = SafeConfigParser() if MANIFEST_FILENAME in target: if six.PY2: fp = target.openFile(MANIFEST_FILENAME) try: manifest.readfp(fp) finally: fp.close() else: # configparser can only read/write text # but in py3 plone.resource objects are BytesIO objects. fp = target.openFile(MANIFEST_FILENAME) try: data = fp.read() finally: fp.close() manifest.read_string(safe_unicode(data)) if not manifest.has_section('theme'): manifest.add_section('theme') if six.PY2 and isinstance(title, six.text_type): title = title.encode('utf-8') if six.PY2 and isinstance(description, six.text_type): description = description.encode('utf-8') manifest.set('theme', 'title', title) manifest.set('theme', 'description', description) if manifest.has_option('theme', 'prefix'): prefix = u"/++%s++%s" % (THEME_RESOURCE_NAME, themeName) manifest.set('theme', 'prefix', prefix) if manifest.has_option('theme', 'rules'): rule = manifest.get('theme', 'rules') rule_file_name = rule.split('/')[-1] # extract real rules file name rules = u"/++%s++%s/%s" % (THEME_RESOURCE_NAME, themeName, rule_file_name) manifest.set('theme', 'rules', rules) paths_to_fix = [ 'development-css', 'production-css', 'tinymce-content-css', 'development-js', 'production-js' ] for var_path in paths_to_fix: if not manifest.has_option('theme', var_path): continue val = manifest.get('theme', var_path) if not val: continue template_prefix = '++%s++%s/' % (THEME_RESOURCE_NAME, baseOn) if template_prefix in val: # okay, fix val = val.replace(template_prefix, '++%s++%s/' % (THEME_RESOURCE_NAME, themeName)) manifest.set('theme', var_path, val) if six.PY2: manifestContents = six.StringIO() manifest.write(manifestContents) else: # in py3 plone.resource is BytesIO objects # but configparser can only deal with text (StringIO). # So we need to do this stupid dance to write manifest.cfg tempfile = six.StringIO() manifest.write(tempfile) tempfile.seek(0) data = tempfile.read() tempfile.close() manifestContents = six.BytesIO(data.encode('utf8')) target.writeFile(MANIFEST_FILENAME, manifestContents) return themeName
import os from configparser import ConfigParser from sys import platform import pyttsx3 config = ConfigParser() config.add_section('system') config.add_section('linkedin') config.add_section('profiles_data') config.add_section('profiles_data_by_name') print("Welcome to the configuration process.") try: print("Performing some system checks...") engine = pyttsx3.init() engine.say(' ') engine.runAndWait() config.set('system', 'speak', 'Y') except OSError: config.set('system', 'speak', 'N') if platform.lower() == "linux": driver = 'Linux/chromedriver' config.set('system', 'os', 'linux') elif platform.lower() == "darwin": driver = 'MacOS/chromedriver' config.set('system', 'os', 'macos') elif platform.lower() == "windows":
class Censys(): def __init__(self, conf_path=paths.POCSUITE_RC_PATH, uid='', secret=''): self.uid = uid self.secret = secret self.conf_path = conf_path self.credits = 0 if self.conf_path: self.parser = ConfigParser() self.parser.read(self.conf_path) if not (self.secret or self.uid): try: self.secret = self.parser.get("Censys", "secret") self.uid = self.parser.get("Censys", "uid") except Exception: pass def token_is_available(self): if self.secret and self.uid: try: resp = requests.get("https://censys.io/api/v1/account", auth=(self.uid, self.secret)) if resp.status_code == 200 and "email" in resp.json(): logger.info( "[PLUGIN] Censys login success email:{}".format( resp.json()["email"])) self.credits = resp.json( )["quota"]["allowance"] - resp.json()["quota"]["used"] return True except Exception as ex: logger.error(ex) return False def check_token(self): if self.token_is_available(): return True else: new_UID = input("Censys API UID:") new_secret = input("Censys API SECRET") self.uid = new_UID self.secret = new_secret if self.token_is_available(): self.write_conf() return True else: logger.error("The shodan api token is incorrect. " "Please enter the correct api token.") self.check_token() def write_conf(self): if not self.parser.has_section("Censys"): self.parser.add_section("Censys") try: self.parser.set("Censys", "secret", self.secret) self.parser.set("Censys", "uid", self.uid) self.parser.write(open(self.conf_path, "w")) except Exception as ex: logger.error(str(ex)) def get_resource_info(self): if self.check_token(): return True return False def search(self, dork, pages=1, resource='ipv4'): search_result = set() try: for page in range(1, pages + 1): url = "https://censys.io/api/v1/search/{}".format(resource) data = { "query": dork, # Search keywords, "fields": ["ip"], "page": page } resp = requests.post(url, data=json.dumps(data), auth=(self.uid, self.secret)) if resp and resp.status_code == 200 and "results" in resp.json( ): content = resp.json()["results"] for match in content: ans = match["ip"] search_result.add(ans) except Exception as ex: logger.error(str(ex)) return search_result
def test_configure(): if not tstconf: print( "Error: the directory for storing the test configuration file", "is not available. Exiting." ) return print( "Please select the test type to perform:\n" ) print( "1. BASIC: testing regd as a file socket based server.\n" ) print( "2. NETWORK: testing regd as an IP-based server.\n" ) print( "3. MULTIUSER: testing accessing a regd server running on another user account.\n" ) s = input( "Type the test number (or 'q' for exit) and press 'Enter':\n> " ) while s not in ['1', '2', '3', 'q']: s = input( "Please type a number from 1 to 3 or 'q' and press 'Enter':\n> " ) if s in 'Qq': print( "Exiting." ) return cp = ConfigParser() cp.read( tstconf ) if not cp.has_section( "general" ): cp.add_section( "general" ) cp.set( "general", "test_type", s ) def conf2(): rehost = "[a-zA-Z0-9\.-]+" while True: host = input( ( "\n\n\nPlease enter the host name to which the regd server should be bound" " ('.' for quit):\n> " ) ) if host == '.': return '.' if not re.match( rehost, host ): print( "This name contains invalid characters. Only numbers, letters, dots and", "hyphens are allowed." ) continue if len( host ) > 255: print( "This name is too long." ) continue while True: port = input( ( "\n\n\nPlease enter the port number on which the regd server should be listening" "\n('.' for quit, 'Enter' for default port number 48473):\n> " ) ) if port == '.': return '.' if not port: port = '48473' elif not re.match( "[0-9]+", port ): print( "Port number must only contain digits." ) continue elif int( port ) > 65536: print( "The number must be no greater than 65536." ) continue break ans = input( ( "\n\n\nregd will be tried to start on host: {0} port: {1}. Accept these settings?" "\n([Y]es, [n]o, [q]uit)> " ).format( host, port ) ) if ans in "Yy": return ( host, port ) if ans in "Qq": return '.' def conf3(): print( "\n\n\nIn the multiuser test the regd server runs on another user account", "and is contacted from this account." ) while True: name = input( ( "\n\n\nPlease enter the username of the user" " account where the test regd server will run ('.' for quit):\n> " ) ) if name == '.': return '.' try: pwd.getpwnam( name ).pw_uid except KeyError: print( "No such user account is found on this system." ) continue return name def getloglevel(): print( "\n\n\nPlease select the level of verbosity of information output during testing: \n" ) print( "1. NORMAL (recommended)" ) print( "2. VERBOSE (may help if some tests went wrong)" ) print( "3. DEBUG (for developers)\n" ) while True: loglevel = input( "Number (1, 2 or 3) or 'q' for quit: " ) if loglevel in "123qQ": break return loglevel def write_conf(): nonlocal cp try: fp = open( tstconf, "w" ) cp.write( fp, True ) except OSError as e: print( "Error: could not write the test configuration file: ", e.strerror ) return False return True if s == '1': prompt = "The test has been configured. You can now run 'regd test-start' to start it." elif s == '2': ret = conf2() if ret == '.': return cp.set( "general", "host", ret[0] ) cp.set( "general", "port", ret[1] ) prompt = "The test has been configured. You can now run 'regd test-start' to start it." elif s == '3': ret = conf2() if ret == '.': return cp.set( "general", "host", ret[0] ) cp.set( "general", "port", ret[1] ) ret = conf3() if ret == '.': return cp.set( "general", "multiuser", ret ) prompt = ( "The test has been configured. Now the 'regd test-multiuser-begin' command", "must be called from the '{0}' user account. After that, the 'regd test-start'", "command must be called from this account. After the tests are done," "'regd test-multiuser-end' should be called on '{0}' user account." ).format( ret ) ret = getloglevel() if ret == 'q': return cp.set( "general", "loglevel", ret ) if not write_conf(): prompt = "The test configuration failed." input( "\n\n\n" + prompt + "\n\nPress 'Enter' to quit." )
#-------------------------------------------------------------------------------- # --- MAIN body of the script. This is where the pieces come together #-------------------------------------------------------------------------------- if __name__ == '__main__': # =================================================================== # --- Logging info # =================================================================== if not os.path.exists(configfile): # In case there wasn't a config file located in the current directory # so it creates one. config.add_section('tenable.sc') config.set('tenable.sc', 'SC_host', input('tenable.sc IP Address | Hostname : ')) config.set('tenable.sc', 'sc_username', input('tenable.sc Username : '******'tenable.sc', 'sc_password', getpass.getpass('tenable.sc Password : '******'SrvNow') config.set('SrvNow', 'SrvNow_url', input('SrvNow Service API link : ')) config.set('SrvNow', 'SrvNow_username', input('SrvNow Username : '******'SrvNow', 'SrvNow_password', getpass.getpass('SrvNow Password : '))
class Settings: def __init__(self, config_file): self.conf = ConfigParser() self.config_section = "main" self.config_file = config_file # default configuration self.def_cmd_prefix = "$" self.def_all_phrases = 5 self.def_line_phrases = 5 self.def_capitalize = True self.def_allow_repeats = False self.def_user_mute = True self.def_bot_token = 0 self.def_user_ids = 373115287828037632 def initialize_file(self): if not self.conf.read(self.config_file): self.conf.add_section(self.config_section) self.conf.set(self.config_section, 'cmd_prefix', str(self.def_cmd_prefix)) self.conf.set(self.config_section, 'all_phrases', str(self.def_all_phrases)) self.conf.set(self.config_section, 'line_phrases', str(self.def_line_phrases)) self.conf.set(self.config_section, 'capitalize', str(self.def_capitalize)) self.conf.set(self.config_section, 'allow_repeats', str(self.def_allow_repeats)) self.conf.set(self.config_section, 'user_mute', str(self.def_user_mute)) self.conf.set(self.config_section, 'bot_token', str(self.def_bot_token)) self.conf.set(self.config_section, 'user_ids', str(self.def_user_ids)) with open(self.config_file, "w") as file: self.conf.write(file) def read_config(self): self.initialize_file() self.conf.read(self.config_file) cmd_prefix = self.conf[self.config_section]["cmd_prefix"] all_phrases = int(self.conf[self.config_section]["all_phrases"]) line_phrases = int(self.conf[self.config_section]["line_phrases"]) capitalize = strtobool(self.conf[self.config_section]["capitalize"]) allow_repeats = strtobool( self.conf[self.config_section]["allow_repeats"]) user_mute = strtobool(self.conf[self.config_section]["user_mute"]) bot_token = self.conf[self.config_section]["bot_token"] user_ids = self.conf[self.config_section]["user_ids"].split(",") return cmd_prefix, all_phrases, line_phrases, capitalize, allow_repeats, user_mute, bot_token, user_ids def write_config(self, *args): if len(args[0]) >= 1: self.conf.set(self.config_section, 'cmd_prefix', str(args[0][0])) if len(args[0]) >= 2: self.conf.set(self.config_section, 'all_phrases', str(args[0][1])) if len(args[0]) >= 3: self.conf.set(self.config_section, 'line_phrases', str(args[0][2])) if len(args[0]) >= 4: self.conf.set(self.config_section, 'capitalize', str(args[0][3])) if len(args[0]) >= 5: self.conf.set(self.config_section, 'allow_repeats', str(args[0][4])) if len(args[0]) >= 6: self.conf.set(self.config_section, 'user_mute', str(args[0][5])) if len(args[0]) == 7: self.conf.set(self.config_section, 'user_ids', str(''.join(args[0][6]))) with open(self.config_file, "w") as file: self.conf.write(file)
# Create directory suffix = "_" + str(int(time.time())) cov_dir = tempfile.mkdtemp(suffix, "m2_coverage_") # Create configuration file coveragerc = os.path.join(cov_dir, ".coveragerc") coverage = os.path.join(cov_dir, ".coverage") from configparser import ConfigParser from os.path import expanduser config = ConfigParser() config.read(['/etc/coveragerc', expanduser('~/.coveragerc')]) if not config.has_section('run'): config.add_section('run') config.set('run', 'data_file', coverage) config.write(open(coveragerc, 'w')) # Add arguments to tests command line testset.add_additional_args( ["-m", "coverage", "run", "--rcfile", coveragerc, "-a"]) # Inform the user d = { "blue": cosmetics.colors['blue'], "end": cosmetics.colors['end'], "cov_dir": cov_dir } print( "[%(blue)sCoverage%(end)s] Report will be written in %(cov_dir)s" %
def get_config_for_testing(temp_dir=None): config = ConfigParser() config.add_section('data_storage') config.set('data_storage', 'mongo_server', 'localhost') config.set('data_storage', 'main_database', 'tmp_unit_tests') config.set('data_storage', 'intercom_database_prefix', 'tmp_unit_tests') config.set('data_storage', 'statistic_database', 'tmp_unit_tests') config.set('data_storage', 'view_storage', 'tmp_tests_view') config.set('data_storage', 'mongo_port', '27018') config.set('data_storage', 'report_threshold', '2048') config.set('data_storage', 'password_salt', '1234') config.add_section('unpack') config.set('unpack', 'whitelist', '') config.set('unpack', 'max_depth', '10') config.add_section('default_plugins') config.add_section('ExpertSettings') config.set('ExpertSettings', 'block_delay', '0.1') config.set('ExpertSettings', 'ssdeep_ignore', '1') config.set('ExpertSettings', 'authentication', 'false') config.set('ExpertSettings', 'intercom_poll_delay', '0.5') config.set('ExpertSettings', 'nginx', 'false') config.add_section('database') config.set('database', 'results_per_page', '10') load_users_from_main_config(config) config.add_section('Logging') if temp_dir is not None: config.set('data_storage', 'firmware_file_storage_directory', temp_dir.name) config.set('Logging', 'mongoDbLogFile', os.path.join(temp_dir.name, 'mongo.log')) config.set('ExpertSettings', 'radare2_host', 'localhost') return config
def run_check(tmpdir, encoding, requirements=None, constraints=None, ignore=None, ignorefile=None, verbose=0, exclude=None, sourcecode=None, use_setupcfg=False): # Compose args runner_args = [] setup_cfg = {} setup_cfg_args = [] if requirements: for requirements_file in requirements: runner_args.extend(['--requirements', requirements_file]) setup_cfg['requirements'] = requirements if constraints: for constraints_file in constraints: runner_args.extend(['--constraints', constraints_file]) setup_cfg['constraints'] = constraints if ignore: for ignored_requirement in ignore: runner_args.extend(['--ignore', ignored_requirement]) setup_cfg['ignore'] = ignore if ignorefile: for ignorefile_path in ignorefile: runner_args.extend(['--ignorefile', ignorefile_path]) setup_cfg['ignorefile'] = ignorefile if verbose: runner_args.append('-' + verbose * 'v') setup_cfg_args.append('-' + verbose * 'v') if exclude: for excluded_path in exclude: runner_args.extend(['--exclude', excluded_path]) setup_cfg['exclude'] = exclude runner_args.append(sourcecode) setup_cfg['sourcecode'] = sourcecode # Write a config file if use_setupcfg: config = ConfigParser() config.add_section('important') for key, value in setup_cfg.items(): if key not in ('sourcecode',): value = '\n'.join(value) config.set('important', key, value) filepath = str(tmpdir.join('setup.cfg')) with codecs.open(filepath, 'w', encoding=encoding) as f: config.write(f) # Run cli runner = CliRunner() cwd = os.getcwd() try: # Change the cwd and reload the module to alter the defaults to come # from the new cwd os.chdir(str(tmpdir)) reload(important.__main__) if use_setupcfg: return runner.invoke(important.__main__.check, setup_cfg_args, catch_exceptions=False) else: return runner.invoke(important.__main__.check, runner_args, catch_exceptions=False) finally: os.chdir(cwd)
def Preferences(): """Function to define a first preferences file""" config = ConfigParser() # General config.add_section("General") config.set("General", "Color_Resaltado", "#ffff00") config.set("General", "Color_ReadOnly", "#eaeaea") config.set("General", "Recent_Files", "10") config.set("General", "Load_Last_Project", "True") config.set("General", "Tray", "False") # PFD config.add_section("PFD") config.set("PFD", "x", "800") config.set("PFD", "y", "600") config.set("PFD", "Color_Entrada", "#c80000") config.set("PFD", "Color_Salida", "#0000c8") config.set("PFD", "Color_Stream", "#000000") config.set("PFD", "Width", "1.0") config.set("PFD", "Union", "0") config.set("PFD", "Miter_limit", "2.0") config.set("PFD", "Punta", "0") config.set("PFD", "Guion", "0") config.set("PFD", "Dash_offset", "0.0") # Tooltip config.add_section("Tooltip") config.set("Tooltip", "Show", "True") config.set("Tooltip", "SI", "False") config.set("Tooltip", "CGS", "False") config.set("Tooltip", "AltSI", "False") config.set("Tooltip", "English", "False") config.set("Tooltip", "Metric", "False") for i, magnitud in enumerate(magnitudes[:-1]): config.set("Tooltip", magnitud, "[0,1]") # TooltipEntity config.add_section("TooltipEntity") config.set("TooltipEntity", "Corriente", "[0,1]") for equipo in equipos: config.set("TooltipEntity", equipo, "[0,1]") # NumericFactor config.add_section("NumericFormat") for magnitud in magnitudes: kwarg = {'total': 0, 'signo': False, 'decimales': 4, 'format': 0} config.set("NumericFormat", magnitud, str(kwarg)) # Petro config.add_section("petro") config.set("petro", "M", "0") config.set("petro", "critical", "0") config.set("petro", "vc", "0") config.set("petro", "f_acent", "0") config.set("petro", "Tb", "0") config.set("petro", "SG", "0") config.set("petro", "n", "0") config.set("petro", "Zc", "0") config.set("petro", "PNA", "0") config.set("petro", "H", "0") config.set("petro", "curve", "0") # Applications config.add_section("Applications") config.set("Applications", "Calculator", calculator) config.set("Applications", "TextViewer", editor) config.set("Applications", "Shell", shell) config.set("Applications", "ipython", "False") config.set("Applications", "maximized", "False") config.set("Applications", "foregroundColor", "#ffffff") config.set("Applications", "backgroundColor", "#000000") config.set("Applications", "elementalColorby", "serie") config.set("Applications", "elementalDefinition", "10") config.set("Applications", "elementalLog", "False") # mEoS config.add_section("MEOS") config.set("MEOS", "coolprop", "False") config.set("MEOS", "refprop", "False") config.set("MEOS", "saturation"+"Color", "#000000") config.set("MEOS", "saturation"+"alpha", "255") config.set("MEOS", "saturation"+"lineWidth", "1.0") config.set("MEOS", "saturation"+"lineStyle", "-") config.set("MEOS", "saturation"+"marker", "None") config.set("MEOS", "saturation"+"markersize", "3") config.set("MEOS", "saturation"+"markerfacecolor", "#ff0000") config.set("MEOS", "saturation"+"markeredgewidth", "1") config.set("MEOS", "saturation"+"markeredgecolor", "#000000") config.set("MEOS", "grid", "False") config.set("MEOS", "definition", "1") lineas = ["Isotherm", "Isobar", "Isoenthalpic", "Isoentropic", "Isochor", "Isoquality"] for linea in lineas: config.set("MEOS", linea+"Start", "0") config.set("MEOS", linea+"End", "0") config.set("MEOS", linea+"Step", "0") config.set("MEOS", linea+"Custom", "True") config.set("MEOS", linea+"List", "") if linea != "Isoquality": config.set("MEOS", linea+"Critic", "True") config.set("MEOS", linea+"Color", "#000000") config.set("MEOS", linea+"alpha", "255") config.set("MEOS", linea+"lineWidth", "0.5") config.set("MEOS", linea+"lineStyle", "-") config.set("MEOS", linea+"marker", "None") config.set("MEOS", linea+"markersize", "3") config.set("MEOS", linea+"markerfacecolor", "#ff0000") config.set("MEOS", linea+"markeredgewidth", "1") config.set("MEOS", linea+"markeredgecolor", "#000000") config.set("MEOS", linea+"Label", "False") config.set("MEOS", linea+"Variable", "False") config.set("MEOS", linea+"Units", "False") config.set("MEOS", linea+"Position", "50") # Psychr config.add_section("Psychr") config.set("Psychr", "chart", "True") config.set("Psychr", "virial", "False") config.set("Psychr", "coolprop", "False") config.set("Psychr", "refprop", "False") config.set("Psychr", "saturation"+"Color", "#000000") config.set("Psychr", "saturation"+"alpha", "255") config.set("Psychr", "saturation"+"lineWidth", "0.5") config.set("Psychr", "saturation"+"lineStyle", "-") config.set("Psychr", "saturation"+"marker", "None") config.set("Psychr", "saturation"+"markersize", "3") config.set("Psychr", "saturation"+"markerfacecolor", "#ff0000") config.set("Psychr", "saturation"+"markeredgewidth", "1") config.set("Psychr", "saturation"+"markeredgecolor", "#000000") config.set("Psychr", "crux"+"Color", "#0000ff") config.set("Psychr", "crux"+"alpha", "255") config.set("Psychr", "crux"+"lineWidth", "0.5") config.set("Psychr", "crux"+"lineStyle", "-") config.set("Psychr", "crux"+"marker", "None") config.set("Psychr", "crux"+"markersize", "3") config.set("Psychr", "crux"+"markerfacecolor", "#ff0000") config.set("Psychr", "crux"+"markeredgewidth", "1") config.set("Psychr", "crux"+"markeredgecolor", "#000000") lineas = ["IsoTdb", "IsoW", "IsoHR", "IsoTwb", "Isochor"] values = [ {"start": 274.0, "end": 330.0, "step": 1.0, "color": "#000000", "linewidth": 0.5, "linestyle": ":", "label": "False", "units": "False", "position": 50}, {"start": 0.0, "end": 0.04, "step": 0.001, "color": "#000000", "linewidth": 0.5, "linestyle": ":", "label": "False", "units": "False", "position": 50}, {"start": 10.0, "end": 100.0, "step": 10.0, "color": "#000000", "linewidth": 0.5, "linestyle": "--", "label": "True", "units": "True", "position": 85}, {"start": 250.0, "end": 320.0, "step": 1.0, "color": "#aa0000", "linewidth": 0.8, "linestyle": ":", "label": "False", "units": "False", "position": 90}, {"start": 0.8, "end": 1.0, "step": 0.01, "color": "#00aa00", "linewidth": 0.8, "linestyle": ":", "label": "False", "units": "False", "position": 90}] for linea, value in zip(lineas, values): config.set("Psychr", linea+"Start", str(value["start"])) config.set("Psychr", linea+"End", str(value["end"])) config.set("Psychr", linea+"Step", str(value["step"])) config.set("Psychr", linea+"Custom", "False") config.set("Psychr", linea+"List", "") config.set("Psychr", linea+"Color", str(value["color"])) config.set("Psychr", linea+"alpha", "255") config.set("Psychr", linea+"lineWidth", str(value["linewidth"])) config.set("Psychr", linea+"lineStyle", str(value["linestyle"])) config.set("Psychr", linea+"marker", "None") config.set("Psychr", linea+"markersize", "3") config.set("Psychr", linea+"markerfacecolor", "#ff0000") config.set("Psychr", linea+"markeredgewidth", "1") config.set("Psychr", linea+"markeredgecolor", "#000000") config.set("Psychr", linea+"Label", str(value["label"])) config.set("Psychr", linea+"Units", str(value["units"])) config.set("Psychr", linea+"Position", str(value["position"])) config.set("Psychr", linea+"variable", str(False)) # Moody config.add_section("Moody") config.set("Moody", "fanning", "False") config.set("Moody", "method", "0") config.set("Moody", "ed", "[0, 1e-6, 5e-6, 1e-5, 2e-5, 5e-5, 1e-4, 2e-4, " "4e-4, 6e-4, 8e-4, .001, .0015, .002, .003, .004, .006, .008, " ".01, .0125, .015, .0175, .02, .025, .03, .035, .04, .045, " ".05, .06, .07]") config.set("Moody", "line"+"Color", "#000000") config.set("Moody", "line"+"alpha", "255") config.set("Moody", "line"+"lineWidth", "0.5") config.set("Moody", "line"+"lineStyle", "-") config.set("Moody", "line"+"marker", "None") config.set("Moody", "line"+"markersize", "3") config.set("Moody", "line"+"markerfacecolor", "#ff0000") config.set("Moody", "line"+"markeredgewidth", "1") config.set("Moody", "line"+"markeredgecolor", "#000000") config.set("Moody", "crux"+"Color", "#0000ff") config.set("Moody", "crux"+"alpha", "255") config.set("Moody", "crux"+"lineWidth", "0.5") config.set("Moody", "crux"+"lineStyle", "-") config.set("Moody", "crux"+"marker", "None") config.set("Moody", "crux"+"markersize", "3") config.set("Moody", "crux"+"markerfacecolor", "#ff0000") config.set("Moody", "crux"+"markeredgewidth", "1") config.set("Moody", "crux"+"markeredgecolor", "#000000") # Standing-Katz config.add_section("Standing_Katz") config.set("Standing_Katz", "method", "0") config.set("Standing_Katz", "Prmin", "0.8") config.set("Standing_Katz", "Prmax", "10") config.set("Standing_Katz", "Tr", "[1.05, 1.1, 1.15, 1.2, 1.25, 1.3, 1.35," "1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2., 2.2, 2.4, 2.6, 2.8, 3.]") config.set("Standing_Katz", "line"+"Color", "#000000") config.set("Standing_Katz", "line"+"alpha", "255") config.set("Standing_Katz", "line"+"lineWidth", "0.5") config.set("Standing_Katz", "line"+"lineStyle", "-") config.set("Standing_Katz", "line"+"marker", "None") config.set("Standing_Katz", "line"+"markersize", "3") config.set("Standing_Katz", "line"+"markerfacecolor", "#ff0000") config.set("Standing_Katz", "line"+"markeredgewidth", "1") config.set("Standing_Katz", "line"+"markeredgecolor", "#000000") config.set("Standing_Katz", "crux"+"Color", "#0000ff") config.set("Standing_Katz", "crux"+"alpha", "255") config.set("Standing_Katz", "crux"+"lineWidth", "0.5") config.set("Standing_Katz", "crux"+"lineStyle", "-") config.set("Standing_Katz", "crux"+"marker", "None") config.set("Standing_Katz", "crux"+"markersize", "3") config.set("Standing_Katz", "crux"+"markerfacecolor", "#ff0000") config.set("Standing_Katz", "crux"+"markeredgewidth", "1") config.set("Standing_Katz", "crux"+"markeredgecolor", "#000000") # Openbabel config.add_section("Openbabel") config.set("Openbabel", "BondColor", "#000000") config.set("Openbabel", "BackColor", "#ffffff") config.set("Openbabel", "BackColorAlpha", "0") config.set("Openbabel", "AtomsColor", "True") config.set("Openbabel", "AtomsAll", "False") config.set("Openbabel", "AtomsEnd", "True") config.set("Openbabel", "AtomsNone", "False") config.set("Openbabel", "TighBond", "True") return config
server-host = "*" """ if __name__ == "__main__": ini_file = ConfigParser() if exists(CONFIG_FILE): with open(CONFIG_FILE) as f: ini_file.read_file(f) conn_data = dict(host="localhost", port=5432) if ini_file.has_section("postgres"): conn_data.update(dict(ini_file.items("postgres"))) conn_data["sudo"] = ini_file.getboolean("postgres", "sudo", fallback=None) else: ini_file.add_section("postgres") argp = argparse.ArgumentParser("Create the base databases for SenseCraft") argp.add_argument("--host", default=conn_data["host"], help="the database host") argp.add_argument("--port", default=conn_data["port"], help="the database port") argp.add_argument( "-u", "--user", default=conn_data.get("user"), help="a postgres role that can create databases", ) argp.add_argument("-p", "--password",
from configparser import ConfigParser, DuplicateSectionError from sosia.establishing.constants import CONFIG_FILE from sosia.utils.defaults import CONFIG_DEFAULTS config = ConfigParser() config.optionxform = str try: config.read(CONFIG_FILE) except FileNotFoundError: # Create config for sec_key, d in CONFIG_DEFAULTS.items(): try: config.add_section(sec_key) except DuplicateSectionError: pass for key, val in d.items(): config.set(sec_key, key, val) try: with open(CONFIG_FILE, 'w') as f: config.write(f) except FileNotFoundError: # Fix for sphinx build pass
def config(): """Function to define a first project config file""" config = ConfigParser() # Components config.add_section("Components") config.set("Components", "Components", "[]") config.set("Components", "Solids", "[]") # Thermodynamics config.add_section("Thermo") config.set("Thermo", "K", "0") config.set("Thermo", "Alfa", "0") config.set("Thermo", "Mixing", "0") config.set("Thermo", "H", "0") config.set("Thermo", "Cp_ideal", "0") config.set("Thermo", "MEoS", "False") config.set("Thermo", "iapws", "False") config.set("Thermo", "GERG", "False") config.set("Thermo", "freesteam", "False") config.set("Thermo", "coolProp", "False") config.set("Thermo", "refprop", "False") # Transport config.add_section("Transport") config.set("Transport", "RhoL", "0") config.set("Transport", "MuL", "0") config.set("Transport", "MuG", "0") config.set("Transport", "Tension", "0") config.set("Transport", "ThCondL", "0") config.set("Transport", "ThCondG", "0") config.set("Transport", "Pv", "0") config.set("Transport", "f_acent", "0") config.set("Transport", "Corr_RhoL", "0") config.set("Transport", "Corr_MuL", "0") config.set("Transport", "Corr_MuG", "0") config.set("Transport", "Corr_ThCondL", "0") config.set("Transport", "Corr_ThCondG", "0") config.set("Transport", "RhoLMix", "0") config.set("Transport", "MuLMix", "0") config.set("Transport", "MuGMix", "0") config.set("Transport", "ThCondLMix", "0") config.set("Transport", "ThCondGMix", "0") config.set("Transport", "Corr_RhoLMix", "0") config.set("Transport", "Corr_MuGMix", "0") config.set("Transport", "Corr_ThCondGMix", "0") # Units config.add_section("Units") config.set("Units", "System", "0") for magnitud in magnitudes[:-1]: config.set("Units", magnitud, "0") # Resolution config.add_section("PFD") config.set("PFD", "x", "600") config.set("PFD", "y", "480") return config
import logging import os import socket from django.core.management.utils import get_random_secret_key from trosnoth import data log = logging.getLogger(__name__) # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) data.makeDirs(data.getPath(data.user, 'authserver')) CONFIG_PATH = data.getPath(data.user, 'authserver', 'config') config = ConfigParser(interpolation=None) config.add_section('security') config.add_section('web') config.read(CONFIG_PATH) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = config.get('security', 'key', fallback=None) if not SECRET_KEY: SECRET_KEY = get_random_secret_key().replace('%', '-') config.set('security', 'key', SECRET_KEY) with open(CONFIG_PATH, 'w') as f: config.write(f) # SECURITY WARNING: don't run with debug turned on in production!
class CEye(object): def __init__(self, conf_path=paths.POCSUITE_RC_PATH, username=None, password=None, token=None): self.headers = None self.token = token self.conf_path = conf_path self.username = username self.password = password if self.conf_path: self.parser = ConfigParser() self.parser.read(self.conf_path) if not self.token: try: self.token = self.parser.get("Telnet404", 'Jwt token') except Exception: pass if not self.check_account(): msg = "Ceye verify faild!" raise Exception(msg) def token_is_available(self): if self.token: headers = {'Authorization': 'JWT %s' % self.token} headers2 = {'Authorization': self.token} try: resp = requests.get('http://api.ceye.io/v1/identify', headers=headers) if resp and resp.status_code == 200 and "data" in resp.json(): self.headers = headers return True resp = requests.get('http://api.ceye.io/v1/identify', headers=headers2) if resp and resp.status_code == 200 and "data" in resp.json(): self.headers = headers2 return True except Exception as ex: logger.error(str(ex)) return False def new_token(self): data = '{{"username": "******", "password": "******"}}'.format( self.username, self.password) try: resp = requests.post( 'https://api.zoomeye.org/user/login', data=data, ) if resp.status_code != 401 and "access_token" in resp.json(): content = resp.json() self.token = content['access_token'] self.headers = {'Authorization': 'JWT %s' % self.token} return True except Exception as ex: logger.error(str(ex)) return False def check_account(self): if self.token_is_available(): return True else: if self.username and self.password: if self.new_token(): self.write_conf() return True else: username = input("Telnet404 email account:") password = getpass.getpass("Telnet404 password:"******"The username or password is incorrect. " "Please enter the correct username and password.") return False def write_conf(self): if not self.parser.has_section("Telnet404"): self.parser.add_section("Telnet404") try: self.parser.set("Telnet404", "Jwt token", self.token) self.parser.write(open(self.conf_path, "w")) except Exception as ex: logger.error(str(ex)) def verify_request(self, flag, type="request"): """ 校验ceye接口是否有数据 :param flag: 输入的flag :param type: 请求类型(dns|request),默认是request :return: Boolean """ ret_val = False counts = 3 url = "http://api.ceye.io/v1/records?token={token}&type={type}&filter={flag}".format( token=self.token, type=type, flag=flag) while counts: try: time.sleep(1) resp = requests.get(url) if resp and resp.status_code == 200 and flag in resp.text: ret_val = True break except Exception as ex: logger.warn(ex) time.sleep(1) counts -= 1 return ret_val def exact_request(self, flag, type="request"): """ 通过访问ceye接口获取相关数据 :param flag: 输入的flag :param type: 请求类型(dns|request),默认是request :return:返回获取的数据 """ counts = 3 url = "http://api.ceye.io/v1/records?token={token}&type={type}&filter={flag}".format( token=self.token, type=type, flag=flag) while counts: try: time.sleep(1) resp = requests.get(url) if resp and resp.status_code == 200 and flag in resp.text: data = json.loads(resp.text) for item in data["data"]: name = item.get("name", '') pro = "/" + flag suffix = flag t = get_middle_text(name, pro, suffix, 7 + len(flag)) if t: return t break except Exception as ex: logger.warn(ex) time.sleep(1) counts -= 1 return False def build_request(self, value, type="request"): """ 生成发送的字符串 :param value: 输入的要发送的信息 :param type: 请求类型(dns|request),默认是request :return: dict { url:返回接收的域名,flag:返回随机的flag } Example: { 'url': 'http://htCb.jwm77k.ceye.io/htCbpingaaahtCb', 'flag': 'htCb' } """ ranstr = random_str(4) domain = self.getsubdomain() url = "" if type == "request": url = "http://{}.{}/{}{}{}".format(ranstr, domain, ranstr, value, ranstr) elif type == "dns": url = "{}{}{}.{}".format(ranstr, re.sub("\W", "", value), ranstr, domain) return {"url": url, "flag": ranstr} def getsubdomain(self): """ 通过ceye token获取子域名 :return:返回获取的域名 """ r = requests.get("http://api.ceye.io/v1/identify", headers=self.headers).json() suffix = ".ceye.io" try: indetify = r["data"]["identify"] except KeyError: return None return indetify + suffix
def load_schematic(): DT.clear() globals.ParsedSchematicFiles = [] # clear this list. config = ConfigParser() config.read('config.ini') initialDirectory = config.get('main', 'lastDirectory', fallback="") if initialDirectory == "": root.filename = filedialog.askopenfilename( filetypes=(("KiCAD Schematic Files", ".sch"), ("All Files", ".*"))) else: root.filename = filedialog.askopenfilename( initialdir=initialDirectory, filetypes=(("KiCAD Schematic Files", ".sch"), ("All Files", ".*"))) filename = root.filename root.lastSchematicFileName = filename config.read('config.ini') if config.has_section('main') == FALSE: config.add_section('main') config.set('main', 'lastDirectory', os.path.dirname(filename)) globals.CsvSeparator = config.get('main', 'csvSeparator', fallback=",") if globals.CsvSeparator is ",": config.set('main', 'csvSeparator', globals.CsvSeparator) with open('config.ini', 'w') as f: config.write(f) if filename[-4:] == ".sch" or filename[-4:] == ".SCH": try: f = open(filename) except IOError: DT.error("can\'t find file or read data") else: mainSchematicFile.setPath(filename) dataTestDump = f.readlines()[0] f.close() if dataTestDump[: -1] == "EESchema Schematic File Version 2" or dataTestDump[: -1] == "EESchema Schematic File Version 4": # support for KiCad4 and KiCad5 # TODO 2: move the laoding of a schematic file into the Schematic class! # --> make use of recursive loading of schematics #verify it conforms to KiCAD specs if mainSchematicFile.components: mainSchematicFile.deleteContents() try: f = open(filename) except IOError: DT.error("can\'t find file or read data") else: mainSchematicFile.SetContents(f.readlines()) mainSchematicFile.fieldList = fieldList mainSchematicFile.schematicName = os.path.basename(filename) f.close() if mainSchematicFile.ParseComponents(): if (len(mainSchematicFile.getSubCircuits()) != len( mainSchematicFile.getSubCircuitName())): messagebox.showerror( "FileParseError", "Hierarchical schematics could not be found") else: messagebox.showerror( "FileParseError", "This is not a valid KiCAD schematic document.") else: messagebox.showerror( "FileParseError", "This is not a valid KiCAD schematic document.") else: if filename: messagebox.showerror( "FileParseError", "This is not a valid KiCAD schematic document (*.sch or *.SCH)" ) for i in range(len(mainSchematicFile.components)): if "?" in mainSchematicFile.components[i].getReference(): if messagebox.askyesno( "Annotation Incomplete", "The program is unable to process unanotated components. Do you want to clear imported data?" ): mainSchematicFile.deleteContents() break root.initialDirectory = set_initial_directory(filename) if mainSchematicFile.schematicName: unlistedCount = 0 for c in mainSchematicFile.components: if c.unlisted: unlistedCount += 1 statusLabel['text'] = "Loaded schematic: " + mainSchematicFile.schematicName + "\n" +\ str(len(mainSchematicFile.components) - unlistedCount ) +\ " + " + str(unlistedCount) + " hidden components were found" else: statusLabel['text'] = "Start by loading a KiCad schematic file..." print("Summary of loading schematic: ") DT.summary()
else: flash_global = {} try: s_price_round = config.get('round', 's_price_round') counts_round = config.get('round', 'counts_round') price_round = config.get('round', 'price_round') except BaseException: print('======开始输入范围信息,格式千万不能错哦!======') s_price_round = input('输入单价范围(格式:最小值-最大值):') counts_round = input('输入数量范围(格式:最小值-最大值):') price_round = input('输入总价范围(格式:最小值-最大值):') with open('key_info.cfg', 'w') as cfgfile: config.add_section('round') config.set('round', 's_price_round', s_price_round) config.set('round', 'counts_round', counts_round) config.set('round', 'price_round', price_round) config.write(cfgfile) print(s_price_round, counts_round, price_round) try: s_price_round = [float(x) for x in s_price_round.split('-')] counts_round = [float(x) for x in counts_round.split('-')] price_round = [float(x) for x in price_round.split('-')] print( '[成功] 系统已经接受范围信息(已记住,如果重新输入请修改目录文件key_info.cfg):单价%s~%s,数量%s~%s,总价%s~%s' % (s_price_round[0], s_price_round[1], counts_round[0], counts_round[1], price_round[0], price_round[1]))
class ReadConifg: PROJECT_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) CONFIG_PATH = os.path.join(PROJECT_PATH, "config", "config.ini") REPORT_PATH = os.path.join(PROJECT_PATH, "report") # 导入/上传excel文件路径 EXCEL_PATH = os.path.join(PROJECT_PATH, "config\\excel") def __init__(self): self.cf = ConfigParser() self.cf.read(self.CONFIG_PATH, encoding="utf-8") def get_email(self, option): value = self.cf.get("EMAIL", option) return value def get_db(self, option): value = self.cf.get("DB", option) return value def get_env(self): value = self.cf.get("ENV", "environment") return str(value).lower() def get_report(self, option): value = self.cf.get("REPORT", option) return value def get_token(self, option): value = self.cf.get("TOKEN", option) return value def set_section(self, section): self.cf.add_section(section) def set_value(self, section, option, value): section = self.set_section(section) self.cf.set(section, option, value) def set_token(self, option, value): self.cf.set(section='TOKEN', option=option, value=value) with open(self.CONFIG_PATH, 'w+', encoding='utf-8') as f: self.cf.write(f) def get_account_info(self, env): partner = self.cf.get('ACCOUNT', "{}_partner".format(env)) leader = self.cf.get('ACCOUNT', "{}_leader".format(env)) students = self.cf.get('ACCOUNT', "{}_student".format(env)) cookie = self.cf.get('ACCOUNT', "{}_cookie".format(env)) return partner, leader, students, cookie def set_environment(self, url): env = self.get_env() compile = re.compile(r'(test)|(fat)|(pre)') pattern = compile.search(url) if pattern is not None: pattern = pattern.group() new_url = url.replace(pattern, env) else: new_url = url return new_url
HOST = input( "\nConfig file not detected. Enter the host prefix for your edsby instance (*.edsby.com): " ) if HOST != "": HOST += ".edsby.com" USERNAME = input("Username: "******"n" if not exists or USERNAME != rUSERNAME: save = input( "Would you like to save this to the config file? This will overwrite the current config. (y/n)" ) if save == "y": file = open(directory + "/testConfig.ini", "w") if not exists: config.add_section("auth") config.set("auth", "host", HOST) config.set("auth", "username", USERNAME) config.set("auth", "password", PASSWORD) config.write(file) file.close() exists = True else: HOST = rHOST USERNAME = rUSERNAME PASSWORD = rPASSWORD printd( BOLD + HEADER + "–----------------------------------------------------------------------------------------------------------------------" )
class IniCfg(): def __init__(self): self.conf = ConfigParser() self.cfgpath = '' def check_section(self, section): try: self.conf.items(section) except Exception: print(">> 无此section,请核对[%s]" % section) return None return True # 读取ini,并获取所有的section名 def readSectionItems(self, cfgpath): if not os.path.isfile(cfgpath): print(">> 无此文件,请核对路径[%s]" % cfgpath) return None self.cfgpath = cfgpath self.conf.read(cfgpath, encoding="utf-8") return self.conf.sections() # 读取一个section,list里面对象是元祖 def readOneSection(self, section): try: item = self.conf.items(section) except Exception: print(">> 无此section,请核对[%s]" % section) return None return item # 读取一个section到字典中 def prettySecToDic(self, section): if not self.check_section(section): return None res = {} for key, val in self.conf.items(section): res[key] = val return res # 读取所有section到字典中 def prettySecsToDic(self): res_1 = {} res_2 = {} sections = self.conf.sections() for sec in sections: for key, val in self.conf.items(sec): res_2[key] = val res_1[sec] = res_2.copy() res_2.clear() return res_1 # 删除一个 section中的一个item(以键值KEY为标识) def removeItem(self, section, key): if not self.check_section(section): return self.conf.remove_option(section, key) # 删除整个section这一项 def removeSection(self, section): if not self.check_section(section): return self.conf.remove_section(section) # 添加一个section def addSection(self, section): self.conf.add_section(section) # 往section添加key和value def addItem(self, section, key, value): if not self.check_section(section): return self.conf.set(section, key, value) # 执行write写入, remove和set方法并没有真正的修改ini文件内容,只有当执行conf.write()方法的时候,才会修改ini文件内容 def actionOperate(self, mode): if mode == 'r+': self.conf.write(open(self.cfgpath, "r+", encoding="utf-8")) # 修改模式 elif mode == 'w': self.conf.write(open(self.cfgpath, "w")) # 删除原文件重新写入 elif mode == 'a': self.conf.write(open(self.cfgpath, "a")) # 追加模式写入
def load_csv(): DT.clear() config = ConfigParser() config.read('config.ini') initialDirectory = config.get('main', 'lastDirectory', fallback="") #mainSchematicFile.printprops() if initialDirectory == "": root.filename = filedialog.askopenfilename( filetypes=(("KiCAD Partslist-editor files", ".csv"), ("All Files", ".*"))) else: root.filename = filedialog.askopenfilename( initialdir=initialDirectory, filetypes=(("KiCAD Partslist-editor files", ".csv"), ("All Files", ".*"))) filename = root.filename config.read('config.ini') if config.has_section('main') == FALSE: config.add_section('main') config.set('main', 'lastDirectory', os.path.dirname(filename)) globals.CsvSeparator = config.get('main', 'csvSeparator', fallback=",") if globals.CsvSeparator is ",": config.set('main', 'csvSeparator', globals.CsvSeparator) with open('config.ini', 'w') as f: config.write(f) if filename[-4:] == ".csv" or filename[-4:] == ".CSV": try: f = open(filename) except IOError: messagebox.showerror("File IO Error", "Cannot open CSV File " + filename) else: f.close() if csvFile.components: csvFile.deleteContents() f = open(filename) csvFile.setContents(f.readlines()) error = csvFile.extractCsvComponents() if error: messagebox.showerror("Incorrect Fileformat", error) else: statusLabel['text'] = "Import: " + str( root.filename) + " complete" + "\n" + str( len(csvFile.components)) + " components were imported" f.close() else: if filename: messagebox.showerror( "FileParseError", "This is not a valid CSV document (*.csv or *.CSV)") print("Summary for importing from CSV: ") DT.summary() DT.info( str(len(csvFile.components)) + ' components were imported from CSV')
def _fake_config() -> ConfigParser: cp = ConfigParser() cp.add_section("mirror") cp["mirror"]["directory"] = "/tmp/unittest" cp["mirror"]["workers"] = "1" return cp
class InventoryFilter(object): __metaclass__ = abc.ABCMeta def __init__(self): self.config = ConfigParser(allow_no_value=True) @abc.abstractmethod def get_host_ips(self, topo): pass @abc.abstractmethod def get_inventory(self, topo, layout): pass def get_layout_hosts(self, inv): count = 0 for host_group in inv['hosts']: if 'count' in inv['hosts'][host_group]: count += inv['hosts'][host_group]['count'] else: count += 1 return count def get_layout_host_groups(self, inv): """ get all the list of host groups in layout """ host_groups = [] for host in inv['hosts']: if "host_groups" in inv["hosts"][host].keys(): host_groups.extend(inv['hosts'][host]["host_groups"]) return list(set(host_groups)) def add_sections(self, section_list): for section in section_list: self.config.add_section(section) # adding a default section all if "all" not in self.config.sections(): self.config.add_section("all") def set_children(self, inv): if 'host_groups' not in inv.keys(): return for host_group in inv['host_groups']: if "children" in inv['host_groups'][host_group]: self.config.add_section(host_group+":"+"children") for child in inv['host_groups'][host_group]['children']: self.config.set(host_group+":"+"children", child) def set_vars(self, inv): if 'host_groups' not in inv.keys(): return for host_group in inv['host_groups']: if "vars" in inv['host_groups'][host_group]: self.config.add_section(host_group+":"+"vars") for var in inv['host_groups'][host_group]['vars']: grp_vars = inv['host_groups'][host_group]['vars'][var] grp_vars = str(grp_vars) self.config.set(host_group + ":" + "vars", var, grp_vars) def add_ips_to_groups(self, inven_hosts, layout): # create a ip to host mapping based on count ip_to_host = {} host_grps = self.get_layout_host_groups(layout) for host_name in layout['hosts']: if 'count' in layout['hosts'][host_name]: count = layout['hosts'][host_name]['count'] else: count = 1 host_list = [] for i in range(0, count): item = inven_hosts.pop() host_list.append(item) ip_to_host[host_name] = host_list # add ips to the respective host groups in inventory for host_name in layout['hosts']: host_ips = ip_to_host[host_name] for ip in host_ips: for host_group in layout['hosts'][host_name]['host_groups']: self.config.set(host_group, ip) self.config.set("all", ip) def add_common_vars(self, host_groups, layout): # defaults common_vars to [] when they doesnot exist host_groups.append("all") common_vars = layout['vars'] if 'vars' in layout.keys() else [] for group in host_groups: items = dict(self.config.items(group)).keys() self.config.remove_section(group) self.config.add_section(group) for item in items: host_string = item for var in common_vars: if common_vars[var] == "__IP__": host_string += " " + var + "=" + item + " " self.config.set(group, host_string)
DEFAULT_AUTO_HINT = 'true' DEFAULT_AUTO_SELECT_FIRST_HINT = 'true' autostart_dir = base.save_config_path("autostart") autostart_file = os.path.join(autostart_dir, POCO_DESKTOP) config_dir = base.load_first_config(POCO_PACKAGE) if not config_dir: config_dir = base.save_config_path(POCO_PACKAGE) config_file_path = os.path.join(config_dir, "poco.cfg") parser = ConfigParser(interpolation=None) parser.read(config_file_path) need_write = False if not parser.has_section('interface'): parser.add_section('interface') need_write = True if not parser.has_option('interface', 'prefix_key'): parser.set('interface', 'prefix_key', DEFAULT_PREFIX_KEY) need_write = True if not parser.has_option('interface', 'list_workspaces'): parser.set('interface', 'list_workspaces', DEFAULT_LIST_WORKSPACES) need_write = True if not parser.has_option('interface', 'position'): parser.set('interface', 'position', DEFAULT_POSITION) need_write = True if not parser.has_option('interface', 'width'): parser.set('interface', 'width', DEFAULT_WIDTH) need_write = True if not parser.has_option('interface', 'auto_hint'): parser.set('interface', 'auto_hint', DEFAULT_AUTO_HINT)
def create_sample_config(): parser = ConfigParser() parser.add_section('test_env') parser.set('test_env', 'index_driver', 'default')
def main(): # Start off by running egg_info subprocess.call([sys.executable, 'setup.py', 'egg_info']) # Check that only one egg-info directory exists egg_info_dir = glob.glob('*.egg-info') if len(egg_info_dir) != 1: print("Expected only one .egg-info directory, got {0}".format( egg_info_dir)) sys.exit(1) else: egg_info_dir = egg_info_dir[0] # Read in any existing setup.cfg file (this will just create an empty # configuration if the file doesn't exist yet) conf = ConfigParser() conf.read('setup.cfg') # Add required sections if not conf.has_section('metadata'): conf.add_section('metadata') if not conf.has_section('options'): conf.add_section('options') # Parse the PKG-INFO file dist = Develop('.') # Populate the metadata for key, handler in METADATA_KEYS.items(): translated = handler.serialize(getattr(dist, key)) if translated is not None: conf.set('metadata', handler.setup_cfg_name, translated) # Populate the options conf.set( 'options', 'zip_safe', str(not os.path.exists(os.path.join(egg_info_dir, 'not-zip-safe')))) conf.set('options', 'packages', 'find:') if dist.requires_python is not None: conf.set('options', 'python_requires', dist.requires_python) # Check entry points, if they exist if os.path.exists(os.path.join(egg_info_dir, 'entry_points.txt')): # Entry points are stored in a file that has a config-compatible syntax entry = ConfigParser() entry.read(os.path.join(egg_info_dir, 'entry_points.txt')) if not conf.has_section('options.entry_points'): conf.add_section('options.entry_points') for section in entry.sections(): entry_points = [] for item in entry[section]: entry_points.append('{0} = {1}'.format(item, entry[section][item])) conf.set('options.entry_points', section, os.linesep + os.linesep.join(entry_points)) # Check install and extras requirements if os.path.exists(os.path.join(egg_info_dir, 'requires.txt')): # The syntax is # # install requirements, one per line # # [extra name] # requirements, one per line # # [another extra name] # etc. with open(os.path.join(egg_info_dir, 'requires.txt')) as f: requires = defaultdict(list) section = 'default' for req in f: if req.startswith('['): section = req.strip()[1:-1] elif req.strip() != '': requires[section].append(req.strip()) conf.set('options', 'install_requires', os.linesep + os.linesep.join(requires['default'])) # If there are more than one entries in the requires dict, there are some extras if len(requires) > 1: if not conf.has_section('options.extras_require'): conf.add_section('options.extras_require') for section in sorted(requires): if section != 'default': conf.set('options.extras_require', section, '; '.join(requires[section])) # Sort the sections def sections_key(section): if section == 'metadata': return 'a' elif section == 'options': return 'b' elif section.startswith('options'): return 'c' + section else: return 'd' conf._sections = OrderedDict([ (section, conf._sections[section]) for section in sorted(conf._sections, key=sections_key) ]) with open('setup.cfg', 'w') as f: conf.write(f)
class GPIniParser(GPParser): ini_conf = None def parse(self, contents): # Required dict_type in Python 2.7 self.ini_conf = ConfigParser(dict_type=collections.OrderedDict, interpolation=None) self.ini_conf.optionxform = str self.ini_conf.readfp(StringIO(contents.decode(self.encoding))) def build_xml_parameter(self, section_xml, section, key_ini, val_ini): child = SubElement(section_xml, 'Parameter') key = SubElement(child, 'Key') value = SubElement(child, 'Value') key.text = key_ini value.text = val_ini return child def load_xml_parameter(self, param_xml, section): key = param_xml.find('Key').text value = param_xml.find('Value').text if value is None: value = '' self.ini_conf.set(section, key, value) return (key, value) def build_xml_section(self, root_xml, sec_ini): section = SubElement(root_xml, 'Section') section.attrib['name'] = sec_ini return section def load_xml_section(self, section_xml): section_name = section_xml.attrib['name'] self.ini_conf.add_section(section_name) return section_name def write_xml(self, filename): with open(filename, 'wb') as f: root = Element('IniFile') for sec_ini in self.ini_conf.sections(): section = self.build_xml_section(root, sec_ini) for key_ini, val_ini in self.ini_conf.items(sec_ini, raw=True): self.build_xml_parameter(section, sec_ini, key_ini, val_ini) self.write_pretty_xml(root, f) # from xml.etree.ElementTree import fromstring # contents = codecs.open(filename, encoding='utf-8').read() # self.load_xml(fromstring(contents)) def load_xml(self, root): # Required dict_type in Python 2.7 self.ini_conf = ConfigParser(dict_type=collections.OrderedDict, interpolation=None) self.ini_conf.optionxform = str for s in root.findall('Section'): section_name = self.load_xml_section(s) for param in s.findall('Parameter'): self.load_xml_parameter(param, section_name) def write_binary(self, filename): with codecs.open(filename, 'wb+', self.encoding) as f: self.ini_conf.write(f)