Пример #1
0
    def __init__(self, config=None):
        self.builder = None
        self.www_dir = None
        conf = SafeConfigParser()
        if config:
            if hasattr(config, "readline"):
                conf.readfp(config)
            else:
                conf.read(config)
        if conf.has_section("tarpyt"):
            try:
                mkvfile = conf.get("tarpyt", "markov_file")
                mfile = open(mkvfile, "rb")
                self.set_builder(pickle.load(mfile))
            except NoOptionError:
                self.builder = None
            try:
                www = conf.get("tarpyt", "www_dir")
                self.www_dir = os.path.abspath(www) if os.path.isdir(www) else None
            except NoOptionError:
                self.www_dir = None
        self.weight_total = 0
        self.responses = []
        if conf.has_section("responses"):

            def update(response):
                self.responses.append(getattr(self, "response_" + response[0]))
                self.weight_total += int(response[1])
                return self.weight_total

            self.weights = map(update, sorted(conf.items("responses"), key=lambda x: int(x[1])))
        else:
            self.responses.append(self.response_linkpage)
            self.weights = [1]
            self.weight_total = 1
Пример #2
0
def prepare_caching():
    ''' Value caching (used for coin and TB counts).'''
    cache = ConfigParser()
    try:
        cache.read('cache.dat')
        if not cache.has_section('TRAVELITEMS'):
            raise KeyError("TRAVELITEMS")
        if not cache.has_section('OWNCACHES'):
            raise KeyError("OWNCACHES")
    except KeyError as e:
        cache.add_section(e.args[0])
        print("No Travelbug/ Own Caches section found . A new one was "
              "generated.")
    except IOError:
        print("No cache file found . A new one was "
              "generated.")
    finally:
        try:
            with open('cache.dat', 'wb') as cachefile:
                cache.write(cachefile)
            cache.read('cache.dat')
        except IOError:
            print "Cachefile could not be written. Continuing without saving."
 
    return cache
Пример #3
0
def parseConfig(f):
	"""Use configparser module to parse configuration file
	   config file must be in right format, otherwise, server cannot start
	   This function returns 2 dictionaries:
			- handle_servers stores requested server, handles server and weight
			- constructed_cluster store requested server and handle server 
	   This thing is ugly but it works :-)
	"""
	print 'Reading configuration file....'
	cluster = {} 
	group = {}
	constructed_cluster = {} 
	parser = SafeConfigParser()
	parser.read(f)
	if parser.has_section('farms'):
		farm_list = [value for name, value in parser.items('farms')] 
		for item in farm_list[0].split(','):
			if parser.has_section(item):
				cluster[item] = {name:value for name, value in parser.items(item)}  
	else:
		sys.stderr.write("Configuration file error, no item 'farms' defined\n") 
		sys.stderr.write("Exit!\n") 
		sys.exit(2) 
	
	for i in parser.get('farms','list').split(','):
		list_redirect_domain = [parser.get(parser.get(i, 'list').split(',')[j], 'domain') for j in range(len(parser.get(i, 'list').split(','))) ]
		constructed_cluster[parser.get(i, 'domain')]  = list_redirect_domain 
	for server_group, server_list in cluster.iteritems():
		temporary_list = [] 
		origin_domain_name = server_list['domain']
		servers = [server for server in server_list['list'].split(',')]
		for s in servers: 
			temporary_list.append([v for k, v in parser.items(s) if v != 'on']) 
		group[origin_domain_name] = temporary_list 
	return (group, constructed_cluster) 
Пример #4
0
class Configuration(object):
    def __init__(self, config_dir):
        self.config_file = os.path.join(config_dir, "settings.cfg")
        self.config = SafeConfigParser()

        if os.path.isfile(self.config_file):
            self.reload()
        else:
            self._setup_config()

    def save(self):
        with open(self.config_file, 'w') as f:
            self.config.write(f)

    def reload(self):
        self.config.read([self.config_file])

    def _setup_config(self):
        self.general__ticket_limit = "3000"
        self.general__min_class_occur = "30"

        self.defaults__schedule = "0 0 * * *"

        self.svm__coefficient = "240.0"
        self.svm__cache_size = "2000"

        self.jira__default_resolutions = "Fixed"
        self.jira__default_statuses = "Resolved,Closed"

        self.auth__admin = hash_pwd("admin")

        self.save()

    def __getattr__(self, name):
        section_option = name.split("__", 1)
        if len(section_option) == 2 and section_option[0]:
            section, option = section_option

            if self.config.has_section(section) \
                    and self.config.has_option(section, option):
                return self.config.get(section, option)

        raise AttributeError("%s has no attribute '%s'"
                             % (self.__class__.__name__, name))

    def __setattr__(self, name, value):
        section_option = name.split("__", 1)
        if len(section_option) == 2 and section_option[0]:
            section, option = section_option

            if not self.config.has_section(section):
                self.config.add_section(section)

            if name != "auth__admin" or value:
                self.config.set(section, option, str(value))
        elif name in ['config_file', 'config']:
            super(Configuration, self).__setattr__(name, value)
        else:
            raise AttributeError("%s has no attribute '%s'"
                                 % (self.__class__.__name__, name))
Пример #5
0
    def _parse_config_files(self):
        """
        Parse the possible config files and set appropriate values
        default values
        """
        parser = SafeConfigParser(self.defaults)
        parser.read(self.config_files)
        self.config = dict(parser.defaults())

        if parser.has_section(self.command):
            self.config.update(dict(parser.items(self.command, raw=True)))

        for section in self.sections:
            if parser.has_section(section):
                self.config.update(dict(parser.items(section, raw=True)))
            else:
                raise NoSectionError("Mandatory section [%s] does not exist."
                                     % section)

        # filter can be either a list or a string, always build a list:
        if self.config['filter']:
            if self.config['filter'].startswith('['):
                self.config['filter'] = eval(self.config['filter'])
            else:
                self.config['filter'] = [ self.config['filter'] ]
        else:
            self.config['filter'] = []
Пример #6
0
    def __init__( self ):
        config = SafeConfigParser( defaults )
        config.read( os.path.expanduser( "~/.pyjacksmrc" ) )
        self.jackname = config.get( "DEFAULT", "jackclientname" )

	# setup infra
        if config.has_section( "infra" ):
            self.infra_clients = {}
            for inf in config.items( "infra" ):
                self.infra_clients[inf[0]] = inf[1]
        else:
            self.infra_clients = { "a2j": "a2jmidid" }

	# setup path
        if config.has_section( "path" ):
            self.path_map = {}
            for p in config.items( "path" ):
                self.path_map[p[0]] = p[1]
        else:
            self.path_map = {}


	# implicit clients... should b conf too
        self.implicit_clients = [ "system" ]

        self.sessiondir = os.path.expanduser( config.get( "DEFAULT", "sessiondir" ) ) + "/" 
        self.templatedir = os.path.expanduser( config.get( "DEFAULT", "templatedir" ) ) + "/" 
Пример #7
0
    def load_config_file(self):
        """Parse configuration file and get config values."""
        config_parser = SafeConfigParser()

        config_parser.read(self.CONFIG_FILE)

        if config_parser.has_section('handlers'):
            self._config['handlers_package'] = config_parser.get('handlers', 'package')

        if config_parser.has_section('auth'):
            self._config['consumer_key'] = config_parser.get('auth', 'consumer_key')
            self._config['consumer_secret'] = config_parser.get('auth', 'consumer_secret')
            self._config['token_key'] = config_parser.get('auth', 'token_key')
            self._config['token_secret'] = config_parser.get('auth', 'token_secret')

        if config_parser.has_section('stream'):
            self._config['user_stream'] = config_parser.get('stream', 'user_stream').lower() == 'true'
        else:
            self._config['user_stream'] = False

        if config_parser.has_option('general', 'min_seconds_between_errors'):
            self._config['min_seconds_between_errors'] = config_parser.get('general', 'min_seconds_between_errors')
        if config_parser.has_option('general', 'sleep_seconds_on_consecutive_errors'):
            self._config['sleep_seconds_on_consecutive_errors'] = config_parser.get(
                'general', 'sleep_seconds_on_consecutive_errors')
