class SystemProvider(object): def __init__(self, config): self.config = config self.log = kaizen.logging.getLogger(self) self.configparser = RawConfigParser() def load(self, filename=None): self.configparser = RawConfigParser() if not filename: filename = self.config.get("system") if not filename or not os.path.isfile(filename): self.log.debug("no config file found for system povided "\ "dependencies. Config file %r will be created" % \ filename) else: self.log.debug("Reading system provide file %s" % filename) self.configparser.read(filename) if not self.configparser.has_section("provides"): self.log.debug("system config file '%s' has to provides section. "\ "Section will be created." % filename) self.configparser.add_section("provides") def provides(self, name): return self.configparser and self.configparser.has_option("provides", name) def get(self, name): if not self.provides(name): return None version = self.configparser.get("provides", name) return SystemDependency(name, version) def add(self, name, version): if not self.configparser: return if version is None: version = '' self.configparser.set("provides", name, version) def remove(self, name): if not self.configparser: return False return self.configparser.remove_option("provides", name) def save(self, filename=None): if not filename: filename = self.config.get("system") f = open(filename, "w") self.configparser.write(f) f.close() def is_empty(self): return self.configparser is None and \ len(self.configparser.items("provides")) == 0 def list(self): if not self.configparser: return [] return self.configparser.items("provides")
def populate_random(random_file, random_templates=None, saml_info=None): """Populate random.ini Create missing random values according to the template Do not change existing values""" from base64 import b64encode from os import urandom from assembl.auth.make_saml import (make_saml_key, make_saml_cert, cleanup_x509_text) base = Parser() assert random_templates, "Please give one or more templates" for template in random_templates: assert exists(template), "Cannot find template " + template base.read(template) existing = Parser() if exists(random_file): existing.read(random_file) combine_ini(base, existing) saml_keys = {} changed = False for section in base.sections(): for key, value in base.items(section): keyu = key.upper() # too much knowdledge, but hard to avoid if "SAML" in keyu and keyu.endswith("_PRIVATE_KEY"): prefix = keyu[:-12] if value == "{saml_key}": saml_key_text, saml_key = make_saml_key() saml_key_text = cleanup_x509_text(saml_key_text) base.set(section, key, saml_key_text) saml_keys[prefix] = saml_key changed = True else: saml_keys[prefix] = value elif value.startswith('{random') and value.endswith("}"): size = int(value[7:-1]) assert 0 < size < 100 value = b64encode(urandom(size)) base.set(section, key, value) changed = True # Do certs in second pass, to be sure keys are set for section in base.sections(): for key, value in base.items(section): keyu = key.upper() if ("SAML" in keyu and keyu.endswith("_PUBLIC_CERT") and value == '{saml_cert}'): assert saml_info prefix = keyu[:-12] # If key is not there, it IS a mismatch and and error. saml_key = saml_keys[prefix] saml_cert_text, _ = make_saml_cert(saml_key, **saml_info) saml_cert_text = cleanup_x509_text(saml_cert_text) base.set(section, key, saml_cert_text) changed = True if changed: with open(random_file, 'w') as f: base.write(f) return base
def get_config(defaults, section): dir_root = get_config_dir() if dir_root is None: return {} configdir = os.path.join(dir_root, '.bittorrent') if not os.path.isdir(configdir): try: os.mkdir(configdir, 0700) except: pass p = RawConfigParser() p.read(os.path.join(configdir, 'config')) values = {} if p.has_section(section): for name, value in p.items(section): if name in defaults: values[name] = value if p.has_section('common'): for name, value in p.items('common'): if name in defaults and name not in values: values[name] = value if defaults.get('data_dir') == '' and \ 'data_dir' not in values and os.path.isdir(configdir): datadir = os.path.join(configdir, 'data') values['data_dir'] = datadir parseargs.parse_options(defaults, values) return values
def __init__(self, base): syncdir = os.path.dirname(base) self.topdir = os.path.split(syncdir)[0] if 'WEAVE_TESTFILE' in os.environ: test_filename = 'tests_%s.ini' % os.environ['WEAVE_TESTFILE'] else: test_filename = 'tests.ini' while True: ini_file = os.path.join(self.topdir, test_filename) if os.path.exists(ini_file): break if ini_file == ("/%s" % test_filename) \ or ini_file == test_filename: raise IOError("cannot locate %s" % test_filename) self.topdir = os.path.split(self.topdir)[0] cfg = RawConfigParser() cfg.read(ini_file) # loading loggers if cfg.has_section('loggers'): fileConfig(ini_file) here = {'here': os.path.dirname(os.path.realpath(ini_file))} config = dict([ (key, value % here) for key, value in cfg.items('DEFAULT') + cfg.items('app:main') ]) self.config = convert_config(config)
def load_config(path): config = {} defaults = resource_stream("tiktok.config", "defaults.cfg") parser = RawConfigParser(allow_no_value=True) parser.readfp(defaults, "defaults.cfg") if os.path.exists(path): parser.read(path) sections = [x for x in parser.sections() if x != "general"] config.update(dict(parser.items("general"))) for section in sections: config[section] = dict(parser.items(section)) config["config_dir"] = CONFIG_DIR task_properties = zip( [int(x) for x in config["task"]["property_ids"].split(",")], [int(x) for x in config["task"]["property_values"].split(",")], ) config["task_properties"] = task_properties return config
def parse_config(filename, dirs=None): if dirs: filenames = [os.path.join(d, filename) for d in dirs] else: filenames = [filename] config = RawConfigParser() n = config.read(filenames) if not len(n) >= 1: raise PkgNotFound("Could not find file(s) %s" % str(filenames)) # Parse meta and variables sections meta = parse_meta(config) vars = {} if config.has_section('variables'): for name, value in config.items("variables"): vars[name] = _escape_backslash(value) # Parse "normal" sections secs = [s for s in config.sections() if not s in ['meta', 'variables']] sections = {} requires = {} for s in secs: d = {} if config.has_option(s, "requires"): requires[s] = config.get(s, 'requires') for name, value in config.items(s): d[name] = value sections[s] = d return meta, vars, sections, requires
def parse_config(filename, dirs=None): if dirs: filenames = [os.path.join(d, filename) for d in dirs] else: filenames = [filename] config = RawConfigParser() n = config.read(filenames) if not len(n) >= 1: raise PkgNotFound("Could not find file(s) %s" % str(filenames)) # Parse meta and variables sections meta = parse_meta(config) vars = {} if config.has_section('variables'): for name, value in config.items("variables"): vars[name] = _escape_backslash(value) # Parse "normal" sections secs = [s for s in config.sections() if not s in ['meta', 'variables']] sections = {} requires = {} for s in secs: d = {} if config.has_option(s, "requires"): requires[s] = config.get(s, 'requires') for name, value in config.items(s): d[name] = value sections[s] = d return meta, vars, sections, requires
def __init__(self, base): syncdir = os.path.dirname(base) self.topdir = os.path.split(syncdir)[0] if 'WEAVE_TESTFILE' in os.environ: test_filename = 'tests_%s.ini' % os.environ['WEAVE_TESTFILE'] else: test_filename = 'tests.ini' while True: ini_file = os.path.join(self.topdir, test_filename) if os.path.exists(ini_file): break if ini_file == ("/%s" % test_filename) \ or ini_file == test_filename: raise IOError("cannot locate %s" % test_filename) self.topdir = os.path.split(self.topdir)[0] cfg = RawConfigParser() cfg.read(ini_file) # loading loggers if cfg.has_section('loggers'): fileConfig(ini_file) here = {'here': os.path.dirname(os.path.realpath(ini_file))} config = dict([(key, value % here) for key, value in cfg.items('DEFAULT') + cfg.items('app:main')]) self.config = convert_config(config)
def load_settings(config_file): config = RawConfigParser() if not config.read(config_file): raise Exception('Not a valid config file: {0!r}'.format(config_file)) if config.has_section('app:main_helper'): settings = dict(config.items('app:main_helper')) else: settings = dict(config.items('app:main')) return settings
def __init__(self, scfg, bcfg, bindir, boardfilter=None): assert os.path.exists(scfg) # do not check bcfg, it might be optional # retrieve setup data from setup.cfg cfg_setup = RawConfigParser() cfg_setup.read(scfg) setup = {} for sec in cfg_setup.sections(): d = dict(cfg_setup.items(sec)) # skip all sections that have not a "board" key. if 'board' in d: d['name'] = sec setup[sec] = d self.buildcmds = ["scons %(targets)s"] # if board config is invalid till here, try to get it from setupcfg if not os.path.exists(str(bcfg)): bcfg = None # read config section of the tool if cfg_setup.has_section("hbm251.py"): for k, v in cfg_setup.items("hbm251.py"): if k == "bindir": # overwrite function parameter to be used later bindir = v elif k == "boardcfg" and bcfg == None: bcfg = v elif k == "buildcmd": self.buildcmds = v.split("\n") # get a flat board dictionary cfg_board = RawConfigParser() if os.path.exists(bcfg): cfg_board.read(bcfg) boards = {} for b, v in [(s, dict(cfg_board.items(s))) for s in cfg_board.sections()]: boards[b] = v for a in v.get("aliases", "").split(): boards[a.strip()] = v # update setup with needed data from board.cfg self.boards = [] for b, v in setup.items(): # update the board from board.cfg with the section # from config-file, this ensures priorization of # config-file parameters overwriting board parameters brd = boards[v['board']] brd.update(v) brd.update({'bindir': bindir}) if boardfilter == None or v[ 'board'] in boardfilter or b in boardfilter: self.boards.append(Board(brd))
def readconf(conf_path, section_name=None, log_name=None, defaults=None, raw=False): """ Read config file(s) and return config items as a dict :param conf_path: path to config file/directory, or a file-like object (hasattr readline) :param section_name: config section to read (will return all sections if not defined) :param log_name: name to be used with logging (will use section_name if not defined) :param defaults: dict of default values to pre-populate the config with :returns: dict of config items """ if defaults is None: defaults = {} if raw: c = RawConfigParser(defaults) else: c = ConfigParser(defaults) if hasattr(conf_path, 'readline'): c.readfp(conf_path) else: if os.path.isdir(conf_path): # read all configs in directory success = read_conf_dir(c, conf_path) else: success = c.read(conf_path) if not success: print _("Unable to read config from %s") % conf_path sys.exit(1) if section_name: if c.has_section(section_name): conf = dict(c.items(section_name)) else: print _("Unable to find %s config section in %s") % \ (section_name, conf_path) sys.exit(1) if "log_name" not in conf: if log_name is not None: conf['log_name'] = log_name else: conf['log_name'] = section_name else: conf = {} for s in c.sections(): conf.update({s: dict(c.items(s))}) if 'log_name' not in conf: conf['log_name'] = log_name conf['__file__'] = conf_path return conf
def test_init_admin_repository(): tmp = maketemp() admin_repository = os.path.join(tmp, 'admin.git') pubkey = ( 'ssh-somealgo ' +'0123456789ABCDEFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' +'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' +'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' +'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= fakeuser@fakehost') user = '******' init.init_admin_repository( git_dir=admin_repository, pubkey=pubkey, user=user, ) eq(os.listdir(tmp), ['admin.git']) hook = os.path.join( tmp, 'admin.git', 'hooks', 'post-update', ) util.check_mode(hook, 0755, is_file=True) got = util.readFile(hook).splitlines() assert 'gitosis-run-hook post-update' in got export_dir = os.path.join(tmp, 'export') repository.export(git_dir=admin_repository, path=export_dir) eq(sorted(os.listdir(export_dir)), sorted(['gitosis.conf', 'keydir'])) eq(os.listdir(os.path.join(export_dir, 'keydir')), ['jdoe.pub']) got = util.readFile( os.path.join(export_dir, 'keydir', 'jdoe.pub')) eq(got, pubkey) # the only thing guaranteed of initial config file ordering is # that [gitosis] is first got = util.readFile(os.path.join(export_dir, 'gitosis.conf')) got = got.splitlines()[0] eq(got, '[gitosis]') cfg = RawConfigParser() cfg.read(os.path.join(export_dir, 'gitosis.conf')) eq(sorted(cfg.sections()), sorted([ 'gitosis', 'group gitosis-admin', ])) eq(cfg.items('gitosis'), []) eq(sorted(cfg.items('group gitosis-admin')), sorted([ ('writable', 'gitosis-admin'), ('members', 'jdoe'), ]))
def read_config(filename = None): """ Reads the config file at filename (or at the default location if filename is ``None``). """ global IGNORED_BUNDLES global LAYOUT global MIN_SIZES global HOTKEYS if filename is None: filename = os.path.join(get_config_dir(), __config_file__) if not os.path.exists(filename): copy_config(internal = True) config = RawConfigParser() config.read(filename) IGNORED_BUNDLES = ast.literal_eval(config.get('General', 'ignored_bundles')) for name, value in config.items('Minimum Sizes'): MIN_SIZES[name] = ast.literal_eval(value) # Load arbitrary hotkeys (mod + [, mod...] + keycode) for name, value in config.items('HotKeys'): entries = value.split(' ') length = len(entries) if length < 2: raise Exception('Bad config file. Hotkeys must have >= 2 entries, ex. \'alt shift 16\'.') hotkey = [0, int(entries[length - 1])] # [mod_mask, keycode] for i in entries[:length - 2]: hotkey[0] |= int(__mod_masks__[i]) HOTKEYS[name] = hotkey logging.debug('Hotkey registered for \'%s\': %d, %d', name, hotkey[0], hotkey[1]) # Load layout class and use the rest of the items in the constructor classname = None layout_dict = dict() for name, value in config.items('Layout'): if name == 'class': classname = value else: layout_dict[name] = ast.literal_eval(value) if classname is None: raise Exception('Bad config file. Layout section must have a class.') name = classname.rsplit('.', 1) layout_module = __import__(name[0], globals(), locals(), [], -1) layout_class = getattr(layout_module, name[1]) LAYOUT = layout_class(**layout_dict)
def readconf( conffile, section_name=None, log_name=None, defaults=None, raw=False, ): """ Read config file and return config items as a dict :param conffile: path to config file, or a file-like object (hasattr readline) :param section_name: config section to read (will return all sections if not defined) :param log_name: name to be used with logging (will use section_name if not defined) :param defaults: dict of default values to pre-populate the config with :returns: dict of config items """ if defaults is None: defaults = {} if raw: c = RawConfigParser(defaults) else: c = ConfigParser(defaults) if hasattr(conffile, 'readline'): c.readfp(conffile) else: if not c.read(conffile): print _('Unable to read config file %s') % conffile sys.exit(1) if section_name: if c.has_section(section_name): conf = dict(c.items(section_name)) else: print _('Unable to find %s config section in %s') \ % (section_name, conffile) sys.exit(1) if 'log_name' not in conf: if log_name is not None: conf['log_name'] = log_name else: conf['log_name'] = section_name else: conf = {} for s in c.sections(): conf.update({s: dict(c.items(s))}) if 'log_name' not in conf: conf['log_name'] = log_name conf['__file__'] = conffile return conf
def readconf( conffile, section_name=None, log_name=None, defaults=None, raw=False, ): """ Read config file and return config items as a dict :param conffile: path to config file, or a file-like object (hasattr readline) :param section_name: config section to read (will return all sections if not defined) :param log_name: name to be used with logging (will use section_name if not defined) :param defaults: dict of default values to pre-populate the config with :returns: dict of config items """ if defaults is None: defaults = {} if raw: c = RawConfigParser(defaults) else: c = ConfigParser(defaults) if hasattr(conffile, 'readline'): c.readfp(conffile) else: if not c.read(conffile): print _('Unable to read config file %s') % conffile sys.exit(1) if section_name: if c.has_section(section_name): conf = dict(c.items(section_name)) else: print _('Unable to find %s config section in %s') \ % (section_name, conffile) sys.exit(1) if 'log_name' not in conf: if log_name is not None: conf['log_name'] = log_name else: conf['log_name'] = section_name else: conf = {} for s in c.sections(): conf.update({s: dict(c.items(s))}) if 'log_name' not in conf: conf['log_name'] = log_name conf['__file__'] = conffile return conf
def from_file(cls, file, app_name=DEFAULT_APP_NAME): """Initialize the class from an INI file.""" settings = {} global_settings = {} with open(file, 'r') as f: config = RawConfigParser() config.readfp(f) for section in config.sections(): if section == app_name: settings.update(config.items(section)) else: global_settings[section] = dict(config.items(section)) return cls(settings, global_settings)
class KETIDriver(SmapDriver): CHANNELS = {'temperature': ('C', 'double'), 'humidity': ('%RH', 'double'), 'illumination': ('lx', 'long'), 'pir': ('#', 'long'), 'co2': ('ppm', 'long')} def setup(self, opts): self.port = opts.get('UDP_port', 7000) self.s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM) self.s.setblocking(False) self.s.bind(('', self.port)) self.config = RawConfigParser() self.config.read(opts.get('config','motes.cfg')) self.monitor = None self.socker = None globalmetadata = self.config.items('global') globalmetadata = dict(((k.title(), v) for k,v in globalmetadata)) self.set_metadata('/', globalmetadata) for mote in self.config.sections(): if mote == 'global': continue moteid = str(mote.split(':')[-1]) motetype = self.config.get(mote, 'type') metadata = dict(((k.title(), v) for k,v in self.config.items(mote))) metadata.update({'moteid': moteid}) if motetype == 'TH': self.add_timeseries('/' + str(moteid) + '/temperature', self.CHANNELS['temperature'][0], data_type=self.CHANNELS['temperature'][1]) self.add_timeseries('/' + str(moteid) + '/humidity', self.CHANNELS['humidity'][0], data_type=self.CHANNELS['humidity'][1]) self.add_timeseries('/' + str(moteid) + '/illumination', self.CHANNELS['illumination'][0], data_type=self.CHANNELS['illumination'][1]) self.set_metadata('/' + str(moteid) + '/temperature', metadata) self.set_metadata('/' + str(moteid) + '/humidity', metadata) self.set_metadata('/' + str(moteid) + '/illumination', metadata) elif motetype == 'CO2': self.add_timeseries('/' + str(moteid) + '/co2', self.CHANNELS['co2'][0], data_type=self.CHANNELS['co2'][1]) self.set_metadata('/' + str(moteid) + '/co2', metadata) elif motetype == 'PIR': self.add_timeseries('/' + str(moteid) + '/pir', self.CHANNELS['pir'][0], data_type=self.CHANNELS['pir'][1]) self.set_metadata('/' + str(moteid) + '/pir', metadata) def start(self): self.monitor = Monitor(self) self.socket = reactor.adoptDatagramPort(self.s.fileno(), socket.AF_INET6, self.monitor) self.s.close()
def merge(master, updates, outfile, **kwargs): if not master: master = _find_master() if not isinstance(updates, (list, tuple)): updates = [updates] print "Merging files: %s and %s" % (master, ', '.join(updates)) parser = RawConfigParser() parser.read(updates) with open(master, 'r') as orig: with open(outfile, 'w') as new: current_section = None for line in orig: sec_m = re.match("^\[([\w\d_\-\s]+)\]\s*", line, re.IGNORECASE) if sec_m: current_section = sec_m.group(1) new.write(line) else: if not parser.has_section(current_section): new.write(line) continue var_m = re.match("^(?:;)?([\w\d\_\-\.]+)\s*=\s*(.*)\n$", line, re.IGNORECASE) if var_m: key, value = var_m.groups() if parser.has_option(current_section, key): new_value = parser.get(current_section, key) # print "REPLACING: %s = %s with value %s" % (key, value, new_value) new.write("%s = %s\n" % (key, new_value % kwargs)) parser.remove_option(current_section, key) if not parser.items(current_section): parser.remove_section(current_section) else: new.write(line) else: new.write(line) if parser.sections(): #print "The following values were not set:" for s in parser.sections(): new.write("") new.write("[%s]\n" % s) for t in parser.items(s): new.write("%s = %s\n" % t)
def parse_rc(file): defaults = {} rc = RawConfigParser() rc.optionxform = optionxform rc.read(file) if rc.has_section('core'): parser.set_defaults(**{ k: v for (k,v) in rc.items('core') }) if rc.has_section('tag'): mtag.subparser.set_defaults(**{ k: v for (k,v) in rc.items('tag') }) if rc.has_section('sort'): msort.subparser.set_defaults(**{ k: v for (k,v) in rc.items('sort') }) if rc.has_section('stat'): mstat.subparser.set_defaults(**{ k: v for (k,v) in rc.items('stat') }) return defaults
def merge(master, updates, outfile, **kwargs): if not master: master = _find_master() if not isinstance(updates, (list, tuple)): updates = [updates] print "Merging files: %s and %s" % (master, ', '.join(updates)) parser = RawConfigParser() parser.read(updates) with open(master, 'r') as orig: with open(outfile, 'w') as new: current_section=None for line in orig: sec_m = re.match("^\[([\w\d_\-\s]+)\]\s*", line, re.IGNORECASE) if sec_m: current_section=sec_m.group(1) new.write(line) else: if not parser.has_section(current_section): new.write(line) continue var_m = re.match("^(?:;)?([\w\d\_\-\.]+)\s*=\s*(.*)\n$", line, re.IGNORECASE) if var_m: key, value = var_m.groups() if parser.has_option(current_section, key): new_value = parser.get(current_section, key) # print "REPLACING: %s = %s with value %s" % (key, value, new_value) new.write("%s = %s\n" % (key, new_value % kwargs)) parser.remove_option(current_section, key) if not parser.items(current_section): parser.remove_section(current_section) else: new.write(line) else: new.write(line) if parser.sections(): #print "The following values were not set:" for s in parser.sections(): new.write("") new.write("[%s]\n" % s) for t in parser.items(s): new.write("%s = %s\n" % t)
def handle_server_config_file(self, cnf_path, server, special_processing_reqs, desired_server_options): # We have a config reader so we can do # special per-server magic for setting up more # complex scenario-based testing (eg we use a certain datadir) config_reader = RawConfigParser() config_reader.read(cnf_path) # Do our checking for config-specific madness we need to do if config_reader and config_reader.has_section(server.name): # mark server for restart in case it hasn't yet # this method is a bit hackish - need better method later if '--restart' not in desired_server_options: desired_server_options.append('--restart') # We handle various scenarios server_config_data = config_reader.items(server.name) for cnf_option, data in server_config_data: if cnf_option == 'load-datadir': datadir_path = data request_key = 'datadir_requests' if request_key not in special_processing_reqs: special_processing_reqs[request_key] = [] special_processing_reqs[request_key].append( (datadir_path, server))
def __config_to_dict(conf_fp): config_parser = RawConfigParser() config_parser.read(conf_fp) config = {} # shortcut, but everything comes in as a str [config.__setitem__(section, dict(config_parser.items(section))) for section in config_parser.sections()] # convert bools b = bool(int(config['loris.Loris']['enable_caching'])) config['loris.Loris']['enable_caching'] = b b = bool(int(config['loris.Loris']['redirect_conneg'])) config['loris.Loris']['redirect_conneg'] = b b = bool(int(config['loris.Loris']['redirect_base_uri'])) config['loris.Loris']['redirect_base_uri'] = b b = bool(int(config['loris.Loris']['redirect_cannonical_image_request'])) config['loris.Loris']['redirect_cannonical_image_request'] = b b = bool(int(config['loris.Loris']['enable_cors'])) config['loris.Loris']['enable_cors'] = b # convert lists l = map(string.strip, config['loris.Loris']['cors_whitelist'].split(',')) config['loris.Loris']['cors_whitelist'] = l # convert transforms.*.target_formats to lists for tf in __transform_sections_from_config(config): config[tf]['target_formats'] = [s.strip() for s in config[tf]['target_formats'].split(',')] return config
def test_imap_config_values_should_be_stored(): am = AccountManager() option_spec = get_mailbox_parameter_specs('imap') options = { 'user': '******', 'password': '', 'server': 'imap.example.org', 'port': '', 'ssl': True, 'imap': True, 'idle': True, 'folders': ['a', 'b'], } config = RawConfigParser() config.add_section('account1') am._set_cfg_options(config, 'account1', options, option_spec) expected_config_items = [ ('user', 'you'), ('password', ''), ('server', 'imap.example.org'), ('port', ''), ('ssl', '1'), ('imap', '1'), ('idle', '1'), ('folder', '["a", "b"]'), ] assert set(expected_config_items) == set(config.items('account1'))
def load(*cfgs, **kwargs): '''Load betterconfig config files into a python dict. cfgs - iterable of file-ish types (file, StringIO, etc) include='include' - name of the 'include' directive in a betterconfig file, to disable includes, send None default='default' - name used for default section, whose values are stored that the top level of the returned python dictionary. seen=set() - a set of seen cfg identifiers (inodes), to prevent infinitely recursive includes. raw=True - enables interpolation of %(tokens)s when falsy mapper=dict - type to use for the mapping.''' opts = { 'include': kwargs.pop('include', 'include'), 'default': kwargs.pop('default', '_'), 'seen': kwargs.pop('seen', set()), 'raw': kwargs.pop('raw', True), 'mapper': kwargs.pop('mapper', dict) } if kwargs: raise TypeError('{} is an invalid keyword argument'\ 'for this function'.format(kwargs.keys()[0])) compiled_config = opts['mapper']() for cfg in cfgs: includes = [] with _Sectionless(cfg, opts['default']) as cfg_file: # prevent infinite include recursion id_, cfg_dir = _id_and_dir(cfg_file) if id_ in opts['seen']: continue elif id_ is not None: opts['seen'].add(id_) parser = RawConfigParser() if opts['raw'] else SafeConfigParser() parser.optionxform = lambda x: x #ConfigParser calls lower() on each key. parser.readfp(cfg_file) for sect in parser.sections(): sect_config = compiled_config if sect != opts['default']: sect_config = compiled_config.setdefault(sect, {}) for key, val in parser.items(sect): val = ast.literal_eval(val) if sect == opts['default'] and key == opts['include']: includes.extend(_expand_includes(val, cfg_dir)) else: sect_config[key] = val # after we've finished one file, overlay any includes if includes: compiled_config.update(load(*includes, **opts)) return compiled_config
class OriginAuthStore(object): def __init__(self, config_file): self.config_file = config_file self.config = RawConfigParser() self.config.read(config_file) def origin(self, name): return OriginAuth(self, name) def __getitem__(self, origin_name): try: return dict(self.config.items(origin_name)) except NoSectionError: return {} def __setitem__(self, origin_name, auth): try: self.config.remove_section(origin_name) except NoSectionError: pass if auth: self.config.add_section(origin_name) for key, val in auth.iteritems(): self.config.set(origin_name, key, val) with open(self.config_file, 'w') as f: self.config.write(f) try: os.chmod(self.config_file, stat.S_IRUSR | stat.S_IWUSR) except OSError: print 'Unable to chmod 600 %s' % self.config_file # TODO: Test
class Config: """ Config stores the configuration and reads it from a file. It's class with shared state across all instances (the Borg pattern) """ __shared_state = {} def __init__(self): self.__dict__ = self.__shared_state self.config = None def read_config_file(self, config_file=os.path.join(BASEDIR, CONFIG_FILE_NAME)): self.config = RawConfigParser() try: self.config.readfp( open(os.path.join(BASEDIR, DEFAULT_CONFIG_FILE_NAME), 'r')) self.config.readfp(open(config_file, 'r')) except (OSError, IOError): LOG.exception('Could not open configuration file') raise ConfigurationError('Could not open configuration file') def get(self, section, option): if self.config is None: self.read_config_file() return self.config.get(section, option) def items(self, section): if self.config is None: self.read_config_file() return self.config.items(section)
def read(self, path): """ Reads the config file specified by 'path' then reads all the files in the directory obtained by adding '.d' to 'path'. The files in the '.d' directory are read in normal sorted order and section entries in these files override entries in the main file. """ if os.path.exists(path): RawConfigParser.readfp(self, codecs.open(path, 'r', 'utf-8')) path_d = path + ".d" files = [] if os.path.exists(path_d): files = [ os.path.join(path_d, f) for f in os.listdir(path_d) ] files.sort() for fname in files: p = RawConfigParser() p.readfp(codecs.open(fname, 'r', 'utf-8')) for section_name in p.sections(): # New files override old, so remove first to avoid DuplicateSectionError. self.remove_section(section_name) self.add_section(section_name) for (name, value) in p.items(section_name): self.set(section_name, name, value) # Store the filename this section was read from. self.set(section_name, '_filename_', fname)
def do_10_install_packages(): """Install or remove packages (as per debfix/debfix-packages.conf""" from ConfigParser import RawConfigParser config = RawConfigParser(allow_no_value=True) config.read(data_dir + 'debfix-packages.conf') sections = config.sections() run('apt-get -y -q update') run('apt-get -y -q --allow-unauthenticated install aptitude debian-archive-keyring deb-multimedia-keyring') run('apt-get -y -q update') if user_choice('Upgrade all upgradable packages'): run('aptitude -y -q full-upgrade') marked = {'install':'', 'remove':'', 'sections':''} for section in sections: question = "{} packages from '{}' section".format('Install' if section != 'remove' else 'Remove', section) packages = ' '.join(i[0] for i in config.items(section)) while True: choice = user_choice(question, other_choices='?') if choice == '?': log.info("Section '{}' contains packages: {}".format(section, packages)) continue if choice: marked['sections'] += section + ' ' if section == 'remove': marked['remove'] += packages + ' ' else: marked['install'] += packages + ' ' break if user_choice('Install: {install}\nRemove: {remove}\nApply changes'.format(**marked)): _apt_install_packages(marked) # due to assume-yes-based decisions, some packages may not be successfully installed (yet), retry _apt_install_packages(marked, second_time_around=True) run('aptitude -y -q clean') log.info('Done installing packages')
def configparse(self,client_name,platformname): logger_test.debug("parsing the configuration file for processing the data") try: parser =RawConfigParser() parser.read('config.ini') print parser.sections() if str.lower(platformname)=="twitter": clientname=platformname+"_"+client_name m=parser.items(clientname) #print "m",m consumer_key=m[0][1] consumer_secret=m[1][1] access_token_key=m[2][1] access_token_secret=m[3][1] brokerlist=m[4][1].split(",") topic=m[5][1] return client_name,consumer_key,consumer_secret,access_token_key,access_token_secret,brokerlist,topic else: logger_test.info("No such(%s) platform exist "%platformname) except Exception,e: logger_test.exception("error occured while parsing the congig file with exception :%s"%str(e)) sys.exit("error ocuured while parsing")
def read_ini(ini): """Read CAMB ini into dictionary""" if os.path.exists(ini): ini = open(ini).read() config = RawConfigParser() config.optionxform = str config.readfp(StringIO('[root]\n' + ini)) return dict(config.items('root'))
def retrieve_password(self): # print the title Header().title_debug('Wifi (from Network Manager)') directory = '/etc/NetworkManager/system-connections' if os.path.exists(directory): if os.getuid() != 0: print_debug('INFO', 'You need more privileges (run it with sudo)\n') wireless_ssid = [ f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory,f))] pwdFound = [] for w in wireless_ssid: cp = RawConfigParser() cp.read(os.path.join(directory, w)) values = {} values['SSID'] = w if cp.sections(): for section in cp.sections(): if 'wireless' in section: for i in cp.items(section): values[i[0]] = i[1] # write credentials into a text file if len(values) != 0: pwdFound.append(values) # print the results print_output('Wifi', pwdFound) else: print_debug('WARNING', 'the path "%s" does not exist' %(directory))
def get_config(config_dir, filename): """Parses <filename> from <config_dir> and returns configuration. <filename> is assumed to be in `ini` format, and is parsed by RawConfigParser (instead of ConfigParser or SafeConfigParser) to avoid accidental '%' interpolation when an option's value includes '%' character (such as password strings). Please see more details at: https://docs.python.org/2/library/configparser.html If "[include]" section exists in <filename> and the value of "FILE_NAME" option in this section is not empty, also read included file from <config_dir> directory. The final configuration will be a combination of these two files. For options that are specified in both <filename> and included file, values in <filename> will always take precedence. """ config = RawConfigParser() config.read(normpath(join(config_dir, filename))) # If "[include]" section exists, and its "FILE_NAME" option has a # non-empty value, parse the included file as secondary configuration. if config.has_section('include') and config.get('include', 'FILE_NAME'): included_filename = config.get('include', 'FILE_NAME') secondary_config = RawConfigParser() secondary_config.read(normpath(join(config_dir, included_filename))) for section in secondary_config.sections(): if not config.has_section(section): config.add_section(section) for option, value in secondary_config.items(section): if not config.has_option(section, option): config.set(section, option, value) return config
def edit_profilevars(self): config = ProfileVariablesConfig(self.conn, self.current.profile) tmp, path = tempfile.mkstemp('variable', 'paella') tmp = file(path, 'w') config.write(tmp) tmp.close() os.system('$EDITOR %s' %path) tmp = file(path, 'r') newconfig = RawConfigParser() newconfig.readfp(tmp) tmp.close() os.remove(path) cursor = self.variables.env.cursor pclause = Eq('profile', self.current.profile) for trait in config.sections(): tclause = pclause & Eq('trait', trait) if not newconfig.has_section(trait): cursor.delete(clause=tclause) else: for name, value in newconfig.items(trait): nclause = tclause & Eq('name', name) if config.has_option(trait, name): if value != config.get(trait, name): cursor.update(data={'value' : value}, clause=nclause) else: idata = { 'profile' : self.current.profile, 'trait' : trait, 'name' : name, 'value' : value} cursor.insert(data=idata) if config.has_section(trait): for name, value in config.items(trait): if not newconfig.has_option(trait, name): cursor.delete(clause=tclause & Eq('name', name)) self.select_profile(self.current.profile)
def test_imap_config_values_should_be_stored(): am = AccountManager() option_spec = get_mailbox_parameter_specs('imap') options = { 'user': '******', 'password': '', 'server': 'imap.example.org', 'port': '', 'ssl': True, 'imap': True, 'idle': True, 'folders': ['a', 'b'], } config = RawConfigParser() config.add_section('account1') am._set_cfg_options(config, 'account1', options, option_spec) expected_config_items = [ ('user', 'you'), ('password', ''), ('server', 'imap.example.org'), ('port', ''), ('ssl', '1'), ('imap', '1'), ('idle', '1'), ('folder', '["a", "b"]'), ] assert set(expected_config_items) == set(config.items('account1'))
def ini_to_context(filenames=CLEVERCSS_CONTEXTFILES): """Loads a config.ini-formatted file at filename and returns a flat context object that always returns strings for both keys and values (e.g. no natives) -- see ConfigParser.RawConfigParser in the standard library for format details """ context = {} # If a single filename was provided, recast as an iterable now if isinstance(filenames, (str, unicode)): filenames = (filenames, ) for filename in filenames: cparser = RawConfigParser() try: fob = open(filename, 'rb') except IOError, msg: raise # Read in our configuration file cparser.readfp(fob, filename) sections = cparser.sections() for section in sections: items = cparser.items(section) for item in items: context[item[0]] = item[1]
def config(argv=sys.argv): argparser = ArgumentParser() argparser.add_argument( '--no-comments', dest='no_comments', action='store_true', help="Don't include settings description in comments") argparser.add_argument('--preseed', metavar='file.ini', default=None, help="Presets file") args = argparser.parse_args(argv[1:]) # trying to fix utf-8 sys.stdout = codecs.getwriter('utf-8')(sys.stdout) from .component import Component, load_all load_all() preseedcfg = RawConfigParser() if args.preseed: with codecs.open(args.preseed, 'r', 'utf-8') as fd: preseedcfg.readfp(fd) for comp in Component.registry: if hasattr(comp, 'settings_info'): print u'[%s]' % comp.identity if not args.no_comments: print u'' preseedsect = dict( preseedcfg.items(comp.identity) if preseedcfg. has_section(comp.identity) else ()) preseedkeys = set() for s in comp.settings_info: if not args.no_comments and 'desc' in s: print u'# %s ' % s['desc'] if s['key'] in preseedsect: print '%s = %s' % (s['key'], preseedsect[s['key']]) preseedkeys.add(s['key']) elif 'default' in s: print '%s = %s' % (s['key'], s['default']) elif not args.no_comments: print '# %s = ' % s['key'] for k, v in preseedsect.iteritems(): if k not in preseedkeys: print '%s = %s' % (k, v) print ''
def _get_config(profile='Default', config_path='~/.pushoverrc', user_key=None, api_token=None, device=None): config_path = os.path.expanduser(config_path) config = RawConfigParser() config.read(config_path) params = {"user_key": None, "api_token": None, "device": None} try: params.update(dict(config.items(profile))) except NoSectionError: pass if user_key: params["user_key"] = user_key if api_token: params["api_token"] = api_token if device: params["device"] = device if not TOKEN: init(params["api_token"]) if not TOKEN: raise InitError return params
def pms_readConf(filename): class multiKeyDict(DictMixin): def __init__(self, d=None): self._data = d or {} def __setitem__(self, key, value): if key in self._data and isinstance(self._data[key], list) and isinstance(value, list): self._data[key].extend(value) else: self._data[key] = value def __getitem__(self, key): return self._data[key] def __delitem__(self, key): del self._data[key] def keys(self): return self._data.keys() def copy(self): return multiKeyDict(self._data.copy()) conf = RawConfigParser(dict_type=multiKeyDict) for c in [filename, os.path.join(pms.getProfileDir(), os.path.basename(filename))]: if os.path.exists(c): conf.readfp(StringIO('[.]\n'+open(c).read())) try: conf = dict(conf.items('.')) print '%s: %s' % (filename, conf) return conf except: return {}
def __init__(self, configfiles): self.configfiles = configfiles configparser = RawConfigParser() config_tmp = configparser.read(self.configfiles) self.conf = dict() for section in configparser.sections(): self.conf[section] = dict(configparser.items(section)) #self.conf = ConfigObj(self.configfile, interpolation=False) @message("file could not be found") def check_file(v): f = os.path.expanduser(os.path.expanduser(v)) if os.path.exists(f): return f else: raise Invalid("file could not be found `%s`" % v) @message("Unsupported nova API version") def nova_api_version(version): try: from novaclient import client, exceptions client.get_client_class(version) return version except exceptions.UnsupportedVersion as ex: raise Invalid( "Invalid option for `nova_api_version`: %s" % ex) self.schemas = { "cloud": Schema( {"provider": Any('ec2_boto', 'google', 'openstack'), "ec2_url": Url(str), "ec2_access_key": All(str, Length(min=1)), "ec2_secret_key": All(str, Length(min=1)), "ec2_region": All(str, Length(min=1)), "auth_url": All(str, Length(min=1)), "username": All(str, Length(min=1)), "password": All(str, Length(min=1)), "tenant_name": All(str, Length(min=1)), Optional("region_name"): All(str, Length(min=1)), "gce_project_id": All(str, Length(min=1)), "gce_client_id": All(str, Length(min=1)), "gce_client_secret": All(str, Length(min=1)), "nova_client_api": nova_api_version()}, extra=True), "cluster": Schema( {"cloud": All(str, Length(min=1)), "setup_provider": All(str, Length(min=1)), "login": All(str, Length(min=1))}, required=True, extra=True), "setup": Schema( {"provider": All(str, Length(min=1)), }, required=True, extra=True), "login": Schema( {"image_user": All(str, Length(min=1)), "image_user_sudo": All(str, Length(min=1)), "image_sudo": Boolean(str), "user_key_name": All(str, Length(min=1)), "user_key_private": check_file(), "user_key_public": check_file()}, required=True) }
def get_backend(self, backend_name): """ Get options of backend. :returns: a tuple with the module name and the module options dict :rtype: tuple """ config = RawConfigParser() config.read(self.confpath) if not config.has_section(backend_name): raise KeyError(u'Configured backend "%s" not found' % backend_name) items = dict(config.items(backend_name)) try: module_name = items.pop('_module') except KeyError: try: module_name = items.pop('_backend') self.edit_backend(backend_name, module_name, items) except KeyError: warning('Missing field "_module" for configured backend "%s"', backend_name) raise KeyError(u'Configured backend "%s" not found' % backend_name) return module_name, items
class Configuration: def __init__(self, configfile=None): print os.getenv("CONF") CONF_FILE = "%s/kst.conf" % os.getenv("CONF") self._configFile = CONF_FILE if not configfile else configfile self._genConf() def _setConfigFile(self, configFile=None): self._configFile = configFile if not self._configFile: raise Exception("配置文件不存在") self._genConf() def _genConf(self): if not self._configFile: raise Exception("没有配置文件") self._config = RawConfigParser() self._config.read(self._configFile) def get(self, sect, opt): return self._config.get(sect, opt) def get_section(self, section): if not self._config.has_section(section): return {} items = self._config.items(section) return dict(items)
def iter_backends(self): """ Iterate on backends. :returns: each tuple contains the backend name, module name and module options :rtype: :class:`tuple` """ config = RawConfigParser() config.read(self.confpath) changed = False for backend_name in config.sections(): params = dict(config.items(backend_name)) try: module_name = params.pop('_module') except KeyError: try: module_name = params.pop('_backend') config.set(backend_name, '_module', module_name) config.remove_option(backend_name, '_backend') changed = True except KeyError: warning('Missing field "_module" for configured backend "%s"', backend_name) continue yield backend_name, module_name, params if changed: with open(self.confpath, 'wb') as f: config.write(f)
def __init__(self, config): _config = RawConfigParser() _config.optionxform = lambda s: s _config.read(config) self.path = os.path.dirname(config) for section in _config.sections(): if ':' in section: sectiongroupname, sectionname = section.split(':') else: sectiongroupname, sectionname = 'global', section items = dict(_config.items(section)) sectiongroup = self.setdefault(sectiongroupname, {}) sectiongroup.setdefault(sectionname, {}).update(items) seen = set() for sectiongroupname in self: sectiongroup = self[sectiongroupname] for sectionname in sectiongroup: section = sectiongroup[sectionname] if '<' in section: self._expand(sectiongroupname, sectionname, section, seen) for key in section: fname = 'massage_%s_%s' % (sectiongroupname, key) massage = getattr(self, fname, None) if callable(massage): section[key] = massage(section[key])
def parse_config_file(filename): parser = RawConfigParser(allow_no_value=True) parser.read(filename) creds = {} index = dict(hostname=0, password=1) for key, value in parser.items('credentials'): k1, k2 = key.split('.') if k1 not in creds: creds[k1] = [None, None] creds[k1][index[k2]] = value config = Config() config.hosts = {} for hostname, cred_key in parser.items('targets'): config.hosts[hostname] = (creds[cred_key]) config.wqls = parser.options('wqls') return config
def test_config_before_parsing_is_plain(self): rawConfig = RawConfigParser() rawConfig.readfp(StringIO(self.config_string)) self.assertEqual([(section, sorted(self.config.items(section))) for section in self.config.sections()], [(section, sorted(rawConfig.items(section))) for section in rawConfig.sections()])
class Configuration: def __init__(self, configfile): config_home = os.getenv("CONF_HOME") if config_home: self._configFile = "%s/%s" % (config_home, configfile) else: self._configFile = configfile self._genConf() def _setConfigFile(self, configFile=None): """设置configure文件""" self._configFile = configFile if not self._configFile: raise Exception("配置文件不存在") self._genConf() def _genConf(self): if not self._configFile: raise Exception("没有配置文件") self._config = RawConfigParser() self._config.read(self._configFile) def get(self, sect, opt, default=None): if self._config.has_option(sect, opt): return self._config.get(sect, opt) return default def getint(self, sect, opt, default=None): if self._config.has_option(sect, opt): return self._config.getint(sect, opt) return default def items(self, sect): return self._config.items(sect)
def from_config_file(self, config_file, allow_profile = False): """ Get the settings from a configuration file. :param config_file: Configuration file. :type config_file: str :param allow_profile: True to allow reading the profile name from the config file, False to forbid it. Global config files should allow setting a default profile, but profile config files should not, as it wouldn't make sense. """ parser = RawConfigParser() parser.read(config_file) if parser.has_section("golismero"): options = { k:v for k,v in parser.items("golismero") if v } if "profile" in options: if allow_profile: self.profile = options["profile"] self.profile_file = get_profile(self.profile) else: del options["profile"] for k in self._forbidden_: if k in options: del options[k] if options: self.from_dictionary(options)
def from_configfile(self, configfile): """ Initialize the authentication store from a "config"-style file. Auth "config" file is parsed with C{ConfigParser.RawConfigParser} and must contain an [auth] section which contains the usernames (keys) and passwords (values). Example auth file:: [auth] someuser = somepass anotheruser = anotherpass @param configfile: Path to config file or file-like object. @type configfile: C{any} @raise ValueError: If file could not be read or does not contain [auth] section. """ cfg = RawConfigParser() if hasattr(configfile, 'read'): cfg.readfp(configfile) else: filesread = cfg.read(configfile) if not filesread: raise ValueError('Could not parse auth file: %s' % configfile) if not cfg.has_section('auth'): raise ValueError('Config file contains no [auth] section.') self.store = dict(cfg.items('auth'))
def parseSettings(self): try: self.prnt("Reading in settings from settings.ini...") oldchans = self.joinchans settings = ConfigParser() settings.read("settings.ini") channels = settings.items("channels") for element in self.joinchans: self.joinchans.remove(element) for element in channels: self.joinchans.append(element) if not element in oldchans and not self.firstjoin == 1: self.join("#%s" % element[0]) for element in oldchans: if element not in self.joinchans and not self.firstjoin == 1: self.part("#%s" % element[0]) self.password = settings.get("info", "password") if not self.firstjoin == 1: self.sendmsg("nickserv", "IDENTIFY %s" % self.password) self.loginpass = settings.get("info", "loginpass") self.control_char = settings.get("info", "control_character") self.data_dir = settings.get("info", "data_folder") self.index_file = settings.get("info", "index_file") self.api_key = settings.get("mcbans", "api_key") except: return False else: self.prnt("Done!") return True
def readconf(self, conf_path, defaults=None, raw=False): """ Read config file(s) and return config items as a dict :param conf_path: path to config file/directory, or a file-like object (hasattr readline) :param defaults: dict of default values to pre-populate the config with :returns: dict of config items """ conf = {} if defaults is None: defaults = {} if raw: c = RawConfigParser(defaults) else: c = ConfigParser(defaults) if hasattr(conf_path, 'readline'): c.readfp(conf_path) else: if os.path.isdir(conf_path): # read all configs in directory success = read_conf_dir(c, conf_path) else: success = c.read(conf_path) if not success: self.logger.debug("Unable to read config from %s" % conf_path) return conf for s in c.sections(): conf.update(c.items(s)) conf['__file__'] = conf_path return conf
def my_cnf_inspect(self): """ Fix nonexistent paths to log-error and pid-file """ self.my_cnf_manager('backup') track = {'files': ('log-error', ), 'paths': ('pid-file', )} default_log = '/var/lib/mysql/mysqld.error.log' default_pid = '/var/lib/mysql/mysqld.pid' conf = RawConfigParser(allow_no_value=True) conf.read('/etc/my.cnf.prev') # try to find non-existent paths, defined in /etc/my.cnf for s in conf.sections(): for opt, val in conf.items(s): if opt in track['files']: # inspect whole path if not os.path.exists(val): print 'NO LOG for {opt} --> {v}'.format(opt=opt, v=val) conf.set(s, opt, default_log) elif opt in track['paths']: # inspect dir path if not os.path.exists(os.path.dirname(val)): print 'NO PATH for {opt} --> {v}'.format(opt=opt, v=val) conf.set(s, opt, default_pid) with open('/etc/my.cnf', 'wb') as configfile: conf.write(configfile)
def validate_remove_file(): """ Validate the remove file """ import stat if not os.path.isfile(constants.collection_remove_file): sys.exit("WARN: Remove file does not exist") # Make sure permissions are 600 mode = stat.S_IMODE(os.stat(constants.collection_remove_file).st_mode) if not mode == 0o600: sys.exit("ERROR: Invalid remove file permissions" "Expected 0600 got %s" % oct(mode)) else: print "Correct file permissions" if os.path.isfile(constants.collection_remove_file): from ConfigParser import RawConfigParser parsedconfig = RawConfigParser() parsedconfig.read(constants.collection_remove_file) rm_conf = {} for item, value in parsedconfig.items('remove'): rm_conf[item] = value.strip().split(',') # Using print here as this could contain sensitive information print "Remove file parsed contents" print rm_conf logger.info("JSON parsed correctly")
def main(): print "reading configuration" logging.basicConfig() config = RawConfigParser() config.read(['SystemInfoJabberBot.cfg',expanduser('~/.config/SystemInfoJabberBot.cfg')]) username = config.get('systembot','username') password = config.get('systembot','password') auth_users_raw= config.get('systembot','auth_users') auth_users=auth_users_raw.replace(' ','').split(',') print "set config" bot = SystemInfoJabberBot(username,password,auth_users) # Transmission config if config.has_section('transmissionrpc'): host = config.get('transmissionrpc','host') port = config.getint('transmissionrpc','port') try: user = config.get('transmissionrpc','user') psw = config.get('transmissionrpc','password') bot.setTransmissionConfig(host,port=port,user=user,psw=psw) except NoOptionError: bot.setTransmissionConfig(host,port=port) if config.has_section('logs'): log_files=config.items('logs') bot.setLogFiles( dict(log_files) ) print "start serve" bot.serve_forever() try: bot.quit() except Exception: pass
def retrieve_password(self): # print title Header().title_debug('Wifi') if not windll.Shell32.IsUserAnAdmin(): print_debug('ERROR', '[!] This script should be run as admin!') return else: if 'ALLUSERSPROFILE' in os.environ: directory = os.environ[ 'ALLUSERSPROFILE'] + os.sep + 'Microsoft\Wlansvc\Profiles\Interfaces' else: print_debug( 'ERROR', 'Environment variable (ALLUSERSPROFILE) has not been found.' ) return if not os.path.exists(directory): print_debug( 'INFO', 'No credentials found.\nFile containing passwords not found:\n%s' % directory) return try: print_debug('INFO', '[!] Trying to elevate our privilege') get_system_priv() print_debug( 'INFO', '[!] Elevation ok - Passwords decryption is in progress') except: print_debug( 'ERROR', '[!] An error occurs during the privilege elevation process. Wifi passwords have not been decrypted' ) time.sleep(5) # read temp file containing all passwords found pwdFound = [] filepath = tempfile.gettempdir() + os.sep + 'TEMP123A.txt' if os.path.exists(filepath): cp = RawConfigParser() cp.read(filepath) for section in cp.sections(): values = {} for c in cp.items(section): values[str(c[0])] = str(c[1]) pwdFound.append(values) # remove file on the temporary directory os.remove(filepath) # print the results print_output("Wifi", pwdFound) else: print_debug('INFO', 'No passwords found')
def validate_remove_file(): """ Validate the remove file """ import stat if not os.path.isfile(constants.collection_remove_file): sys.exit("WARN: Remove file does not exist") # Make sure permissions are 600 mode = stat.S_IMODE(os.stat(constants.collection_remove_file).st_mode) if not mode == 0o600: sys.exit("ERROR: Invalid remove file permissions" "Expected 0600 got %s" % oct(mode)) else: print "Correct file permissions" if os.path.isfile(constants.collection_remove_file): from ConfigParser import RawConfigParser parsedconfig = RawConfigParser() parsedconfig.read(constants.collection_remove_file) rm_conf = {} for item, value in parsedconfig.items('remove'): rm_conf[item] = value.strip().split(',') # Using print here as this could contain sensitive information print "Remove file parsed contents" print rm_conf logger.info("JSON parsed correctly")
def from_config_file(self, config_file, allow_profile=False): """ Get the settings from a configuration file. :param config_file: Configuration file. :type config_file: str :param allow_profile: True to allow reading the profile name from the config file, False to forbid it. Global config files should allow setting a default profile, but profile config files should not, as it wouldn't make sense. """ parser = RawConfigParser() parser.read(config_file) if parser.has_section("golismero"): options = {k: v for k, v in parser.items("golismero") if v} if "profile" in options: if allow_profile: self.profile = options["profile"] self.profile_file = get_profile(self.profile) else: del options["profile"] for k in self._forbidden_: if k in options: del options[k] if options: self.from_dictionary(options)