Beispiel #1
0
	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))
Beispiel #2
0
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)
Beispiel #3
0
    def test_write(self):
        self.inst.update()
        self.inst.write()

        new_config = RawConfigParser()
        new_config.read(self.outfile_path)

        actual = new_config.sections()
        expected = self.inst.config.sections()
        self.assertItemsEqual(actual, expected)

        for section in new_config.sections():
            actual = dict(new_config.items(section))
            expected = dict(self.inst.config.items(section))
            self.assertEqual(actual, expected)
Beispiel #4
0
 def parse_config(self, filename):
     from ConfigParser import RawConfigParser
     import io
     config = RawConfigParser()
     config.readfp(io.open(filename, 'r', encoding='utf_8_sig'))
     for s in config.sections():
         port = int(config.get(s, 'port'))
         config.remove_option(s, 'port')
         
         xsize, ysize = [int(d) for d in config.get(s, 'size').split(",")]
         config.remove_option(s, 'size')
         
         x_off, y_off = [int(d) for d in config.get(s, 'offset').split(",")]
         config.remove_option(s, 'offset')
         self.offsets[s] = (x_off, y_off)
         
         for device, offset in config.items(s):
             x_off, y_off = [int(d) for d in offset.split(",")]
             if self.offsets.has_key(device):
                 if (x_off, y_off) != self.offsets[device]:
                     raise RuntimeError("conflicting offsets for device %s" % device)
             self.offsets[device] = (x_off, y_off)
             
             if s in self.transtbl: self.transtbl[s].append(device)
             else: self.transtbl[s] = [device]
             if device in self.transtbl: self.transtbl[device].append(s)
             else: self.transtbl[device] = [s]
         
         self.add_virtual(s, xsize, ysize, port)
Beispiel #5
0
class Config():

    """
    Load and access the carbonate configuration.
    """

    def __init__(self, config_file):
        self.config_file = config_file
        self.config = RawConfigParser()
        self.config.read(config_file)

    def clusters(self):
        """Return the clusters defined in the config file."""
        return self.config.sections()

    def destinations(self, cluster='main'):
        """Return a list of destinations for a cluster."""
        if not self.config.has_section(cluster):
            raise SystemExit("Cluster '%s' not defined in %s"
                             % (cluster, self.config_file))
        destinations = self.config.get(cluster, 'destinations')
        return destinations.replace(' ', '').split(',')

    def replication_factor(self, cluster='main'):
        """Return the replication factor for a cluster as an integer."""
        if not self.config.has_section(cluster):
            raise SystemExit("Cluster '%s' not defined in %s"
                             % (cluster, self.config_file))
        return int(self.config.get(cluster, 'replication_factor'))

    def ssh_user(self, cluster='main'):
        """Return the ssh user for a cluster or current user if undefined."""
        if not self.config.has_section(cluster):
            raise SystemExit("Cluster '%s' not defined in %s"
                             % (cluster, self.config_file))
        try:
            return self.config.get(cluster, 'ssh_user')
        except NoOptionError:
            return pwd.getpwuid(os.getuid()).pw_name

    def whisper_lock_writes(self, cluster='main'):
        """Lock whisper files during carbon-sync."""
        if not self.config.has_section(cluster):
            raise SystemExit("Cluster '%s' not defined in %s"
                             % (cluster, self.config_file))
        try:
            return bool(self.config.get(cluster, 'whisper_lock_writes'))
        except NoOptionError:
            return False

    def hashing_type(self, cluster='main'):
        """Hashing type of cluster."""
        if not self.config.has_section(cluster):
            raise SystemExit("Cluster '%s' not defined in %s"
                             % (cluster, self.config_file))
        hashing_type = 'carbon_ch'
        try:
            return self.config.get(cluster, 'hashing_type')
        except NoOptionError:
            return hashing_type
Beispiel #6
0
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 __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
Beispiel #8
0
def get_firefox_home_file(needed_file):
    for firefox_dir in (os.path.expanduser(p) for p in
			("~/.mozilla/firefox-3.5/", "~/.mozilla/firefox/")):
        if os.path.exists(firefox_dir):
            break
    else:
        # no break
        return None
    # here we leak firefox_dir
    config = RawConfigParser({"Default" : 0})
    config.read(os.path.join(firefox_dir, "profiles.ini"))
    path = None

    for section in config.sections():
        if config.has_option(section, "Default") and config.get(section, "Default") == "1":
            path = config.get (section, "Path")
            break
        elif path == None and config.has_option(section, "Path"):
            path = config.get (section, "Path")
        
    if path == None:
        return ""

    if path.startswith("/"):
        return os.path.join(path, needed_file)

    return os.path.join(firefox_dir, path, needed_file)
Beispiel #9
0
    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)
Beispiel #10
0
 def backend_exists(self, name):
     """
     Return True if the backend exists in config.
     """
     config = RawConfigParser()
     config.read(self.confpath)
     return name in config.sections()
Beispiel #11
0
 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])