Пример #8
0
    def test_rights_file_generation(self):
        mbox = Mailbox.objects.get(address="admin", domain__name="test.com")
        cal = UserCalendarFactory(mailbox=mbox)

        AccessRuleFactory(
            mailbox=Mailbox.objects.get(
                address="user", domain__name="test.com"),
            calendar=cal, read=True)
        management.call_command("generate_rights", verbosity=False)

        cfg = SafeConfigParser()
        with open(self.rights_file_path) as fpo:
            cfg.readfp(fpo)

        # Check mandatory rules
        self.assertTrue(cfg.has_section("domain-shared-calendars"))
        self.assertTrue(cfg.has_section("owners-access"))

        # Check user-defined rules
        section = "[email protected] calendar 1-acr"
        self.assertTrue(cfg.has_section(section))
        self.assertEqual(cfg.get(section, "user"), "*****@*****.**")
        self.assertEqual(
            cfg.get(section, "collection"),
            "test.com/user/admin/User calendar 1"
        )
        self.assertEqual(cfg.get(section, "permission"), "r")
Пример #9
0
def checkAppINI(appLocation):
    if (os.path.isdir(appLocation)):
        zipApp = None
        appINIPath = os.path.join(appLocation, "application.ini")
        if not (os.path.isfile(appINIPath)):
            raise Exception(appINIPath + " does not exist")
        appINI = open(appINIPath)
    elif (zipfile.is_zipfile(appLocation)):
        zipApp = zipfile.ZipFile(appLocation)
        if not ("application.ini" in zipApp.namelist()):
            raise Exception("jar:" + appLocation + "!/application.ini does not exist")
        appINI = zipApp.open("application.ini")
    else:
        raise Exception("appLocation must be a directory containing application.ini or a zip file with application.ini at its root")

    # application.ini verification
    iniparser = SafeConfigParser()
    iniparser.readfp(appINI)
    if not (iniparser.has_section("App")):
        raise Exception("application.ini must have an App section")
    if not (iniparser.has_section("Gecko")):
        raise Exception("application.ini must have a Gecko section")
    requireINIOption(iniparser, "App", "Name")
    requireINIOption(iniparser, "App", "Version")
    requireINIOption(iniparser, "App", "BuildID")
    requireINIOption(iniparser, "App", "ID")
    requireINIOption(iniparser, "Gecko", "MinVersion")

    return zipApp, iniparser
    pass
Пример #10
0
def main(argv):
  config = SafeConfigParser()
  config.read(SYSTEM_BOTO_CONFIG_TEMPLATE)

  # TODO(jbeda): Figure out if we need a retry here.
  project_id = GetNumericProjectId()
  if not project_id:
    # Our project doesn't support service accounts.
    return

  if not config.has_section('GSUtil'):
    config.add_section('GSUtil')
  config.set('GSUtil', 'default_project_id', project_id)
  config.set('GSUtil', 'default_api_version', '2')

  if not config.has_section('GoogleCompute'):
    config.add_section('GoogleCompute')
  # TODO(jbeda): Plumb a metadata value to set this.  We probably want
  # to namespace the metadata values in some way like
  # 'boto_auth.servicee_account'.
  config.set('GoogleCompute', 'service_account', 'default')

  if not config.has_section('Plugin'):
    config.add_section('Plugin')
  config.set('Plugin', 'plugin_directory', AUTH_PLUGIN_DIR)

  with open(SYSTEM_BOTO_CONFIG, 'w') as configfile:
    AddConfigFileHeader(configfile)
    config.write(configfile)
Пример #11
0
 def reload_config(cls, filename):
     cls.config_file = filename
     print "Loading configuration from " + filename
     if not os.path.isfile(filename):
         print "Config file not found - failing"
         sys.exit(1)
     cls.remote = None
     cls.local = None
     cls.nova = None
     parser = SafeConfigParser()
     parser.read(filename)
     if not parser.has_section('remote'):
         print "No remote database configuration - failing"
         sys.exit(1)
     if not parser.has_section('local'):
         print "No local database configuration - failing"
         sys.exit(1)
     if not parser.has_section('nova') and cls.nova is None:
         print "No nova credentials provided - failing"
         sys.exit(1)
     cls.remote = {}
     cls.local = {}
     cls.nova = {}
     for (name, value) in parser.items('remote'):
         cls.remote[name] = value
     for (name, value) in parser.items('local'):
         cls.local[name] = value
     for (name, value) in parser.items('nova'):
         cls.nova[name] = value
     verify_nova_creds(cls.nova)
def _config_from_config_profile(config_file, profile, app):
    """
    Configures ec2stack app based on configuration profile.

    @param config_file: current config file configuration.
    @param profile: the profile to set the attribute in.
    """
    config = SafeConfigParser()
    config.read(config_file)

    if not config.has_section(profile):
        sys.exit('No profile matching ' + profile +
                 ' found in configuration, please run ec2stack-configure -p ' + profile)

    for attribute in config.options(profile):
        app.config[attribute.upper()] = config.get(profile, attribute)

    instance_type_map = {}
    instance_section = profile + "instancemap"
    if config.has_section(instance_section):
        for attribute in config.options(instance_section):
            instance_type_map[attribute] = config.get(
                instance_section, attribute)

    app.config['INSTANCE_TYPE_MAP'] = instance_type_map

    resource_type_map = {}
    resource_section = profile + "resourcemap"
    if config.has_section(resource_section):
        for attribute in config.options(resource_section):
            resource_type_map[attribute] = config.get(
                resource_section, attribute)

    app.config['RESOURCE_TYPE_MAP '] = resource_type_map
Пример #13
0
    def _load_schema(self, location, xml=None):
        """
        location -- location of schema, also used as a key
        xml -- optional string representation of schema
        """
        cachedir = self._cachedir
        # wsdl2py: deal with XML Schema
        if not os.path.isdir(cachedir): os.mkdir(cachedir)
    
        file = os.path.join(cachedir, '.cache')
        section = 'TYPES'
        cp = ConfigParser()
        try:
            cp.readfp(open(file, 'r'))
        except IOError:
            del cp;  cp = None
            
        option = location.replace(':', '-') # colons seem to screw up option
        if (cp is not None and cp.has_section(section) and 
            cp.has_option(section, option)):
            types = cp.get(section, option)
        else:
            # dont do anything to anames
            if not self._pyclass:
                containers.ContainerBase.func_aname = lambda instnc,n: str(n)
                
            from ZSI.wstools import XMLSchema
            reader = XMLSchema.SchemaReader(base_url=location)
            if xml is not None and isinstance(xml, basestring):
                schema = reader.loadFromString(xml)
            elif xml is not None:
                raise RuntimeError, 'Unsupported: XML must be string'
            elif not os.path.isfile(location):
                schema = reader.loadFromURL(location)
            else:
                schema = reader.reader.loadFromFile(location)
                
            # TODO: change this to keyword list
            class options:
                output_dir = cachedir
                schema = True
                simple_naming = False
                address = False
                lazy = self._lazy
                complexType = self._pyclass

            schema.location = location
            files = commands._wsdl2py(options, schema)
            if cp is None: cp = ConfigParser()
            if not cp.has_section(section): cp.add_section(section)
            types = filter(lambda f: f.endswith('_types.py'), files)[0]
            cp.set(section, option, types)
            cp.write(open(file, 'w'))
            
        if os.path.abspath(cachedir) not in sys.path:
            sys.path.append(os.path.abspath(cachedir))
            
        mod = os.path.split(types)[-1].rstrip('.py')
        return __import__(mod)
Пример #14
0
def fetch_repository(
    repository,
    workdir=None,
    branch=None,
    cache_config_dir=None,
    cache_config_file_name="cached_repositories.ini",
    reference_repository=None,
):
    """
    Fetch repository inside a workdir. Return filesystem path of newly created dir.
    if cache_config_dir is False, no attempt to use caching is used. If None, curdir is used, if string, it's taken as path to directory.
        If given directory is not writeable, warning is logged and fetch proceeds as if cache_config_dir would be False
    """
    write_repository_cache = False

    if cache_config_dir is not False:
        if not cache_config_dir:
            cache_config_dir = os.curdir

        if not os.path.isdir(cache_config_dir) or not os.access(cache_config_dir, os.W_OK):
            cache_config_dir = False
        else:
            cache_file_path = os.path.join(cache_config_dir, cache_config_file_name)
            parser = SafeConfigParser()
            write_repository_cache = True

            if os.path.exists(cache_file_path):
                parser.read([cache_file_path])
                if parser.has_section(repository) and parser.has_option(repository, "cache_dir"):
                    cached_repo = parser.get(repository, "cache_dir")
                    if os.path.exists(cached_repo):
                        return cached_repo

    # HACK: I'm now aware about some "generate me temporary dir name" function,
    # so I'll make this create/remove workaround - patch welcomed ,)
    dir = os.path.abspath(os.path.join(mkdtemp(dir=workdir), "repository"))

    clone = ["git", "clone", repository, dir]

    if reference_repository and os.path.exists(reference_repository):
        clone.extend(["--reference", reference_repository])

    check_call(clone, stdout=PIPE, stdin=PIPE, stderr=PIPE)

    if branch and branch != "master":
        check_call(
            ["git", "checkout", "-b", branch, "origin/%s" % branch], cwd=dir, stdout=PIPE, stdin=PIPE, stderr=PIPE
        )

    if write_repository_cache:
        if not parser.has_section(repository):
            parser.add_section(repository)
        parser.set(repository, "cache_dir", dir)
        f = open(cache_file_path, "w")
        parser.write(f)
        f.close()

    return dir
