def get_option(self, key, default=NoDefault, asbool=False, section="options"):
        config = RawConfigParser()
        fp = StringIO(self.config)
        try:
            config.readfp(fp)
        except MissingSectionHeaderError:
            if default is NoDefault:
                raise
            return default
        try:
            value = config.get(section, key)
        except (NoOptionError, NoSectionError):
            if default is NoDefault:
                raise
            return default

        if not asbool:
            return value.strip()

        value = value.lower()
        if value in ("1", "true", "t", "yes", "y", "on"):
            return True
        elif value in ("0", "false", "f", "no", "n", "off"):
            return False
        else:
            raise TypeError("Cannot convert to bool: %s" % value)
Beispiel #2
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 #3
0
class HostTest(unittest.TestCase):
    """
    Test various host configurations
    """
    
    def setUp(self):
        optparser = BaseOptions()
        optparser.parseOptions(['dummyfile.xml', '--debug=%s' % logging._levelNames[log.level].lower()])

        self.defaults = RawConfigParser()
        configfiles = self.defaults.read(TESTCONF)
        self.defaults.get('global', 'dns_domain_name')
        if len(configfiles) == 0:
            raise ValueError("Cannot load configuration file: %s" % optparser.options.configfile)

        self.sitea = Site()
        self.sitea.name = 'sitea'
        self.sitea.type = 'primary'
        self.sitea.location = 'testlab'

    def test_empty_host(self):
        """
        Test an empty host node
        """
        xmlfile = os.path.join(XML_FILE_LOCATION, 'host_empty.xml')
        tree = etree.parse(xmlfile)
        node = tree.getroot()
        self.failUnlessRaises(KeyError, host.create_host_from_node, node, self.defaults, self.sitea)
        
    def test_named_host(self):
        """
        Test a host node with just a name
        """
        xmlfile = os.path.join(XML_FILE_LOCATION, 'host_named.xml')
        tree = etree.parse(xmlfile)
        node = tree.getroot()
        self.failUnlessRaises(KeyError, host.create_host_from_node, node, self.defaults, self.sitea)

    def test_named_host_os(self):
        """
        Test a host node with just a name
        """
        xmlfile = os.path.join(XML_FILE_LOCATION, 'host_named_os.xml')
        tree = etree.parse(xmlfile)
        node = tree.getroot()
        hostobj = host.create_host_from_node(node, self.defaults, self.sitea)
        self.failUnlessEqual(hostobj.name, 'fred')
        self.failUnlessEqual(hostobj.operatingsystem, 'Linux')

    def test_named_host_os_platform(self):
        """
        Test a host node with just a name
        """
        xmlfile = os.path.join(XML_FILE_LOCATION, 'host_named_os_platform.xml')
        tree = etree.parse(xmlfile)
        node = tree.getroot()
        hostobj = host.create_host_from_node(node, self.defaults, self.sitea)
        self.failUnlessEqual(hostobj.name, 'fred')
        self.failUnlessEqual(hostobj.operatingsystem, 'Linux')
        self.failUnlessEqual(hostobj.platform, 'intel')
Beispiel #4
0
def load():
    """Loads FileDirectives from ConfigFile into this module's attributes."""
    section = "*"
    module = sys.modules[__name__]
    parser = RawConfigParser()
    parser.optionxform = str # Force case-sensitivity on names
    try:
        parser.read(ConfigFile)

        def parse_value(name):
            try: # parser.get can throw an error if value not found
                value_raw = parser.get(section, name)
            except Exception:
                return False, None
            # First, try to interpret as JSON
            try:
                value = json.loads(value_raw)
            except ValueError: # JSON failed, try to eval it
                try:
                    value = eval(value_raw)
                except SyntaxError: # Fall back to string
                    value = value_raw
            return True, value

        for oldname, name in FileDirectiveCompatiblity.items():
            [setattr(module, name, v) for s, v in [parse_value(oldname)] if s]
        for name in FileDirectives:
            [setattr(module, name, v) for s, v in [parse_value(name)] if s]
        for name in OptionalFileDirectives:
            OptionalFileDirectiveDefaults[name] = getattr(module, name, None)
            success, value = parse_value(name)
            if success:
                setattr(module, name, value)
    except Exception:
        pass # Fail silently
Beispiel #5
0
    def render_POST(self, request):

        def _renderResponse(response):
            if isinstance(response, Failure):
                request.write(json.dumps({'success': False, 'message': response.getErrorMessage()}))
            else:
                request.write(json.dumps({'success': True}))
            request.finish()

        content = parse_qs(request.content.read())

        command = content.get('playlist.add', [None])[0]
        item = content['item'][0]


        config_parser = RawConfigParser()
        config_parser.read(os.path.expanduser("~/.config/smplayer/smplayer.ini"))
        port = config_parser.getint('instances', 'temp\\autoport')


        creator = ClientCreator(reactor, SMPlayer, item = item)
        creator.connectTCP('127.0.0.1', port).addBoth(_renderResponse)

        request.setHeader('Access-Control-Allow-Origin', '*')
        request.setHeader('Content-Type', 'application/json')
        return NOT_DONE_YET
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 __init__(self, hashfile=LOCATION, storeKey="", configFile=CONFIGFILE):
          self.userDict = { }                       # Mapping of username -> salt:hash
          self.hashfile = hashfile                  # Encrypted file, contains lines of user:salt:hash 
          self.h = SHA256.new()
          self.crypto = crypto.TBCrypt()            # AES encryption/IV functions
          self.storeKey = storeKey                  # Key material to open encrypted hash store
          if configFile:
               try:
                   fp = open(configFile)
               except IOError as e:
                   error = 'IOError: can''t access file ''%s'' (%s).' % (configFile, os.strerror(e.errno))
                   raise HashStoreException(error)
          
               config = RawConfigParser()
               config.read(configFile)
               self.storeKey = config.get('hashstore', 'key')
               self.hashfile = config.get('hashstore', 'location')
          else:
               if os.path.exists(self.hashfile):
                    self.updateUserDict()
               else:
                    self.__saveHashstore()

          if self.storeKey == '':
               print "WARNING: hashstoremanager.py: no hashstore key defined!"

#          print "h.update"
          self.h.update(self.storeKey)
#          print "self.storeKey = self.h.digest()"
          self.storeKey = self.h.digest()
Beispiel #8
0
 def parseSettings(self):
     try:
         self.prnt("Reading in settings from settings.ini...")
         oldchans = self.joinchans
         settings = ConfigParser()
         settings.read("settings.ini")
         channels = settings.items("channels")
         for element in self.joinchans:
             self.joinchans.remove(element)
         for element in channels:
             self.joinchans.append(element)
             if not element in oldchans and not self.firstjoin == 1:
                 self.join("#%s" % element[0])
         for element in oldchans:
             if element not in self.joinchans and not self.firstjoin == 1:
                 self.part("#%s" % element[0])
         self.password = settings.get("info", "password")
         if not self.firstjoin == 1:
             self.sendmsg("nickserv", "IDENTIFY %s" % self.password)
         self.loginpass = settings.get("info", "loginpass")
         self.control_char = settings.get("info", "control_character")
         self.data_dir = settings.get("info", "data_folder")
         self.index_file = settings.get("info", "index_file")
         self.api_key = settings.get("mcbans", "api_key")
     except:
         return False
     else:
         self.prnt("Done!")
         return True
Beispiel #9
0
class ApplicationConfig(object):
    """A thin wrapper around ConfigParser that remembers what we read.

    The remembered settings can then be written out to a minimal config file
    when building the Elastic Beanstalk zipfile.

    """
    def __init__(self):
        self.input = RawConfigParser()
        with open("production.ini") as f:
            self.input.readfp(f)
        self.output = RawConfigParser()

    def get(self, section, key):
        value = self.input.get(section, key)

        # remember that we needed this configuration value
        if (section.upper() != "DEFAULT" and
            not self.output.has_section(section)):
            self.output.add_section(section)
        self.output.set(section, key, value)

        return value

    def to_config(self):
        io = cStringIO.StringIO()
        self.output.write(io)
        return io.getvalue()
Beispiel #10
0
    def from_config_file(self, config_file, allow_profile = False):
        """
        Get the settings from a configuration file.

        :param config_file: Configuration file.
        :type config_file: str

        :param allow_profile: True to allow reading the profile name
            from the config file, False to forbid it. Global config
            files should allow setting a default profile, but profile
            config files should not, as it wouldn't make sense.
        """
        parser = RawConfigParser()
        parser.read(config_file)
        if parser.has_section("golismero"):
            options = { k:v for k,v in parser.items("golismero") if v }
            if "profile" in options:
                if allow_profile:
                    self.profile = options["profile"]
                    self.profile_file = get_profile(self.profile)
                else:
                    del options["profile"]
            for k in self._forbidden_:
                if k in options:
                    del options[k]
            if options:
                self.from_dictionary(options)
Beispiel #11
0
def load():
    """Loads FileDirectives from ConfigFile into this module's attributes."""
    section = "*"
    module = sys.modules[__name__]
    parser = RawConfigParser()
    parser.optionxform = str # Force case-sensitivity on names
    try:
        parser.read(ConfigFile)
        for name in FileDirectives:
            try: # parser.get can throw an error if not found
                value_raw = parser.get(section, name)
                success = False
                # First, try to interpret as JSON
                try:
                    value = json.loads(value_raw)
                    success = True
                except:
                    pass
                if not success:
                    # JSON failed, try to eval it
                    try:
                        value = eval(value_raw)
                        success = True
                    except:
                        # JSON and eval failed, fall back to string
                        value = value_raw
                        success = True
                if success:
                    setattr(module, name, value)
            except:
                pass
    except Exception, e:
        pass # Fail silently
Beispiel #12
0
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 #13
0
def get_license_devices():
    """
    Retrieves the number of assets for a given license

    Return:
        Number of devices signed for a license
        0 if an error occurs
        1000000 if 'devices' is not specified in the ossim.lic file
    """
    rc, pro = system_is_professional()
    devices = 0
    if rc and pro:
        if os.path.isfile("/etc/ossim/ossim.lic"):
            try:
                config = RawConfigParser()
                license_file = config.read('/etc/ossim/ossim.lic')
                devices = config.getint('appliance', 'devices')
            except NoOptionError:
                devices = 1000000

        else:
            api_log.debug("License devices can't be determined: License file not found")
            devices = 0

    else:
        devices = 1000000

    return devices
Beispiel #14
0
def test_no_notListed():
    cfg = RawConfigParser()
    cfg.add_section('group hackers')
    cfg.set('group hackers', 'members', 'wsmith')
    gen = group.getMembership(config=cfg, user='******')
    eq(gen.next(), 'all')
    assert_raises(StopIteration, gen.next)
Beispiel #15
0
def test_imap_config_values_should_be_stored():
	am = AccountManager()
	option_spec = get_mailbox_parameter_specs('imap')
	options = {
		'user': '******',
		'password': '',
		'server': 'imap.example.org',
		'port': '',
		'ssl': True,
		'imap': True,
		'idle': True,
		'folders': ['a', 'b'],
	}
	config = RawConfigParser()
	config.add_section('account1')
	am._set_cfg_options(config, 'account1', options, option_spec)
	expected_config_items = [
		('user', 'you'),
		('password', ''),
		('server', 'imap.example.org'),
		('port', ''),
		('ssl', '1'),
		('imap', '1'),
		('idle', '1'),
		('folder', '["a", "b"]'),
	]
	assert set(expected_config_items) == set(config.items('account1'))
 def parser_to_dict(self, filename):
     parser = RawConfigParser()
     parser.readfp(open(filename, "r"))
     dict_ = {}
     options = parser.options("LIB")
     for option in options: dict_[option] = parser.get("LIB", option)
     return dict_
Beispiel #17
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)
        }
Beispiel #18
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 #19
0
 def __init__(self, node_type, config_file):
     self.__params = self.__get_fields(self.CommonConfig)
     self.node_type = node_type
     if node_type == NodeType.VXRD:
         self.__params.update(self.__get_fields(self.VxrdConfig))
         pidfile = DefaultPidFile.VXRD
         udsfile = DefaultUdsFile.VXRD
     elif node_type == NodeType.VXSND:
         self.__params.update(self.__get_fields(self.VxsndConfig))
         pidfile = DefaultPidFile.VXSND
         udsfile = DefaultUdsFile.VXSND
     else:
         raise RuntimeError('Invalid node type %s. Acceptable values are '
                            '%s' % (node_type, ', '.join(NodeType.VALUES)))
     default_dict = {field.name: field.default for field in self.__params}
     config_parser = RawConfigParser(defaults=default_dict,
                                     allow_no_value=True)
     config_parser.read([config_file, config_file + '.override'])
     for field in self.__params:
         try:
             value = config_parser.get(field.section, field.name)
         except NoSectionError:
             value = field.default
         setattr(self, field.name, field(value))
     self.pidfile = self.pidfile or pidfile
     self.udsfile = self.udsfile or udsfile
	def __init__(self, conf_path = '..'):
		'''
		初始化配置文件
		'''
		#数据库、redis等配置
		self.conncfg = '%s/conf/connect.cfg' % (conf_path)
		#日志文件配置 
		self.logcfg = '%s/conf/logger.conf' % (conf_path)
		logging.config.fileConfig(self.logcfg)
		self.logger = logging.getLogger('Request')
		#sql语句配置
		self.sqlcfg = '%s/conf/sql.cfg' % (conf_path)

		self.config = RawConfigParser()
		self.config.read(self.conncfg)
		self.con_lock = threading.Lock()
		self.sqlfig = RawConfigParser()
		self.sqlfig.read(self.sqlcfg)		

		self.custid_ip_count = 0
		self.ip_in_db = 0
		self.tel_in_spamlist = 0
		self.permid_custid_count = 0
		self.tel_total = 0
		self.tel_custid_count = 0