Beispiel #12
0
def main():
    #Argument parsing
    parser = argparse.ArgumentParser(description = 'Get Time series data from MYSQL and push it to influxdb' )

    parser.add_argument( '-d', '--debug', help = 'set logging level to debug', action = 'store_true')
    parser.add_argument( '-c', '--config', help = 'config file location', nargs = 1, default = 'settings.ini' )
    parser.add_argument( '-s', '--server', help = 'run as server with interval ',action = 'store_true' )

    args = parser.parse_args()


    # Init logging
    logging.basicConfig(level=(logging.DEBUG if True or args.debug else logging.INFO))

    logger.debug('Starting up with config file  %s' % (args.config))
    #get config file
    config = RawConfigParser()
    config.read(args.config)

    _sleep_time = float(config.get('server','interval'))

    logger.debug('configs  %s' % (config.sections()))
    #start
    mclient = Mysql2Influx(config)
    if not args.server:
        mclient.transfer_data()
    else:
        logger.info('Starting up server mode interval:  %s' % _sleep_time)
        while True:
            try:
                mclient.transfer_data()
            except Exception,e:
                logger.exception("Error occured will try again")
            time.sleep(_sleep_time)
            mclient.initialise_database()
Beispiel #13
0
def set_schema_fpath(project_name, schema_fpath):
    """Set the schema path for a given project.

    :param project_name: Name of the project
    :param schema_fpath: path to the yaml file to be used as the schema for \
            the project.
    :type project_name: str
    :type schema_fpath: str
    :return: True, if setting the schema path was successful.
    :Example:

    >>> set_schema_fpath('skynet', '/path/to/new/schema.yaml')
    True
    """
    path = locate_config_file()
    parser = RawConfigParser()
    parser.read(path)
    if project_name in parser.sections():
        if not parser.remove_option(project_name, "specfile"):
            raise MissingProject
        else:
            parser.set(project_name, "specfile", schema_fpath)
            with open(path, "w") as f:
                parser.write(f)
            return True
    raise MissingProject
Beispiel #14
0
def parse_items():
    """Parses the items.ini file."""
    global ITEMS

    raws = RawConfigParser(item_defaults, allow_no_value=True)
    raws.read('items.ini')
    for section in raws.sections():
        ITEMS[section] = {}
        ITEMS[section]['name'] = section
        ITEMS[section]['slot'] = raws.get(section, 'slot')

        damage = re.split( r'\D+', raws.get(section, 'damage') )
        if len(damage) >= 2:
            damage = map(int, damage)
            ITEMS[section]['damage'] = damage
        else:
            #turn list to int if only one value
            ITEMS[section]['damage'] = int(damage[0])

        ITEMS[section]['defense'] = raws.getint(section, 'defense')
        ITEMS[section]['weight'] = raws.getint(section, 'weight')
        ITEMS[section]['char'] = raws.get(section, 'symbol')
        assert len(ITEMS[section]['char']) == 1

        #seperates the color values and converts them into the libtcod standard
        color_tuple = re.split( r'\D+', raws.get(section, 'color') )
        color_tuple = map(int, color_tuple)
        ITEMS[section]['color'] = libtcod.Color( *color_tuple )

    print ITEMS
Beispiel #15
0
 def parse(self):
     _config = RawConfigParser()
     _config.optionxform = lambda s: s
     if getattr(self.config, 'read', None) is not None:
         _config.readfp(self.config)
     else:
         _config.read(self.config)
         self.path = os.path.dirname(self.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)
     self._load_plugins()
     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:
                 massage = self.massagers.get((sectiongroupname, key))
                 if callable(massage):
                     section[key] = massage(self, sectionname)
     return self
Beispiel #16
0
	def __init__(self, filename=config_file):
		self._filename = filename
		_parser = RawConfigParser()
		if self.read(_parser):
			for section in _parser.sections():
				for option in _parser.options(section):
					self._values['__'.join((section, option))] = _parser.get(section, option)
Beispiel #17
0
    def _parse_config(self, checkout_path="."):
        gconf_path = os.path.join(checkout_path, self.CONFIG_NAME)
        gconf = RawConfigParser()
        gconf.read([gconf_path])

        rs = {"repositories": {}, "usergroups": {}, "keys": {}}

        for user in map(
            lambda x: x.partition(".pub")[0],
            filter(lambda x: x.endswith(".pub"), os.listdir(os.path.join(checkout_path, "keydir"))),
        ):
            fd = open(os.path.join(checkout_path, "keydir", user + ".pub"))
            rs["keys"].update({user: {}})
            for key in fd.readlines():
                rs["keys"][user].update({user + "_key_" + str(len(rs["keys"][user])): key})
            fd.close()

        for section in gconf.sections():
            for (option, translated_option) in [("writable", "rw"), ("readonly", "r")]:
                if gconf.has_option(section, option):
                    for repo_name in gconf.get(section, option).strip().split():
                        if not rs["repositories"].has_key(repo_name):
                            rs["repositories"].update({repo_name: {}})
                        if gconf.has_option(section, "members"):
                            rs["repositories"][repo_name].update({translated_option: {}})
                            for member in gconf.get(section, "members").split():
                                rs["repositories"][repo_name][translated_option].update(
                                    {member: rs["keys"][member].keys()}
                                )

        return rs
Beispiel #18
0
def _path_fixer(filepath, root=None):
    """Change all the relative paths in `filepath` to absolute ones.

    :param filepath: File to be changed
    :param root: Root path with which the relative paths are prefixed. If None
    (default), the directory with this file is the root.
    """
    if root is None:
        root = op.join(op.abspath(op.dirname(__file__)))
    if filepath.endswith((".yaml", ".yml")):
        with open(filepath, "r") as fileobj:
            data = yaml.load(fileobj, Loader=Loader)
        for specs in data.itervalues():
            specs['path'] = op.join(root, specs['path'])
        with open(filepath, "w") as fileobj:
            yaml.dump(data, fileobj, Dumper=Dumper,
                      default_flow_style=False)
    elif filepath.endswith(".conf"):
        parser = RawConfigParser()
        parser.read(filepath)
        for section in parser.sections():
            path = parser.get(section, "specfile")
            parser.remove_option(section, "specfile")
            parser.set(section, "specfile", op.join(root, path))
        with open(filepath, "w") as fileobj:
            parser.write(fileobj)