Пример #15
0
 def _get_file(self, config):
     """ Read a per-user .ini file, which is expected to have either
     a ``[scraperkit]`` or a ``[$SCRAPER_NAME]`` section. """
     config_file = SafeConfigParser()
     config_file.read([os.path.expanduser('~/.scrapekit.ini')])
     if config_file.has_section('scrapekit'):
         config.update(dict(config_file.items('scrapekit')))
     if config_file.has_section(self.scraper.name):
         config.update(dict(config_file.items(self.scraper.name)))
     return config
Пример #16
0
class Config():
    def __init__(self, configfile='./wap.conf'):
        self.config = SafeConfigParser()
        self.config.read(configfile)
        self.configfile = configfile
        self.database = dict()
        self.zonedirector = dict()
        self.ldap = dict()

        if self.config.has_section('database'):
            try:
                self.database['user'] = self.config.get('database', 'user')
                self.database['pass'] = self.config.get('database', 'pass')
                self.database['host'] = self.config.get('database', 'host')
                self.database['db'] = self.config.get('database', 'db')   
            except:
                self.database['user'] = None
                self.database['host'] = None
                self.database['pass'] = None
                self.database['db'] = None

        if self.config.has_section('zonedirector'):
            try:
                self.zonedirector['login'] = self.config.get('zonedirector', 'login')
                self.zonedirector['logout'] = self.config.get('zonedirector', 'logout')
            except:
                self.zonedirector['login'] = None
                self.zonedirector['logout'] = None

        if self.config.has_section('ldap'):
            try:
                self.ldap['enabled'] = self.config.get('ldap', 'enabled')
                self.ldap['basedn'] = self.config.get('ldap', 'basedn')
                self.ldap['binddn'] = self.config.get('ldap', 'binddn')
                self.ldap['bindpw'] = self.config.get('ldap', 'bindpw')
                self.ldap['server'] = self.config.get('ldap', 'server')
                self.ldap['port'] = int(self.config.get('ldap', 'port'))
                self.ldap['bindonauth'] = self.config.get('ldap', 'bindonauth')
                self.ldap['searchkey'] = self.config.get('ldap', 'searchkey')
            except:
                self.ldap = dict()
                self.ldap['enabled'] = False

            self.ldap['enabled'] = self.translateliteral(self.ldap['enabled'])
            self.ldap['binddn'] = self.translateliteral(self.ldap['enabled'])
            self.ldap['bindpw'] = self.translateliteral(self.ldap['enabled'])
            self.ldap['bindonauth'] = self.translateliteral(self.ldap['bindonauth'])

    def translateliteral(self, literal='None'):
        if literal == 'False':
            return False;
        if literal == 'True':
            return True;
        if literal == 'None':
            return None;
Пример #17
0
class ConfigBasedTests(unittest.TestCase):
    
    def setUp(self):
        self.log = logging.getLogger("Fourchapy.tests.ConfigBase.%s" % type(self).__name__)
        
        self.log.debug("Loading config")
        self.cfg = SafeConfigParser()
        self.log.debug("Reading config file")
        self.cfg.read(DEFAULT_CONFIG_FILE)
        
        self.log.debug("Setting up logging")
        if not self.cfg.has_section('Global'):
            self.cfg.add_section('Global')
        # Global config - Logging level
        if not self.cfg.has_option('Global', 'loggingLevel'):
            self.cfg.set('Global', 'loggingLevel', str(DEFAULT_LOGGING_LEVEL))
        self.log.setLevel(self.cfg.getint('Global', 'loggingLevel'))
        # Global config - proxies
        self.proxy = {}
        self.proxy['http'] = self._get_option(
                                              section = 'Global',
                                              option = 'proxy_http',
                                              default = None,
                                              vtype = 'str',
                                              )
# Doesn't actually work atm - urllib doesn't support using
# https and a proxy at the same time. 
#        self.proxy['https'] = self._get_option(
#                                              section = 'Global',
#                                              option = 'proxy_https',
#                                              default = None,
#                                              vtype = 'str',
#                                              )
        for k, v in self.proxy.items():
            if v is None:
                del self.proxy[k]
        self.log.debug("Set proxy to %r", self.proxy)
        
    def _get_option(self, section, option, default, vtype = 'str'):
        """ Get the option and set it if it doesn't exist """
        self.log.debug("Going to get/set %r.%r of type %r, default %r", section, option, vtype, default)
        if not self.cfg.has_section(section):
            self.cfg.add_section(section)
            self.log.debug("Added section %r to config", section)
        if not self.cfg.has_option(section, option):
            self.cfg.set(section, option, str(default))
            self.log.debug("Added option %r.%r to config with value %r", section, option, default)
        if vtype == 'str' or vtype is None:
            ret = self.cfg.get(section, option)
        else:
            attr = "get%s" % vtype
            assert hasattr(self.cfg, attr), "Entry type %r doesn't exist (aka ConfigObj.%r)" % (vtype, attr) 
            ret = getattr(self.cfg, attr)(section, option)
        self.log.debug("%r.%r=%r", section, option, ret)
        return ret
Пример #18
0
    def test_duplicated_command_output(self):
        """ Make sure that StatusProvider only updates the status if the command
        yields a different result than it did previously. """

        self.__setup_parser()

        mock_timer = self.mox.CreateMockAnything()

        self.mox.StubOutWithMock(SafeConfigParser, "has_section")
        self.mox.StubOutWithMock(SafeConfigParser, "get")
        self.mox.StubOutWithMock(threading, "Timer")
        self.mox.StubOutWithMock(threading.Thread, "start")
        self.mox.StubOutWithMock(subprocess.Popen, "__init__")
        self.mox.StubOutWithMock(subprocess.Popen, "communicate")
        self.mox.StubOutWithMock(Client, "change_status")

        SafeConfigParser.has_section("status").AndReturn(True)
        SafeConfigParser.get("status", "command").AndReturn("foobar")
        SafeConfigParser.get("status", "interval").AndReturn(3)

        threading.Timer(3, mox.IgnoreArg()).AndReturn(mock_timer)
        mock_timer.start()

        subprocess.Popen.__init__("foobar", stdout = subprocess.PIPE)
        subprocess.Popen.communicate().AndReturn(("result", ""))

        Client.change_status("result")

        threading.Timer(3, mox.IgnoreArg()).AndReturn(mock_timer)
        mock_timer.start()

        subprocess.Popen.__init__("foobar", stdout = subprocess.PIPE)
        subprocess.Popen.communicate().AndReturn(("result", ""))

        # This time we expect no status change, since this is the same result as
        # returned previously..
        #Client.change_status("result")

        threading.Timer(3, mox.IgnoreArg()).AndReturn(mock_timer)
        mock_timer.start()

        subprocess.Popen.__init__("foobar", stdout = subprocess.PIPE)
        subprocess.Popen.communicate().AndReturn(("another result", ""))

        Client.change_status("another result")

        self.mox.ReplayAll()

        provider = StatusProvider()
        provider.start()
        # We'll have to emulate a timeout here..
        provider.timeout()
        provider.timeout()
        provider.timeout()
def usage3():
    # has section or option
    parser = SafeConfigParser()
    parser.read('my.config')

    for section in ['wiki', 'bug_tracker', 'dvcs', 'section_one']:
        print '%-12s: %s' % (section, parser.has_section(section))
        if parser.has_section(section):
            for candidate in [ 'username', 'password', 'url', 'description' ]:
                print '%s.%-12s  : %s' % (section, candidate, parser.has_option(section, candidate))
            print