Beispiel #21
0
    def __init__(self, config_file, config_type=None):
        """
        Init a fits_reduce config instance.

        Parameters
        ----------
        config_file : string
            Path to the config file

        config_type : string
            Means which script is calling this object.

        See Also
        --------
        ConfigParser.RawConfigParser
        """
        RawConfigParser.__init__(self)

        # Permitted units for the images
        self._permitted_image_units = ['adu', 'electron', 'photon', '1', '']

        # Check if file exists
        if not os.path.exists(config_file):
            raise IOError('File %s does not exists.' % config_file)
        # Load configfile
        self.read(config_file)
        if config_type == 'reducer':
            self.ret = self._load_reducer_vars()
        else:
            print 'Error! Invalid config_type %s.' % config_type
Beispiel #22
0
 def remove_option(self, section, option, also_remove_default=False):
     try:
         if also_remove_default:
             DEFAULTS[section].pop(option)
         RawConfigParser.remove_option(self, section, option)
     except (NoSectionError, KeyError):
         pass
Beispiel #23
0
def _fetchAzureAccountKey(accountName):
    """
    Find the account key for a given Azure storage account.

    The account key is taken from the AZURE_ACCOUNT_KEY_<account> environment variable if it
    exists, then from plain AZURE_ACCOUNT_KEY, and then from looking in the file
    ~/.toilAzureCredentials. That file has format:

    [AzureStorageCredentials]
    accountName1=ACCOUNTKEY1==
    accountName2=ACCOUNTKEY2==
    """
    try:
        return os.environ['AZURE_ACCOUNT_KEY_' + accountName]
    except KeyError:
        try:
            return os.environ['AZURE_ACCOUNT_KEY']
        except KeyError:
            configParser = RawConfigParser()
            configParser.read(os.path.expanduser(credential_file_path))
            try:
                return configParser.get('AzureStorageCredentials', accountName)
            except NoOptionError:
                raise RuntimeError("No account key found for '%s', please provide it in '%s'" %
                                   (accountName, credential_file_path))
Beispiel #24
0
class Config(dict):
    def __init__(self):
        dict.__init__(self)
        self.refresh()

    def __get_conf_val(self, section, param, var_name, p_type=str):
        try:
            val = self.__config.get(section, param)
            self[var_name] = p_type(val)
        except ConfigParser.Error:
            pass

    def refresh(self):
        try:
            self.update(self.get_defaults())
            config_file = self.get_config_file_path()

            if not os.path.exists(config_file):
                self.save()

            self.__config = RawConfigParser()
            self.__config.read(config_file)

            self.__get_conf_val('LOG','log_level', 'log_level')
            self.__get_conf_val('FABNET', 'fabnet_url', 'fabnet_hostname')
            self.__get_conf_val('FABNET', 'parallel_put_count', 'parallel_put_count', int)
            self.__get_conf_val('FABNET', 'parallel_get_count', 'parallel_get_count', int)
            self.__get_conf_val('CACHE', 'data_dir', 'data_dir')
            self.__get_conf_val('CACHE', 'cache_size', 'cache_size', int)
            self.__get_conf_val('WEBDAV', 'bind_hostname', 'webdav_bind_host')
            self.__get_conf_val('WEBDAV', 'bind_port', 'webdav_bind_port')
            self.__get_conf_val('WEBDAV', 'mount_type', 'mount_type')
            self.__get_conf_val('CA', 'ca_address', 'ca_address')
        except ConfigParser.Error, msg:
            raise Exception('ConfigParser: %s' % msg)
Beispiel #25
0
    def check_file(self, pkg, filename):
        root = pkg.dirName()
        f = root + filename
        st = getstatusoutput(('desktop-file-validate', f), True)
        if st[0]:
            error_printed = False
            for line in st[1].splitlines():
                if 'error: ' in line:
                    printError(pkg, 'invalid-desktopfile', filename,
                               line.split('error: ')[1])
                    error_printed = True
            if not error_printed:
                printError(pkg, 'invalid-desktopfile', filename)
        if not is_utf8(f):
            printError(pkg, 'non-utf8-desktopfile', filename)

        cfp = RawConfigParser()
        cfp.read(f)
        binary = None
        if cfp.has_option('Desktop Entry', 'Exec'):
            binary = cfp.get('Desktop Entry', 'Exec').split(' ', 1)[0]
        if binary:
            found = False
            if binary.startswith('/'):
                found = os.path.exists(root + binary)
            else:
                for i in STANDARD_BIN_DIRS:
                    if os.path.exists(root + i + binary):
                        # no need to check if the binary is +x, rpmlint does it
                        # in another place
                        found = True
                        break
            if not found:
                printWarning(pkg, 'desktopfile-without-binary', filename,
                             binary)
Beispiel #26
0
def pms_readConf(filename):
	class multiKeyDict(DictMixin):
		def __init__(self, d=None):
			self._data = d or {}
		def __setitem__(self, key, value):
			if key in self._data and isinstance(self._data[key], list) and isinstance(value, list):
				self._data[key].extend(value)
			else:
				self._data[key] = value
		def __getitem__(self, key):
			return self._data[key]
		def __delitem__(self, key):
			del self._data[key]
		def keys(self):
			return self._data.keys()
		def copy(self):
			return multiKeyDict(self._data.copy())
	conf = RawConfigParser(dict_type=multiKeyDict)
	for c in [filename, os.path.join(pms.getProfileDir(), os.path.basename(filename))]:
		if os.path.exists(c):
			conf.readfp(StringIO('[.]\n'+open(c).read()))
	try:
		conf = dict(conf.items('.'))
		print '%s: %s' % (filename, conf)
		return conf
	except:
		return {}
Beispiel #27
0
    def get_backend(self, backend_name):
        """
        Get options of backend.

        :returns: a tuple with the module name and the module options dict
        :rtype: tuple
        """

        config = RawConfigParser()
        config.read(self.confpath)
        if not config.has_section(backend_name):
            raise KeyError(u'Configured backend "%s" not found' % backend_name)

        items = dict(config.items(backend_name))

        try:
            module_name = items.pop('_module')
        except KeyError:
            try:
                module_name = items.pop('_backend')
                self.edit_backend(backend_name, module_name, items)
            except KeyError:
                warning('Missing field "_module" for configured backend "%s"', backend_name)
                raise KeyError(u'Configured backend "%s" not found' % backend_name)
        return module_name, items
Beispiel #28
0
    def __init__(self, path=None, with_defaults=False):
        RawConfigParser.__init__(self, dict_type=OrderedDict)
        self.path = path or os.environ.get(CONFIG_ENV, CONFIG_PATH)

        # Check if self.path is accessible
        abspath = os.path.abspath(self.path)
        if not os.path.exists(self.path):
            log.warning('Config file %s does not exist' % abspath)
        elif os.access(self.path, os.R_OK):
            if not os.access(self.path, os.W_OK):
                log.warning('Config file %s is not writable' % abspath)
        else:
            raise CLIError(
                'Config file %s is inaccessible' % abspath,
                importance=3, details=['No read permissions for this file'])

        self._overrides = defaultdict(dict)
        if with_defaults:
            self._load_defaults()
        self.read(self.path)

        for section in self.sections():
            r = self.cloud_name(section)
            if r:
                for k, v in self.items(section):
                    self.set_cloud(r, k, v)
                self.remove_section(section)