Beispiel #19
0
 def test_config_before_parse_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()])
Beispiel #20
0
    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)
Beispiel #21
0
    def subthing_names(self):
        """
        Represent all section names from ``.ini`` file reverse sorted by length

        :raises IOError: On failing to read ``.ini`` file or if no sections
        """
        if self._subthing_names is None:
            # Using different parser here, helps validate job performed
            # in this class is correct (at least for section names)
            parser = RawConfigParser()
            if self.ini_filename is not None:
                parser.read([self.ini_filename])
            else:
                parser.readfp(StringIO(self._ini_string))
            section_names = parser.sections()  # Could be empty!
            if len(section_names) < 1:
                if self.ini_filename is not None:
                    raise IOError("No sections found in ini file: %s"
                                  % self.ini_filename)
                else:
                    raise IOError("No sections found in ini string: '%s'"
                                  % self._ini_string)
            section_names.sort(lambda x, y: cmp(len(x), len(y)), reverse=True)
            self._subthing_names = section_names
        return tuple(self._subthing_names)
Beispiel #22
0
    def _get_autoreload_programs(self, cfg_file):
        """Get the set of programs to auto-reload when code changes.

        Such programs will have autoreload=true in their config section.
        This can be affected by config file sections or command-line
        arguments, so we need to read it out of the merged config.
        """
        cfg = RawConfigParser()
        cfg.readfp(cfg_file)
        reload_progs = {}
        graceful_reload_progs = {}
        for section in cfg.sections():
            if section.startswith("program:"):
                try:
                    patterns = (
                        cfg.get(section, "autoreload_patterns").split(",")
                        if cfg.has_option(section, "autoreload_patterns")
                        else AUTORELOAD_PATTERNS
                    )
                    if cfg.getboolean(section, "autoreload"):
                        if cfg.getboolean(section, "autoreload_graceful"):
                            graceful_reload_progs[section.split(":", 1)[1]] = patterns
                        else:
                            reload_progs[section.split(":", 1)[1]] = patterns
                except NoOptionError:
                    pass
        return (reload_progs, graceful_reload_progs)
Beispiel #23
0
    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)
Beispiel #24
0
def parse_locators(file):
    """Parses locators configuration file and
    returns dictionary of locators.

    Arguments:
    file = locators file object (opened with open() method)

    Return:
    Dictionary of parsed locators."""
    # parse file
    parser = RawConfigParser()
    parser.readfp(file)
    # get sections from file
    sections = parser.sections()
    # prepare locators dictionary
    locators = {}
    # don't add sections name
    # when only one section exists
    if len(sections) is 1:
        section = sections[0]
        for name in parser.options(section):
            locators[name] = parser.get(section, name)
    # add section name as a key
    # when more than one section exists
    else:
        for section in sections:
            locators[section] = {}
            for name in parser.options(section):
                locators[section][name] = parser.get(section, name)
    # return dictionary of parsed locators
    return locators
Beispiel #25
0
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
Beispiel #26
0
    def get_firefox_profiles(self, directory):
        """
        List all profiles
        """
        cp = RawConfigParser()
        profile_list = []
        try:
            cp.read(os.path.join(directory, 'profiles.ini'))
            for section in cp.sections():
                if section.startswith('Profile') and cp.has_option(section, 'Path'):
                    profile_path = None

                    if cp.has_option(section, 'IsRelative'):
                        if cp.get(section, 'IsRelative') == '1':
                            profile_path = os.path.join(directory, cp.get(section, 'Path').strip())
                        elif cp.get(section, 'IsRelative') == '0':
                            profile_path = cp.get(section, 'Path').strip()

                    else: # No "IsRelative" in profiles.ini
                        profile_path = os.path.join(directory, cp.get(section, 'Path').strip())

                    if profile_path:
                        profile_list.append(profile_path)

        except Exception as e:
            self.error(u'An error occurred while reading profiles.ini: {}'.format(e))
        return profile_list
Beispiel #27
0
    def __init__(self, filename, mixer):
        from ConfigParser import RawConfigParser
        config = RawConfigParser()
        config.read(filename)

        self.play_intro = config.getboolean('Main', 'play_intro')
        if mixer:
            self.mixer = config.getboolean('Main', 'mixer')
        else:
            self.mixer = False
        self.music = GetDataPath(config.get('Main', 'music'))
        self.music = os.path.expanduser(self.music)

        width = config.getint('Defaults', 'width')
        height = config.getint('Defaults', 'height')

        self.subsettings = {}
        sections = config.sections()
        for defaults in ('Main', 'Defaults'):
            sections.remove(defaults)
        for sec in sections:
            op_dict = {}
            for op in config.options(sec):
                op_dict[op] = config.get(sec, op)
                if op in ('width', 'height'):
                    op_dict[op] = eval(op_dict[op])
            for op in ('width', 'height'):
                if op not in op_dict or op_dict[op] == 0:
                    op_dict[op] = locals()[op]
            self.subsettings[sec] = op_dict
