def main(argv): config = SafeConfigParser() config.read(SYSTEM_BOTO_CONFIG_TEMPLATE) # TODO(user): Figure out if we need a retry here. project_id = GetNumericProjectId() if not project_id: # Our project doesn't support service accounts. return if not config.has_section("GSUtil"): config.add_section("GSUtil") config.set("GSUtil", "default_project_id", project_id) config.set("GSUtil", "default_api_version", "2") if not config.has_section("GoogleCompute"): config.add_section("GoogleCompute") # TODO(user): Plumb a metadata value to set this. We probably want # to namespace the metadata values in some way like # 'boto_auth.servicee_account'. config.set("GoogleCompute", "service_account", "default") if not config.has_section("Plugin"): config.add_section("Plugin") config.set("Plugin", "plugin_directory", AUTH_PLUGIN_DIR) with open(SYSTEM_BOTO_CONFIG, "w") as configfile: AddConfigFileHeader(configfile) config.write(configfile)
def main(settings="settings.ini"): parser = SafeConfigParser() parser.read(settings) settings_dict = {} clients_dict = {} # Covert the settings.ini settings into a dictionary for later processing if parser.has_section("havocbot"): # Create a bundle of havocbot settings settings_dict["havocbot"] = parser.items("havocbot") if parser.has_option("havocbot", "clients_enabled"): clients_string = parser.get("havocbot", "clients_enabled") clients_list = clients_string.strip().split(",") # Create a bundle of settings to pass the client integration for processing # Bundle format is a list of tuples in the format [('integration name'), [('property1', 'value1'), ('property2', 'value12)], ...] for client in clients_list: if parser.has_section(client): clients_dict[client] = parser.items(client) else: sys.exit("Could not find havocbot settings in settings.ini") # Configure logging configure_logging(settings_dict) # Get an instance of the bot if it does not exist havocbot = get_bot() # Pass a dictionary of settings to the bot havocbot.set_settings(havocbot_settings=settings_dict, clients_settings=clients_dict) # Start it. Off we go havocbot.start()
def _load_schema(self, location, xml=None): """ location -- location of schema, also used as a key xml -- optional string representation of schema """ cachedir = self._cachedir # wsdl2py: deal with XML Schema if not os.path.isdir(cachedir): os.mkdir(cachedir) file = os.path.join(cachedir, '.cache') section = 'TYPES' cp = ConfigParser() try: cp.readfp(open(file, 'r')) except IOError: del cp; cp = None option = location.replace(':', '-') # colons seem to screw up option if (cp is not None and cp.has_section(section) and cp.has_option(section, option)): types = cp.get(section, option) else: # dont do anything to anames if not self._pyclass: containers.ContainerBase.func_aname = lambda instnc,n: str(n) from pysphere.ZSI.wstools import XMLSchema reader = XMLSchema.SchemaReader(base_url=location) if xml is not None and isinstance(xml, str): schema = reader.loadFromString(xml) elif xml is not None: raise RuntimeError('Unsupported: XML must be string') elif not os.path.isfile(location): schema = reader.loadFromURL(location) else: schema = reader.reader.loadFromFile(location) # TODO: change this to keyword list class options: output_dir = cachedir schema = True simple_naming = False address = False lazy = self._lazy complexType = self._pyclass schema.location = location files = commands._wsdl2py(options, schema) if cp is None: cp = ConfigParser() if not cp.has_section(section): cp.add_section(section) types = filter(lambda f: f.endswith('_types.py'), files)[0] cp.set(section, option, types) cp.write(open(file, 'w')) if os.path.abspath(cachedir) not in sys.path: sys.path.append(os.path.abspath(cachedir)) mod = os.path.split(types)[-1].rstrip('.py') return __import__(mod)
def _load_schema(self, location, xml=None): """ location -- location of schema, also used as a key xml -- optional string representation of schema """ cachedir = self._cachedir # wsdl2py: deal with XML Schema if not os.path.isdir(cachedir): os.mkdir(cachedir) _file = os.path.join(cachedir, '.cache') section = 'TYPES' cp = ConfigParser() try: cp.readfp(open(_file, 'r')) except IOError: del cp; cp = None option = location.replace(':', '-') # colons seem to screw up option if (cp is not None and cp.has_section(section) and cp.has_option(section, option)): types = cp.get(section, option) else: # dont do anything to anames if not self._pyclass: containers.ContainerBase.func_aname = lambda instnc,n: str(n) from pysphere.ZSI.wstools import XMLSchema reader = XMLSchema.SchemaReader(base_url=location) if xml is not None and isinstance(xml, str): schema = reader.loadFromString(xml) elif xml is not None: raise RuntimeError('Unsupported: XML must be string') elif not os.path.isfile(location): schema = reader.loadFromURL(location) else: schema = reader.reader.loadFromFile(location) # TODO: change this to keyword list class options: output_dir = cachedir schema = True simple_naming = False address = False lazy = self._lazy complexType = self._pyclass schema.location = location files = commands._wsdl2py(options, schema) if cp is None: cp = ConfigParser() if not cp.has_section(section): cp.add_section(section) types = [f for f in files if f.endswith('_types.py')][0] cp.set(section, option, types) cp.write(open(_file, 'w')) if os.path.abspath(cachedir) not in sys.path: sys.path.append(os.path.abspath(cachedir)) mod = os.path.split(types)[-1].rstrip('.py') return __import__(mod)
def __init__(self, config_path): # Read configuration file cfg = SafeConfigParser() cfg.read(config_path) # Case self.v_inf = cfg.getfloat('case', 'v_inf') self.rpm = cfg.getfloat('case', 'rpm') if cfg.has_option('case', 'twist'): self.twist = cfg.getfloat('case', 'twist') else: self.twist = 0.0 if cfg.has_option('case', 'coaxial'): self.coaxial = cfg.getboolean('case', 'coaxial') else: self.coaxial = False # Rotor if cfg.has_section('turbine'): self.mode = 'turbine' self.rotor = Rotor(cfg, 'turbine', self.mode) else: self.mode = 'rotor' self.rotor = Rotor(cfg, 'rotor', self.mode) # Fluid self.fluid = Fluid(cfg) # Output self.T = 0 # Thrust self.Q = 0 # Torque self.P = 0 # Power # Coaxial if self.coaxial: self.rpm2 = cfg.getfloat('case', 'rpm2') if cfg.has_option('case', 'twist2'): self.twist2 = cfg.getfloat('case', 'twist2') else: self.twist2 = 0.0 self.rotor2 = Rotor(cfg, 'rotor2', self.mode) self.zD = cfg.getfloat('case', 'dz') / self.rotor.diameter self.T2 = 0 self.Q2 = 0 self.P2 = 0 # Solver self.solver = 'bisect' self.Cs = 0.625 if cfg.has_section('solver'): self.solver = cfg.get('solver', 'solver') if cfg.has_option('solver', 'Cs'): self.Cs = cfg.getfloat('solver', 'Cs')
def load_information(self): """ Load the add-on's information from file. """ parser = SafeConfigParser() with self.path.open(self.file) as file: parser.readfp(file, self.file) # Read the core information. if not parser.has_section('Core'): raise ValueError( "No Core section in the add-on information file for the " "add-on %r." % self.name ) for key in self.CORE_VALUES + ('version',): if isinstance(key, (list, tuple)): key, key_type = key if isinstance(key_type, basestring): key_type = eval(key_type) else: key_type = lambda thing: thing # If we don't have that key, and we have a default value, just # continue, otherwise raise a ValueError. if not parser.has_option('Core', key): if not hasattr(self, key): raise ValueError( "Core value %r not defined in the add-on " "information file for the add-on %r." % (key, self.name) ) continue # Load the value and set it as an attribute of self. setattr(self, key, key_type(parser.get('Core', key))) # Split the inheritance. if (hasattr(self, 'inherits') and self.inherits and isinstance(self.inherits, basestring)): self.inherits = [x.strip() for x in self.inherits.split(',')] # Now, read the requirements. if parser.has_section('Requires'): for key, value in parser.items('Requires'): name, match = VersionMatch.from_string(value) self.requires[name] = match # Finally, read the data section. This generally just contains a nice # description of the add-on. if parser.has_section('Data'): self.data.update(parser.items('Data')) if parser.has_section('Description'): self.data.update(parser.items('Description'))
class Config(Log): _title= 'cfg' _config = None _configdir = expanduser("~/.ads") _configfile = None _schema = None def __init__(self, filename, schema=None): Log.__init__(self, self._title) self._config = SafeConfigParser() if not isdir(self._configdir): self._logger.info(u"creating configuration directory %s" % self._configdir) makedirs(self._configdir) self._configfile = join(self._configdir, filename) self._schema = schema self.load() def configdir(self): return self._configdir def load(self): self._logger.info(u"loading configuration from %s" % self._configfile) self._config.read(self._configfile) def save(self): self._logger.info(u"saving configuration to %s" % self._configfile) self._config.write(open(self._configfile,'wb')) def set(self, section, option, value): if self._schema: _section = u"%s-%s" % (self._schema, section) else: _section = section if not self._config.has_section(_section): self._config.add_section(_section) self._config.set(_section, option, unicode(value)) return value def get(self, section, option, default): if self._schema: _section = u"%s-%s" % (self._schema, section) else: _section = section if (not self._config.has_section(_section) or not self._config.has_option(_section, option)): return self.set(section, option, default) return self._config.get(_section, option) def getint(self, section, option, default): return int(self.get(section, option, default)) def getfloat(self, section, option, default): return float(self.get(section, option, default))
def load_config(logger): """Loads config values from the settings.cfg file in the script dir""" config_path = get_script_dir() + 'settings.cfg' section_name = 'accounts' if not os.path.exists(config_path): logger.error("Error: No config file found. Copy settings-example.cfg " "to settings.cfg and customize it.") exit() config = SafeConfigParser() config.read(config_path) # Do basic checks on the config file if not config.has_section(section_name): logger.error("Error: The config file doesn't have an accounts " "section. Check the config file format.") exit() if not config.has_option(section_name, 'api_key'): logger.error("Error: No developer key found in the config file. " "Check the config file values.") exit() config_values = { 'api_key': config.get(section_name, 'api_key'), } return config_values
def read_db_config(filename='config.ini', section='mysql'): """ Read database configuration file and return a dictionary object :param filename: name of the configuration file :param section: section of database configuration :return: a dictionary of database parameters """ # create parser and read ini configuration file parser = SafeConfigParser() thisfolder = os.path.dirname(os.path.abspath(__file__)) initfile = os.path.join(thisfolder, 'config.ini') parser.read(initfile) # get section, default to mysql db = {} if parser.has_section(section): items = parser.items(section) for item in items: db[item[0]] = item[1] else: raise Exception('{0} not found in the {1} file'.format( section, filename)) return db
def get(name, rc_file='~/.oerplibrc'): """Return the session configuration identified by `name` from the `rc_file` file. >>> import oerplib >>> oerplib.tools.session.get('foo') {'protocol': 'xmlrpc', 'user': '******', 'timeout': 120, 'database': 'db_name', 'passwd': 'admin', 'type': 'OERP', 'port': 8069, 'server': 'localhost'} :raise: :class:`oerplib.error.Error` """ conf = SafeConfigParser() conf.read([os.path.expanduser(rc_file)]) if not conf.has_section(name): raise error.Error( "'{0}' session does not exist".format(name)) return { 'type': conf.get(name, 'type'), 'server': conf.get(name, 'server'), 'protocol': conf.get(name, 'protocol'), 'port': conf.getint(name, 'port'), 'timeout': conf.getint(name, 'timeout'), 'user': conf.get(name, 'user'), 'passwd': conf.get(name, 'passwd'), 'database': conf.get(name, 'database'), }
async def on_ready(): print('Logged in as') print(client.user.name) print(client.user.id) print('------') print(about_msg) # Create giveaway section in config if it doesn't exist config = SafeConfigParser() config.read('config.ini') if not config.has_section('giveaway'): config.add_section('giveaway') config.set('giveaway', 'rank_restriction', 'False') with open('config.ini', 'w') as f: config.write(f) gw2 = discord.Game(name="Guild Wars 2", type=0) status = discord.Status("online") await client.change_presence(game=gw2, status=status) # Check if a giveaway was going on before the previous boot global giveaway_is_running resultset = session.query(Giveaway) if resultset.count() > 0: giveaway_is_running = True
def write_sts_token(self, profile, access_key_id, secret_access_key, session_token, region=None): """ Writes STS auth information to credentials file """ region = region or self.region output = 'json' if not os.path.exists(self.creds_dir): os.makedirs(self.creds_dir) config = SafeConfigParser() if os.path.isfile(self.creds_file): config.read(self.creds_file) if not config.has_section(profile): config.add_section(profile) config.set(profile, 'output', output) config.set(profile, 'region', region) config.set(profile, 'aws_access_key_id', access_key_id) config.set(profile, 'aws_secret_access_key', secret_access_key) config.set(profile, 'aws_session_token', session_token) config.set(profile, 'aws_security_token', session_token) with open(self.creds_file, 'w+') as configfile: config.write(configfile) print("Temporary credentials written to profile: %s" % profile) self.logger.info("Invoke using: aws --profile %s <service> <command>" % profile)
class QRZ(object): def __init__(self, cfg=None): if cfg: self._cfg = SafeConfigParser() self._cfg.read(cfg) else: self._cfg = None self._session = None self._session_key = None def _get_session(self): if self._cfg and self._cfg.has_section('qrz'): username = self._cfg.get('qrz', 'username') password = self._cfg.get('qrz', 'password') else: username = os.environ.get('QRZ_USER') password = os.environ.get('QRZ_PASSWORD') if not username or not password: raise QRZMissingCredentials("No Username/Password found") url = '''https://xmldata.qrz.com/xml/current/?username={0}&password={1}'''.format( username, password) self._session = requests.Session() self._session.verify = bool(os.getenv('SSL_VERIFY', False)) r = self._session.get(url) if r.status_code == 200: raw_session = xmltodict.parse(r.content) self._session_key = raw_session.get('QRZDatabase').get( 'Session').get('Key') if self._session_key: return True raise QRZsessionNotFound("Could not get QRZ session") def callsign(self, callsign, retry=True): if self._session_key is None: self._get_session() url = """http://xmldata.qrz.com/xml/current/?s={0}&callsign={1}""".format( self._session_key, callsign) r = self._session.get(url) if r.status_code != 200: raise Exception("Error Querying: Response code {}".format( r.status_code)) raw = xmltodict.parse(r.content).get('QRZDatabase') if not raw: raise QRZerror('Unexpected API Result') if raw['Session'].get('Error'): errormsg = raw['Session'].get('Error') if 'Session Timeout' in errormsg or 'Invalid session key' in errormsg: if retry: self._session_key = None self._session = None return self.callsign(callsign, retry=False) elif "not found" in errormsg.lower(): raise CallsignNotFound(errormsg) raise QRZerror(raw['Session'].get('Error')) else: ham = raw.get('Callsign') if ham: return ham raise Exception("Unhandled Error during Query")
def loadConfig(cls): config = SafeConfigParser() if os.path.isfile(CONFIG_FILE): config.read_file(codecs.open(CONFIG_FILE, encoding='utf-8')) # The default config cls.config = {} cls.config['command_open'] = None cls.config['player'] = 1 cls.config['debug'] = 0 # Load the options if config.has_section('options'): for key, value in config.items('options'): cls.config[key] = value # Load the series cls.series = [] for section in config.sections(): if section != 'options': title = config.get(section, 'title') videos = config.get(section, 'videos') theTvDb = config.getint(section, 'theTvDb') lang = config.get(section, 'lang') cls.series.append([section, title, videos, theTvDb, lang])
def from_file(cls, filename: str = None) -> "Config": if not filename: path = Path(__file__).with_name(DEFAULT_SUBREDDIT_SIMULATOR_CONFIG) else: path = Path(filename) if not path.exists(): raise FileNotFoundError(str(path)) parser = SafeConfigParser() parser.read(path, encoding="utf-8") config: Dict[str, Any] = dict.fromkeys( map(attrgetter("name"), attr.fields(cls)), "") sections = ("database", "settings", "accounts", "top_subreddits", "proxies") for section in sections: if not parser.has_section(section): raise KeyError(f"No [{section}] in {path!s}!") for key, value in parser.items(section): config[key] = value config.pop("verbose") return cls(**config)
def parseConfig (self): """Reads the config file (wormBait.ini) and initializes variables accordingly""" config = SafeConfigParser() config.read('wormBait.ini') configDbIds = "" configDegFile = "" configOutFile = "" if config.has_section('wormBait'): configDbIds = config.get('wormBait', 'dbIds') configDegFile = config.get('wormBait', 'degFile') configOutFile = config.get('wormBait', 'outFile') if configDbIds: self.entryList.writeln(configDbIds) else: self.entryList.writeln("Enter DB IDs here") if configDegFile: self.dbFilePath.set(configDegFile) else: self.dbFilePath.set('Enter path to input database file here') if configOutFile: self.outFilePath.set(configOutFile) else: self.outFilePath.set('Enter desired path to output CSV file here')
def load_file_config(path=None): """ Loads configuration from file with following content:: [Credentials] access_key = <your access key> secret_key = <your secret key> associate_tag = <your associate tag> locale = us :param path: path to config file. If not specified, locations ``/etc/amazon-product-api.cfg`` and ``~/.amazon-product-api`` are tried. """ config = SafeConfigParser() if path is None: config.read([os.path.expanduser(path) for path in CONFIG_FILES]) else: config.read(path) if not config.has_section('Credentials'): return {} return dict( (key, val) for key, val in config.items('Credentials') if key in REQUIRED_KEYS )
class Config(object): """ The configuration object for a link to a database. """ def __init__(self, section=None, config_file=None): self.parser = SafeConfigParser()#allow_no_value=True) default_file = os.path.expanduser('~/.EMIS/pyEMIS.ini') if not config_file: config_file = default_file self.config_file = config_file if not section: section = 'DEFAULT' self.section = section read_files = self.parser.read(self.config_file) if not read_files: self.parser.add_section(self.section) with open(default_file, 'wb') as configfile: self.parser.write(configfile) if not self.parser.has_section(self.section): raise ConfigurationFileError('Section [%s] not found in configuration file (sections = %s).' % (self.section, self.parser.sections()), self.config_file) def __getattr__(self, name): try: return self.parser.get(self.section, name) except NoSectionError as e: raise ConfigurationFileError('Section [%s] not found in configuration file (sections = %s).' % (self.section, self.parser.sections()), self.config_file) except NoOptionError as e: raise ConfigurationFileError('Option "%s = ..." not in section [%s] of configuration file (options = %s).' % (name, self.section, self.parser.options(self.section)), self.config_file)
def fixDefaultPath(wspath): """Mimic most often case of hg fdefpath usage, walk across repositories and replace(or add) to hgrc file default = http://closedjdk.us.oracle.com/jdk7u/jdk7u-cpu/jdk/test/closed default-push = ssh://[email protected]/jdk7u/jdk7u-cpu-gate/jdk/test/closed as it doesn't use mercurital api to do it, it works much faster then defpath extension """ config = SafeConfigParser() ini = os.path.join(wspath, '.hg/hgrc') config.read(ini) defaultPull = None defaultPush = None if config.has_section('paths'): if config.has_option('paths', 'default'): defaultPull = config.get('paths', 'default') if config.has_option('paths', 'default-push'): defaultPush = config.get('paths', 'default-push') if defaultPull == None: print("Can't build push path default path is invalid") return ojn = "" if G_ojdkname == None else G_ojdkname + '@' if defaultPull.startswith('http://'): p = defaultPull[7:] elif defaultPull.startswith('https://'): p = defaultPull[8:] elif defaultPull.startswith('ssh://'): p = defaultPull[6:] else: print("Can't build push path default path is invalid or local (%s)" % defaultPull) return ps = p.split('/') if G_gatedpush: ps[2] = ps[2] + '-gate' newDefaultPush = 'ssh://' + ojn + '/'.join( ps) if 'push' in G_defpath else defaultPush newDefaultPull = 'ssh://' + ojn + p if 'pull' in G_defpath else defaultPull if defaultPush == newDefaultPush and defaultPull == newDefaultPull: print("Defpath: %s (not changing)\n %s\n %s" % (ini, defaultPull, defaultPush)) else: print("Defpath: %s\n %s\n %s" % (ini, newDefaultPull, newDefaultPush)) shutil.move(ini, ini + '.old') config.set('paths', 'default', newDefaultPull) config.set('paths', 'default-push', newDefaultPush) fp = open(ini, 'w') config.write(fp) fp.close()
def update(self, section, option, value): parser = SafeConfigParser() parser.read(self.config_path) if not parser.has_section(section): parser.add_section(section) parser.set(section, option, str(value)) with open(self.config_path, "w+") as configfile: parser.write(configfile)
class Config(object): def __init__(self): self._configFileName = "rover.cfg" self._readConfig() def _readConfig(self): self._config = SafeConfigParser({ "db_host": "localhost", "db_port": "27017", "db_name": "rover", "serial_path": "/dev/ttymxc3", "serial_baud": "115200", "serial_timeout": "0.5", "serial_encoding": "windows-1252"}) self._config.read(self._configFileName) self._validateConfig() def _writeConfig(self): self._config.write(open(self._configFileName, "w")) def _validateConfig(self): changed = False if not self._config.has_section("db"): self._config.add_section("db") changed = True if not self._config.has_section("arduino_serial"): self._config.add_section("arduino_serial") changed = True if changed: self._writeConfig() def get(self, section, key): return self._config.get(section, key) def getInt(self, section, key): return self._config.getint(section, key) def getFloat(self, section, key): return self._config.getfloat(section, key) def getBoolean(self, section, key): return self._config.getboolean(section, key) def set(self, section, key, value): self._config.set(section, key, value) self._writeConfig()
class VarEnv(object): """ Allow to load the available variables in a file """ def __init__(self, file): """ 'file' to read """ self._cp = SafeConfigParser() self._cp.read(file) def defaults(self): """ Return a dictionary containing the instance-wide defaults. """ return self._cp.defaults() def has_section(self, section): """ Indicate whether the named section is present in the configuration. """ return self._cp.has_section(section) def items(self, section): """ Return a list of tuples with (name, value) for each option in the section. """ return self._cp.items(section) def sections(self): """ Return a list of section names, excluding [DEFAULT] section. """ return self._cp.sections() def write(self, dest_file): """ Write an ini-format representation of the configuration """ self._cp.write(dest_file) def set_var(self, section, name, value): """ Set an option """ self._cp.set(section, option, value) def get_var(self, var_name, default='', section='DEFAULT'): """ Get a value for given section. The default section will be 'DEFAULT'. """ try: value = dict(self._cp.items(section))[var_name.lower()] if value.startswith('"') and value.endswith('"'): value = value[1:-1] return value except (KeyError, IOError): return default
def _loadini(self, baseini, defaultsett): """Parse master ini configuration file ``baseini`` and ini files refered by `baseini`. Construct a dictionary of settings for special sections and plugin sections.""" from pluggdapps.plugin import pluginnames, plugin_info if not baseini or (not isfile(baseini)): return deepcopy(defaultsett) # Initialize return dictionary. settings = {} # context for parsing ini files. _vars = {"here": abspath(dirname(baseini))} # Read master ini file. cp = SafeConfigParser() cp.read(baseini) # [DEFAULT] overriding global def. s = deepcopy(defaultsett["DEFAULT"]) s.update(dict(cp.defaults())) settings["DEFAULT"] = normalize_defaults(s) # [pluggdapps] s = deepcopy(defaultsett["pluggdapps"]) if cp.has_section("pluggdapps"): s.update(dict(cp.items("pluggdapps", vars=_vars))) s.pop("here", None) # TODO : how `here` gets populated ?? settings["pluggdapps"] = normalize_pluggdapps(s) # Override plugin's package default settings with [DEFAULT] settings. for pluginsec, sett in defaultsett.items(): if not pluginsec.startswith("plugin:"): continue sett = h.mergedict(sett, settings["DEFAULT"]) if cp.has_section(pluginsec): sett.update(dict(cp.items(pluginsec, vars=_vars))) sett.pop("here", None) # TODO : how `here` ?? cls = plugin_info(h.sec2plugin(pluginsec))["cls"] for b in reversed(cls.mro()): if hasattr(b, "normalize_settings"): sett = b.normalize_settings(sett) settings[pluginsec] = sett return settings
def _loadini(self, baseini, defaultsett): """Parse master ini configuration file ``baseini`` and ini files refered by `baseini`. Construct a dictionary of settings for special sections and plugin sections.""" from pluggdapps.plugin import pluginnames, plugin_info if not baseini or (not isfile(baseini)): return deepcopy(defaultsett) # Initialize return dictionary. settings = {} # context for parsing ini files. _vars = {'here': abspath(dirname(baseini))} # Read master ini file. cp = SafeConfigParser() cp.read(baseini) # [DEFAULT] overriding global def. s = deepcopy(defaultsett['DEFAULT']) s.update(dict(cp.defaults())) settings['DEFAULT'] = normalize_defaults(s) # [pluggdapps] s = deepcopy(defaultsett['pluggdapps']) if cp.has_section('pluggdapps'): s.update(dict(cp.items('pluggdapps', vars=_vars))) s.pop('here', None) # TODO : how `here` gets populated ?? settings['pluggdapps'] = normalize_pluggdapps(s) # Override plugin's package default settings with [DEFAULT] settings. for pluginsec, sett in defaultsett.items(): if not pluginsec.startswith('plugin:'): continue sett = h.mergedict(sett, settings['DEFAULT']) if cp.has_section(pluginsec): sett.update(dict(cp.items(pluginsec, vars=_vars))) sett.pop('here', None) # TODO : how `here` ?? cls = plugin_info(h.sec2plugin(pluginsec))['cls'] for b in reversed(cls.mro()): if hasattr(b, 'normalize_settings'): sett = b.normalize_settings(sett) settings[pluginsec] = sett return settings
def loadGamepad(self, _controllerName): pygame.joystick.init() controller_parser = SafeConfigParser() controller_parser.read( os.path.join( os.path.join(self.datadir.replace('main.exe', ''), 'settings'), 'gamepads.ini')) if controller_parser.has_section(_controllerName): joystick = None for pad in range(pygame.joystick.get_count()): joy = pygame.joystick.Joystick(pad) if joy.get_name() == _controllerName: joystick = joy joystick.init() if joystick: jid = joystick.get_id() else: jid = None axes = {} buttons = {} for opt in controller_parser.options(_controllerName): if opt[0] == 'a': axes[int(opt[1:])] = tuple( controller_parser.get(_controllerName, opt)[1:-1].split(',')) elif opt[0] == 'b': buttons[int(opt[1:])] = controller_parser.get( _controllerName, opt) pad_bindings = engine.controller.PadBindings( _controllerName, jid, axes, buttons) return engine.controller.GamepadController(pad_bindings) else: joystick = None for pad in range(pygame.joystick.get_count()): joy = pygame.joystick.Joystick(pad) if joy.get_name() == _controllerName: joystick = joy joystick.init() if joystick: jid = joystick.get_id() else: jid = None axes = dict() buttons = dict() pad_bindings = engine.controller.PadBindings( _controllerName, jid, axes, buttons) self.setting[joystick.get_name()] = pad_bindings return engine.controller.GamepadController(pad_bindings)
def __init__(self, options, gitlab_host): self.options = options self.config = find_config(self.options.config) # Get the host from upstream and remove the https:// # the case for alpine linux, https://gitlab.alpinelinux.org # will be reduced to gitlab.alpinelinux.org # # Do note that it does not matter if we the origin or upstream # remote is used here since gitlab is not federated, so both will # call the same server self.section = gitlab_host.replace("https://", "") """ Write the configuration passed to us via the CLI to the config file if it's not there already or the user wants to overwrite it """ parser = SafeConfigParser() parser.read(self.config) if parser.has_section(self.section) is False: parser.add_section(self.section) with open(self.config, "w") as c: parser.write(c) # In case the 'url' options is not set in the section we are looking for # then just write it out. if parser.has_option(self.section, "url") is False: parser[self.section]["url"] = "https://" + self.section with open(self.config, "w") as c: parser.write(c) if parser.has_option(self.section, "private_token") is False or ( self.options.overwrite is True): # If --token is not passed to us then drop out with a long useful # message, if it is passed to us write it out in the configuration # file if self.options.token is None: token_answer = dict() token_answer["token"] = "" print("Please visit https://" + self.section + "/profile/personal_access_tokens to generate your token") while token_answer is not None and token_answer["token"] == "": questions = [ inquirer.Text("token", message="personal access token") ] token_answer = inquirer.prompt(questions) if token_answer is None: raise ValueError("personal access token not provided") else: parser[ self.section]["private_token"] = token_answer["token"] with open(self.config, "w") as c: parser.write(c) else: parser[self.section]["private_token"] = self.options.token with open(self.config, "w") as c: parser.write(c)
def _load(self, location): """ location -- URL or file location isxsd -- is this a xsd file? """ cachedir = self._cachedir # wsdl2py: deal with XML Schema if not os.path.isdir(cachedir): os.mkdir(cachedir) _file = os.path.join(cachedir, '.cache') section = 'TYPES' cp = ConfigParser() try: cp.readfp(open(_file, 'r')) except IOError: del cp cp = None option = location.replace(':', '-') # colons seem to screw up option if (not self._force and cp is not None and cp.has_section(section) and cp.has_option(section, option)): types = cp.get(section, option) else: # dont do anything to anames if not self._pyclass: containers.ContainerBase.func_aname = lambda instnc, n: str(n) args = ['-o', cachedir, location] if self._lazy: args.insert(0, '-l') if self._pyclass: args.insert(0, '-b') files = commands.wsdl2py(args) if cp is None: cp = ConfigParser() if not cp.has_section(section): cp.add_section(section) types = [f for f in files if f.endswith('_types.py')][0] cp.set(section, option, types) cp.write(open(_file, 'w')) if os.path.abspath(cachedir) not in sys.path: sys.path.append(os.path.abspath(cachedir)) mod = os.path.split(types)[-1].rstrip('.py') return __import__(mod)
def setConnectionFile(self): config = SafeConfigParser() config.read(self.configFile) if not config.has_section('main'): config.add_section('main') config.set('main', 'command_executor', self.driver.command_executor._url) config.set('main', 'session_id', self.driver.session_id) with open(self.configFile, 'w') as f: config.write(f)
def _load(self, location): """ location -- URL or file location isxsd -- is this a xsd file? """ cachedir = self._cachedir # wsdl2py: deal with XML Schema if not os.path.isdir(cachedir): os.mkdir(cachedir) _file = os.path.join(cachedir, '.cache') section = 'TYPES' cp = ConfigParser() try: cp.readfp(open(_file, 'r')) except IOError: del cp; cp = None option = location.replace(':', '-') # colons seem to screw up option if (not self._force and cp is not None and cp.has_section(section) and cp.has_option(section, option)): types = cp.get(section, option) else: # dont do anything to anames if not self._pyclass: containers.ContainerBase.func_aname = lambda instnc,n: str(n) args = ['-o', cachedir, location] if self._lazy: args.insert(0, '-l') if self._pyclass: args.insert(0, '-b') files = commands.wsdl2py(args) if cp is None: cp = ConfigParser() if not cp.has_section(section): cp.add_section(section) types = [f for f in files if f.endswith('_types.py')][0] cp.set(section, option, types) cp.write(open(_file, 'w')) if os.path.abspath(cachedir) not in sys.path: sys.path.append(os.path.abspath(cachedir)) mod = os.path.split(types)[-1].rstrip('.py') return __import__(mod)
def WriteINI(): try: parser = SafeConfigParser() parser.read(Crawler_INI_Name) if parser.has_section("Path") == False: parser.add_section("Path") if parser.has_section("File") == False: parser.add_section("File") if parser.has_section("Data") == False: parser.add_section("Data") if None == Global.GetHomePath() : parser.set("Path", "HomePath", Crawler_Home) if None == Global.GetSystemPath() : parser.set("Path", "SystemPath", Crawler_SYS) if None == Global.GetLogFilePath() : parser.set("Path", "LogFilePath", Crawler_LOG) if None == Global.GetXmlFilePath() : parser.set("Path", "XmlFilePath", Crawler_XML) if None == Global.GetINIFilePath() : parser.set("Path", "INIFilePath", Crawler_INI) if None == Global.GetXmlFileName() : parser.set("File", "XmlFileName", Crawler_XML_Name) if None == Global.GetRankListCount() : parser.set("Data", "RankListCount", "10") if None == Global.GetRelationCount() : parser.set("Data", "RelationCount", "10") if None == Global.GetRelationDepth() : parser.set("Data", "RelationDepth", "1") with open(Crawler_INI_Name, 'w') as configfile: parser.write(configfile) Global.SetHomePath (parser.get("Path", "HomePath" )) Global.SetSystemPath (parser.get("Path", "SystemPath" )) Global.SetLogFilePath (parser.get("Path", "LogFilePath" )) Global.SetXmlFilePath (parser.get("Path", "XmlFilePath" )) Global.SetINIFilePath (parser.get("Path", "INIFilePath" )) Global.SetXmlFileName (parser.get("File", "XmlFileName" )) Global.SetRankListCount (parser.get("Data", "RankListCount")) Global.SetRelationCount (parser.get("Data", "RelationCount")) Global.SetRelationDepth (parser.get("Data", "RelationDepth")) except: return False else: return True
def from_config_file(cls, path, authenticate=True): cfg = SafeConfigParser() cfg.read(path) self = cls.from_settings_dict( dict((k.upper(), v) for k, v in cfg.items("api", raw=True))) if authenticate and cfg.has_section("user"): credentials = [i[1] for i in cfg.items("user", raw=True)] self.login(*credentials) return self
class AospConfig: def __init__(self, config_directory): self.config_directory = config_directory self.config_file = os.path.join(config_directory, 'config') self.dirty = False self.config = SafeConfigParser() self.config.read(self.config_file) def write(self): if not self.dirty: return # Check if dir exists if os.path.isdir(self.config_directory) == False: os.makedirs(self.config_directory) with open(self.config_file, 'w') as f: self.config.write(f) def set(self, section, option, value): if not self.config.has_section(section): self.config.add_section(section) self.config.set(section, option, value) self.dirty = True def get(self, section, option): try: return self.config.get(section, option) except Exception: return None def remove_option(self, section, option): try: self.config.remove_option(section, option) self.dirty = True return True except Exception: return False def remove_section(self, section): try: self.config.remove_section(section) self.dirty = True return True except Exception: return False def remove_configuration(self): self.dirty = False self.config = SafeConfigParser() shutil.rmtree(self.config_directory)
def _mountapps(self): """Create application wise settings, using special section [mountloc], if any. Also parse referred configuration files.""" from pluggdapps.plugin import pluginnames, webapps settings = self.settings # context for parsing ini files. _vars = {'here': abspath(dirname(self.inifile))} # Fetch special section [mountloc]. And override them with [DEFAULT] # settings. cp = SafeConfigParser() cp.read(self.inifile) if cp.has_section('mountloc'): mountloc = cp.items('mountloc', vars=_vars) else: mountloc = [] settings['mountloc'] = dict(mountloc) settings.pop('here', None) # TODO : how `here` gets populated. # Parse mount configuration. appsecs = list(map(h.plugin2sec, webapps())) mountls = [] _skipopts = list(_vars.keys()) + list(settings['DEFAULT'].keys()) for netpath, mounton in mountloc: if netpath in _skipopts: continue parts = [x.strip() for x in mounton.split(',', 1)] appname = parts.pop(0) configini = parts.pop(0) if parts else None appsec = h.plugin2sec(appname) if appsec not in appsecs: raise Exception("%r application not found." % appname) if not configini: raise Exception("configuration file %r not supplied" % configini) if not isfile(configini): raise Exception("configuration file %r not valid" % configini) mountls.append((appsec, netpath, configini)) # Load application configuration from instance configuration file. appsettings = {} for appsec, netpath, instconfig in mountls: appsett = deepcopy(settings) [appsett.pop(k) for k in SPECIAL_SECS] if instconfig: self._loadinstance(appsett, instconfig) appsettings[(appsec, netpath, instconfig)] = appsett return appsettings
def _mountapps(self): """Create application wise settings, using special section [mountloc], if any. Also parse referred configuration files.""" from pluggdapps.plugin import pluginnames, webapps settings = self.settings # context for parsing ini files. _vars = {"here": abspath(dirname(self.inifile))} # Fetch special section [mountloc]. And override them with [DEFAULT] # settings. cp = SafeConfigParser() cp.read(self.inifile) if cp.has_section("mountloc"): mountloc = cp.items("mountloc", vars=_vars) else: mountloc = [] settings["mountloc"] = dict(mountloc) settings.pop("here", None) # TODO : how `here` gets populated. # Parse mount configuration. appsecs = list(map(h.plugin2sec, webapps())) mountls = [] _skipopts = list(_vars.keys()) + list(settings["DEFAULT"].keys()) for netpath, mounton in mountloc: if netpath in _skipopts: continue parts = [x.strip() for x in mounton.split(",", 1)] appname = parts.pop(0) configini = parts.pop(0) if parts else None appsec = h.plugin2sec(appname) if appsec not in appsecs: raise Exception("%r application not found." % appname) if not configini: raise Exception("configuration file %r not supplied" % configini) if not isfile(configini): raise Exception("configuration file %r not valid" % configini) mountls.append((appsec, netpath, configini)) # Load application configuration from instance configuration file. appsettings = {} for appsec, netpath, instconfig in mountls: appsett = deepcopy(settings) [appsett.pop(k) for k in SPECIAL_SECS] if instconfig: self._loadinstance(appsett, instconfig) appsettings[(appsec, netpath, instconfig)] = appsett return appsettings
def config(filename='database.ini', section='postgresql'): parser = SafeConfigParser() parser.read(filename) db = {} if parser.has_section(section): params = parser.items(section) for param in params: db[param[0]] = param[1] else: raise Exception(f'Section {section} not found in {filename} file') return db
def setStarted(self, setBol): config = SafeConfigParser() config.read(self.configFile) if not config.has_section('main'): config.add_section('main') if setBol: pid = os.getpid() config.set('main', 'started', str(pid)) else: if config.has_option('main', 'started'): config.remove_option('main', 'started') with open(self.configFile, 'w') as f: config.write(f)
def _write_missing(path): cp = SafeConfigParser() cp.read(path) for section, values in _options.items(): if not cp.has_section(section): cp.add_section(section) for name, value in values.items(): if not cp.has_option(section, name): cp.set(section, name, str(value.default)) with open(path, "w") as f: cp.write(f)
def merge_write(self, fileobject): """ Update section contents of ``fileobject.name`` by section only. """ scp = SafeConfigParser() # Safe if file doesn't exist scp.read(fileobject.name) # N/B: This won't work with DEFAULTS if not scp.has_section(self._section): scp.add_section(self._section) for key, value in list(self.items()): scp.set(self._section, key, value) scp.write(open(fileobject.name, "w+b")) # truncates file first
async def givecredit(self, ctx, user: discord.Member, amount): """ Used for adding credits to a member's wallet (in case of prizes, etc) """ config = SafeConfigParser() currenttime = datetime.datetime.now() user = str(user.id) config.read('wallet.ini') if config.has_section(user): balance = int(config.get('{}'.format(user), 'balance')) balance = balance + int(amount) balance = str(balance) config.set('{}'.format(user), 'balance', "{}".format(balance)) config.set('{}'.format(user), 'lastused', '{}'.format(currenttime)) with open('wallet.ini', 'w') as f: config.write(f) embed = discord.Embed( title='Added Balance', description='Your balance has been updated successfully!', color=0xFFD000) embed.add_field( name='Balance', value='{} has given you {} credit(s)! Your balance is now {}.'. format(ctx.message.author, amount, balance), inline=True) embed.set_thumbnail(url='https://i.imgur.com/akZqYz8.png') await self.bot.say(embed=embed) else: config.add_section('{}'.format(user)) config.set('{}'.format(user), 'lastused', '{}'.format(currenttime)) credits = int(amount) + 150 credits = str(credits) config.set('{}'.format(user), 'balance', '{}'.format(credits)) with open('wallet.ini', 'w') as f: config.write(f) balance = int(config.get('{}'.format(user), 'balance')) embed = discord.Embed( title='Created Wallet', description= 'Your wallet has been created and updated successfully!', color=0xFFD000) embed.add_field( name='Balance', value='{} has given you {} credit(s)! Your balance is now {}.'. format(ctx.message.author, amount, balance), inline=True) embed.set_thumbnail(url='https://i.imgur.com/akZqYz8.png') await self.bot.say(embed=embed)
def read_server_config(filename='config.ini', section='ServerParams'): parser = SafeConfigParser() parser.read(filename) serverParams = {} if parser.has_section(section): items = parser.items(section) for item in items: serverParams[item[0]] = item[1] else: raise Exception('{0} not found in the {1} file'.format( section, filename)) return serverParams
def __read_config(self): cfg = SafeConfigParser() cfgpath = get_config_file() add_section = False if not os.path.exists(cfgpath): log("Could not find " + cfgpath + ", trying to create one") add_section = True else: cfg.read(cfgpath) try: cfg.get("main", "VSPATH") cfg.get("main", "target_architecture") except: add_section = True if add_section: DIR = os.environ.get("VSINSTALLDIR") if DIR is None: die("Could not work out which studio you are using, make sure you run dobmake.py in a Visual Studio command prompt." ) if target_architecture is None: die("No target_architecture set. Please specify on command line, using the --target switch." ) if not cfg.has_section("main"): cfg.add_section("main") cfg.set("main", "VSPATH", os.path.join(DIR, "Common7", "Tools")) cfg.set("main", "target_architecture", target_architecture) with open(cfgpath, "w") as configfile: cfg.write(configfile) elif target_architecture is not None and cfg.get( "main", "target_architecture") != target_architecture: #if it is set on the command line we want to write that value cfg.set("main", "target_architecture", target_architecture) with open(cfgpath, "w") as configfile: cfg.write(configfile) self.studio = os.path.normcase( os.path.normpath(cfg.get("main", "VSPATH"))) self.arch = cfg.get("main", "target_architecture") if not os.path.isdir(self.studio) or not os.path.isfile( os.path.join(self.studio, os.pardir, os.pardir, "VC", "vcvarsall.bat")): die("Something seems to have happened to your dobmake.ini or Visual Studio installations!" + "\nVSPATH (in dots_generated/dobmake.ini) does not seem to point to a valid path." + "\nTry to delete dots_generated/dobmake.ini and run dobmake.py in a Visual Studio command prompt." )
def save_config(): cfg = get_config() config = SafeConfigParser() config.read(cfg) if not config.has_section('archive'): config.add_section('archive') if not config.has_section('options'): config.add_section('options') if not config.has_section('path'): config.add_section('path') if not config.has_section('sync'): config.add_section('sync') config.set('archive', 'sourcefolder', folder) config.set('archive', 'category', category) config.set('archive', 'storyfolder', sfolder) config.set('archive', 'destfolder', dfolder) config.set('archive', 'textfile', tfile) config.set('options', 'editor', editor) config.set('path', 'path', basePath) config.set('path', 'rootfolder', arcRoot) config.set('path', 'dbfolder', db_folder) config.set('path', 'dbname', db_name) config.set('sync', 'server', sync_server) config.set('sync', 'path', sync_path) if sync_safe: config.set('sync', 'safe', '1') else: config.set('sync', 'safe', '0') with open(cfg, 'w') as f: config.write(f)
def loadGamepad(self,_controllerName): pygame.joystick.init() controller_parser = SafeConfigParser() controller_parser.read(os.path.join(os.path.join(self.datadir.replace('main.exe',''),'settings'),'gamepads.ini')) if controller_parser.has_section(_controllerName): joystick = None for pad in range(pygame.joystick.get_count()): joy = pygame.joystick.Joystick(pad) if joy.get_name() == _controllerName: joystick = joy joystick.init() if joystick: jid = joystick.get_id() else: jid = None axes = {} buttons = {} for opt in controller_parser.options(_controllerName): if opt[0] == 'a': axes[int(opt[1:])] = tuple(controller_parser.get(_controllerName, opt)[1:-1].split(',')) elif opt[0] == 'b': buttons[int(opt[1:])] = controller_parser.get(_controllerName, opt) pad_bindings = engine.controller.PadBindings(_controllerName,jid,axes,buttons) return engine.controller.GamepadController(pad_bindings) else: joystick = None for pad in range(pygame.joystick.get_count()): joy = pygame.joystick.Joystick(pad) if joy.get_name() == _controllerName: joystick = joy joystick.init() if joystick: jid = joystick.get_id() else: jid = None axes = dict() buttons = dict() pad_bindings = engine.controller.PadBindings(_controllerName,jid,axes,buttons) self.setting[joystick.get_name()] = pad_bindings return engine.controller.GamepadController(pad_bindings)
def command(argv=sys.argv[1:]): # Config Files in order of priority config_files = [ 'pagepress.ini', 'source/config.ini', 'source/pagepress.ini', 'pagepress/config.ini', ] bools = ['colour', 'dryrun', 'fail_fast', 'no_logs', 'no_stdout', 'backtrace'] lists = ['locations'] config = {} file_parser = SafeConfigParser() file_parser.read(config_files) if file_parser.has_section('pagepress'): # Tidy up values and keys for key, value in file_parser.items('pagepress'): key = key.strip().replace('-', '_').replace(' ', '_') if key in bools: value = asbool(value) if key in lists: value = value.strip().splitlines() config[key] = value config = process_argv(argv, config) # PagePress Logging # TODO: Let this be configured via ini file handler = logging.StreamHandler() handler.setFormatter(logging.Formatter(LOG_FORMAT)) logger = logging.getLogger('pagepress') logger.addHandler(handler) logger.propagate = False if config['debug'] > 3: logger.setLevel(logging.DEBUG) else: logger.setLevel((4 - config['debug']) * 10) print(config) g = Generator(config) if 'generate'in config['command']: g.update() else: serve(g)
def config_save(): cfg = get_config() config = SafeConfigParser() config.read(cfg) if not config.has_section('path'): config.add_section('path') config.set('path', 'path', basePath) config.set('path', 'rootfolder', arcRoot) config.set('path', 'dbfolder', db_folder) config.set('path', 'dbname', db_name) config.set('path', 'downloadfolder', dl_folder) with open(cfg, 'w') as f: config.write(f)
def saveGamepad(_settings): parser = SafeConfigParser() for controller_name in getSetting().getGamepadList(): gamepad = getSetting(controller_name) if not parser.has_section(controller_name): parser.add_section(controller_name) for key,value in gamepad.key_bindings.axis_bindings.iteritems(): neg,pos = value if not neg: neg = 'none' if not pos: pos = 'none' parser.set(controller_name,'a'+str(key),'('+str(neg)+','+str(pos)+')' ) for key,value in gamepad.key_bindings.button_bindings.iteritems(): parser.set(controller_name,'b'+str(key),str(value)) with open(os.path.join(getSetting().datadir.replace('main.exe',''),'settings','gamepads.ini'), 'w') as configfile: parser.write(configfile)
def loadConfig(cls): """Load the all configuration from the config file.""" config = SafeConfigParser() if os.path.isfile(CONFIG_FILE): with codecs.open(CONFIG_FILE, encoding='utf8') as file_: config.read_file(file_) # The default config cls.config = {} cls.config['command_open'] = None cls.config['player'] = 2 cls.config['debug'] = 0 cls.config['random_duration'] = 0 cls.config['languages'] = 'fr,en' # Load the options if config.has_section('options'): for key, value in config.items('options'): value = None if value == 'None' else value cls.config[key] = value
def saveIniAndDestroy (self): """Writes current information to the ini file (wormBait.ini) and closes the user interface""" config = SafeConfigParser() config.read('wormBait.ini') if not config.has_section('wormBait'): config.add_section('wormBait') dbIds = self.entryList.getValue() if '\n' in dbIds: dbIds = ','.join(dbIds.strip().split('\n')) config.set('wormBait', 'dbIds', dbIds) config.set('wormBait', 'degFile', self.dbFilePath.get()) config.set('wormBait', 'outFile', self.outFilePath.get()) with open('wormBait.ini', 'w') as f: config.write(f) self.destroy()
def read_db_config(filename=config_path, section='mysql'): """ Read database configuration file and return a dictionary object :param filename: name of the configuration file :param section: section of database configuration :return: a dictionary of database parameters """ # create parser and read ini configuration file parser = SafeConfigParser() parser.read(filename) # get section, default to mysql db = {} if parser.has_section(section): items = parser.items(section) for item in items: db[item[0]] = item[1] else: raise Exception('{0} not found in the {1} file'.format(section, filename)) return db
def __init__(self): super(MuninMySQLPlugin, self).__init__() self.default_name = "mobigate" self.dbname = ((sys.argv[0].rsplit('_', 1)[-1] if self.dbname_in_args else None) or os.environ.get('DATABASE') or self.default_name) self.conninfo = dict( user="******", host="localhost", password="******", cursorclass=pymysql.cursors.DictCursor ) cnfpath = "" m = re.findall(r"--defaults-file=([^\s]+)", os.environ.get("mysqlopts") or "") if m: cnfpath = m[0] if not cnfpath: m = re.findall(r"mysql_read_default_file=([^\s;:]+)", os.environ.get("mysqlconnection") or "") if m: cnfpath = m[0] if cnfpath: cnf = SafeConfigParser() cnf.read([cnfpath]) for section in ["client", "munin"]: if not cnf.has_section(section): continue for connkey, opt in [("user", "user"), ("passwd", "password"), ("host", "host"), ("port", "port")]: if cnf.has_option(section, opt): self.conninfo[connkey] = cnf.get(section, opt) for k in ('user', 'passwd', 'host', 'port'): # Use lowercase because that's what the existing mysql plugins do v = os.environ.get(k) if v: self.conninfo[k] = v
def main(settings="settings.ini"): parser = SafeConfigParser() parser.read(settings) settings_dict = {} # Covert the settings.ini settings into a dictionary for later processing if parser.has_section('havocbot'): # Create a bundle of havocbot settings settings_dict['havocbot'] = parser.items('havocbot') else: sys.exit("Could not find havocbot settings in settings.ini") # Configure logging configure_logging(settings_dict) # Get an instance of the bot if it does not exist havocbot = get_bot() # Pass a settings file to the bot havocbot.configure(settings) # Start it. Off we go havocbot.start()
def load_gccxml_configuration( configuration, **defaults ): """ loads GCC-XML configuration from an `.ini` file or any other file class :class:`ConfigParser.SafeConfigParser` is able to parse. :param configuration: configuration could be string( configuration file path ) or instance of :class:`ConfigParser.SafeConfigParser` class :rtype: :class:`.gccxml_configuration_t` Configuration file sceleton:: [gccxml] #path to gccxml executable file - optional, if not provided, os.environ['PATH'] #variable is used to find it gccxml_path= #gccxml working directory - optional, could be set to your source code directory working_directory= #additional include directories, separated by ';' include_paths= #gccxml has a nice algorithms, which selects what C++ compiler to emulate. #You can explicitly set what compiler it should emulate. #Valid options are: g++, msvc6, msvc7, msvc71, msvc8, cl. compiler= """ parser = configuration if isinstance( configuration, str ): try: from configparser import SafeConfigParser except ImportError: from ConfigParser import SafeConfigParser parser = SafeConfigParser() parser.read( configuration ) gccxml_cfg = gccxml_configuration_t() values = defaults if not values: values = {} if parser.has_section( 'gccxml' ): for name, value in parser.items( 'gccxml' ): if value.strip(): values[ name ] = value for name, value in values.items(): if isinstance( value, str ): value = value.strip() if name == 'gccxml_path': gccxml_cfg.gccxml_path = value elif name == 'working_directory': gccxml_cfg.working_directory = value elif name == 'include_paths': for p in value.split( ';' ): p = p.strip() if p: gccxml_cfg.include_paths.append( p ) elif name == 'compiler': gccxml_cfg.compiler = value else: print('\n%s entry was ignored' % name) return gccxml_cfg
class Config(object): def __init__(self, config_dir=None, config_file=None): self.config_dir = config_dir self.config_file = config_file if config_dir is None: self.config_dir = os.path.join(os.environ.get('HOME', './'), '.pyhn') if config_file is None: self.config_file = "config" if not os.path.exists(self.config_dir): os.makedirs(self.config_dir) self.config_path = os.path.join(self.config_dir, self.config_file) self.parser = SafeConfigParser() self.read() def read(self): self.parser.read(self.config_path) # Keybindings if not self.parser.has_section('keybindings'): self.parser.add_section('keybindings') if not self.parser.has_option('keybindings', 'page_up'): self.parser.set('keybindings', 'page_up', 'ctrl d') if not self.parser.has_option('keybindings', 'page_down'): self.parser.set('keybindings', 'page_down', 'ctrl u') if not self.parser.has_option('keybindings', 'up'): self.parser.set('keybindings', 'up', 'j') if not self.parser.has_option('keybindings', 'down'): self.parser.set('keybindings', 'down', 'k') if not self.parser.has_option('keybindings', 'refresh'): self.parser.set('keybindings', 'refresh', 'r,R') if not self.parser.has_option('keybindings', 'open_comments'): self.parser.set('keybindings', 'open_comments', 'c,C') if not self.parser.has_option('keybindings', 'reload_config'): self.parser.set('keybindings', 'reload_config', 'ctrl r,ctrl R') # Paths if not self.parser.has_section('settings'): self.parser.add_section('settings') if not self.parser.has_option('settings', 'cache'): self.parser.set('settings', 'cache', os.path.join(os.environ.get('HOME', './'), '.pyhn', 'cache')) # Colors if not self.parser.has_section('colors'): self.parser.add_section('colors') if not self.parser.has_option('colors', 'body'): self.parser.set('colors', 'body', 'default||standout') if not self.parser.has_option('colors', 'focus'): self.parser.set('colors', 'focus', 'black|light green|underline') if not self.parser.has_option('colors', 'footer'): self.parser.set('colors', 'footer', 'black|light gray') if not self.parser.has_option('colors', 'header'): self.parser.set('colors', 'header', 'dark gray,bold|white|') if not self.parser.has_option('colors', 'title'): self.parser.set('colors', 'title', 'dark red,bold|light gray') if not self.parser.has_option('colors', 'help'): self.parser.set('colors', 'help', 'black,bold|light gray') if not os.path.exists(self.config_path): self.parser.write(open(self.config_path, 'w')) def get_palette(self): palette = [] for item in self.parser.items('colors'): name = item[0] settings = item[1] foreground = "" background = "" monochrome = "" if len(settings.split('|')) == 3: foreground = settings.split('|')[0] background = settings.split('|')[1] monochrome = settings.split('|')[2] elif len(settings.split('|')) == 2: foreground = settings.split('|')[0] background = settings.split('|')[1] elif len(settings.split('|')) == 1: foreground = settings.split('|')[0] palette.append((name,foreground,background,monochrome)) return palette
class Configuration(BaseComponent): """ This component provides a repository for configuration values. The component reads the initial configuration values from an ini-style configuration file when created. During application bootstrap, it intercepts the ``started`` event with a filter with priority 999999. After receiving the ``started`` event, it fires all known configuration values on the ``configuration`` channel as :class:`config_value` events. Then, it re-fires the intercepted started event. Components that depend on configuration values define handlers for ``config_value`` events on the ``configuration`` channel and adapt themselves to the values propagated. Note that due to the intercepted ``started`` event, the initial configuration events are received before the ``startup`` event, so components' configurations can be rearranged before they actually start doing something. Besides initially publishing the stored configuration values, :class:`Configuration` listens for :class:`config_value` events fired by other components, merges them with the already existing configuration values and saves any changes to the configuration file. Other components that are capable of adjusting themselves to changed configuration values should, of course, continue to listen for :class:`config_value` events and adapt their behavior to the changed configuration if possible. If your application requires a different startup behavior, you can also fire an :class:`emit_config` event or call method :meth:`emit_values`. This causes the :class:`Configuration` to emit the configuration values immediately. If this event is received before the ``started`` event, the ``started`` event will not be intercepted. """ channel = "configuration" def __init__(self, filename, initial_config=None, defaults=None, channel=channel): """ The constructor creates a new configuration using the given parameters. :param filename: the name of the file that is used to store the configuration. If the file does not exist it will be created. :type filename: string :param initial_config: a dict of name/section pairs, each section being a dict of option/value pairs that is used to initialize the configuration if no existing configuration file is found :type initial_config: dict of dicts :param defaults: defaults passed to to the :class:`ConfigParser` :param channel: the channel to be used by this :class:`Configuration` (defaults to "configuration") """ super(Configuration, self).__init__(channel=channel) self._emit_done = False self._filename = filename self._config = SafeConfigParser(defaults=defaults) self._config.optionxform = str if os.path.exists(filename): self._config.read(filename) modified = False for section in initial_config: if not self._config.has_section(section): self._config.add_section(section) for option, value in initial_config[section].items(): if not self._config.has_option(section, option): self._config.set(section, option, str(value)) modified = True if modified: with open(filename, "w") as f: self._config.write(f) def emit_values(self): """ Fire all known configuration values as :class:`config_value` events. """ for section in self._config.sections(): for option in self._config.options(section): self.fire(config_value (section, option, self._config.get(section, option))) self._emit_done = True @handler("emit_config") def _on_emit_config(self): self.emit_values() @handler("started", channel="*", filter=True, priority=999999) def _on_started(self, event, component): if not self._emit_done: self.emit_values() self.fire(event, *event.channels) return event.value @handler("config_value") def _on_config_value(self, section, option, value): if self._config.has_option(section, option): if self._config.get(section, option) == str(value): return if not self._config.has_section(section): self._config.add_section(section) self._config.set(section, option, str(value)) with open(self._filename, "w") as f: self._config.write(f) def options(self, section): return self._config.options(section) def get(self, section, option, default=None): if self._config.has_option(section, option): return self._config.get(section, option) else: return default
def load_xml_generator_configuration(configuration, **defaults): """ loads CastXML or GCC-XML configuration from an `.ini` file or any other file class :class:`ConfigParser.SafeConfigParser` is able to parse. :param configuration: configuration could be string(configuration file path) or instance of :class:`ConfigParser.SafeConfigParser` class :rtype: :class:`.xml_generator_configuration_t` Configuration file skeleton:: [gccxml] #path to gccxml or castxml executable file - optional, if not provided, os.environ['PATH'] #variable is used to find it gccxml_path=(deprecated) xml_generator_path= #gccxml working directory - optional, could be set to your source code directory working_directory= #additional include directories, separated by ';' include_paths= #gccxml has a nice algorithms, which selects what C++ compiler to emulate. #You can explicitly set what compiler it should emulate. #Valid options are: g++, msvc6, msvc7, msvc71, msvc8, cl. compiler= # gccxml or castxml xml_generator= # Do we keep xml files or not after errors keep_xml= # Set the path to the compiler compiler_path= """ parser = configuration if utils.is_str(configuration): try: from configparser import SafeConfigParser except ImportError: from ConfigParser import SafeConfigParser parser = SafeConfigParser() parser.read(configuration) # Create a new empty configuration cfg = xml_generator_configuration_t() values = defaults if not values: values = {} if parser.has_section('gccxml'): for name, value in parser.items('gccxml'): if value.strip(): values[name] = value for name, value in values.items(): if isinstance(value, str): value = value.strip() if name == 'gccxml_path': cfg.gccxml_path = value if name == 'xml_generator_path': cfg.xml_generator_path = value elif name == 'working_directory': cfg.working_directory = value elif name == 'include_paths': for p in value.split(';'): p = p.strip() if p: cfg.include_paths.append(p) elif name == 'compiler': cfg.compiler = value elif name == 'xml_generator': cfg.xml_generator = value elif name == 'keep_xml': cfg.keep_xml = value elif name == 'compiler_path': cfg.compiler_path = value else: print('\n%s entry was ignored' % name) # If no compiler path was set and we are using castxml, set the path # Here we overwrite the default configuration done in the cfg because # the xml_generator was set through the setter after the creation of a new # emppty configuration object. cfg.compiler_path = create_compiler_path( cfg.xml_generator, cfg.compiler_path) return cfg
class GlobalSettings(GObject.Object): """ Pitivi app settings. The settings object loads settings from different sources, currently: - the local configuration file, - environment variables. Modules declare which settings they wish to access by calling the addConfigOption() class method during initialization. @cvar options: A dictionnary of available settings. @cvar environment: A list of the controlled environment variables. """ options = {} environment = set() defaults = {} __gsignals__ = {} def __init__(self, **unused_kwargs): GObject.Object.__init__(self) self._config = SafeConfigParser() self._readSettingsFromConfigurationFile() self._readSettingsFromEnvironmentVariables() def _readSettingsFromConfigurationFile(self): """ Read the configuration from the user configuration file. """ try: conf_file_path = os.path.join(xdg_config_home(), "pitivi.conf") self._config.read(conf_file_path) except UnicodeDecodeError: unicode_error_dialog() return except ParsingError: return for (section, attrname, typ, key, env, value) in self.iterAllOptions(): if not self._config.has_section(section): continue if key and self._config.has_option(section, key): if typ == int or typ == int: try: value = self._config.getint(section, key) except ValueError: # In previous configurations we incorrectly stored # ints using float values. value = int(self._config.getfloat(section, key)) elif typ == float: value = self._config.getfloat(section, key) elif typ == bool: value = self._config.getboolean(section, key) else: value = self._config.get(section, key) setattr(self, attrname, value) @classmethod def readSettingSectionFromFile(self, cls, section): """ Force reading a particular section of the settings file. Use this if you dynamically determine settings sections/keys at runtime (like in tabsmanager.py). Otherwise, the settings file would be read only once (at the initialization phase of your module) and your config sections would never be read, and thus values would be reset to defaults on every startup because GlobalSettings would think they don't exist. """ if cls._config.has_section(section): for option in cls._config.options(section): # We don't know the value type in advance, just try them all. try: value = cls._config.getfloat(section, option) except: try: value = cls._config.getint(section, option) except: try: value = cls._config.getboolean(section, option) except: value = cls._config.get(section, option) setattr(cls, section + option, value) def _readSettingsFromEnvironmentVariables(self): """ Override options values using their registered environment variables. """ for section, attrname, typ, key, env, value in self.iterAllOptions(): if not env: # This option does not have an environment variable name. continue var = get_env_by_type(typ, env) if var is not None: setattr(self, attrname, var) def _writeSettingsToConfigurationFile(self): conf_file_path = os.path.join(xdg_config_home(), "pitivi.conf") for (section, attrname, typ, key, env_var, value) in self.iterAllOptions(): if not self._config.has_section(section): self._config.add_section(section) if key: if value is not None: self._config.set(section, key, str(value)) else: self._config.remove_option(section, key) try: file = open(conf_file_path, 'w') except IOError as OSError: return self._config.write(file) file.close() def storeSettings(self): """ Write settings to the user's local configuration file. Note that only those settings which were added with a section and a key value are stored. """ self._writeSettingsToConfigurationFile() def iterAllOptions(self): """ Iterate over all registered options @return: an iterator which yields a tuple of (attrname, type, key, environment, value) for each option. """ for section, options in list(self.options.items()): for attrname, (typ, key, environment) in list(options.items()): yield section, attrname, typ, key, environment, getattr(self, attrname) def isDefault(self, attrname): return getattr(self, attrname) == self.defaults[attrname] def setDefault(self, attrname): setattr(self, attrname, self.defaults[attrname]) @classmethod def addConfigOption(cls, attrname, type_=None, section=None, key=None, environment=None, default=None, notify=False,): """ Add a configuration option. This function should be called during module initialization, before the config file is actually read. By default, only options registered beforehand will be loaded. See mainwindow.py and medialibrary.py for examples of usage. If you want to add configuration options after initialization, use the readSettingSectionFromFile method to force reading later on. See tabsmanager.py for an example of such a scenario. @param attrname: the attribute of this class which represents the option @type attrname: C{str} @param type_: type of the attribute. Unnecessary if default is given. @type type_: a builtin or class @param section: The section of the config file under which this option is saved. This section must have been added with addConfigSection(). Not necessary if key is not given. @param key: the key under which this option is to be saved. Can be none if this option should not be saved. @type key: C{str} @param notify: whether or not this attribute should emit notification signals when modified (default is False). @type notify: C{boolean} """ if section and section not in cls.options: raise ConfigError( "You must add the section \"%s\" first." % section) if key and not section: raise ConfigError( "You must specify a section for key \"%s\"" % key) if section and key in cls.options[section]: raise ConfigError("Option \"%s\" is already in use.") if hasattr(cls, attrname): raise ConfigError("Settings attribute \"%s\" is already in use.") if environment and environment in cls.environment: raise ConfigError("Settings environment varaible \"%s\" is" "already in use.") if not type_ and default is None: raise ConfigError("Settings attribute \"%s\" has must have a" " type or a default." % attrname) if not type_: type_ = type(default) if notify: notification = Notification(attrname) setattr(cls, attrname, notification) setattr(cls, "_" + attrname, default) GObject.signal_new(notification.signame, cls, GObject.SIGNAL_RUN_LAST, None, ()) else: setattr(cls, attrname, default) if section and key: cls.options[section][attrname] = type_, key, environment cls.environment.add(environment) cls.defaults[attrname] = default @classmethod def addConfigSection(cls, section): """ Add a section to the local config file. @param section: The section name. This section must not already exist. @type section: C{str} """ if section in cls.options: raise ConfigError("Duplicate Section \"%s\"." % section) cls.options[section] = {} @classmethod def notifiesConfigOption(cls, attrname): signal_name = Notification.signalName(attrname) GObject.signal_lookup(signal_name, cls)