Beispiel #29
0
def save():
    """Saves FileDirectives into ConfigFile."""
    section = "*"
    module = sys.modules[__name__]
    parser = RawConfigParser()
    parser.optionxform = str # Force case-sensitivity on names
    parser.add_section(section)
    try:
        f, fname = open(ConfigFile, "wb"), util.longpath(ConfigFile)
        f.write("# %s configuration autowritten on %s.\n" %
                (fname, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
        for name in FileDirectives:
            try:
                parser.set(section, name, json.dumps(getattr(module, name)))
            except Exception: pass
        for name in OptionalFileDirectives:
            try:
                value = getattr(module, name, None)
                if OptionalFileDirectiveDefaults.get(name) != value:
                    parser.set(section, name, json.dumps(value))
            except Exception: pass
        parser.write(f)
        f.close()
    except Exception:
        pass # Fail silently
Beispiel #30
0
class Config(object):

    def __init__(self):
        self._load()
        self._read()

    def _load(self):
        path = os.path.join(os.getcwd(), 'syncer.cfg')
        self.config = RawConfigParser()
        self.config.read(path)

    def _read(self):
        self.sync_crons = self._get_multiline(
            'cron', 'sync', [])
        self.unison_executable = self._get(
            'unison', 'executable',
            '/usr/local/bin/unison')

    def _get(self, section, option, default):
        if not self.config.has_option(section, option):
            return default

        value = self.config.get(section, option)
        if value:
            return value
        else:
            return default

    def _get_multiline(self, section, option, default):
        value = self._get(section, option, default)
        if value is not default:
            return value.strip().split('\n')
        else:
            return value
Beispiel #31
0
#!/usr/bin/python
#coding=utf-8
# from __future__ import with_statement # This isn't required in Python 2.6

from ConfigParser import RawConfigParser
from getconfig import *

channels_filename = 'radio'

channels_path = config_folder + channels_filename

channels = RawConfigParser()
channelsfile = open(channels_path)
channels.readfp(channelsfile)
channelsfile.close

Beispiel #32
0
def make_config_file():

    config_init = RawConfigParser()

    config_init.read("shared/node.conf")

    config_init.add_section("NODE")
    config_init.add_section("NETWORK")
    config_init.add_section("SENSOR_PINS")
    config_init.add_section("READING_TYPES")
    config_init.add_section("AUTHORIZATION")

    return config_init
Beispiel #33
0
 def __init__(self):
     self.__config = RawConfigParser()
Beispiel #34
0
class AwsCredentials(object):
    """Wraps a RawConfigParser to treat a section named 'default' as a nomral section."""

    __REAL_DEFAULT_SECTION_NAME = 'default'
    __TEMP_DEFAULT_SECTION_NAME = constant.DEFAULT_SECTION_NAME
    __REAL_DEFAULT_SECTION_HEADING = '[' + __REAL_DEFAULT_SECTION_NAME + ']'
    __TEMP_DEFAULT_SECTION_HEADING = '[' + __TEMP_DEFAULT_SECTION_NAME + ']'

    def __init__(self):
        self.__config = RawConfigParser()

    def read(self, path):

        with open(path, 'r') as file:
            content = file.read()

        content = content.replace(self.__REAL_DEFAULT_SECTION_HEADING,
                                  self.__TEMP_DEFAULT_SECTION_HEADING)

        content_io = StringIO(content)

        self.__config.readfp(content_io)

    def write(self, path):

        content_io = StringIO()

        self.__config.write(content_io)

        content = content_io.getvalue()

        content = content.replace(self.__TEMP_DEFAULT_SECTION_HEADING,
                                  self.__REAL_DEFAULT_SECTION_HEADING)

        with open(path, 'w') as file:
            file.write(content)

    def __to_temp_name(self, name):

        if name == self.__REAL_DEFAULT_SECTION_NAME:
            name = self.__TEMP_DEFAULT_SECTION_NAME

        return name

    def __to_real_name(self, name):

        if name == self.__TEMP_DEFAULT_SECTION_NAME:
            name = self.__REAL_DEFAULT_SECTION_NAME

        return name

    def sections(self):
        sections = self.__config.sections()
        sections = [self.__to_real_name(section) for section in sections]
        return sections

    def add_section(self, section):
        section = self.__to_temp_name(section)
        self.__config.add_section(section)

    def has_section(self, section):
        section = self.__to_temp_name(section)
        return self.__config.has_section(section)

    def options(self, section):
        section = self.__to_temp_name(section)
        return self.__config.options(section)

    def has_option(self, section, option):
        section = self.__to_temp_name(section)
        return self.__config.has_option(section, option)

    def get(self, section, option):
        section = self.__to_temp_name(section)
        return self.__config.get(section, option)

    def items(self, section):
        section = self.__to_temp_name(section)
        return self.__config.items(section)

    def set(self, section, option, value):
        section = self.__to_temp_name(section)
        self.__config.set(section, option, value)

    def remove_option(self, section, option):
        section = self.__to_temp_name(section)
        return self.__config.remove_option(section, section, option)

    def remove_section(self, section):
        section = self.__to_temp_name(section)
        return self.__config.remove_section(section)
Beispiel #35
0
 def _read_config(self):
     config = RawConfigParser()
     with codecs.open(self.confpath, 'r', encoding='utf-8') as fd:
         config.readfp(fd)
     return config
Beispiel #36
0
                random.seed(seed + count)
                if random.random() < edge_prob:
                    edge_name = '{0}_rlab_{1}'.format(apps[i][0], apps[j][0])
                    edges.append(edge_name)
                count += 1
    return edges


def transform_to_row(t):
    fr, to = t[0].split('_rlab_')
    return Row(fr=fr, to=to, weight=int(t[1]))


if __name__ == '__main__':
    print('====> Initializing Spark APP')
    localConf = RawConfigParser()
    localConf.optionxform = str
    localConf.read('../config')
    sparkConf = SparkConf()
    for t in localConf.items('spark-config'):
        sparkConf.set(t[0], t[1])
    spark = SparkSession.builder \
      .appName('RLab_APP_Project___Extract_Edges') \
      .config(conf=sparkConf) \
      .enableHiveSupport() \
      .getOrCreate()
    sc = spark.sparkContext
    sc.setLogLevel('ERROR')

    print('====> Parsing local arguments')
    parser = argparse.ArgumentParser()
class OctoPiPanel():
    """
    @var done: anything can set to True to forcequit
    @var screen: points to: pygame.display.get_surface()        
    """

    cfg = RawConfigParser()
    cfg.read(r'OctoPiPanel.cfg')

    api_baseurl = cfg.get('settings', 'baseurl')
    apikey = cfg.get('settings', 'apikey')
    updatetime = cfg.getint('settings', 'updatetime')
    backlightofftime = cfg.getint('settings', 'backlightofftime')

    addkey = '?apikey={0}'.format(apikey)
    apiurl_printhead = '{0}/api/printer/printhead'.format(api_baseurl)
    apiurl_tool = '{0}/api/printer/tool'.format(api_baseurl)
    apiurl_bed = '{0}/api/printer/bed'.format(api_baseurl)
    apiurl_job = '{0}/api/job'.format(api_baseurl)
    apiurl_status = '{0}/api/printer?apikey={1}'.format(api_baseurl, apikey)
    apiurl_connection = '{0}/api/connection'.format(api_baseurl)

    #print apiurl_job + addkey

    graph_area_left = 30  #6
    graph_area_top = 125
    graph_area_width = 285  #308
    graph_area_height = 110

    def __init__(self, width=320, height=240, caption="OctoPiPanel"):
        """
        .
        """
        self.done = False
        self.color_bg = pygame.Color(41, 61, 70)

        # Button settings
        self.buttonWidth = 100
        self.buttonHeight = 25

        # Status flags
        self.HotEndTemp = 0.0
        self.BedTemp = 0.0
        self.HotEndTempTarget = 0.0
        self.BedTempTarget = 0.0
        self.HotHotEnd = False
        self.HotBed = False
        self.Paused = False
        self.Printing = False
        self.JobLoaded = False
        self.Completion = 0  # In procent
        self.PrintTimeLeft = 0
        self.Height = 0.0
        self.FileName = "Nothing"
        self.getstate_ticks = pygame.time.get_ticks()

        # Lists for temperature data
        self.HotEndTempList = deque([0] * self.graph_area_width)
        self.BedTempList = deque([0] * self.graph_area_width)

        #print self.HotEndTempList
        #print self.BedTempList

        if platform.system() == 'Linux':
            # Init framebuffer/touchscreen environment variables
            os.putenv('SDL_VIDEODRIVER', 'fbcon')
            os.putenv('SDL_FBDEV', '/dev/fb1')
            os.putenv('SDL_MOUSEDRV', 'TSLIB')
            os.putenv('SDL_MOUSEDEV', '/dev/input/touchscreen')

        # init pygame and set up screen
        pygame.init()
        if platform.system() == 'Windows':
            pygame.mouse.set_visible(True)
        else:
            pygame.mouse.set_visible(False)

        self.width, self.height = width, height
        self.screen = pygame.display.set_mode((width, height))
        #modes = pygame.display.list_modes(16)
        #self.screen = pygame.display.set_mode(modes[0], FULLSCREEN, 16)
        pygame.display.set_caption(caption)

        # Set font
        #self.fntText = pygame.font.Font("Cyberbit.ttf", 12)
        self.fntText = pygame.font.Font("Cyberbit.ttf", 12)
        self.fntText.set_bold(True)
        self.fntTextSmall = pygame.font.Font("Cyberbit.ttf", 10)
        self.fntTextSmall.set_bold(True)

        # backlight on off status and control
        self.bglight_ticks = pygame.time.get_ticks()
        self.bglight_on = True

        # Home X/Y/Z buttons
        self.btnHomeXY = pygbutton.PygButton((5, 5, 100, self.buttonHeight),
                                             "Home X/Y")
        self.btnHomeZ = pygbutton.PygButton((5, 35, 100, self.buttonHeight),
                                            "Home Z")
        self.btnZUp = pygbutton.PygButton((110, 35, 100, self.buttonHeight),
                                          "Z +25")

        # Heat buttons
        self.btnHeatBed = pygbutton.PygButton((5, 65, 100, self.buttonHeight),
                                              "Heat bed")
        self.btnHeatHotEnd = pygbutton.PygButton(
            (5, 95, 100, self.buttonHeight), "Heat hot end")

        # Start, stop and pause buttons
        self.btnStartPrint = pygbutton.PygButton(
            (110, 5, 100, self.buttonHeight), "Start print")
        self.btnAbortPrint = pygbutton.PygButton(
            (110, 5, 100, self.buttonHeight), "Abort print", (200, 0, 0))
        self.btnPausePrint = pygbutton.PygButton(
            (110, 35, 100, self.buttonHeight), "Pause print")

        # Shutdown and reboot buttons
        self.btnReboot = pygbutton.PygButton((215, 5, 100, self.buttonHeight),
                                             "Reboot")
        self.btnShutdown = pygbutton.PygButton(
            (215, 35, 100, self.buttonHeight), "Shutdown")

        # I couldnt seem to get at pin 252 for the backlight using the usual method,
        # but this seems to work
        if platform.system() == 'Linux':
            os.system("echo 252 > /sys/class/gpio/export")
            os.system("echo 'out' > /sys/class/gpio/gpio252/direction")
            os.system("echo '1' > /sys/class/gpio/gpio252/value")

    def Start(self):
        """ game loop: input, move, render"""
        while not self.done:
            # Handle events
            self.handle_events()

            # Update info from printer every other seconds
            if pygame.time.get_ticks() - self.getstate_ticks > self.updatetime:
                self.get_state()
                self.getstate_ticks = pygame.time.get_ticks()

            # Is it time to turn of the backlight?
            if pygame.time.get_ticks(
            ) - self.bglight_ticks > self.backlightofftime and platform.system(
            ) == 'Linux':
                # disable the backlight
                os.system("echo '0' > /sys/class/gpio/gpio252/value")
                self.bglight_ticks = pygame.time.get_ticks()
                self.bglight_on = False

            # Update buttons visibility, text, graphs etc
            self.update()

            # Draw everything
            self.draw()
        """ Clean up """
        # enable the backlight before quiting
        if platform.system() == 'Linux':
            os.system("echo '1' > /sys/class/gpio/gpio252/value")
        """ Quit """
        pygame.quit()

    def handle_events(self):
        """handle all events."""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print "quit"
                self.done = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    print "Got escape key"
                    self.done = True

                if event.key == pygame.K_a:
                    print "Got A key"

            # It should only be possible to click a button if you can see it
            #  e.g. the backlight is on
            if self.bglight_on == True:
                if 'click' in self.btnHomeXY.handleEvent(event):
                    self._home_xy()

                if 'click' in self.btnHomeZ.handleEvent(event):
                    self._home_z()

                if 'click' in self.btnZUp.handleEvent(event):
                    self._z_up()

                if 'click' in self.btnHeatBed.handleEvent(event):
                    self._heat_bed()

                if 'click' in self.btnHeatHotEnd.handleEvent(event):
                    self._heat_hotend()

                if 'click' in self.btnStartPrint.handleEvent(event):
                    self._start_print()

                if 'click' in self.btnAbortPrint.handleEvent(event):
                    self._abort_print()

                if 'click' in self.btnPausePrint.handleEvent(event):
                    self._pause_print()

                if 'click' in self.btnReboot.handleEvent(event):
                    self._reboot()

                if 'click' in self.btnShutdown.handleEvent(event):
                    self._shutdown()

            # Did the user click on the screen?
            if event.type == pygame.MOUSEBUTTONDOWN:
                # Reset backlight counter
                self.bglight_ticks = pygame.time.get_ticks()

                if self.bglight_on == False and platform.system() == 'Linux':
                    # enable the backlight
                    os.system("echo '1' > /sys/class/gpio/gpio252/value")
                    self.bglight_on = True
                    print "Background light on."

    """
    Get status update from API, regarding temp etc.
    """

    def get_state(self):
        try:
            req = requests.get(self.apiurl_status)

            if req.status_code == 200:
                state = json.loads(req.text)

                # Set status flags
                self.HotEndTemp = state['temps']['tool0']['actual']
                self.BedTemp = state['temps']['bed']['actual']
                self.HotEndTempTarget = state['temps']['tool0']['target']
                self.BedTempTarget = state['temps']['bed']['target']

                if self.HotEndTempTarget == None:
                    self.HotEndTempTarget = 0.0

                if self.BedTempTarget == None:
                    self.BedTempTarget = 0.0

                if self.HotEndTempTarget > 0.0:
                    self.HotHotEnd = True
                else:
                    self.HotHotEnd = False

                if self.BedTempTarget > 0.0:
                    self.HotBed = True
                else:
                    self.HotBed = False

                #print self.apiurl_status

            # Get info about current job
            req = requests.get(self.apiurl_job + self.addkey)
            if req.status_code == 200:
                jobState = json.loads(req.text)

            req = requests.get(self.apiurl_connection + self.addkey)
            if req.status_code == 200:
                connState = json.loads(req.text)

                #print self.apiurl_job + self.addkey

                self.Completion = jobState['progress'][
                    'completion']  # In procent
                self.PrintTimeLeft = jobState['progress']['printTimeLeft']
                #self.Height = state['currentZ']
                self.FileName = jobState['job']['file']['name']
                self.JobLoaded = connState['current'][
                    'state'] == "Operational" and (
                        jobState['job']['file']['name'] != "") or (
                            jobState['job']['file']['name'] != None)

                # Save temperatures to lists
                self.HotEndTempList.popleft()
                self.HotEndTempList.append(self.HotEndTemp)
                self.BedTempList.popleft()
                self.BedTempList.append(self.BedTemp)

                #print self.HotEndTempList
                #print self.BedTempList

                self.Paused = connState['current']['state'] == "Paused"
                self.Printing = connState['current']['state'] == "Printing"

        except requests.exceptions.ConnectionError as e:
            print "Connection Error ({0}): {1}".format(e.errno, e.strerror)

        return

    """
    Update buttons, text, graphs etc.
    """

    def update(self):
        # Set home buttons visibility
        self.btnHomeXY.visible = not (self.Printing or self.Paused)
        self.btnHomeZ.visible = not (self.Printing or self.Paused)
        self.btnZUp.visible = not (self.Printing or self.Paused)

        # Set abort and pause buttons visibility
        self.btnStartPrint.visible = not (self.Printing
                                          or self.Paused) and self.JobLoaded
        self.btnAbortPrint.visible = self.Printing or self.Paused
        self.btnPausePrint.visible = self.Printing or self.Paused

        # Set texts on pause button
        if self.Paused:
            self.btnPausePrint.caption = "Resume"
        else:
            self.btnPausePrint.caption = "Pause"

        # Set abort and pause buttons visibility
        self.btnHeatHotEnd.visible = not (self.Printing or self.Paused)
        self.btnHeatBed.visible = not (self.Printing or self.Paused)

        # Set texts on heat buttons
        if self.HotHotEnd:
            self.btnHeatHotEnd.caption = "Turn off hot end"
        else:
            self.btnHeatHotEnd.caption = "Heat hot end"

        if self.HotBed:
            self.btnHeatBed.caption = "Turn off bed"
        else:
            self.btnHeatBed.caption = "Heat bed"

        return

    def draw(self):
        #clear whole screen
        self.screen.fill(self.color_bg)

        # Draw buttons
        self.btnHomeXY.draw(self.screen)
        self.btnHomeZ.draw(self.screen)
        self.btnZUp.draw(self.screen)
        self.btnHeatBed.draw(self.screen)
        self.btnHeatHotEnd.draw(self.screen)
        self.btnStartPrint.draw(self.screen)
        self.btnAbortPrint.draw(self.screen)
        self.btnPausePrint.draw(self.screen)
        self.btnReboot.draw(self.screen)
        self.btnShutdown.draw(self.screen)

        # Place temperatures texts
        lblHotEndTemp = self.fntText.render(
            u'Hot end: {0}\N{DEGREE SIGN}C ({1}\N{DEGREE SIGN}C)'.format(
                self.HotEndTemp, self.HotEndTempTarget), 1, (220, 0, 0))
        self.screen.blit(lblHotEndTemp, (112, 60))
        lblBedTemp = self.fntText.render(
            u'Bed: {0}\N{DEGREE SIGN}C ({1}\N{DEGREE SIGN}C)'.format(
                self.BedTemp, self.BedTempTarget), 1, (0, 0, 220))
        self.screen.blit(lblBedTemp, (112, 75))

        # Place time left and compeltetion texts
        if self.JobLoaded == False or self.PrintTimeLeft == None or self.Completion == None:
            self.Completion = 0
            self.PrintTimeLeft = 0

        lblPrintTimeLeft = self.fntText.render(
            "Time left: {0}".format(
                datetime.timedelta(seconds=self.PrintTimeLeft)), 1,
            (200, 200, 200))
        self.screen.blit(lblPrintTimeLeft, (112, 90))

        lblCompletion = self.fntText.render(
            "Completion: {0:.1f}%".format(self.Completion), 1, (200, 200, 200))
        self.screen.blit(lblCompletion, (112, 105))

        # Temperature Graphing
        # Graph area
        pygame.draw.rect(self.screen, (255, 255, 255),
                         (self.graph_area_left, self.graph_area_top,
                          self.graph_area_width, self.graph_area_height))

        # Graph axes
        # X, temp
        pygame.draw.line(self.screen, (0, 0, 0),
                         [self.graph_area_left, self.graph_area_top], [
                             self.graph_area_left,
                             self.graph_area_top + self.graph_area_height
                         ], 2)

        # X-axis divisions
        pygame.draw.line(self.screen, (0, 0, 0), [
            self.graph_area_left - 3, self.graph_area_top +
            (self.graph_area_height / 5) * 5
        ], [
            self.graph_area_left, self.graph_area_top +
            (self.graph_area_height / 5) * 5
        ], 2)  # 0
        pygame.draw.line(self.screen, (0, 0, 0), [
            self.graph_area_left - 3, self.graph_area_top +
            (self.graph_area_height / 5) * 4
        ], [
            self.graph_area_left, self.graph_area_top +
            (self.graph_area_height / 5) * 4
        ], 2)  # 50
        pygame.draw.line(self.screen, (0, 0, 0), [
            self.graph_area_left - 3, self.graph_area_top +
            (self.graph_area_height / 5) * 3
        ], [
            self.graph_area_left, self.graph_area_top +
            (self.graph_area_height / 5) * 3
        ], 2)  # 100
        pygame.draw.line(self.screen, (0, 0, 0), [
            self.graph_area_left - 3, self.graph_area_top +
            (self.graph_area_height / 5) * 2
        ], [
            self.graph_area_left, self.graph_area_top +
            (self.graph_area_height / 5) * 2
        ], 2)  # 150
        pygame.draw.line(self.screen, (0, 0, 0), [
            self.graph_area_left - 3, self.graph_area_top +
            (self.graph_area_height / 5) * 1
        ], [
            self.graph_area_left, self.graph_area_top +
            (self.graph_area_height / 5) * 1
        ], 2)  # 200
        pygame.draw.line(self.screen, (0, 0, 0), [
            self.graph_area_left - 3, self.graph_area_top +
            (self.graph_area_height / 5) * 0
        ], [
            self.graph_area_left, self.graph_area_top +
            (self.graph_area_height / 5) * 0
        ], 2)  # 250

        # X-axis scale
        lbl0 = self.fntTextSmall.render("0", 1, (200, 200, 200))
        self.screen.blit(lbl0,
                         (self.graph_area_left - 26, self.graph_area_top - 6 +
                          (self.graph_area_height / 5) * 5))
        lbl0 = self.fntTextSmall.render("50", 1, (200, 200, 200))
        self.screen.blit(lbl0,
                         (self.graph_area_left - 26, self.graph_area_top - 6 +
                          (self.graph_area_height / 5) * 4))
        lbl0 = self.fntTextSmall.render("100", 1, (200, 200, 200))
        self.screen.blit(lbl0,
                         (self.graph_area_left - 26, self.graph_area_top - 6 +
                          (self.graph_area_height / 5) * 3))
        lbl0 = self.fntTextSmall.render("150", 1, (200, 200, 200))
        self.screen.blit(lbl0,
                         (self.graph_area_left - 26, self.graph_area_top - 6 +
                          (self.graph_area_height / 5) * 2))
        lbl0 = self.fntTextSmall.render("200", 1, (200, 200, 200))
        self.screen.blit(lbl0,
                         (self.graph_area_left - 26, self.graph_area_top - 6 +
                          (self.graph_area_height / 5) * 1))
        lbl0 = self.fntTextSmall.render("250", 1, (200, 200, 200))
        self.screen.blit(lbl0,
                         (self.graph_area_left - 26, self.graph_area_top - 6 +
                          (self.graph_area_height / 5) * 0))

        # X-axis divisions, grey lines
        pygame.draw.line(self.screen, (200, 200, 200), [
            self.graph_area_left + 2, self.graph_area_top +
            (self.graph_area_height / 5) * 4
        ], [
            self.graph_area_left + self.graph_area_width - 2,
            self.graph_area_top + (self.graph_area_height / 5) * 4
        ], 1)  # 50
        pygame.draw.line(self.screen, (200, 200, 200), [
            self.graph_area_left + 2, self.graph_area_top +
            (self.graph_area_height / 5) * 3
        ], [
            self.graph_area_left + self.graph_area_width - 2,
            self.graph_area_top + (self.graph_area_height / 5) * 3
        ], 1)  # 100
        pygame.draw.line(self.screen, (200, 200, 200), [
            self.graph_area_left + 2, self.graph_area_top +
            (self.graph_area_height / 5) * 2
        ], [
            self.graph_area_left + self.graph_area_width - 2,
            self.graph_area_top + (self.graph_area_height / 5) * 2
        ], 1)  # 150
        pygame.draw.line(self.screen, (200, 200, 200), [
            self.graph_area_left + 2, self.graph_area_top +
            (self.graph_area_height / 5) * 1
        ], [
            self.graph_area_left + self.graph_area_width - 2,
            self.graph_area_top + (self.graph_area_height / 5) * 1
        ], 1)  # 200

        # Y, time, 2 seconds per pixel
        pygame.draw.line(self.screen, (0, 0, 0), [
            self.graph_area_left, self.graph_area_top + self.graph_area_height
        ], [
            self.graph_area_left + self.graph_area_width,
            self.graph_area_top + self.graph_area_height
        ], 2)

        # Scaling factor
        g_scale = self.graph_area_height / 250.0

        # Print temperatures for hot end
        i = 0
        for t in self.HotEndTempList:
            x = self.graph_area_left + i
            y = self.graph_area_top + self.graph_area_height - int(t * g_scale)
            pygame.draw.line(self.screen, (220, 0, 0), [x, y], [x + 1, y], 2)
            i += 1

        # Print temperatures for bed
        i = 0
        for t in self.BedTempList:
            x = self.graph_area_left + i
            y = self.graph_area_top + self.graph_area_height - int(t * g_scale)
            pygame.draw.line(self.screen, (0, 0, 220), [x, y], [x + 1, y], 2)
            i += 1

        # Draw target temperatures
        # Hot end
        pygame.draw.line(self.screen, (180, 40, 40), [
            self.graph_area_left, self.graph_area_top +
            self.graph_area_height - (self.HotEndTempTarget * g_scale)
        ], [
            self.graph_area_left + self.graph_area_width, self.graph_area_top +
            self.graph_area_height - (self.HotEndTempTarget * g_scale)
        ], 1)
        # Bed
        pygame.draw.line(self.screen, (40, 40, 180), [
            self.graph_area_left, self.graph_area_top +
            self.graph_area_height - (self.BedTempTarget * g_scale)
        ], [
            self.graph_area_left + self.graph_area_width, self.graph_area_top +
            self.graph_area_height - (self.BedTempTarget * g_scale)
        ], 1)

        # update screen
        pygame.display.update()

    def _home_xy(self):
        data = {"command": "home", "axes": ["x", "y"]}
        self._sendAPICommand(self.apiurl_printhead, data)

        return

    def _home_z(self):
        data = {"command": "home", "axes": ["z"]}
        self._sendAPICommand(self.apiurl_printhead, data)

        return

    def _z_up(self):
        data = {"command": "jog", "x": 0, "y": 0, "z": 25}
        self._sendAPICommand(self.apiurl_printhead, data)

        return

    def _heat_bed(self):
        # is the bed already hot, in that case turn it off
        if self.HotBed:
            data = {"command": "target", "target": 0}
        else:
            data = {"command": "target", "target": 50}

        # Send command
        self._sendAPICommand(self.apiurl_bed, data)

        return

    def _heat_hotend(self):
        # is the bed already hot, in that case turn it off
        if self.HotHotEnd:
            data = {"command": "target", "targets": {"tool0": 0}}
        else:
            data = {"command": "target", "targets": {"tool0": 190}}

        # Send command
        self._sendAPICommand(self.apiurl_tool, data)

        return

    def _start_print(self):
        # here we should display a yes/no box somehow
        data = {"command": "start"}

        # Send command
        self._sendAPICommand(self.apiurl_job, data)

        return

    def _abort_print(self):
        # here we should display a yes/no box somehow
        data = {"command": "cancel"}

        # Send command
        self._sendAPICommand(self.apiurl_job, data)

        return

    # Pause or resume print
    def _pause_print(self):
        data = {"command": "pause"}

        # Send command
        self._sendAPICommand(self.apiurl_job, data)

        return

    # Reboot system
    def _reboot(self):
        if platform.system() == 'Linux':
            os.system("reboot")
        else:
            pygame.image.save(self.screen, "screenshot.jpg")

        self.done = True
        print "reboot"

        return

    # Shutdown system
    def _shutdown(self):
        if platform.system() == 'Linux':
            os.system("shutdown -h 0")

        self.done = True
        print "shutdown"

        return

    # Send API-data to OctoPrint
    def _sendAPICommand(self, url, data):
        headers = {
            'content-type': 'application/json',
            'X-Api-Key': self.apikey
        }
        r = requests.post(url, data=json.dumps(data), headers=headers)
        print r.text
Beispiel #38
0
def read_auto_rx_config(filename, no_sdr_test=False):
    """ Read an Auto-RX v2 Station Configuration File.

	This function will attempt to parse a configuration file.
	It will also confirm the accessibility of any SDRs specified in the config file.

	Args:
		filename (str): Filename of the configuration file to read.
		no_sdr_test (bool): Skip testing the SDRs (used for some unit tests)

	Returns:
		auto_rx_config (dict): The configuration dictionary.
		sdr_config (dict): A dictionary with SDR parameters.
	"""
    global global_config
    # Configuration Defaults:
    auto_rx_config = {
        # Log Settings
        "per_sonde_log": True,
        # Email Settings
        "email_enabled": False,
        #'email_error_notifications': False,
        "email_smtp_server": "localhost",
        "email_smtp_port": 25,
        "email_smtp_authentication": "None",
        "email_smtp_login": "******",
        "email_smtp_password": "******",
        "email_from": "sonde@localhost",
        "email_to": None,
        "email_subject": "<type> Sonde launch detected on <freq>: <id>",
        # SDR Settings
        "sdr_fm": "rtl_fm",
        "sdr_power": "rtl_power",
        "sdr_quantity": 1,
        # Search Parameters
        "min_freq": 400.4,
        "max_freq": 404.0,
        "rx_timeout": 120,
        "whitelist": [],
        "blacklist": [],
        "greylist": [],
        # Location Settings
        "station_lat": 0.0,
        "station_lon": 0.0,
        "station_alt": 0.0,
        "station_code":
        "SONDE",  # NOTE: This will not be read from the config file, but will be left in place for now
        # as a default setting.
        "gpsd_enabled": False,
        "gpsd_host": "localhost",
        "gpsd_port": 2947,
        # Position Filter Settings
        "max_altitude": 50000,
        "max_radius_km": 1000,
        "min_radius_km": 0,
        "radius_temporary_block": False,
        # Habitat Settings
        "habitat_enabled": False,
        "habitat_upload_rate": 30,
        "habitat_uploader_callsign": "SONDE_AUTO_RX",
        "habitat_uploader_antenna": "1/4-wave",
        "habitat_upload_listener_position": False,
        "habitat_payload_callsign": "<id>",
        # APRS Settings
        "aprs_enabled": False,
        "aprs_upload_rate": 30,
        "aprs_user": "******",
        "aprs_pass": "******",
        "aprs_server": "rotate.aprs2.net",
        "aprs_object_id": "<id>",
        #'aprs_use_custom_object_id': False,
        "aprs_custom_comment": "Radiosonde Auto-RX <freq>",
        "aprs_position_report": False,
        "station_beacon_enabled": False,
        "station_beacon_rate": 30,
        "station_beacon_comment": "radiosonde_auto_rx SondeGate v<version>",
        "station_beacon_icon": "/r",
        # Web Settings,
        "web_host": "0.0.0.0",
        "web_port": 5000,
        "web_archive_age": 120,
        "web_control": True,
        #'kml_refresh_rate': 10,
        # Advanced Parameters
        "search_step": 800,
        "snr_threshold": 10,
        "min_distance": 1000,
        "dwell_time": 10,
        "max_peaks": 10,
        "quantization": 10000,
        "decoder_spacing_limit": 15000,
        "synchronous_upload": False,
        "scan_dwell_time": 20,
        "detect_dwell_time": 5,
        "scan_delay": 10,
        "payload_id_valid": 5,
        "temporary_block_time": 60,
        "rs41_drift_tweak": False,
        "decoder_stats": False,
        "ngp_tweak": False,
        # Rotator Settings
        "enable_rotator": False,
        "rotator_update_rate": 30,
        "rotator_hostname": "127.0.0.1",
        "rotator_port": 4533,
        "rotation_threshold": 5.0,
        "rotator_homing_enabled": False,
        "rotator_homing_delay": 10,
        "rotator_home_azimuth": 0,
        "rotator_home_elevation": 0,
        # OziExplorer Settings
        "ozi_enabled": False,
        "ozi_update_rate": 5,
        "ozi_port": 55681,
        "payload_summary_enabled": False,
        "payload_summary_port": 55672,
        # Debugging settings
        "save_detection_audio": False,
        "save_decode_audio": False,
        "save_decode_iq": False,
        # URL for the Habitat DB Server.
        # As of July 2018 we send via sondehub.org, which will allow us to eventually transition away
        # from using the habhub.org tracker, and leave it for use by High-Altitude Balloon Hobbyists.
        # For now, sondehub.org just acts as a proxy to habhub.org.
        # This setting is not exposed to users as it's only used for unit/int testing
        "habitat_url": "https://habitat.sondehub.org/",
    }

    try:

        # Check the file exists.
        if not os.path.isfile(filename):
            logging.critical("Config file %s does not exist!" % filename)
            return None

        config = RawConfigParser(auto_rx_config)
        config.read(filename)

        # Log Settings
        auto_rx_config["per_sonde_log"] = config.getboolean(
            "logging", "per_sonde_log")

        # Email Settings
        if config.has_option("email", "email_enabled"):
            try:
                auto_rx_config["email_enabled"] = config.getboolean(
                    "email", "email_enabled")
                auto_rx_config["email_smtp_server"] = config.get(
                    "email", "smtp_server")
                auto_rx_config["email_smtp_port"] = config.get(
                    "email", "smtp_port")
                auto_rx_config["email_smtp_authentication"] = config.get(
                    "email", "smtp_authentication")
                auto_rx_config["email_smtp_login"] = config.get(
                    "email", "smtp_login")
                auto_rx_config["email_smtp_password"] = config.get(
                    "email", "smtp_password")
                auto_rx_config["email_from"] = config.get("email", "from")
                auto_rx_config["email_to"] = config.get("email", "to")
                auto_rx_config["email_subject"] = config.get(
                    "email", "subject")

                if auto_rx_config["email_smtp_authentication"] not in [
                        "None",
                        "TLS",
                        "SSL",
                ]:
                    logging.error(
                        "Config - Invalid email authentication setting. Must be None, TLS or SSL."
                    )
                    return None

            except:
                logging.error(
                    "Config - Invalid or missing email settings. Disabling.")
                auto_rx_config["email_enabled"] = False

        # SDR Settings
        auto_rx_config["sdr_fm"] = config.get("advanced", "sdr_fm_path")
        auto_rx_config["sdr_power"] = config.get("advanced", "sdr_power_path")
        auto_rx_config["sdr_quantity"] = config.getint("sdr", "sdr_quantity")

        # Search Parameters
        auto_rx_config["min_freq"] = config.getfloat("search_params",
                                                     "min_freq")
        auto_rx_config["max_freq"] = config.getfloat("search_params",
                                                     "max_freq")
        auto_rx_config["rx_timeout"] = config.getint("search_params",
                                                     "rx_timeout")
        auto_rx_config["whitelist"] = json.loads(
            config.get("search_params", "whitelist"))
        auto_rx_config["blacklist"] = json.loads(
            config.get("search_params", "blacklist"))
        auto_rx_config["greylist"] = json.loads(
            config.get("search_params", "greylist"))

        # Location Settings
        auto_rx_config["station_lat"] = config.getfloat(
            "location", "station_lat")
        auto_rx_config["station_lon"] = config.getfloat(
            "location", "station_lon")
        auto_rx_config["station_alt"] = config.getfloat(
            "location", "station_alt")

        # Position Filtering
        auto_rx_config["max_altitude"] = config.getint("filtering",
                                                       "max_altitude")
        auto_rx_config["max_radius_km"] = config.getint(
            "filtering", "max_radius_km")

        # Habitat Settings
        auto_rx_config["habitat_enabled"] = config.getboolean(
            "habitat", "habitat_enabled")
        auto_rx_config["habitat_upload_rate"] = config.getint(
            "habitat", "upload_rate")
        auto_rx_config["habitat_uploader_callsign"] = config.get(
            "habitat", "uploader_callsign")
        auto_rx_config["habitat_upload_listener_position"] = config.getboolean(
            "habitat", "upload_listener_position")
        auto_rx_config["habitat_uploader_antenna"] = config.get(
            "habitat", "uploader_antenna").strip()
        try:  # Use the default configuration if not found
            auto_rx_config["habitat_url"] = config.get("habitat", "url")
        except:
            pass

        if auto_rx_config["habitat_upload_rate"] < MINIMUM_HABITAT_UPDATE_RATE:
            logging.warning(
                "Config - Habitat Update Rate clipped to minimum of %d seconds. Please be respectful of other users of Habitat."
                % MINIMUM_HABITAT_UPDATE_RATE)
            auto_rx_config["habitat_upload_rate"] = MINIMUM_HABITAT_UPDATE_RATE

        # APRS Settings
        auto_rx_config["aprs_enabled"] = config.getboolean(
            "aprs", "aprs_enabled")
        auto_rx_config["aprs_upload_rate"] = config.getint(
            "aprs", "upload_rate")
        auto_rx_config["aprs_user"] = config.get("aprs", "aprs_user")
        auto_rx_config["aprs_pass"] = config.get("aprs", "aprs_pass")
        auto_rx_config["aprs_server"] = config.get("aprs", "aprs_server")
        auto_rx_config["aprs_object_id"] = config.get("aprs", "aprs_object_id")
        auto_rx_config["aprs_custom_comment"] = config.get(
            "aprs", "aprs_custom_comment")
        auto_rx_config["aprs_position_report"] = config.getboolean(
            "aprs", "aprs_position_report")
        auto_rx_config["station_beacon_enabled"] = config.getboolean(
            "aprs", "station_beacon_enabled")
        auto_rx_config["station_beacon_rate"] = config.getint(
            "aprs", "station_beacon_rate")
        auto_rx_config["station_beacon_comment"] = config.get(
            "aprs", "station_beacon_comment")
        auto_rx_config["station_beacon_icon"] = config.get(
            "aprs", "station_beacon_icon")

        if auto_rx_config["aprs_upload_rate"] < MINIMUM_APRS_UPDATE_RATE:
            logging.warning(
                "Config - APRS Update Rate clipped to minimum of %d seconds. Please be respectful of other users of APRS-IS."
                % MINIMUM_APRS_UPDATE_RATE)
            auto_rx_config["aprs_upload_rate"] = MINIMUM_APRS_UPDATE_RATE

        # OziPlotter Settings
        auto_rx_config["ozi_enabled"] = config.getboolean(
            "oziplotter", "ozi_enabled")
        auto_rx_config["ozi_update_rate"] = config.getint(
            "oziplotter", "ozi_update_rate")
        auto_rx_config["ozi_port"] = config.getint("oziplotter", "ozi_port")
        auto_rx_config["payload_summary_enabled"] = config.getboolean(
            "oziplotter", "payload_summary_enabled")
        auto_rx_config["payload_summary_port"] = config.getint(
            "oziplotter", "payload_summary_port")

        # Advanced Settings
        auto_rx_config["search_step"] = config.getfloat(
            "advanced", "search_step")
        auto_rx_config["snr_threshold"] = config.getfloat(
            "advanced", "snr_threshold")
        auto_rx_config["min_distance"] = config.getfloat(
            "advanced", "min_distance")
        auto_rx_config["dwell_time"] = config.getint("advanced", "dwell_time")
        auto_rx_config["quantization"] = config.getint("advanced",
                                                       "quantization")
        auto_rx_config["max_peaks"] = config.getint("advanced", "max_peaks")
        auto_rx_config["scan_dwell_time"] = config.getint(
            "advanced", "scan_dwell_time")
        auto_rx_config["detect_dwell_time"] = config.getint(
            "advanced", "detect_dwell_time")
        auto_rx_config["scan_delay"] = config.getint("advanced", "scan_delay")
        auto_rx_config["payload_id_valid"] = config.getint(
            "advanced", "payload_id_valid")
        auto_rx_config["synchronous_upload"] = config.getboolean(
            "advanced", "synchronous_upload")

        # Rotator Settings
        auto_rx_config["rotator_enabled"] = config.getboolean(
            "rotator", "rotator_enabled")
        auto_rx_config["rotator_update_rate"] = config.getint(
            "rotator", "update_rate")
        auto_rx_config["rotator_hostname"] = config.get(
            "rotator", "rotator_hostname")
        auto_rx_config["rotator_port"] = config.getint("rotator",
                                                       "rotator_port")
        auto_rx_config["rotator_homing_enabled"] = config.getboolean(
            "rotator", "rotator_homing_enabled")
        auto_rx_config["rotator_home_azimuth"] = config.getfloat(
            "rotator", "rotator_home_azimuth")
        auto_rx_config["rotator_home_elevation"] = config.getfloat(
            "rotator", "rotator_home_elevation")
        auto_rx_config["rotator_homing_delay"] = config.getint(
            "rotator", "rotator_homing_delay")
        auto_rx_config["rotation_threshold"] = config.getfloat(
            "rotator", "rotation_threshold")

        # Web interface settings.
        auto_rx_config["web_host"] = config.get("web", "web_host")
        auto_rx_config["web_port"] = config.getint("web", "web_port")
        auto_rx_config["web_archive_age"] = config.getint("web", "archive_age")

        auto_rx_config["save_detection_audio"] = config.getboolean(
            "debugging", "save_detection_audio")
        auto_rx_config["save_decode_audio"] = config.getboolean(
            "debugging", "save_decode_audio")
        auto_rx_config["save_decode_iq"] = config.getboolean(
            "debugging", "save_decode_iq")

        # NOTE 2019-09-21: The station code will now be fixed at the default to avoid multiple iMet callsign issues.
        # auto_rx_config['station_code'] = config.get('location', 'station_code')
        # if len(auto_rx_config['station_code']) > 5:
        # 	auto_rx_config['station_code'] = auto_rx_config['station_code'][:5]
        # 	logging.warning("Config - Clipped station code to 5 digits: %s" % auto_rx_config['station_code'])

        auto_rx_config["temporary_block_time"] = config.getint(
            "advanced", "temporary_block_time")

        # New demod tweaks - Added 2019-04-23
        # Default to all experimental decoders on.
        auto_rx_config["experimental_decoders"] = {
            "RS41": True,
            "RS92": True,
            "DFM": True,
            "M10": True,
            "M20": True,
            "IMET": False,
            "LMS6": True,
            "MK2LMS": False,
            "MEISEI": False,
            "UDP": False,
        }
        auto_rx_config["rs41_drift_tweak"] = config.getboolean(
            "advanced", "drift_tweak")
        auto_rx_config["decoder_spacing_limit"] = config.getint(
            "advanced", "decoder_spacing_limit")
        auto_rx_config["experimental_decoders"]["RS41"] = config.getboolean(
            "advanced", "rs41_experimental")
        auto_rx_config["experimental_decoders"]["RS92"] = config.getboolean(
            "advanced", "rs92_experimental")
        auto_rx_config["experimental_decoders"]["M10"] = config.getboolean(
            "advanced", "m10_experimental")
        auto_rx_config["experimental_decoders"]["DFM"] = config.getboolean(
            "advanced", "dfm_experimental")
        auto_rx_config["experimental_decoders"]["LMS6"] = config.getboolean(
            "advanced", "lms6-400_experimental")

        try:
            auto_rx_config["web_control"] = config.getboolean(
                "web", "web_control")
            auto_rx_config["ngp_tweak"] = config.getboolean(
                "advanced", "ngp_tweak")
            auto_rx_config["gpsd_enabled"] = config.getboolean(
                "location", "gpsd_enabled")
            auto_rx_config["gpsd_host"] = config.get("location", "gpsd_host")
            auto_rx_config["gpsd_port"] = config.getint(
                "location", "gpsd_port")
        except:
            logging.warning(
                "Config - Did not find web control / ngp_tweak / gpsd options, using defaults (disabled)"
            )
            auto_rx_config["web_control"] = False
            auto_rx_config["ngp_tweak"] = False
            auto_rx_config["gpsd_enabled"] = False

        try:
            auto_rx_config["min_radius_km"] = config.getint(
                "filtering", "min_radius_km")
            auto_rx_config["radius_temporary_block"] = config.getboolean(
                "filtering", "radius_temporary_block")
        except:
            logging.warning(
                "Config - Did not find minimum radius filter setting, using default (0km)."
            )
            auto_rx_config["min_radius_km"] = 0
            auto_rx_config["radius_temporary_block"] = False

        try:
            auto_rx_config["aprs_use_custom_object_id"] = config.getboolean(
                "aprs", "aprs_use_custom_object_id")
        except:
            logging.warning(
                "Config - Did not find aprs_use_custom_object_id setting, using default (False)"
            )
            auto_rx_config["aprs_use_custom_object_id"] = False

        try:
            auto_rx_config["aprs_port"] = config.getint("aprs", "aprs_port")
        except:
            logging.warning(
                "Config - Did not find aprs_port setting - using default of 14590. APRS packets might not be forwarded out to the wider APRS-IS network!"
            )
            auto_rx_config["aprs_port"] = 14590

        try:
            auto_rx_config["email_error_notifications"] = config.getboolean(
                "email", "error_notifications")
            auto_rx_config["email_launch_notifications"] = config.getboolean(
                "email", "launch_notifications")
            auto_rx_config["email_landing_notifications"] = config.getboolean(
                "email", "landing_notifications")
            auto_rx_config["email_landing_range_threshold"] = config.getfloat(
                "email", "landing_range_threshold")
            auto_rx_config[
                "email_landing_altitude_threshold"] = config.getfloat(
                    "email", "landing_altitude_threshold")
        except:
            logging.warning(
                "Config - Did not find new email settings (v1.3.3), using defaults"
            )
            auto_rx_config["email_error_notifications"] = False
            auto_rx_config["email_launch_notifications"] = True
            auto_rx_config["email_landing_notifications"] = True
            auto_rx_config["email_landing_range_threshold"] = 30
            auto_rx_config["email_landing_altitude_threshold"] = 1000

        try:
            auto_rx_config["kml_refresh_rate"] = config.getint(
                "web", "kml_refresh_rate")
        except:
            logging.warning(
                "Config - Did not find kml_refresh_rate setting, using default (10 seconds)."
            )
            auto_rx_config["kml_refresh_rate"] = 11

        # If we are being called as part of a unit test, just return the config now.
        if no_sdr_test:
            return auto_rx_config

        # Now we attempt to read in the individual SDR parameters.
        auto_rx_config["sdr_settings"] = {}

        for _n in range(1, auto_rx_config["sdr_quantity"] + 1):
            _section = "sdr_%d" % _n
            try:
                _device_idx = config.get(_section, "device_idx")
                _ppm = round(config.getfloat(_section, "ppm"))
                _gain = config.getfloat(_section, "gain")
                _bias = config.getboolean(_section, "bias")

                if (auto_rx_config["sdr_quantity"] > 1) and (_device_idx
                                                             == "0"):
                    logging.critical(
                        "Config - SDR Device ID of 0 used with a multi-SDR configuration. Go read the warning in the config file!"
                    )
                    return None

                # See if the SDR exists.
                _sdr_valid = rtlsdr_test(_device_idx)
                if _sdr_valid:
                    auto_rx_config["sdr_settings"][_device_idx] = {
                        "ppm": _ppm,
                        "gain": _gain,
                        "bias": _bias,
                        "in_use": False,
                        "task": None,
                    }
                    logging.info("Config - Tested SDR #%s OK" % _device_idx)
                else:
                    logging.warning("Config - SDR #%s invalid." % _device_idx)
            except Exception as e:
                logging.error("Config - Error parsing SDR %d config - %s" %
                              (_n, str(e)))
                continue

        # Sanity checks when using more than one SDR
        if (len(auto_rx_config["sdr_settings"].keys()) >
                1) and (auto_rx_config["aprs_object_id"] != "<id>"):
            logging.critical(
                "Fixed APRS object ID used in a multi-SDR configuration. Go read the warnings in the config file!"
            )
            return None

        if (len(auto_rx_config["sdr_settings"].keys()) >
                1) and (auto_rx_config["rotator_enabled"]):
            logging.critical(
                "Rotator enabled in a multi-SDR configuration. Go read the warnings in the config file!"
            )
            return None

        # TODO: Revisit this limitation once the OziPlotter output sub-module is complete.
        if (len(auto_rx_config["sdr_settings"].keys()) >
                1) and auto_rx_config["ozi_enabled"]:
            logging.critical(
                "Oziplotter output enabled in a multi-SDR configuration.")
            return None

        if len(auto_rx_config["sdr_settings"].keys()) == 0:
            # We have no SDRs to use!!
            logging.error("Config - No working SDRs! Cannot run...")
            return None
        else:
            # Create a global copy of the configuration file at this point
            global_config = copy.deepcopy(auto_rx_config)

            # Excise some sensitive parameters from the global config.
            global_config.pop("email_smtp_login")
            global_config.pop("email_smtp_password")
            global_config.pop("email_smtp_server")

            return auto_rx_config

    except:
        traceback.print_exc()
        logging.error("Could not parse config file.")
        return None
Beispiel #39
0
    def save(self, filename, private=False):
        """
        Save repository into a file (modules.list for example).

        :param filename: path to file to save repository.
        :type filename: str
        :param private: if enabled, save URL of repository.
        :type private: bool
        """
        config = RawConfigParser()
        config.set(DEFAULTSECT, 'name', self.name)
        config.set(DEFAULTSECT, 'update', self.update)
        config.set(DEFAULTSECT, 'maintainer', self.maintainer)
        config.set(DEFAULTSECT, 'signed', int(self.signed))
        config.set(DEFAULTSECT, 'key_update', self.key_update)
        if private:
            config.set(DEFAULTSECT, 'url', self.url)

        for module in self.modules.itervalues():
            config.add_section(module.name)
            for key, value in module.dump():
                config.set(module.name, key, to_unicode(value).encode('utf-8'))

        with open(filename, 'wb') as f:
            config.write(f)
Beispiel #40
0
def parse_module_definition(mod_info):

    database_names = set()

    for root, dirs, filenames in os.walk(mod_dir):
        for f in filenames:
            if f.endswith(".txt"):
                mod_def = os.path.join(root, f)
                fread = open(mod_def, 'r')
                contents = fread.read()

                parser = RawConfigParser()
                parser.read(mod_def)

                mod_name = mod_def
                query_name = parser.get('Query Metadata', 'QUERY_NAME')
                database_name = parser.get('Database Metadata',
                                           'DATABASE').split(',')
                activity = parser.get('Query Metadata', 'ACTIVITY')
                key_timestamp = parser.get('Query Metadata', 'KEY_TIMESTAMP')

                for database in database_name:
                    database_names.add(database)

                for db in database_name:
                    uniquekey = mod_def + "#" + db
                    mod_info[uniquekey] = []

                if version == 'yolo':
                    for section in parser.sections():
                        try:
                            if "SQL Query" in section:
                                sql_query = parser.get(section, 'QUERY')
                                mod_info[uniquekey] = [
                                    query_name, db, activity, key_timestamp,
                                    sql_query
                                ]
                        except:
                            pass
                else:
                    for section in parser.sections():
                        try:
                            if version in section:
                                sql_query = parser.get(section, 'QUERY')
                                mod_info[uniquekey] = [
                                    query_name, db, activity, key_timestamp,
                                    sql_query
                                ]
                        except:
                            pass

    print "\n==> Parsing", len(
        mod_info
    ), "modules (Note: Some modules may be run on more than one database.)"

    count = 1
    modules = set()

    for item in sorted(mod_info):
        dbs = item.split('#')
        for mod in dbs:
            modules.add(dbs[0])
        print "\t[" + str(count) + "] " + str(dbs[0]) + " on " + str(dbs[1])
        count = count + 1

    print "\n==> Will lazily run APOLLO on " + str(
        len(modules)) + " unique modules and " + str(
            len(database_names)) + " unique databases."

    print "\n==> Searching for database files...this may take a hot minute..."
    print
    for root, dirs, filenames in os.walk(data_dir):
        for f in filenames:
            if f in database_names:
                for mod_def, mod_data in mod_info.items():
                    if mod_data:
                        if mod_data[1] == f:
                            mod_info[mod_def].append(os.path.join(root, f))

    for mod_def, mod_data in mod_info.items():
        mod_def_split = mod_def.split('#')
        if mod_data:
            print mod_def_split[0] + " on " + mod_def_split[1], ":", len(
                mod_data) - 5, "databases."
            run_module(mod_def, mod_data[0], mod_data[5:], mod_data[2],
                       mod_data[3], mod_data[4])
            print
        else:
            print mod_def_split[0] + " on " + mod_def_split[
                1], ": Module not supported for version of data provided."
            print
Beispiel #41
0
def load_config(environ):
    """Load configuration options

    Options are read from a config file. The config file location is
    controlled by the PythonOption ConfigFile in the httpd config.

    Backwards compatibility:
        - if ConfigFile is not set, opts are loaded from http config
        - if ConfigFile is set, then the http config must not provide Koji options
        - In a future version we will load the default hub config regardless
        - all PythonOptions (except ConfigFile) are now deprecated and support for them
          will disappear in a future version of Koji
    """
    logger = logging.getLogger("koji")
    #get our config file(s)
    if 'modpy.opts' in environ:
        modpy_opts = environ.get('modpy.opts')
        cf = modpy_opts.get('ConfigFile', None)
        # to aid in the transition from PythonOptions to hub.conf, we only load
        # the configfile if it is explicitly configured
        if cf == '/etc/koji-hub/hub.conf':
            cfdir = modpy_opts.get('ConfigDir', '/etc/koji-hub/hub.conf.d')
        else:
            cfdir = modpy_opts.get('ConfigDir', None)
        if not cf and not cfdir:
            logger.warn(
                'Warning: configuring Koji via PythonOptions is deprecated. Use hub.conf'
            )
    else:
        cf = environ.get('koji.hub.ConfigFile', '/etc/koji-hub/hub.conf')
        cfdir = environ.get('koji.hub.ConfigDir', '/etc/koji-hub/hub.conf.d')
        modpy_opts = {}
    if cfdir:
        configs = koji.config_directory_contents(cfdir)
    else:
        configs = []
    if cf and os.path.isfile(cf):
        configs.append(cf)
    if configs:
        config = RawConfigParser()
        config.read(configs)
    else:
        config = None
    cfgmap = [
        #option, type, default
        ['DBName', 'string', None],
        ['DBUser', 'string', None],
        ['DBHost', 'string', None],
        ['DBhost', 'string', None],  # alias for backwards compatibility
        ['DBPass', 'string', None],
        ['KojiDir', 'string', None],
        ['AuthPrincipal', 'string', None],
        ['AuthKeytab', 'string', None],
        ['ProxyPrincipals', 'string', ''],
        ['HostPrincipalFormat', 'string', None],
        ['DNUsernameComponent', 'string', 'CN'],
        ['ProxyDNs', 'string', ''],
        ['CheckClientIP', 'boolean', True],
        ['LoginCreatesUser', 'boolean', True],
        ['KojiWebURL', 'string', 'http://localhost.localdomain/koji'],
        ['EmailDomain', 'string', None],
        ['NotifyOnSuccess', 'boolean', True],
        ['DisableNotifications', 'boolean', False],
        ['Plugins', 'string', ''],
        ['PluginPath', 'string', '/usr/lib/koji-hub-plugins'],
        ['KojiDebug', 'boolean', False],
        ['KojiTraceback', 'string', None],
        ['VerbosePolicy', 'boolean', False],
        ['EnableFunctionDebug', 'boolean', False],
        ['LogLevel', 'string', 'WARNING'],
        [
            'LogFormat', 'string',
            '%(asctime)s [%(levelname)s] m=%(method)s u=%(user_name)s p=%(process)s r=%(remoteaddr)s %(name)s: %(message)s'
        ],
        ['MissingPolicyOk', 'boolean', True],
        ['EnableMaven', 'boolean', False],
        ['EnableWin', 'boolean', False],
        ['EnableImageMigration', 'boolean', False],
        ['RLIMIT_AS', 'string', None],
        ['RLIMIT_CORE', 'string', None],
        ['RLIMIT_CPU', 'string', None],
        ['RLIMIT_DATA', 'string', None],
        ['RLIMIT_FSIZE', 'string', None],
        ['RLIMIT_MEMLOCK', 'string', None],
        ['RLIMIT_NOFILE', 'string', None],
        ['RLIMIT_NPROC', 'string', None],
        ['RLIMIT_OFILE', 'string', None],
        ['RLIMIT_RSS', 'string', None],
        ['RLIMIT_STACK', 'string', None],
        ['MemoryWarnThreshold', 'integer', 5000],
        ['MaxRequestLength', 'integer', 4194304],
        ['LockOut', 'boolean', False],
        ['ServerOffline', 'boolean', False],
        ['OfflineMessage', 'string', None],
    ]
    opts = {}
    for name, dtype, default in cfgmap:
        if config:
            key = ('hub', name)
            if config.has_option(*key):
                if dtype == 'integer':
                    opts[name] = config.getint(*key)
                elif dtype == 'boolean':
                    opts[name] = config.getboolean(*key)
                else:
                    opts[name] = config.get(*key)
            else:
                opts[name] = default
        else:
            if modpy_opts.get(name, None) is not None:
                if dtype == 'integer':
                    opts[name] = int(modpy_opts.get(name))
                elif dtype == 'boolean':
                    opts[name] = modpy_opts.get(name).lower() in ('yes', 'on',
                                                                  'true', '1')
                else:
                    opts[name] = modpy_opts.get(name)
            else:
                opts[name] = default
    if opts['DBHost'] is None:
        opts['DBHost'] = opts['DBhost']
    # load policies
    # (only from config file)
    if config and config.has_section('policy'):
        #for the moment, we simply transfer the policy conf to opts
        opts['policy'] = dict(config.items('policy'))
    else:
        opts['policy'] = {}
    for pname, text in _default_policies.iteritems():
        opts['policy'].setdefault(pname, text)
    # use configured KojiDir
    if opts.get('KojiDir') is not None:
        koji.BASEDIR = opts['KojiDir']
        koji.pathinfo.topdir = opts['KojiDir']
    return opts
Beispiel #42
0
 def save(self):
     config = RawConfigParser()
     for name, version in self.versions.iteritems():
         config.set(DEFAULTSECT, name, version)
     with open(os.path.join(self.path, self.VERSIONS_LIST), 'wb') as fp:
         config.write(fp)
Beispiel #43
0
    def run(self):

        # print title
        Header().title_info('Wifi')

        if not windll.Shell32.IsUserAnAdmin():
            print_debug('WARNING', '[!] This script should be run as admin!')
            return
        else:

            if 'ALLUSERSPROFILE' in os.environ:
                directory = os.environ[
                    'ALLUSERSPROFILE'] + os.sep + 'Microsoft\Wlansvc\Profiles\Interfaces'
            else:
                print_debug(
                    'ERROR',
                    'Environment variable (ALLUSERSPROFILE) has not been found.'
                )
                return

            if not os.path.exists(directory):
                print_debug(
                    'INFO',
                    'No credentials found.\nFile containing passwords not found:\n%s'
                    % directory)
                return

            try:
                print_debug('INFO', '[!] Trying to elevate our privilege')
                get_system_priv()
                print_debug(
                    'INFO',
                    '[!] Elevation ok - Passwords decryption is in progress')
            except Exception, e:
                print_debug('DEBUG', '{0}'.format(e))
                print_debug(
                    'ERROR',
                    '[!] An error occurs during the privilege elevation process. Wifi passwords have not been decrypted'
                )

            time.sleep(5)

            # read temp file containing all passwords found
            pwdFound = []
            filepath = tempfile.gettempdir() + os.sep + 'TEMP123A.txt'

            # the file has not been created yet
            if not os.path.exists(filepath):
                time.sleep(5)

            if os.path.exists(filepath):
                cp = RawConfigParser()
                cp.read(filepath)
                for section in cp.sections():
                    values = {}
                    for c in cp.items(section):
                        values[str(c[0])] = str(c[1])
                    pwdFound.append(values)

                # remove file on the temporary directory
                os.remove(filepath)

                # print the results
                print_output("Wifi", pwdFound)
            else:
                print_debug('INFO', 'No passwords found')
Beispiel #44
0
    def test_valid_rule(self):
        """ Checks that rules are parsed correctly """
        config = RawConfigParser()
        config.add_section("rule1")
        config.set("rule1", "check", "valid_checker")
        config.set("rule1", "check.param_true", "true")
        config.set("rule1", "check.param_number", "1337")
        config.set("rule1", "check.param_str", '"foobar"')
        config.set("rule1", "check.param_obj", '{"foo":"bar"}')
        config.set("rule1", "check.param_arr", '[true, 1337, ["foobar"]]')
        config.set("rule1", "filter_pattern", "foo")
        config.set("rule1", "filter_field", "bar")
        config.set("rule1", "filter_collection", "baz")
        config.set("rule1", "holdingpen", "true")

        config.add_section("rule2")
        config.set("rule2", "check", "other_checker")

        rule1 = load_rule(config, PLUGINS_MOCK, "rule1")
        rule2 = load_rule(config, PLUGINS_MOCK, "rule2")

        self.assertEqual(rule1["check"], "valid_checker")
        self.assertTrue(rule1["checker_params"]["param_true"])
        self.assertEqual(rule1["checker_params"]["param_number"], 1337)
        self.assertEqual(rule1["checker_params"]["param_str"], "foobar")
        self.assertEqual(rule1["checker_params"]["param_obj"], {"foo": "bar"})
        self.assertEqual(rule1["checker_params"]["param_arr"],
                         [True, 1337, ["foobar"]])
        self.assertEqual(rule1["filter_pattern"], "foo")
        self.assertEqual(rule1["filter_field"], "bar")
        self.assertEqual(rule1["filter_collection"], "baz")
        self.assertEqual(rule1["holdingpen"], True)

        self.assertEqual(rule2["check"], "other_checker")
Beispiel #45
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 #46
0
    def __init__(self, configfile):
        """
        :param configfile: path to configfile
        """
        self.configfile = configfile

        configparser = RawConfigParser()
        config_tmp = configparser.read(self.configfile)
        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)

        self.schemas = {
            "cloud":
            Schema({
                "provider": Any('ec2_boto', 'google'),
                "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)),
                "gce_project_id": All(str, Length(min=1)),
                "gce_client_id": All(str, Length(min=1)),
                "gce_client_secret": All(str, Length(min=1)),
            }),
            "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)
        }
Beispiel #47
0
class SncConfig(object):
    def __init__(self):
        self.config = RawConfigParser(allow_no_value=False)
        try:
            if ENVBUILDER_CONF in os.environ:
                self.config_file_path = os.environ[ENVBUILDER_CONF]
                if len(str(self.config_file_path).strip()) > 0 and (
                        ENVBUILDER_CONF in self.config_file_path):
                    self.config.read(self.config_file_path)
                else:
                    self.config.read(ENVBUILDER_CONF)
            else:
                self.config.read(ENVBUILDER_CONF)
        except:
            ColorPrint.err("Config file {0} not found".format(ENVBUILDER_CONF))
            exit(1)

    def getstring(self, section, param_name):
        try:
            return self.config.get(section, param_name)
        except:
            ColorPrint.err(
                "Config file {0} or section not found".format(ENVBUILDER_CONF))
            exit(1)

    def getboolean(self, section, param_name):
        try:
            return self.config.getboolean(section, param_name)
        except:
            ColorPrint.err(
                "Config file {0} or section not found".format(ENVBUILDER_CONF))
            exit(1)

    def getint(self, section, param_name):
        try:
            return self.config.getint(section, param_name)
        except:
            ColorPrint.err(
                "Config file {0} or section not found".format(ENVBUILDER_CONF))
            exit(1)

    def getlist(self, section, param_name):
        try:
            cfg_list = self.config.get(section, param_name)
            return cfg_list.split(",")
        except:
            ColorPrint.err(
                "Config file {0} or section not found".format(ENVBUILDER_CONF))
            exit(1)

    def getsection(self, section_name):
        try:
            return self.config.items(section_name)
        except:
            ColorPrint.err(
                "Config file {0} or section not found".format(ENVBUILDER_CONF))
            exit(1)
Beispiel #48
0
    def test_call_signature(self, m_time_delta, m_req):
        m_res = m_req.return_value
        m_json = m_res.json.return_value
        m_time_delta.return_value = 42

        api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, CONSUMER_KEY)
        _run(api.init())

        # nominal
        m_res.status = 200
        self.assertEqual(m_json, _run(api.call(FAKE_METHOD, FAKE_PATH, None, True)))
        m_time_delta.assert_called_once_with()
        m_req.assert_called_once_with(
            FAKE_METHOD, BASE_URL+'/unit/test',
            headers={
                'X-Ovh-Consumer': CONSUMER_KEY,
                'X-Ovh-Application': APPLICATION_KEY,
                'X-Ovh-Signature': '$1$16ae5ba8c63841b1951575be905867991d5f49dc',
                'X-Ovh-Timestamp': '1404395931',
            }, data='', timeout=TIMEOUT
        )
        m_time_delta.reset_mock()
        m_req.reset_mock()


        # data, nominal
        data = OrderedDict([('some', 'random'), ('data', 42)])
        m_res.status = 200
        self.assertEqual(m_json, _run(api.call(FAKE_METHOD, FAKE_PATH, data, True)))
        m_time_delta.assert_called_once_with()
        m_req.assert_called_once_with(
            FAKE_METHOD, BASE_URL+'/unit/test',
            headers={
                'X-Ovh-Consumer': CONSUMER_KEY,
                'X-Ovh-Application': APPLICATION_KEY,
                'Content-type': 'application/json',
                'X-Ovh-Timestamp': '1404395931',
                'X-Ovh-Signature': '$1$9acb1ac0120006d16261a635aed788e83ab172d2',
                }, data=json.dumps(data), timeout=TIMEOUT
        )
        m_time_delta.reset_mock()
        m_req.reset_mock()

        # Overwrite configuration to avoid interfering with any local config
        from asyncovh.client import config
        try:
            from ConfigParser import RawConfigParser
        except ImportError:
            # Python 3
            from configparser import RawConfigParser

        self._orig_config = config.config
        config.config = RawConfigParser()

        # errors
        try:
            api = Client(ENDPOINT, APPLICATION_KEY, None, CONSUMER_KEY)
            _run(api.init())
            self.assertRaises(InvalidKey, _run, api.call(FAKE_METHOD, FAKE_PATH, None, True))
            api = Client(ENDPOINT, APPLICATION_KEY, APPLICATION_SECRET, None)
            _run(api.init())
            self.assertRaises(InvalidKey, _run, api.call(FAKE_METHOD, FAKE_PATH, None, True))
        finally:
            # Restore configuration
            config.config = self._orig_config