Beispiel #28
0
    def get_items(self):
        parameters = {}
        mysql_config = RawConfigParser()

        sections = []

        try:
            contents = file(path(MY_CNF))
            lines = contents.readlines()

            # Workaround for `allow_no_value` (which is available in only Python 2.7+).
            body = ''
            for line in lines:
                stripped_line = line.strip()
                if not stripped_line or stripped_line[0] in ('#', ';'):
                    pass

                elif line[0] != '[' and line[-1] != ']' and \
                   '=' not in line:
                    line = '%s=\n' % line.rstrip('\n')

                body += line

            mysql_config.readfp(cStringIO.StringIO(body))

            sections = mysql_config.sections()
        except IOError:
            #sys.exit("Notice: Cannot open MySQL configuration file '%s'" % MY_CNF)
            print "Notice: Cannot open MySQL configuration file '%s'" % MY_CNF
        except ParsingError, error:
            print "Notice: Cannot parse PHP configuration file '%s'\n%s" % (PHP_INI, error)
Beispiel #29
0
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
Beispiel #30
0
    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_firefox_profiles(self, directory):
     cp = RawConfigParser()
     cp.read(os.path.join(directory, 'profiles.ini'))
     profile_list = []
     for section in cp.sections():
         if section.startswith('Profile'):
             if cp.has_option(section, 'Path'):
                 profile_list.append(os.path.join(directory, cp.get(section, 'Path').strip()))
     return profile_list
Beispiel #32
0
def settingsvar():
    testconfig()
    parser = RawConfigParser()
    results = parser.read("config.ini")
    sections = parser.sections()
    settings = []
    if parser.has_option(sections[2], "autoupdate"):
        settings.append(parser.getboolean(sections[2], "autoupdate"))
    return settings
Beispiel #33
0
def check_buildout(script_path):
    """ Are we running from within a buildout which supplies 'zopepy'?
    """
    buildout_cfg = os.path.join(os.path.dirname(script_path), 'buildout.cfg')
    if os.path.exists(buildout_cfg):
        from ConfigParser import RawConfigParser
        parser = RawConfigParser()
        parser.read(buildout_cfg)
        return 'zopepy' in parser.sections()
 def from_file(cls, queries_file):
     qs = cls()
     qs.queries_file = queries_file
     config = RawConfigParser()
     config.read(qs.queries_file)
     for query_name in config.sections():
         qs.queries_definition[query_name] = Query(query_name,
                                                   config.items(query_name))
     return qs
Beispiel #35
0
    def _check_submodule_no_git(self):
        """
        Like ``_check_submodule_using_git``, but simply parses the .gitmodules file
        to determine if the supplied path is a git submodule, and does not exec any
        subprocesses.

        This can only determine if a path is a submodule--it does not perform
        updates, etc.  This function may need to be updated if the format of the
        .gitmodules file is changed between git versions.
        """

        gitmodules_path = os.path.abspath('.gitmodules')

        if not os.path.isfile(gitmodules_path):
            return False

        # This is a minimal reader for gitconfig-style files.  It handles a few of
        # the quirks that make gitconfig files incompatible with ConfigParser-style
        # files, but does not support the full gitconfig syntax (just enough
        # needed to read a .gitmodules file).
        gitmodules_fileobj = io.StringIO()

        # Must use io.open for cross-Python-compatible behavior wrt unicode
        with io.open(gitmodules_path) as f:
            for line in f:
                # gitconfig files are more flexible with leading whitespace; just
                # go ahead and remove it
                line = line.lstrip()

                # comments can start with either # or ;
                if line and line[0] in (':', ';'):
                    continue

                gitmodules_fileobj.write(line)

        gitmodules_fileobj.seek(0)

        cfg = RawConfigParser()

        try:
            cfg.readfp(gitmodules_fileobj)
        except Exception as exc:
            log.warn('Malformatted .gitmodules file: {0}\n'
                     '{1} cannot be assumed to be a git submodule.'.format(
                         exc, self.path))
            return False

        for section in cfg.sections():
            if not cfg.has_option(section, 'path'):
                continue

            submodule_path = cfg.get(section, 'path').rstrip(os.sep)

            if submodule_path == self.path.rstrip(os.sep):
                return True

        return False
Beispiel #36
0
    def __load_config(self):
        config = RawConfigParser()

        try:
            fp = open(CFGFILE)
        except IOError:
            return

        # read values
        config.readfp(fp)

        required_sections = ['HTTPS', 'BANKING']
        required_options = {
            'HTTPS': ['port', 'address', 'keyfile', 'certfile'],
            'BANKING': ['account', 'blz', 'value', 'receipient']
        }

        for section in required_options:
            for option in required_options[section]:
                try:
                    self.cfg[option] = config.get(section, option)
                except:
                    self.cc.cc_err(
                        "Missing configuration parameter: section: %s - option: %s\n"
                        % (section, option))
                    sys.exit(1)

        # load user defined banking profiles
        bank_data = set(config.sections()) - set(required_sections)
        if not len(bank_data):
            self.cc.cc_err("No banking profile found in %s. Exiting.\n" %
                           (CFGFILE, ))
            sys.exit(1)

        required_options = [
            'target_host', 'login_site', 'transfer_site', 'itan_site',
            'login_field', 'pin_field', 'itan_field', 'receipient_field',
            'value_field', 'acctnr_field', 'banknr_field', 'validate_sites'
        ]
        for bank in bank_data:
            self.cc.cc_warn("loading profile %s... " % (bank, ))
            self.banking_profiles[bank] = {}

            for option in required_options:
                try:
                    self.banking_profiles[bank][option] = config.get(
                        bank, option)
                except:
                    print self.cc.cc_text('red', "Failed!"),
                    del self.banking_profiles[bank]
                    break

            print ""

        print ""

        return