Пример #20
0
	def is_project_selected_in_config(self, target=None):
		config = SafeConfigParser()
		config.read(self.config_path)
		if target != None:
			assert config.has_section('Project')
			assert config.has_option('Project', 'selected')
			assert config.get('Project', 'selected') == target, \
				"<%s> in config should be <%s>" \
					% (config.get('Project', 'selected'), target)
		else:
			assert not config.has_section('Project')
Пример #21
0
def do_mqtt_forward(config, section, verbose):
    # default configuration
    default = SafeConfigParser({"hostname": "localhost", "port": "1883", "auth": "False", "user": "******", "password": "******", "retain": "False", "qos": "0"})
    default.optionxform = str
    default.read(config)

    # configuration
    cfg = SafeConfigParser({"client_id": "mqtt-forward-"+section+'-'+str(os.getpid()), "hostname": default.get("mqtt-forward", "hostname"), "port": default.get("mqtt-forward", "port"), "auth": default.get("mqtt-forward", "auth"), "transform": None, "plain": "False", "retain": default.get("mqtt-forward", "retain"), "qos": default.get("mqtt-forward", "qos"), "user": default.get("mqtt-forward", "user"), "password": default.get("mqtt-forward", "password")})
    cfg.optionxform = str
    cfg.read(config)

    # publication setup
    pubsection = cfg.get(section, "pub")
    if not cfg.has_section(pubsection):
        transform = pubsection
        pubtopic = None
        pubsection = section
    else:
        transform = cfg.get(pubsection, "transform")
        pubtopic = cfg.get(pubsection, "topic")
    if transform in transforms:
        transform = transforms[transform]
    if eval(cfg.get(pubsection, "auth")):
        pubauth = { "username": cfg.get(pubsection, "user"), "password": cfg.get(pubsection, "password") }
    else:
        pubauth = None
    pub = { "hostname": cfg.get(pubsection, "hostname"), "port": eval(cfg.get(pubsection, "port")), "auth": pubauth, "client_id": cfg.get(pubsection, "client_id"), "topic": pubtopic, "transform": transform, "plain": eval(cfg.get(pubsection, "plain")), "retain": eval(cfg.get(pubsection, "retain")), "qos": eval(cfg.get(pubsection, "qos")), "last": {} }

    # subscription setup
    subsection = cfg.get(section, "sub")
    if not cfg.has_section(subsection):
        subtopic = subsection
        subsection = section
    else:
        subtopic = cfg.get(subsection, "topic")
    subplain = eval(cfg.get(subsection, "plain"))
    sub = mqtt.Client(client_id=cfg.get(subsection, "client_id")+"_sub", userdata=(verbose, pub, subtopic, eval(cfg.get(subsection, "plain"))))
    sub.on_connect = on_connect
    sub.on_message = on_message
    if eval(cfg.get(subsection, "auth")):
        sub.username_pw_set(cfg.get(subsection, "user"), cfg.get(subsection, "password"))
    sub.connect(cfg.get(subsection, "hostname"), eval(cfg.get(subsection, "port")), 60)
    if verbose > 3:
        print(str(datetime.datetime.utcnow())+" subscribed @"+subtopic+" [" + subsection + "]: Plain = "+str(subplain))

    # Loop until done...
    sub.loop_start()
    while not done:
        sleep(1)
    sub.loop_stop()
    return 0
Пример #22
0
    def load_config_file(self):
        """Parse configuration file and get config values"""
        config_parser = SafeConfigParser()

        config_parser.read(self.CONFIG_FILE)

        if config_parser.has_section('handlers'):
            self._config['handlers_package'] = config_parser.get('handlers', 'package')

        if config_parser.has_section('auth'):
            self._config['consumer_key'] = config_parser.get('auth', 'consumer_key')
            self._config['consumer_secret'] = config_parser.get('auth', 'consumer_secret')
            self._config['token_key'] = config_parser.get('auth', 'token_key')
            self._config['token_secret'] = config_parser.get('auth', 'token_secret')
Пример #23
0
    def test_nonexisting_status_config_section(self):
        """ If there is no status section in the configuration file, no timer
        will be started, and no status updates will be performed. """

        self.__setup_parser()

        self.mox.StubOutWithMock(SafeConfigParser, "has_section")

        SafeConfigParser.has_section("status").AndReturn(False)

        self.mox.ReplayAll()

        provider = StatusProvider()
        provider.start()
Пример #24
0
 def _save_settings(self):
     config = self._get_config()
     if config is None: 
         config = SafeConfigParser()
     #setup common and program sections
     if not config.has_section('common'):
         config.add_section('common')
     if not config.has_section(self._name):
         config.add_section(self._name)
     #set options
     config.set('common', 'email', unicode(self._email).encode('UTF-8'))
     config.set(self._name, 'executable', unicode(self._bin).encode('UTF-8'))
     #save
     config.write(open(self._config_file(), 'wb'))
Пример #25
0
class ConfigStore(object):
    def __init__(self):
        self.config = SafeConfigParser()
        if hasattr(appdirs, 'user_config_dir'):
            data_dir = appdirs.user_config_dir('Photini')
        else:
            data_dir = appdirs.user_data_dir('Photini')
        if not os.path.isdir(data_dir):
            os.makedirs(data_dir, mode=0700)
        self.file_name = os.path.join(data_dir, 'photini.ini')
        old_file_name = os.path.expanduser('~/photini.ini')
        if os.path.exists(old_file_name):
            self.config.read(old_file_name)
            self.save()
            os.unlink(old_file_name)
        self.config.read(self.file_name)
        self.timer = QtCore.QTimer()
        self.timer.setSingleShot(True)
        self.timer.setInterval(3000)
        self.timer.timeout.connect(self.save)
        self.has_section = self.config.has_section

    def get(self, section, option, default=None):
        if self.config.has_option(section, option):
            return self.config.get(section, option)
        if default is not None:
            self.set(section, option, default)
        return default

    def set(self, section, option, value):
        if not self.config.has_section(section):
            self.config.add_section(section)
        if (self.config.has_option(section, option) and
                self.config.get(section, option) == value):
            return
        self.config.set(section, option, value)
        self.timer.start()

    def remove_section(self, section):
        if not self.config.has_section(section):
            return
        for option in self.config.options(section):
            self.config.remove_option(section, option)
        self.config.remove_section(section)
        self.timer.start()

    def save(self):
        self.config.write(open(self.file_name, 'w'))
        os.chmod(self.file_name, 0600)
Пример #26
0
class PackageConfigHandler(object):
    def __init__(self):
        self.package_cfg = os.path.expanduser('~/Documents/site-packages/.pypi_packages')
        if not os.path.isfile(self.package_cfg):
            print 'Creating package file'
            f = open(self.package_cfg,'w')
            f.close()
        self.parser = SafeConfigParser()
        self.parser.read(self.package_cfg)
            
    def save(self):
        with open(self.package_cfg,'w') as f:
            self.parser.write(f)
            
    def add_module(self):
        tbl = self.parent.pkg_info
        if not self.parser.has_section(tbl['name']):
            self.parser.add_section(tbl['name'])
        if self.pypi_package:
            self.parser.set(tbl['name'],'url','pypi')
        else:
            self.parser.set(tbl['name'],'url',tbl['url'])
        self.parser.set(tbl['name'],'version',tbl['version'])
        self.parser.set(tbl['name'],'summary',tbl['summary'])
        self.save()

    def list_modules(self):
        lst = []
        for module in self.parser.sections():
            lst.append(module)
        return lst
        
  
    def module_exists(self,name):
        if self.parser.has_section(name):
            return True
        else:
            return False
            
    def get_info(self,name):
        if self.parser.has_section(name):
            tbl = {}
            for opt, value in self.parser.items(name):
                tbl[opt] = value
            return tbl
            
    def remove_module(self,name):
        self.parser.remove_section(name)
        self.save()