def main():
    """
    Method main, set output dir and call a specific function, as given in the options
    :param argv:
    :return: None
    """
    config = RawConfigParser()
    config.readfp(resource_stream('drf_gen', 'config.ini'))

    outputdir = config.get('outputdir', 'dir')
    os.mkdir(outputdir) if not os.path.exists(outputdir) else outputdir

    ap = ArgumentParser()
    ap.add_argument('-vv',
                    '--verbose',
                    default=False,
                    help='Increase verbosity.')

    ap.add_argument('-m',
                    '--model',
                    required=True,
                    action='store',
                    dest='models_path',
                    help='Path to your models.py file.')

    ap.add_argument('-a',
                    '--admin',
                    action='store_true',
                    help='Will create a admin.py file from your models.py.')

    ap.add_argument('-v',
                    '--views',
                    action='store_true',
                    help='Will create a views.py file from your models.py.')

    ap.add_argument(
        '-s',
        '--serializers',
        action='store_true',
        help='Will create a serializers.py file from your models.py.')

    ap.add_argument('-u',
                    '--urls',
                    action='store_true',
                    help='Will create a urls.py file from your models.py.')

    ap.add_argument(
        '-A',
        '--All',
        action='store_true',
        help=
        'Will create four files: urls.py, admin.py, views.py, serializers.py, from your models.py.'
    )

    ap.add_argument('-D',
                    '--Delete',
                    action='store_true',
                    help='\033[91m' + outputdir +
                    ' directory will be destroyed!!!'
                    '\033[0m')

    args = ap.parse_args()

    models = extractor_obj(args.models_path)
    if models:
        if args.admin:
            make_admin(outputdir)
            if args.verbose:
                print("\033[91madmin.py genereted at!---> \033[93m" +
                      outputdir + "/admin.py")

        if args.views:
            make_views(outputdir)
            if args.verbose:
                print("\033[91mviews.py genereted at!---> \033[93m" +
                      outputdir + "/views.py")

        if args.urls:
            make_urls(outputdir)
            if args.verbose:
                print("\033[91murls.py genereted at!---> \033[93m" +
                      outputdir + "/urls.py")

        if args.serializers:
            make_serializers(outputdir)
            if args.verbose:
                print("\033[91serializers.py genereted at!---> \033[93m" +
                      outputdir + "/serializers.py")

        if args.All:
            make_admin(outputdir)
            make_views(outputdir)
            make_urls(outputdir)
            make_serializers(outputdir)
            if args.verbose:
                print("\033[91madmin.py genereted at!---> \033[93m" +
                      outputdir + "/admin.py")
                print("\033[91mviews.py genereted at!---> \033[93m" +
                      outputdir + "/views.py")
                print("\033[91murls.py genereted at!---> \033[93m" +
                      outputdir + "/urls.py")
                print("\033[91serializers.py genereted at!---> \033[93m" +
                      outputdir + "/serializers.py")

        if args.Delete:
            op = raw_input(
                '\033[91m Warning!!! ' + outputdir +
                'directory will be destroyed!!! do you have sure? yes|not '
                '\033[0m')
            if op == 'yes':
                shutil.rmtree(outputdir)
                if args.verbose:
                    print('\033[91m' + outputdir +
                          ' directory was destroyed!!!'
                          '\033[0m')
                    sys.exit(0)
            else:
                print("OK nothing was destroyed.")
                sys.exit(0)

        make_models_improve()
        sys.exit(0)
    else:
        print(
            "can't read models.py, make sure that you was used a valid path/file."
        )
        sys.exit(1)