Beispiel #37
0
 def _readfp(cls, fp, filename=None):
     config = RawConfigParser()
     config.readfp(fp, filename)
     data = OrderedDict()
     for section in config.sections():
         data[section] = OrderedDict()
         for k, v in config.items(section):
             data[section][k] = v
     return data
class AccountWriter():
    def __init__(self):
        # Location of account.cfg file
        # Store account information
        self.account_dir = path.expanduser('~/')
        self.account_file_name = '.account.cfg'
        self.account_config_location = path.join(self.account_dir,
                                                 self.account_file_name)
        self.account_config = RawConfigParser()
        if path.exists(self.account_config_location):
            self.account_config.read(self.account_config_location)

    def set_example(self):
        if not path.exists(self.account_config_location):
            self.account_config_file = open(self.account_config_location, 'w+')
            self.account_config.add_section('product-account-0')
            self.account_config.set('product-account-0',
                                    ";= This is a example", "")
            self.account_config.set('product-account-0', 'account', 'account')
            self.account_config.set('product-account-0', 'password',
                                    'password')
            self.account_config.write(self.account_config_file)
            self.account_config_file.close()
            assert False, "No Accouts information file.\n" + \
                          "Example of configuration added\n" + \
                          "Please add account to this file: %s\n" % \
                          self.account_config_location + \
                          "You can add more account for KKBOX like this: \n[KKBOX-account-1]\n" + \
                          "account = [email protected]\n" + \
                          "password = password"
        else:
            print "sample existed in Account file"

    def get_account_sections(self):
        if len(self.account_config.sections()) <= 1:
            self.set_example()
            return 0  # for no account config
        else:
            result = self.account_config.sections()
            result.pop(0)  # remove example
            return result

    def get_account_option(self, section, option):
        return self.account_config.get(section, option)
Beispiel #39
0
def main(argv=sys.argv):
    argparser = ArgumentParser()

    argparser.add_argument('--config', help="nextgisweb configuration file")
    argparser.add_argument('--logging',
                           help="logging library configuration file")

    config = None
    logging = None

    i = 1

    while i < len(argv):
        if argv[i] == '--config' and (i < len(argv) - 1):
            config = argv[i + 1]
        if argv[i] == '--logging' and (i < len(argv) - 1):
            logging = argv[i + 1]

        i += 2 if argv[i].startswith('--') else 1

    if config is None:
        config = os.environ.get('NEXTGISWEB_CONFIG')

    if logging is None:
        logging = os.environ.get('NEXTGISWEB_LOGGING')

    if logging:
        setup_logging(logging)

    cfg = RawConfigParser()

    if config:
        cfg.readfp(codecs.open(config, 'r', 'utf-8'))

    for section in cfg.sections():
        for item, value in cfg.items(section):
            cfg.set(section, item, value % os.environ)

    env = Env(cfg=cfg)
    env.initialize()

    setenv(env)

    subparsers = argparser.add_subparsers()

    for cmd in Command.registry:
        subparser = subparsers.add_parser(cmd.identity)
        cmd.argparser_setup(subparser, env)
        subparser.set_defaults(command=cmd)

    args = argparser.parse_args(argv[1:])

    if args.logging:
        setup_logging(args.logging)

    args.command.execute(args, env)
Beispiel #40
0
def setup_handle_fax(cursor):
    # Raise an error if a backend creation failed, etc.
    # 1. read config
    config = RawConfigParser()
    fobj = open(CONFIG_FILE)
    try:
        config.readfp(fobj)
    finally:
        fobj.close()

    # 2. read general section...
    global TIFF2PDF_PATH
    global MUTT_PATH
    global LP_PATH
    if config.has_option("general", "tiff2pdf"):
        TIFF2PDF_PATH = config.get("general", "tiff2pdf")
    if config.has_option("general", "mutt"):
        MUTT_PATH = config.get("general", "mutt")
    if config.has_option("general", "lp"):
        LP_PATH = config.get("general", "lp")

    # 3. create backends
    backends = {}
    for backend_prefix, backend_factory in _BACKENDS_FACTORY:
        for section in filter(lambda s: s.startswith(backend_prefix),
                              config.sections()):
            backend_factory_args = dict(config.items(section))
            logger.debug("Creating backend, name %s, factory %s", section,
                         backend_factory)
            backends[section] = backend_factory(**backend_factory_args)
    logger.debug("Created %s backends", len(backends))

    # 4. creation destinations
    global DESTINATIONS
    DESTINATIONS = {}
    for section in filter(lambda s: s.startswith("dstnum_"), config.sections()):
        cur_destination = section[7:]  # 6 == len("dstnum_")
        cur_backend_ids = map(lambda s: s.strip(), config.get(section, "dest").split(","))
        cur_backends = _build_backends_list(backends, cur_backend_ids, cur_destination)
        logger.debug('Creating destination, dstnum %s, backends %s', cur_destination,
                     cur_backend_ids)
        DESTINATIONS[cur_destination] = cur_backends
    logger.debug("Created %s destinations", len(DESTINATIONS))