Пример #27
0
    def parse_config_files(self):
        """
        Parse the possible config files and set appropriate values
        default values
        """
        parser = SafeConfigParser()
        # Fill in the built in values
        self.config = dict(self.__class__.defaults)
        # Update with the values from the defaults section. This is needed
        # in case the config file doesn't have a [<command>] section at all
        parser.read(self.config_files)
        self.config.update(dict(parser.defaults()))

        # Make sure we read any legacy sections prior to the real subcommands
        # section i.e. read [gbp-pull] prior to [pull]
        if (self.command.startswith('gbp-') or
            self.command.startswith('git-')):
            oldcmd = self.command
            if parser.has_section(oldcmd):
                self.config.update(dict(parser.items(oldcmd, raw=True)))
            cmd = self.command[4:]
        else:
            for prefix in ['gbp', 'git']:
                oldcmd = '%s-%s' % (prefix, self.command)
                if parser.has_section(oldcmd):
                    self.config.update(dict(parser.items(oldcmd, raw=True)))
            cmd = self.command

        # Update with command specific settings
        if parser.has_section(cmd):
            # Don't use items() until we got rid of the compat sections
            # since this pulls in the defaults again
            self.config.update(dict(parser._sections[cmd].items()))

        for section in self.sections:
            if parser.has_section(section):
                self.config.update(dict(parser._sections[section].items()))
            else:
                raise NoSectionError("Mandatory section [%s] does not exist."
                                     % section)

        # filter can be either a list or a string, always build a list:
        if self.config['filter']:
            if self.config['filter'].startswith('['):
                self.config['filter'] = eval(self.config['filter'])
            else:
                self.config['filter'] = [ self.config['filter'] ]
        else:
            self.config['filter'] = []
Пример #28
0
class PackageConfigHandler(object):
    def __init__(self):
        self.package_cfg = os.path.expanduser("~/Documents/site-packages/.pypi_packages")
        if not os.path.isfile(self.package_cfg):
            print "Creating package file"
            f = open(self.package_cfg, "w")
            f.close()
        self.parser = SafeConfigParser()
        self.parser.read(self.package_cfg)

    def save(self):
        with open(self.package_cfg, "w") as f:
            self.parser.write(f)

    def add_module(self):
        tbl = self.parent.pkg_info
        if not self.parser.has_section(tbl["name"]):
            self.parser.add_section(tbl["name"])
        if self.pypi_package:
            self.parser.set(tbl["name"], "url", "pypi")
        else:
            self.parser.set(tbl["name"], "url", tbl["url"])
        self.parser.set(tbl["name"], "version", tbl["version"])
        self.parser.set(tbl["name"], "summary", tbl["summary"])
        self.save()

    def list_modules(self):
        lst = []
        for module in self.parser.sections():
            lst.append(module)
        return lst

    def module_exists(self, name):
        if self.parser.has_section(name):
            return True
        else:
            return False

    def get_info(self, name):
        if self.parser.has_section(name):
            tbl = {}
            for opt, value in self.parser.items(name):
                tbl[opt] = value
            return tbl

    def remove_module(self, name):
        self.parser.remove_section(name)
        self.save()
Пример #29
0
 def loadStatus(self, name="./conf"):
     config = SafeConfigParser()
     config.read(name)
     try:
         if config.sections() != []:
             if config.has_section("GAME"):
                 self._quest = config.getint("GAME", "quest")
                 self.setState(config.getint("GAME", "state"))
                 self._secState = config.get("GAME", "secState")
                 if self._secState != "None":
                     self._secState = int(self._secState)
             if config.has_section("PLAYABLE_AGENTS"):
                 self.agentManager.reset()
                 for l in config.options("PLAYABLE_AGENTS"):
                     name = "{:s}:{:s}".format(l.split('_')[0].upper(), l.split('_')[1])
                     a = self.agentManager.getAgentByName(name)
                     params = config.get("PLAYABLE_AGENTS", l).split(';')
                     if a != None:
                         a.health = int(params[0])
                         a.magic = int(params[1])
                         l = a.agent.getLocation()
                         l.setLayerCoordinates(fife.ModelCoordinate(*(int(float(params[2])), int(float(params[3])))))
                         a.agent.setLocation(l)
                         a._mode = int(params[4])
                     self.agentManager.addNewPlayableAgent(name)
             if config.has_section("BEES"):
                 for l in config.options("BEES"):
                     name = "{:s}:{:s}:{:s}".format(l.split('_')[0].upper(), l.split('_')[1], l.split('_')[2])
                     b = self.agentManager.getAgentByName(name)
                     params = config.get("BEES", l).split(';')
                     if b != None:
                         l = fife.Location(self.world.map.getLayer('TechdemoMapGroundObjectLayer'))
                         l.setLayerCoordinates(fife.ModelCoordinate(*(int(float(params[0])), int(float(params[1])))))
                         b.agent.setLocation(l)
                         b.state = int(params[2])
                         b.mode = int(params[3])
                         b.start()
             self.dialog.hide_exit_menu()
             self.dialog.hide_load_menu()
     except AttributeError as e:
         print "###################################################################################"
         print "Unexpected error:", sys.exc_info()[0]
         print e.message
         print "--- Configuration file malformed. Deleted. ---"
         print "--- Please, restart again the game ---"
         print "###################################################################################"
         self.deleteStatus()
         self.exit()
Пример #30
0
    def load_information(self):
        """ Load the add-on's information from file. """
        parser = SafeConfigParser()
        with self.path.open(self.file) as file:
            parser.readfp(file, self.file)

        # Read the core information.
        if not parser.has_section("Core"):
            raise ValueError("No Core section in the add-on information file for the " "add-on %r." % self.name)

        for key in self.CORE_VALUES + ("version",):
            if isinstance(key, (list, tuple)):
                key, key_type = key
                if isinstance(key_type, basestring):
                    key_type = eval(key_type)
            else:
                key_type = lambda thing: thing

            # If we don't have that key, and we have a default value, just
            # continue, otherwise raise a ValueError.
            if not parser.has_option("Core", key):
                if not hasattr(self, key):
                    raise ValueError(
                        "Core value %r not defined in the add-on "
                        "information file for the add-on %r." % (key, self.name)
                    )
                continue

            # Load the value and set it as an attribute of self.
            setattr(self, key, key_type(parser.get("Core", key)))

        # Split the inheritance.
        if hasattr(self, "inherits") and self.inherits and isinstance(self.inherits, basestring):
            self.inherits = [x.strip() for x in self.inherits.split(",")]

        # Now, read the requirements.
        if parser.has_section("Requires"):
            for key, value in parser.items("Requires"):
                name, match = VersionMatch.from_string(value)
                self.requires[name] = match

        # Finally, read the data section. This generally just contains a nice
        # description of the add-on.
        if parser.has_section("Data"):
            self.data.update(parser.items("Data"))

        if parser.has_section("Description"):
            self.data.update(parser.items("Description"))
Пример #31
0
def ReadConfig(file, section):
    config = SafeConfigParser()
    mydir = os.path.dirname(os.path.abspath(__file__))
    path = os.path.join(os.getcwd(), file)
    dict1 = {}
    if os.path.isfile(path):
        config.read(path)
        if config.has_section(section):
            options = config.options(section)
            for option in options:
                try:
                    dict1[option] = config.get(section, option)
                    if dict1[option] == -1:
                        print("Skip: %s" % option)
                except:
                    print("Exception on %s" % option)
                    dict1[option] = None
        else:
            return {}
    else:
        print("Config file not found")
    return dict1
Пример #32
0
def read_conf(conf_path, section_name=None, defaults=None, use_yaml=False):
    if use_yaml:
        return parse_config(conf_path)
    if defaults is None:
        defaults = {}
    c = SafeConfigParser(defaults)
    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 section %s in config %s' %
                  (section_name, conf_path))
            sys.exit(1)
    else:
        conf = {}
        for s in c.sections():
            conf.update({s: dict(c.items(s))})
    return conf
Пример #33
0
def storage_init(conf, **kwargs):
    config_path = conf['path']

    global ACCESS_TOKEN, ACCESS_ADDRESS, HUB_SERVER, HUB_URL_PREFIX

    if os.path.exists( config_path ):
        parser = SafeConfigParser()
        try:
            parser.read(config_path)
        except Exception, e:
            log.exception(e)
            return False

        if parser.has_section('gaia_hub'):
            if parser.has_option('gaia_hub', 'token'):
                ACCESS_TOKEN = parser.get('gaia_hub', 'token')
            if parser.has_option('gaia_hub', 'address'):
                ACCESS_ADDRESS = parser.get('gaia_hub', 'address')
            if parser.has_option('gaia_hub', 'server'):
                HUB_SERVER = parser.get('gaia_hub', 'server')
            if parser.has_option('gaia_hub', 'url_prefix'):
                HUB_URL_PREFIX = parser.get('gaia_hub', 'url_prefix')