Beispiel #50
0
For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

import os
from ConfigParser import RawConfigParser

BASE_DIR = os.path.dirname(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
SETTINGS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

config = RawConfigParser()
config.read(os.path.join(SETTINGS_DIR, 'config.cfg'))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*&3&r^&^j0c%vp28yqun9!4_r1%72mr40j0^upg&ooyf%*g@%e'

ALLOWED_HOSTS = ['*']

LOGIN_URL = '/login'

# Application definition

INSTALLED_APPS = [
Beispiel #51
0
 def _load(self):
     path = os.path.join(os.getcwd(), 'syncer.cfg')
     self.config = RawConfigParser()
     self.config.read(path)
Beispiel #52
0
    def __init__(self, ini_path=None, ini_dir=None, load_sections=None):
        """
        :param ini_dir: Directory path in which to start looking for the ini
        file.  Will climb the file tree from here looking for 'tests.ini' file,
        unless 'WEAVE_TESTFILE' env var is set, in which case it will climb the
        file tree from here looking for 'tests_${WEAVE_TESTFILE}.ini'.

        :param ini_path: Full path to configuration file.  Takes precedence
        over ini_dir, if both are provided.  Raises IOError if file doesn't
        exist.

        One or the other of `ini_dir` or `ini_path` arguments MUST be provided.

        :param load_sections: A sequence of strings that name the configuration
        sections that should be dynamically loaded.  Any entry in this sequence
        could alternately be a 2-tuple containing the name of the section and
        the corresponding class parameter value to use.
        """
        self.start_dir = ini_dir
        if ini_path:
            if not os.path.isfile(ini_path):
                raise IOError("invalid config file: %s" % ini_path)
            ini_dir = os.path.dirname(ini_path)
        elif ini_dir:
            if 'WEAVE_TESTFILE' in os.environ:
                test_filename = 'tests_%s.ini' % os.environ['WEAVE_TESTFILE']
            else:
                test_filename = 'tests.ini'

            while True:
                ini_path = os.path.join(ini_dir, test_filename)
                if os.path.exists(ini_path):
                    break

                if ini_path == ("/%s" % test_filename) \
                    or ini_path == test_filename:
                    raise IOError("cannot locate %s" % test_filename)

                ini_dir = os.path.split(ini_dir)[0]
            else:
                raise ValueError('No ini_path or ini_dir specified.')

        self.ini_dir = ini_dir
        self.ini_path = ini_path

        ini_cfg = RawConfigParser()
        ini_cfg.read(ini_path)

        # loading loggers
        self.config = self.convert_config(ini_cfg, ini_path)

        # Ensure that metlog is available, either from the config
        # or by setting up a default client.
        try:
            loader = load_and_configure(self.config, "metlog_loader")
            client = loader.default_client
        except KeyError:
            sender = DebugCaptureSender()
            client = MetlogClient(sender, "syncserver")
        CLIENT_HOLDER.set_client(client.logger, client)
        if not hasattr(client, "cef"):
            log_cef_fn = metlog_cef.cef_plugin.config_plugin(dict())
            client.add_method(log_cef_fn)

        if load_sections is not None:
            for section in load_sections:
                if isinstance(section, tuple):
                    self.add_class(section[0], cls_param=section[1])
                else:
                    self.add_class(section)
Beispiel #53
0
def adjust_paths(dest, **paths):
    """Adjusts paths in `dest`/.hg/hgrc so that names in `paths` are set to
    paths[name].

    Note that any comments in the hgrc will be lost if changes are made to the
    file."""
    hgrc = os.path.join(dest, '.hg', 'hgrc')
    config = RawConfigParser()
    config.read(hgrc)

    if not config.has_section('paths'):
        config.add_section('paths')

    changed = False
    for path_name, path_value in paths.items():
        if (not config.has_option('paths', path_name) or
                config.get('paths', path_name) != path_value):
            changed = True
            config.set('paths', path_name, path_value)

    if changed:
        config.write(open(hgrc, 'w'))
#!/usr/bin/python

from ConfigParser import RawConfigParser
import email, os, smtplib, sys, traceback, markdown, syslog, requests
from M2Crypto import BIO, Rand, SMIME, X509

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# Read configuration from /etc/gpg-mailgate.conf
_cfg = RawConfigParser()
_cfg.read('/etc/gpg-mailgate.conf')
cfg = dict()
for sect in _cfg.sections():
    cfg[sect] = dict()
    for (name, value) in _cfg.items(sect):
        cfg[sect][name] = value


def log(msg):
    if cfg.has_key('logging') and cfg['logging'].has_key('file'):
        if cfg['logging']['file'] == "syslog":
            syslog.syslog(syslog.LOG_INFO | syslog.LOG_MAIL, msg)
        else:
            logfile = open(cfg['logging']['file'], 'a')
            logfile.write(msg + "\n")
            logfile.close()


CERT_PATH = cfg['smime']['cert_path'] + "/"
Beispiel #55
0
class PreferenceManager(GObject):
    __gtype_name__ ='PreferenceManager'
    
    def __init__(self, app):
        GObject.__init__(self)

        self.app = app
        
        self._config = ConfigParser()
        self._filename = os.path.join(save_config_path("garmon"), "config")
        self._config.read(self._filename)
        
        self._dialog = _PrefsDialog()
        
        self._watches = []
        

    def _pref_notify_cb(self, pname, pvalue, args):
        widget = args[0]
        if hasattr(widget, 'set_text'):
            widget.set_text(pvalue)
        elif isinstance(widget, gtk.ColorButton):
            widget.set_color(gtk.gdk.color_parse(pvalue))
        elif isinstance(widget, gtk.ToggleButton):
            widget.set_active(bool(pvalue))
        if isinstance(widget, gtk.ComboBox):
            def foreach_cb(model, path, iter):
                value = model.get_value(iter, 0)
                value = str(value)
                if value == pvalue:
                    widget.set_active_iter(iter)
            model = widget.get_model()
            model.foreach(foreach_cb)
            
              
    def _toggle_widget_cb(self, toggle, pname):
        active = toggle.get_active()
        self.set(pname, active)
        
        
    def _text_widget_activate_cb(self, widget, pname):
        value = widget.get_text()
        self.set(pname, value)
        
        
    def _text_widget_focus_out_cb(self, widget, event, pname):
        value = widget.get_text()
        self.set(pname, value)
        
        
    def _color_widget_cb(self, widget, pname):
        value = widget.get_color().to_string()
        self.set(pname, value)
        
        
    def _combo_widget_cb(self, widget, pname):
        iter = widget.get_active_iter()
        if iter:
            value = widget.get_model().get_value(iter, 0)
            self.set(pname, value)
        
        
    def notify(self, name):
        for watch in self._watches:
            if watch.name == name:
                value = self.get(name)
                for cb_id, cb, args in watch.cb_ids:
                    cb(name, value, args)


    def add_watch(self, name, cb, *args):
        if not callable(cb):
            raise AttributeError, 'cb is not callable'
        watch = None
        for item in self._watches:
            if item.name == name:
                watch = item
        if watch is None:
            watch = _Watch(name)
            self._watches.append(watch)
        cb_id = random.randint(1, 1000000)
        watch.cb_ids.append((cb_id, cb, args))
        return cb_id
        

    def remove_watch(self, name, cb_id):
        for watch in self._watches:
            if watch == name:
                for item in cb_ids:
                    if item[0] == cb_id:
                        cb_ids.remove(item)
  
      
    def get(self, name, default=None):
        if not '.' in name:
            section = 'General'
            option = name
        else:
            section, option = name.split('.')
            
        try:
            value = self._config.get(section, option)
        except:
            if default:
                self.set(name, default)
                value = default
            else:
                raise ValueError, 'No pref with name "%s" found and no default value given' % name 
        return value

       
    def set(self, name, value):
        if not '.' in name:
            section = 'General'
            option = name
        else:
            section, option = name.split('.')
        if not self._config.has_section(section):
            self._config.add_section(section)
        self._config.set(section, option, value)
                
    
    def register(self, name, default):
        if not '.' in name:
            section = 'General'
            option = name
        else:
            section, option = name.split('.')
        
        if not self._config.has_section(section):
            self._config.add_section(section)
        if not self._config.has_option(section, option):
            self._config.set(section, option, default)
        
        
    def show_dialog(self):
        res = self._dialog.run()
        self._dialog.hide()
        self.app.reload_active_plugin()
        
        
    def hide_dialog(self):
        self._dialog.hide()
        
        
    def add_dialog_page(self, xml, root, name):
        top = xml.get_widget(root)
        top.cb_ids = []
        self._dialog.notebook.append_page(top, gtk.Label(name))
        widgets = xml.get_widget_prefix('preference')
        for widget in widgets:
            name = gtk.glade.get_widget_name(widget)[len('preference;'):]
            wtype, ptype, pname = string.split(name, ';')
            if wtype == 'toggle':
                widget.connect('toggled', self._toggle_widget_cb, pname)
            elif wtype == 'text':
                widget.connect('activate', self._text_widget_activate_cb, pname)
                widget.connect('focus-out-event', self._text_widget_focus_out_cb, pname)
            elif wtype == 'color':
                widget.connect('color-set', self._color_widget_cb, pname)
            elif wtype == 'combo':
                widget.connect('changed', self._combo_widget_cb, pname)
            else:
                #FIXME: should not reach here
                pass
                
            cb_id = self.add_watch(pname, 
                                   self._pref_notify_cb, 
                                   widget)
            top.cb_ids.append(cb_id)
            self.notify(pname)


    def save(self):
        f = file(self._filename, 'w')
        self._config.write(f)
        f.close()
Beispiel #56
0
    def write_sts_token(self, profile, access_key_id, secret_access_key,
                        session_token):
        """ Writes STS auth information to credentials file """
        region = 'us-east-1'
        output = 'json'
        if not os.path.exists(self.creds_dir):
            os.makedirs(self.creds_dir)
        config = RawConfigParser()

        if os.path.isfile(self.creds_file):
            config.read(self.creds_file)

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

        config.set(profile, 'output', output)
        config.set(profile, 'region', region)
        config.set(profile, 'aws_access_key_id', access_key_id)
        config.set(profile, 'aws_secret_access_key', secret_access_key)
        config.set(profile, 'aws_session_token', session_token)

        with open(self.creds_file, 'w+') as configfile:
            config.write(configfile)
        print("Temporary credentials written to profile: %s" % profile)
        print("Invoke using: aws --profile %s <service> <command>" % profile)
Beispiel #57
0
 def __init__(self):
     self.input = RawConfigParser()
     with open("production.ini") as f:
         self.input.readfp(f)
     self.output = RawConfigParser()
Beispiel #58
0
def main():
    # -options-
    parser = OptionParser("python %prog [options] CONFIG_FILE")
    parser.add_option('-v',
                      '--verbose',
                      action='store_true',
                      dest='verbose',
                      default=False)
    (options, args) = parser.parse_args()

    # -settings-
    if len(args) < 1:
        parser.print_help()
        exit()

    if not os.path.exists(args[0]):
        raise IOError(ENOENT, 'Archivo de configuracion no encontrado',
                      args[0])

    config = RawConfigParser()
    config.read(args[0])
    # put read config options (from the .ini) into global namespace and in uppercase
    for name, value in config.items('chkemail'):
        globals()[name.upper()] = value

    # -workflow-
    # create, connect, and login to server
    server = IMAPClient(HOST, use_uid=True)
    try:
        server.login(USER, PASSWORD, port=PORT)
    except NameError:  # if PORT is not defined
        server.login(USER, PASSWORD)

    inbox = server.select_folder('INBOX')
    if options.verbose:
        print '%d messages in INBOX (included deleted)' % inbox['EXISTS']

    messages = server.search(['NOT DELETED', 'HEADER Content-Type mixed'])
    if options.verbose:
        print "%d messages with possible attch" % len(messages)

    # fetch data from messages, put each message (Mess object) into the msgs list
    scan = server.fetch(messages, ['BODYSTRUCTURE', 'ENVELOPE'])
    msgs = dict()
    for mid, response in scan.iteritems():
        # Mess class only works with mulipart messages
        if response['BODYSTRUCTURE'].is_multipart:
            msgs[mid] = Mess(mid, response['ENVELOPE'],
                             response['BODYSTRUCTURE'])

    # Select the messages with attachements I want, put them in group_msgs
    group_msgs = dict()
    for msg in msgs.itervalues():
        group_msgs[msg.id] = list()
        for part_num, part_info in msg.parts.iteritems():
            if part_info.filename:
                filename = part_info.filename.lower()
                if filename.endswith('.pdf') or filename.endswith('.xml') or \
                 filename.endswith('.zip'):
                    group_msgs[msg.id] += [part_num]
        if not group_msgs[msg.id]:
            del group_msgs[msg.id]

    # fetch all interesting parts
    for msg_id, parts in group_msgs.iteritems():
        request = ['BODY[' + str(part) + ']' for part in parts]
        response = server.fetch(msg_id, request)
        for body_part in response[msg_id].iterkeys():
            if 'BODY' in body_part:
                msgs[msg_id].parts[
                    body_part[5:-1]].data = response[msg_id][body_part]

    # move messages to trash
    if len(group_msgs.keys()) > 0:
        server.copy(group_msgs.keys(), 'INBOX.Trash')
        server.delete_messages(group_msgs.keys())
    server.logout()

    # ensure there's an OUTPUT_DIR directory
    pdf_dir = os.path.join(OUTPUT_PDF, strftime('%Y-%m'))
    ensure_dir(pdf_dir)
    ensure_dir(OUTPUT_DIR)

    # decode and write data to file
    num_attch = 0
    for msg in msgs.itervalues():
        for part in msg.parts:
            if part.data:
                filename = part.filename.lower()
                if filename.endswith('.pdf') or filename.endswith('.xml'):
                    if filename.endswith('.pdf'):
                        ensure_dir(os.path.join(pdf_dir, str(msg.envelope)))
                        attachment_filename = generate_filename(
                            os.path.join(pdf_dir, str(msg.envelope),
                                         os.path.basename(part.filename)))
                    else:
                        attachment_filename = generate_filename(
                            os.path.join(OUTPUT_DIR,
                                         os.path.basename(part.filename)))
                    with open(attachment_filename, 'wb') as file_:
                        if part.encoding == 'quoted-printable':
                            file_.write(decodestring(part.data))
                        elif part.encoding == 'base64':
                            file_.write(b64decode(part.data))
                    num_attch += 1
                elif filename.endswith('.zip') and part.encoding == 'base64':
                    with tempfile.TemporaryFile() as tmp_zip:
                        tmp_zip.write(b64decode(part.data))
                        zip_file = ZipFile(tmp_zip)
                        for f_info in zip_file.infolist():
                            if f_info.filename.lower().endswith('.xml'):
                                attachment_filename = generate_filename(
                                    os.path.join(
                                        OUTPUT_DIR,
                                        os.path.basename(f_info.filename)))
                            elif f_info.filename.lower().endswith('.pdf'):
                                ensure_dir(
                                    os.path.join(pdf_dir, str(msg.envelope)))
                                attachment_filename = generate_filename(
                                    os.path.join(
                                        pdf_dir, str(msg.envelope),
                                        os.path.basename(f_info.filename)))
                            else:
                                continue
                            with open(attachment_filename, 'wb') as file_:
                                file_.write(zip_file.read(f_info))
                            num_attch += 1

    if options.verbose:
        print '%d files extracted' % num_attch
Beispiel #59
0
# permission of Adobe.
#**************************************************************************

import sys
if sys.version_info[0] == 2:
    from ConfigParser import RawConfigParser
if sys.version_info[0] >= 3:
    from configparser import RawConfigParser
import json
import requests
import random
import string

# read configuration file
config_file_name = "usermanagement.config"
config = RawConfigParser()
config.read(config_file_name)

# server parameters
host = config.get("server", "host")
endpoint = config.get("server", "endpoint")

# enterprise parameters
domain = config.get("enterprise", "fed_domain")
org_id = config.get("enterprise", "org_id")
api_key = config.get("enterprise", "api_key")
access_token = config.get("enterprise", "access_token")

# method parameters
url = "https://" + host + endpoint + "/action/" + org_id
headers = {
Beispiel #60
0
def filter_factory(global_conf, **local_conf):
    """Standard filter factory to use the middleware with paste.deploy"""

    register_swift_info('vertigo')

    conf = global_conf.copy()
    conf.update(local_conf)

    vertigo_conf = dict()
    vertigo_conf['devices'] = conf.get('devices', '/srv/node')
    vertigo_conf['execution_server'] = conf.get('execution_server')
    vertigo_conf['mc_timeout'] = conf.get('mc_timeout', 5)
    vertigo_conf['mc_pipe'] = conf.get('mc_pipe', 'vertigo_pipe')
    # vertigo_conf['api_pipe'] = conf.get('mc_pipe', 'api_pipe')
    vertigo_conf['metadata_visibility'] = conf.get('metadata_visibility', True)
    vertigo_conf['mc_dir'] = conf.get('mc_dir', '/home/docker_device/vertigo/scopes')
    vertigo_conf['cache_dir'] = conf.get('cache_dir', '/home/docker_device/cache/scopes')
    vertigo_conf['mc_container'] = conf.get('mc_container', 'microcontroller')
    vertigo_conf['mc_dependency'] = conf.get('mc_dependency', 'dependency')

    ''' Load storlet parameters '''
    configParser = RawConfigParser()
    configParser.read(conf.get('__file__'))
    storlet_parameters = configParser.items('filter:storlet_handler')
    for key, val in storlet_parameters:
        vertigo_conf[key] = val

    """ Load Storlets Gateway configuration """
    configParser = RawConfigParser()
    configParser.read(vertigo_conf['storlet_gateway_conf'])
    additional_items = configParser.items("DEFAULT")
    for key, val in additional_items:
        vertigo_conf[key] = val

    """ Load Storlets Gateway class """
    module_name = vertigo_conf.get('storlet_gateway_module', 'stub')
    gateway_class = load_gateway(module_name)
    vertigo_conf['storlets_gateway_module'] = gateway_class

    """
    Register Lua script to retrieve policies in a single redis call
    """
    vertigo_conf['redis_host'] = conf.get('redis_host', 'controller')
    vertigo_conf['redis_port'] = int(conf.get('redis_port', 6379))
    vertigo_conf['redis_db'] = int(conf.get('redis_db', 0))

    if vertigo_conf['execution_server'] == 'proxy':
        r = redis.StrictRedis(vertigo_conf['redis_host'],
                              vertigo_conf['redis_port'],
                              vertigo_conf['redis_db'])
        lua = """
            local t = {}
            if redis.call('EXISTS', 'mc_pipeline:'..ARGV[1]..':'..ARGV[2]..':'..ARGV[3])==1 then
              t = redis.call('HGETALL', 'mc_pipeline:'..ARGV[1]..':'..ARGV[2]..':'..ARGV[3])
            elseif redis.call('EXISTS', 'mc_pipeline:'..ARGV[1]..':'..ARGV[2])==1 then
              t = redis.call('HGETALL', 'mc_pipeline:'..ARGV[1]..':'..ARGV[2])
            end
            return t"""

        lua_sha = r.script_load(lua)
        vertigo_conf['LUA_get_mc_sha'] = lua_sha

    def swift_vertigo(app):
        return VertigoHandlerMiddleware(app, global_conf, vertigo_conf)

    return swift_vertigo