Beispiel #41
0
Datei: main.py Projekt: rusi/mcdp
    def go(self):
        options = self.get_options()

        if options.config is not None:
            logger.info('Reading configuration from %s' % options.config)
            logger.warn('Other options from command line will be ignored. ')
            parser = RawConfigParser()
            parser.read(options.config)
            sections = parser.sections()
            logger.info('sections: %s' % sections)
            s = 'app:main'
            if not s in sections:
                msg = 'Could not find section "%s": available are %s.' % (
                    s, format_list(sections))
                msg += '\n file %s' % options.config
                raise Exception(msg)  # XXX
            settings = dict((k, parser.get(s, k)) for k in parser.options(s))

            prefix = 'mcdp_web.'
            mcdp_web_settings = get_only_prefixed(settings,
                                                  prefix,
                                                  delete=True)
            #             mcdp_web_settings = {}
            #             for k,v in list(settings.items()):
            #                 if k.startswith(prefix):
            #                     mcdp_web_settings[k[len(prefix):]] = v
            #                     del settings[k]
            options = parse_mcdpweb_params_from_dict(mcdp_web_settings)

            logger.debug('Using these options: %s' % options)
        else:
            logger.info('No configuration .ini specified (use --config).')
            settings = {}

        wa = WebApp(options, settings=settings)
        # Write warning messages now
        wa.get_authomatic_config()
        msg = """Welcome to PyMCDP!
        
To access the interface, open your browser at the address
    
    http://localhost:%s/
    
Use Chrome, Firefox, or Opera - Internet Explorer is not supported.
""" % options.port
        logger.info(msg)

        if options.delete_cache:
            pass  # XXX: warning deprecated


#             logger.info('Deleting cache...')
# wa._refresh_library(None)

        wa.serve(port=options.port)
Beispiel #42
0
class Config(object):
    """
    Manage configuration - a simple wrapper around RawConfigParser.
    Upon initialization, the loaded file is updated with the default values.
    config.save() will save the current state.
    """
    def __init__(self):
        self.filename = get_config_fn()
        try:
            self.parser = RawConfigParser(dict_type=OrderedDict)
        except TypeError:
            # Python versions < 2.6 don't support dict_type
            self.parser = RawConfigParser()
        f = StringIO(default_config)
        self.parser.readfp(f)
        self.parser.read(self.filename)
        self.save()

    def get(self, key, section='DreamPie'):
        return self.parser.get(section, key)

    def get_bool(self, key, section='DreamPie'):
        return self.parser.getboolean(section, key)

    def get_int(self, key, section='DreamPie'):
        return self.parser.getint(section, key)

    def set(self, key, value, section='DreamPie'):
        self.parser.set(section, key, value)

    def set_bool(self, key, value, section='DreamPie'):
        value_str = 'True' if value else 'False'
        self.set(key, value_str, section)

    def set_int(self, key, value, section='DreamPie'):
        if value != int(value):
            raise ValueError("Expected an int, got %r" % value)
        self.set(key, '%d' % value, section)

    def sections(self):
        return self.parser.sections()

    def has_section(self, section):
        return self.parser.has_section(section)

    def add_section(self, section):
        return self.parser.add_section(section)

    def remove_section(self, section):
        return self.parser.remove_section(section)

    def save(self):
        f = open(self.filename, 'w')
        self.parser.write(f)
        f.close()
Beispiel #43
0
def read(where):

    # TODO: Allow the user the option of just choosing a single checkout to
    # process - this makes it easier to recover if some patches worked, and
    # others didn't, but the user has figured out why and fixed things...

    where = canonical_path(where)

    manifest = os.path.join(where, 'MANIFEST.txt')
    config = RawConfigParser()
    with open(manifest) as fd:
        config.readfp(fd)

    sections = config.sections()
    for section in sections:
        print 'Section %s' % section
        if section.startswith("BZR"):
            co_leaf = config.get(section, 'co_leaf')
            co_dir = config.get(section, 'co_dir')
            if co_dir == 'None':
                co_dir = None
            filename = config.get(section, 'patch')
            print '  co_leaf %s, co_dir %s, filename %s' % (co_leaf, co_dir,
                                                            filename)
            bzr_merge_from_send(co_leaf, co_dir, os.path.join(where, filename))
        elif section.startswith("SVN"):
            co_leaf = config.get(section, 'co_leaf')
            co_dir = config.get(section, 'co_dir')
            if co_dir == 'None':
                co_dir = None
            filename = config.get(section, 'patch')
            print '  co_leaf %s, co_dir %s, filename %s' % (co_leaf, co_dir,
                                                            filename)
            svn_patch(co_leaf, co_dir, os.path.join(where, filename))
        elif section.startswith("GIT"):
            co_leaf = config.get(section, 'co_leaf')
            co_dir = config.get(section, 'co_dir')
            if co_dir == 'None':
                co_dir = None
            patch_dir = config.get(section, 'patch')
            print '  co_leaf %s, co_dir %s, patch_dir %s' % (co_leaf, co_dir,
                                                             patch_dir)
            git_am(co_leaf, co_dir, os.path.join(where, patch_dir))
        elif section.startswith("TAR"):
            co_leaf = config.get(section, 'co_leaf')
            co_dir = config.get(section, 'co_dir')
            if co_dir == 'None':
                co_dir = None
            filename = config.get(section, 'patch')
            print '  co_leaf %s, co_dir %s, filename %s' % (co_leaf, co_dir,
                                                            filename)
            tar_unpack(co_leaf, co_dir, os.path.join(where, filename))
        else:
            print 'No support for %s yet - ignoring...' % section.split()[0]