Пример #34
0
def set_to_config(option, value, section=None, path=conf_path):
    cnf = SafeConfigParser()
    try:
        if not section:
            section = conf_contents.get('section')
        cnf.readfp(open(path, 'r'))
        if not cnf.has_section(section) and section.lower() != 'default':
            cnf.add_section(section)
        cnf.set(section, option, value)
        with open(path, 'w') as f:
            cnf.write(f)
        return True
    except IOError:
        print "Error reading/writing {}".format(path)
    except KeyError:
        print "Section: {} not found!".format(section)
    except ValueError as e:
        print e
    except NoSectionError:
        print "Section: {} not found!".format(section)
    except NoOptionError:
        print "Option: {} not found!".format(option)
Пример #35
0
def load_namespace_conf(namespace):
    def places():
        yield '/etc/oio/sds.conf'
        for f in glob('/etc/oio/sds.conf.d/*'):
            yield f
        yield path.expanduser('~/.oio/sds.conf')

    c = SafeConfigParser({})
    success = c.read(places())
    if not success:
        print('Unable to read namespace config')
        exit(1)
    if c.has_section(namespace):
        conf = dict(c.items(namespace))
    else:
        print('Unable to find [%s] section config' % namespace)
        exit(1)
    proxy = conf.get('proxy')
    if not proxy:
        print("Missing field proxy in namespace config")
        exit(1)
    return conf
def main_hg(drepo):  # {{{1
    fname = os.path.join(drepo, ".hg", "hgrc")
    fname = os.path.abspath(fname)
    if not os.path.isfile(fname):
        info(".hgrc not found and skip...")
        return ""

    info("parse '%s'" % fname)
    cfg = SafeConfigParser()
    cfg.read(fname)
    if not cfg.has_section("paths"):
        eror("?? .hgrc has no section [paths], ignored.")
        return ""

    for opt in cfg.options("paths"):
        v = cfg.get("paths", opt)
        if not (v.startswith("git://") or v.startswith("git+ssh://")):
            continue
        if v.endswith(".git"):
            v = v[:-4]
        return v  # use 1st URL to git.
    return ""
Пример #37
0
def storage_init(conf, **kw):
    # read config options from the config file, if given
    global SERVER_NAME, SERVER_PORT

    config_path = conf['path']
    if os.path.exists(config_path):

        parser = SafeConfigParser()

        try:
            parser.read(config_path)
        except Exception, e:
            log.exception(e)
            return False

        if parser.has_section('ysi-server-storage'):

            if parser.has_option('ysi-server-storage', 'server'):
                SERVER_NAME = parser.get('ysi-server-storage', 'server')

            if parser.has_option('ysi-server-storage', 'port'):
                SERVER_PORT = int(parser.get('ysi-server-storage', 'port'))
Пример #38
0
def get_config(configfile, repos_name):
    """ read config for the given repository """
    logger = get_logger()
    defaults = dict(
        largediff=100,
        subject='',
        blacklist=
        "*/Ohai/*.json */Probes/probed.xml */SSHbase/ssh_host*_key.[GH]* */Packages/packages.conf"
    )
    config = SafeConfigParser(defaults)
    if os.path.exists(configfile):
        config.read(configfile)
    else:
        logger.fatal("Config file %s does not exist" % configfile)
        raise SystemExit(1)

    if not config.has_section(repos_name):
        logger.fatal("No configuration section found for '%s' repo, aborting" %
                     repos_name)
        raise SystemExit(2)

    return config
Пример #39
0
def write_config_file(opts, config_file):
    """
    Write our config file with the given options dict.
    Each key is a section name, and each value is the list of options.

    Return True on success
    Raise on error
    """

    if 'blockstack-client' in opts:
        assert 'path' not in opts['blockstack-client']
        assert 'dir' not in opts['blockstack-client']

    assert 'path' not in opts
    assert 'dir' not in opts

    parser = SafeConfigParser()

    if os.path.exists(config_file):
        parser.read(config_file)

    for sec_name in opts:
        sec_opts = opts[sec_name]

        if parser.has_section(sec_name):
            parser.remove_section(sec_name)

        parser.add_section(sec_name)
        for opt_name, opt_value in sec_opts.items():
            if opt_value is None:
                opt_value = ''

            parser.set(sec_name, opt_name, '{}'.format(opt_value))

    with open(config_file, 'w') as fout:
        os.fchmod(fout.fileno(), 0600)
        parser.write(fout)

    return True
Пример #40
0
    def loadSettings(self, fname):
        fpath = os.path.join(self.config_dir, fname)
        if not os.path.exists(fpath):
            fpath = os.path.join(APP_DIR, fname)
            if not os.path.exists(fpath):
                print("no %s settings file found" % fname)
                return False

        parser = SafeConfigParser()
        parser.read(fpath)
        if not all([
                parser.has_section(s)
                for s in ['global', 'source', 'destination', 'upload']
        ]):
            print("incomplete config file %s" % fpath)
            return False

        did_load = self.settingsFrame.loadSettings(parser)
        self.last_settings = getConfigOption(parser, 'global', 'last_settings')
        self.debug = getConfigOption(parser, 'global', 'debug', False, 'bool')
        print("settings loaded from %s" % fpath)
        return did_load
Пример #41
0
    def __init__(self):
        if not self.section_name:
            return

        candidates = [
            op.join(op.dirname(basedir), 'config.ini'),
            '/etc/suda/config.ini',
        ]

        parser = SafeConfigParser()
        parser.read(candidates)
        if not parser.has_section(self.section_name):
            import logging
            logging.warning('no section [%s]' % self.section_name)
            sys.exit(1)

        for key, value in parser.items(self.section_name):
            key = str(key).upper()
            setattr(self, key, value)

            if key.startswith('SQLALCHEMY_BINDS_'):
                self.SQLALCHEMY_BINDS[key.replace('SQLALCHEMY_BINDS_', '').lower()] = value
Пример #42
0
def import_theme_to_group(fname):
    """
    Import theme to group mapping configuration from path

    Function will parse .ini file and populate mapping tables. 

    This function will make commits internally, so caller should create fresh revision before commiting later.

    Sample configuration file:

[dcatapit:theme_group_mapping]

# can be one line of values separated by coma
Economy = economy01, economy02, test01, test02

# can be per-line list
Society = society
    economy01
    other02

# or mixed
OP_DATPRO = test01
    test02, test03, dupatest

    """
    fpath = os.path.abspath(fname)
    conf = ConfigParser()
    # otherwise theme names will be lower-cased
    conf.optionxform = str
    conf.read([fpath])
    if not conf.has_section(MAPPING_SECTION):
        log.warning("Theme to groups mapping config: cannot find %s section in %s",
                    MAPPING_SECTION, fpath)
        return
    out = {}
    for theme_name, groups in conf.items(MAPPING_SECTION, raw=True):
        out[theme_name] = groups.replace('\n', ',').split(',')
    log.info("Read theme to groups mapping definition from %s. %s themes to map.", fpath, len(out.keys()))
    return out
Пример #43
0
def _modify_config_profile(config_file, profile, advanced_network_enabled):
    """
    Modify configuration profile.

    @param config_file: current config file configuration.
    @param profile: the profile to set the attribute in.
    @return: configparser configuration.
    """
    config = SafeConfigParser()
    config.read(config_file)

    if not config.has_section(profile):
        config.add_section(profile)

    config = _set_mandatory_attributes_of_profile(config, profile)

    if advanced_network_enabled:
        config = _set_advanced_network_attributes_of_profile(config, profile)

    config = _set_optional_attributes_of_profile(config, profile)

    return config
Пример #44
0
def main():
    if len(sys.argv) < 3:
        sys.exit(
            'Usage: AddUser.py <username> <password> \n Set password to "-" to delete a user'
        )

    parser = SafeConfigParser()
    parser.read('users.ini')

    if not parser.has_section('users'):
        parser.add_section('users')

    try:
        if (sys.argv[2] == "-"):
            parser.remove_option('users', sys.argv[1])
        else:
            parser.set('users', sys.argv[1],
                       hashlib.md5(sys.argv[2].strip()).hexdigest())
    except:
        pass

    # prevent an empty file -- always have a default user if there is no other user
    if len(parser.options('users')) == 0:
        parser.set('users', 'default', hashlib.md5('default').hexdigest())

    file = open('users.ini', 'w')
    file.write(
        '#This is the list of users/encrypted passwords for the Linux CNC Web Server\n\n'
    )
    file.write('#Use the included python script AddUser to add users\n')
    file.write(
        '#Users can be removed by deleting the entries in this file with their user name\n'
    )
    file.write(
        '#This file will be auto-generated and overwritten by the AddUser program\n\n'
    )
    parser.write(file)
    file.close()
Пример #45
0
    def __gen_B0(self, command, repeat, profile, callback):
        # TODO: check for existing CONFIG_FILE
        config = SafeConfigParser()
        config.read(self.CONFIG_FILE)
        if not config.has_section(profile):
            raise NameError('Profile '%s' not found' % profile)
        buckets = map(int, config.get(profile, 'buckets').split(','))
        device = int(config.get(profile, 'Device'), 0)
        rollingCode = config.getint(profile, 'RollingCode') + 1
        longPulse = config.get(profile, 'Long')
        shortPulse = config.get(profile, 'Short')
        hwSync = config.get(profile, 'HWSync')
        swSync = config.get(profile, 'SWSync')

        payload = self.__gen_payload(command, rollingCode, device)
        payload = self.__calc_checksum(payload)
        self.__printFrame(payload)
        payload = self.__obfuscate(payload)
        bitvec = self.__to_bitvec(payload)

        encoder = ManchesterEncode()
        encoder.init(longPulse, shortPulse)
        encoder.addData(bitvec)
        dataStr = encoder.get_encoded()
        # FIXME: generate HWSync/SWSync string
        tmpStr = '05 %02X %04X %04X %04X %04X %04X %s%s%s%s4' % (
            repeat, buckets[0], buckets[1], buckets[2], buckets[3], buckets[4],
            hwSync * 14, swSync, longPulse, dataStr)
        strLen = int(len(tmpStr.replace(' ', '')) / 2)

        b0String = 'RfRaw AA B0 %02X %s 55' % (strLen, tmpStr)
        if self.debug:
            logging.debug(b0String)
        callback(b0String, config, profile)

        config.set(profile, 'RollingCode', str(rollingCode))
        with open(self.CONFIG_FILE, 'wb') as configfile:
            config.write(configfile)
Пример #46
0
    def getWebServer(cls, config_path):
        """
        Return a service suitable for creating an application object.
        """
    
        if not config_path.startswith("/"):
            raise ValueError, "config_path must be an absolute path"
    
        config = ConfigParser()
        config.read(config_path)
        bind = config.get("webserver", "bind")
        port = int(config.get("webserver", "port"))
        root_path = config.get("webserver", "root")
    
        if not root_path.startswith("/"):
            root_path = os.path.join(os.path.dirname(config_path), root_path)
    
        # create a resource to serve static files
        root = static.File(root_path)
        web_server = cls(root, port, bind)
    
        # setup proxies
        if config.has_section('url-maps'):
            for url, dest in config.items('url-maps'):
                tcp_host, tcp_port = dest.split(':')
                service = "generic"
                if ',' in tcp_port:
                    tcp_port, service = tcp_port.split(',')
                
                proxy = ProxyFactory(tcp_host, int(tcp_port))

                ws = WebSocketWrapperFactory(proxy)
                ws.protocol = services_wrappers.get(service, 
                                                services_wrappers["generic"])

                web_server.addHandler(url, ws.buildHandler)
    
        return web_server
Пример #47
0
    def __init__(self, path=None):
        if path is not None:
            self.path = path
        else:
            self.path = os.path.join(os.environ["HOME"], ".isb-cgc-pipelines",
                                     "config")

        config = SafeConfigParser()
        requiredParams = {
            "gcp": ["project_id", "zones", "scopes", "service_account_email"],
            "pipelines": [
                "pipelines_home", "max_running_jobs", "autorestart_preempted",
                "polling_interval"
            ]
        }

        innerDict = {}
        try:
            config.read(self.path)
        except IOError as e:
            print "Couldn't open ~/.isb-cgc-pipelines/config : {reason}".format(
                reason=e)
            exit(-1)
        else:
            for s, o in requiredParams.iteritems():
                if not config.has_section(s):
                    print "ERROR: missing required section {s} in the configuration!\nRUN `isb-cgc-pipelines config` to correct the configuration".format(
                        s=s)
                    exit(-1)

                for option in o:
                    if not config.has_option(s, option):
                        print "ERROR: missing required option {o} in section {s}!\nRun `isb-cgc-pipelines config` to correct the configuration".format(
                            s=s, o=option)
                        exit(-1)
                    innerDict[option] = config.get(s, option)

        self.__dict__.update(innerDict)
Пример #48
0
def remove(name, rc_file='~/.odoorpcrc'):
    """Remove the session configuration identified by `name`
    from the `rc_file` file.

    >>> import odoorpc
    >>> odoorpc.session.remove('foo')     # doctest: +SKIP

    .. doctest::
        :hide:

        >>> import odoorpc
        >>> session = '%s_session' % DB
        >>> odoorpc.session.remove(session)

    :raise: `ValueError` (wrong session name)
    """
    conf = ConfigParser()
    conf.read([os.path.expanduser(rc_file)])
    if not conf.has_section(name):
        raise ValueError("'%s' session does not exist in %s" % (name, rc_file))
    conf.remove_section(name)
    with open(os.path.expanduser(rc_file), 'wb') as file_:
        conf.write(file_)
Пример #49
0
def storage_init(conf, **kw):
    # read config options from the config file, if given
    global STORAGE_URL, RESOLVER_URL

    config_path = conf['path']
    if os.path.exists(config_path):

        parser = SafeConfigParser()

        try:
            parser.read(config_path)
        except Exception, e:
            log.exception(e)
            return False

        if parser.has_section('ysi-resolver-storage'):

            if parser.has_option('ysi-resolver-storage', 'storage_url'):
                SERVER_NAME = parser.get('ysi-resolver-storage', 'storage_url')

            if parser.has_option('ysi-resolver-storage', 'resolver_url'):
                SERVER_PORT = int(
                    parser.get('ysi-resolver-storage', 'resolver_url'))
Пример #50
0
def get_conf():
    # Create paths
    user_home = os.getenv('HOME')
    sushi_path = os.path.join(user_home, '.sushi')
    sushi_conf = os.path.join(sushi_path, 'sushi.conf')
    sushi_recipes = os.path.join(sushi_path, 'recipes')
    sushi_cookbooks = os.path.join(sushi_path, 'cookbooks')

    if not os.path.exists(sushi_path):
        logger.info('Create sushi conf folder')
        os.makedirs(sushi_path)
        os.makedirs(sushi_recipes)
        os.makedirs(sushi_cookbooks)
        with codecs.open(sushi_conf, mode='w', encoding='utf-8') as f:
            f.write(default_conf)

    parser = SafeConfigParser()
    parser.read(sushi_conf)

    parser.add_section('paths')
    parser.set('paths', 'home', user_home)
    parser.set('paths', 'sushi', sushi_path)
    parser.set('paths', 'sushi_conf', sushi_conf)
    parser.set('paths', 'sushi_recipes', sushi_recipes)
    parser.set('paths', 'sushi_cookbooks', sushi_cookbooks)

    # Defaults
    if not parser.has_section('settings'):
        parser.add_section('settings')
    if not parser.has_option('settings', 'license'):
        parser.set('settings', 'license', 'agpl-v3')
    if not parser.has_option('settings', 'recipe'):
        parser.set('settings', 'recipe', 'default')
    if not parser.has_option('settings', 'helpers'):
        parser.set('settings', 'helpers', '')

    return parser