Beispiel #44
0
def load(config_files):
    global config
    config = {}
    cp = RawConfigParser()
    cp.read(config_files)

    for section in cp.sections():
        if config.get(section, None) == None:
            config[section] = {}
        for option in cp.options(section):
            config[section][option] = cp.get(section, option)  #.strip()
Beispiel #45
0
def read_config(f):
    c = RawConfigParser()
    c.readfp(f)

    def get_options(s):
        return dict(parse_options(c.items(s), section=s))

    general = {}
    pairs = {}
    storages = {}

    def handle_storage(storage_name, options):
        storages.setdefault(storage_name, {}).update(options)
        storages[storage_name]['instance_name'] = storage_name

    def handle_pair(pair_name, options):
        _validate_pair_section(options)
        a, b = options.pop('a'), options.pop('b')
        pairs[pair_name] = a, b, options

    def handle_general(_, options):
        if general:
            raise exceptions.UserError(
                'More than one general section in config file.')
        general.update(options)

    def bad_section(name, options):
        cli_logger.error('Unknown section: {}'.format(name))

    handlers = {
        'storage': handle_storage,
        'pair': handle_pair,
        'general': handle_general
    }

    for section in c.sections():
        if ' ' in section:
            section_type, name = section.split(' ', 1)
        else:
            section_type = name = section

        try:
            validate_section_name(name, section_type)
            f = handlers.get(section_type, bad_section)
            f(name, get_options(section))
        except ValueError as e:
            raise exceptions.UserError('Section `{}`: {}'.format(
                section, str(e)))

    _validate_general_section(general)
    if getattr(f, 'name', None):
        general['status_path'] = os.path.join(
            os.path.dirname(f.name), expand_path(general['status_path']))
    return general, pairs, storages
Beispiel #46
0
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
Beispiel #47
0
 def _load_configs(self):
     for conf_file in os.listdir(self.path):
         if not conf_file.endswith('.conf'):
             continue
         full_path = os.path.join(self.path, conf_file)
         conf = RawConfigParser()
         conf.read(full_path)
         for section in conf.sections():
             if section not in self.plugins:
                 self.plugins[section] = dict(conf.items(section))
                 self.plugins[section]['scheduled'] = False
Beispiel #48
0
    def load_config(self, filename):
        '''Loads facility definitions from a config file.'''

        db = self.__class__()

        cp = RawConfigParser()

        # NOTE: apparently, cp.read() will happily do nothing if the file doesn't exist.
        # Thus, we use cp.readfp() instead, letting open() fail if something is wrong.
        f = open(filename, 'r')
        cp.readfp(f, filename)
        f.close()

        root_facilities = {}
        # First we add all root facilities
        for section in cp.sections():
            app_id, _, mod_str = section.partition(':')
            if mod_str:
                # Skip non-root facilities
                continue

            facility = self.parse_section(cp, section)
            db.add_facility(facility)
            root_facilities[app_id] = facility
        
        for section in cp.sections():
            app_id, _, mod_str = section.partition(':')
            if not mod_str:
                # Skip root facilities
                continue

            try:
                root_facility = root_facilities[app_id]
            except KeyError:
                raise FacilityError('Application {0} lacks a root module. Define [{0}] section in the facility config file.'.format(app_id, app_id))

            facility = self.parse_section(cp, section, root_facility=root_facility)
            db.add_facility(facility)

        self.facilities = db.facilities
        self.filename = filename
Beispiel #49
0
class HaroldConfiguration(object):
    def __init__(self, filename):
        self.parser = RawConfigParser()
        with open(filename, "r") as config_file:
            self.parser.readfp(config_file)

    def plugin_names(self):
        for section in self.parser.sections():
            if not section.startswith(plugin_prefix):
                continue

            yield section[len(plugin_prefix):]
Beispiel #50
0
 def parse_config(self, config_file):
     """Parses the config file supplied and calls parse_file(config, section) for
     every section in the config file. You must implement this method yourself."""
     config = RawConfigParser()
     config.read(config_file)
     
     sections = config.sections()
     
     for section in sections:
         if section == DEFAULTS_CONFIG_SECTION: continue
         
         self.parse_file(config, section)
Beispiel #51
0
def get_config(path):
    cfgobj = RawConfigParser()
    cfgobj.read(path)
    cfg = dict([(section, dict(cfgobj.items(section)))
                for section in cfgobj.sections()])
    assert cfg.get('global', {}).get('dbpath'), 'dbpath is required'
    assert cfg.get('collector',
                   {}).get('secret_header'), 'secret_header is required'
    assert cfg.get('collector', {}).get('hosts'), 'hosts required'

    cfg['collector']['hosts'] = cfg['collector']['hosts'].split(',')
    return cfg