Пример #51
0
    def __init__(self):
        super(MuninMySQLPlugin, self).__init__()

        self.dbname = ((sys.argv[0].rsplit('_', 1)[-1] if self.dbname_in_args else None)
            or os.environ.get('DATABASE') or self.default_table)

        self.conninfo = dict(
            user = "******",
            host = "localhost",
        )

        cnfpath = ""

        m = re.findall(r"--defaults-file=([^\s]+)", os.environ.get("mysqlopts") or "")
        if m:
            cnfpath = m[0]

        if not cnfpath:
            m = re.findall(r"mysql_read_default_file=([^\s;:]+)", os.environ.get("mysqlconnection") or "")
            if m:
                cnfpath = m[0]

        if cnfpath:
            cnf = SafeConfigParser()
            cnf.read([cnfpath])
            for section in ["client", "munin"]:
                if not cnf.has_section(section):
                    continue
                for connkey, opt in [("user", "user"), ("passwd", "password"), ("host", "host"), ("port", "port")]:
                    if cnf.has_option(section, opt):
                        self.conninfo[connkey] = cnf.get(section, opt)

        for k in ('user', 'passwd', 'host', 'port'):
            # Use lowercase because that's what the existing mysql plugins do
            v = os.environ.get(k)
            if v:
                self.conninfo[k] = v
Пример #52
0
class IniFileConfigurationLoader(AbstractFileConfigurationLoader):
    default_section = "settings"
    patterns = ("*.ini", "*.cfg")

    def __init__(self, filename, section=None):
        self.filename = filename

        if not section:
            section = self.default_section

        self.section = section

        self.parser = ConfigParser(allow_no_value=True)

        with open(self.filename) as inifile:
            try:
                if sys.version_info[0] < 3:
                    # ConfigParser.readfp is deprecated for Python3, read_file replaces it
                    # noinspection PyDeprecation
                    self.parser.readfp(inifile)
                else:
                    self.parser.read_file(inifile)
            except (UnicodeDecodeError, MissingSectionHeaderError):
                raise InvalidConfigurationFile()

        if not self.parser.has_section(self.section):
            raise InvalidConfigurationFile("Missing [{}] section in {}".format(
                self.section, self.filename))

    def __contains__(self, item):
        return self.parser.has_option(self.section, item)

    def __getitem__(self, item):
        try:
            return self.parser.get(self.section, item)
        except NoOptionError:
            raise KeyError("{!r}".format(item))
Пример #53
0
def default_chaincom_opts( config_file=None ):
   """
   Get our default chain.com options from a config file.
   """

   if config_file is None:
      config_file = virtualchain.get_config_filename()

   parser = SafeConfigParser()
   parser.read( config_file )

   chaincom_opts = {}

   api_key_id = None
   api_key_secret = None

   if parser.has_section('chain_com'):

      if parser.has_option('chain_com', 'api_key_id'):
         api_key_id = parser.get('chain_com', 'api_key_id')

      if parser.has_option('chain_com', 'api_key_secret'):
         api_key_secret = parser.get('chain_com', 'api_key_secret')

   chaincom_opts = {
       'utxo_provider': "chain_com",
       'api_key_id': api_key_id,
       'api_key_secret': api_key_secret
   }


   # strip Nones
   for (k, v) in chaincom_opts.items():
      if v is None:
         del chaincom_opts[k]

   return chaincom_opts
Пример #54
0
def storage_init(conf, index=False, force_index=False):
    """
    Initialize dropbox storage driver
    """
    global DROPBOX_TOKEN, DVCONF
    compress = False
    config_path = conf['path']

    if os.path.exists( config_path ):

        parser = SafeConfigParser()
        
        try:
            parser.read(config_path)
        except Exception, e:
            log.exception(e)
            return False

        if parser.has_section('dropbox'):
            if parser.has_option('dropbox', 'token'):
                DROPBOX_TOKEN = parser.get('dropbox', 'token')

            if parser.has_option('dropbox', 'compress'):
                compress = (parser.get('dropbox', 'compress').lower() in ['1', 'true', 'yes'])
Пример #55
0
def write_config_section(config_path, section_name, section_data):
    """
    Write a whole config section.
    Overwrite it if it exists.
    Return True on success
    Return False on failure
    """
    if not os.path.exists(config_path):
        return False

    parser = SafeConfigParser()
    parser.read(config_path)

    if not parser.has_section(section_name):
        parser.add_section(section_name)

    for field_name, field_value in section_data.items():
        parser.set(section_name, field_name, field_value)

    with open(config_path, 'w') as fout:
        os.fchmod(fout.fileno(), 0600)
        parser.write(fout)

    return True
Пример #56
0
class Loader(object):
    def __init__(self, profile):

        self._config = SafeConfigParser()
        self._config.read(profile)

        self.login_name = self._config.get('account', 'name')
        self.login_pwd = self._config.get('account', 'pwd')

        # TODO: should check date format... 2016-09-05
        self.date_start = self._config.get('timecard-setting', 'date_start')
        self.date_end = self._config.get('timecard-setting', 'date_end')
        self.max_work_hour = self._config.get('timecard-setting',
                                              'max_work_hour')

        # build weekday activities
        self.weekday_activities = []
        for weekday in range(1, 6):

            weekday_section = "weekday%s" % str(weekday)
            assert self._config.has_section(weekday_section), \
                    'There\'s no section:"%s"' % weekday_section

            self.weekday_activities.append(self._config.items(weekday_section))
Пример #57
0
def storage_init(conf, index=False, force_index=False):
    """
   Local disk implementation of the storage_init API call.
   Do one-time global setup--i.e. make directories.
   Return True on success
   Return False on error 
   """
    global DISK_ROOT, INDEXED_STORAGE, CONFIG_PATH, DVCONF

    config_path = conf['path']
    if os.path.exists(config_path):

        parser = SafeConfigParser()

        try:
            parser.read(config_path)
        except Exception, e:
            log.exception(e)
            return False

        if parser.has_section('test-storage'):

            if parser.has_option('test-storage', 'root'):
                DISK_ROOT = parser.get('disk', 'root')
Пример #58
0
def parseConfig(file_path):
    global path, freq, url, ipv4_net, ipv6_net, interface
    parser = SafeConfigParser()
    parser.read(file_path)
    section = "firewall_config"
    if (parser.has_section(section)):
        for candidate in [
                'capt_interface', 'capt_path', 'dest_url', 'send_freq',
                'net_ipv4_addr', 'net_ipv6_addr'
        ]:
            if (parser.has_option(section, candidate)):
                if (candidate == "capt_path"):
                    path = parser.get(section, candidate)

                elif (candidate == "capt_interface"):
                    interface = parser.get(section, candidate)

                elif (candidate == "dest_url"):
                    url = parser.get(section, candidate)

                elif (candidate == "send_freq"):
                    freq = parser.get(section, candidate)

                elif (candidate == "net_ipv4_addr"):
                    ipv4_net = parser.get(section, candidate)

                elif (candidate == "net_ipv6_addr"):
                    ipv6_net = parser.get(section, candidate)
            else:
                print candidate + " not found."
                return 0

    else:
        print "No section found : [firewall_config]"
        return 0
    return 1
Пример #59
0
def get_scrapycfg_targets(cfgfiles=None):
    cfg = SafeConfigParser()
    cfg.read(cfgfiles or [])
    baset = dict(cfg.items('deploy')) if cfg.has_section('deploy') else {}
    targets = {}
    targets['default'] = baset
    for x in cfg.sections():
        if x.startswith('deploy:'):
            t = baset.copy()
            t.update(cfg.items(x))
            targets[x[7:]] = t
    for tname, t in targets.items():
        try:
            int(t.get('project', 0))
        except ValueError:
            # Don't import non-numeric project IDs, and also throw away the
            # URL and credentials associated with these projects (since the
            # project ID does not belong to SH, neither do the endpoint or the
            # auth information)
            del targets[tname]
        if t.get('url', "").endswith('scrapyd/'):
            t['url'] = t['url'][:-8]
    targets.setdefault('default', {})
    return targets
Пример #60
0
def default_blockstack_utxo_opts(config_file=None):
    """
   Get our default Blockstack UTXO proxy options from a config file.
   """

    if config_file is None:
        raise Exception("No config file given")

    parser = SafeConfigParser()
    parser.read(config_file)

    blockstack_utxo_opts = {}

    server = None
    port = None

    if parser.has_section("blockstack_utxo"):

        if parser.has_option("blockstack_utxo", "server"):
            server = parser.get('blockstack_utxo', 'server')

        if parser.has_option("blockstack_utxo", "port"):
            port = int(parser.get("blockstack_utxo", "port"))

    blockstack_utxo_opts = {
        "utxo_provider": "blockstack_utxo",
        "server": server,
        "port": port
    }

    # strip Nones
    for (k, v) in blockstack_utxo_opts.items():
        if v is None:
            del blockstack_utxo_opts[k]

    return blockstack_utxo_opts