Beispiel #52
0
    def load_from_file(self, path):
        """
        Load the INI file into the instance.

        path -- a string of path to the INI file.
        """
        schema = self.schema

        # Set up the default values.
        if schema is not None:
            for sect, sect_obj in schema.items():
                for opt, val in sect_obj.items():
                    # This call is to convert the value to
                    # the type specified.  We do this to
                    # prevent the programmer from specifying
                    # inconsistent type with the value in the
                    # schema.
                    self.set(*_convert(schema, sect, opt, val[1]))

        # Parse the INI file.
        parser = RawConfigParser()
        parser.read(path)

        sections = parser.sections()
        for section in sections:

            # If application has supplied a schema,
            # and it does not has such a section, we skip
            # it.  No error raised.
            if schema is not None and \
               not schema.has_key(section):
                continue

            options = parser.options(section)

            for option in options:

                # If application has supplied a schema,
                # we know the section is valid since it pass the
                # previus test, but if the option is not included
                # in the section, we skip it.  No error raised.
                if schema is not None and \
                   (option not in schema[section]):
                    continue

                # If there is a schema, then we convert the
                # option to its type stated in the schema,
                # otherwise we just leave it as string.
                if schema is not None:
                    self.set(*_convert(schema, section, option,
                                       parser.get(section, option)))
                else:
                    self.set(section, option, parser.get(section, option))
Beispiel #53
0
 def load_values(self):
     """Read values of options from file."""
     config = RawConfigParser()
     config.read(self.filename)
     sections = config.sections()
     for section in sections:
         if self.has_section(section):
             options = config.options(section)
             for option in options:
                 if self.has_option(section, option):
                     self.set_value(section, option,
                                    config.get(section, option))
Beispiel #54
0
    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]
                return client_name, consumer_key, consumer_secret, access_token_key, access_token_secret
            elif str.lower(platformname) == "github":
                clientname = platformname + "_" + client_name
                m = parser.items(clientname)

                access_token = m[0][1]
                return client_name, access_token
                #logger_test.exception("error occured while reading the config file with error :%s"%str(e))

            elif str.lower(platformname) == "meetup":

                clientname = platformname + "_" + client_name
                m = parser.items(clientname)

            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")
Beispiel #55
0
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
Beispiel #56
0
class _UnittestSettings(Settings):
    """
    Subclass of Settings which loads configuration from a dict of dicts.  Only
    useful for unit tests.
    """
    def __init__(self, usage='', description='', appname=''):
        """
        :param usage: The usage string, displayed in --help output.
        :type usage: str
        :param description: A short application description, displayed in --help output.
        :type description: str
        :param appname: The application name.
        :type appname: str
        """
        Parser.__init__(self, appname, usage, description)
        self.appname = appname
        self._config = RawConfigParser()
        self._overrides = RawConfigParser()
        self._cwd = os.getcwd()
        self._overrides.add_section('settings')
        self._overrides.set('settings', 'config file',
                            "/etc/terane/%s.conf" % self.appname)
        self.addOption('c',
                       'config-file',
                       'settings',
                       'config file',
                       help="Load configuration from FILE",
                       metavar="FILE")

    def load(self, sections, cmdline=[]):
        """
        Load the configuration from the specified dict.

        :param sections: A dict whose keys are section names, and whose values are
          dicts containing option key-value pairs.
        :type sections: dict
        :param cmdline: A list of command line arguments.  Note this list excludes
          the executable name at cmdline[0].
        :type cmdline: list
        """
        # parse command line arguments
        self._parser, self._args = self._parse(cmdline, self._overrides)
        for sectionname, kvs in sections.items():
            self._config.add_section(sectionname)
            for key, value in kvs.items():
                self._config.set(sectionname, key, value)
        # merge command line settings with config file settings
        for section in self._overrides.sections():
            for name, value in self._overrides.items(section):
                if not self._config.has_section(section):
                    self._config.add_section(section)
                self._config.set(section, name, str(value))
Beispiel #57
0
def parseConfig(ctx):
    '''
   Parse config.ini file
   '''
    config = RawConfigParser()
    try:
        printDebug("Parsing config file = %s" % (ctx._config_file))
        config.readfp(open(ctx._config_file, 'r'))
        # If _scidb_name has not been assigned, use the first section name as the dbname.
        if ctx._scidb_name == '':
            ctx._scidb_name = config.sections()[0]
    except Exception, e:
        raise AppError("Cannot read config file: %s" % e)
 def _extend(self, filename):
     """Expand the config with another file."""
     if not os.path.isfile(filename):
         raise IOError('No such file: %s' % filename)
     parser = RawConfigParser()
     parser.read([filename])
     for section in parser.sections():
         if not self.has_section(section):
             self.add_section(section)
         for option, value in parser.items(section):
             if self.has_option(section, option):
                 continue
             RawConfigParser.set(self, section, option, value)
def get_addressbook_dirs():
    ''' Get path to addressbook file from default profile. '''
    for thome, tprofile in THUNDERBIRD_PROFILES:
        if os.path.isfile(tprofile):
            config = RawConfigParser()
            config.read(tprofile)
            for section in config.sections():
                if config.has_option(section, "Path"):
                    path = config.get(section, "Path")
                    if not os.path.isabs(path):
                        path = os.path.join(thome, path)
                    if os.path.isdir(path):
                        yield path
Beispiel #60
0
def load_hhtypes(filename=HHTYPE):
    p = RawConfigParser()
    with open(filename) as fp:
        p.readfp(fp)
    result = {}
    for s in p.sections():
        _, _, expl = s.partition(', ')
        hht = p.get(s, 'id')
        rank = p.getint(s, 'rank')
        abbv = p.get(s, 'abbv')
        bibabbv = p.get(s, 'bibabbv')
        result[hht] = (rank, expl, abbv, bibabbv)
    return result