Python validate.Validator() Examples

The following are 14 code examples of validate.Validator(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module validate , or try the search function .
Example #1
Source File: config.py    From burp-ui with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def parse(self, config, defaults=None):
        """Parse the conf

        :param config: Configuration to parse
        :type config: str, list or File

        :param defaults: Default options
        :type defaults: dict
        """
        self.conf = {}
        self.conffile = config
        self.section = None
        if defaults is not None or not hasattr(self, 'defaults'):
            self.defaults = defaults
        self.validator = validate.Validator()
        try:
            self.conf = configobj.ConfigObj(config, encoding='utf-8')
            self.mtime = os.path.getmtime(self.conffile)
        except configobj.ConfigObjError as exp:
            # We were unable to parse the config
            self.logger.critical('Unable to parse configuration')
            raise exp 
Example #2
Source File: config.py    From burp-ui with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def parse(self, config, defaults=None):
        """Parse the conf

        :param config: Configuration to parse
        :type config: str, list or File

        :param defaults: Default options
        :type defaults: dict
        """
        self.conf = {}
        self.conffile = config
        self.section = None
        if defaults is not None or not hasattr(self, 'defaults'):
            self.defaults = defaults
        self.validator = validate.Validator()
        try:
            self.conf = configobj.ConfigObj(config, encoding='utf-8')
            self.mtime = os.path.getmtime(self.conffile)
        except configobj.ConfigObjError as exp:
            # We were unable to parse the config
            self.logger.critical('Unable to parse configuration')
            raise exp 
Example #3
Source File: config.py    From burp-ui with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def parse(self, config, defaults=None):
        """Parse the conf

        :param config: Configuration to parse
        :type config: str, list or File

        :param defaults: Default options
        :type defaults: dict
        """
        self.conf = {}
        self.conffile = config
        self.section = None
        if defaults is not None or not hasattr(self, 'defaults'):
            self.defaults = defaults
        self.validator = validate.Validator()
        try:
            self.conf = configobj.ConfigObj(config, encoding='utf-8')
            self.mtime = os.path.getmtime(self.conffile)
        except configobj.ConfigObjError as exp:
            # We were unable to parse the config
            self.logger.critical('Unable to parse configuration')
            raise exp 
Example #4
Source File: controlpanel.py    From darkc0de-old-stuff with GNU General Public License v3.0 6 votes vote down vote up
def validate(self):
		
		for key in self.entrydict.keys():
			if key.find("Password") == -1:
				self.settings[self.section][key] = self.entrydict[key].get()
			else:
				self.settings[self.section][key] = myutils.password_obfuscate(self.entrydict[key].get())
		
		errortext="Some of your input contains errors. Detailed error output below.\n\n"
		
		val = Validator()
		valresult=self.settings.validate(val, preserve_errors=True)
		if valresult != True:
			if valresult.has_key(self.section):
				sectionval = valresult[self.section]
				for key in sectionval.keys():
					if sectionval[key] != True:
						errortext += "Error in item \"" + str(key) + "\": " + str(sectionval[key]) + "\n"
				tkMessageBox.showerror("Erroneous input. Please try again.", errortext)
			return 0
		else:
			return 1 
Example #5
Source File: config.py    From cli_helpers with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def read_default_config(self):
        """Read the default config file.

        :raises DefaultConfigValidationError: There was a validation error with
                                              the *default* file.
        """
        if self.validate:
            self.default_config = ConfigObj(configspec=self.default_file,
                                            list_values=False, _inspec=True,
                                            encoding='utf8')
            valid = self.default_config.validate(Validator(), copy=True,
                                                 preserve_errors=True)
            if valid is not True:
                for name, section in valid.items():
                    if section is True:
                        continue
                    for key, value in section.items():
                        if isinstance(value, ValidateError):
                            raise DefaultConfigValidationError(
                                'section [{}], key "{}": {}'.format(
                                    name, key, value))
        elif self.default_file:
            self.default_config, _ = self.read_config_file(self.default_file)

        self.update(self.default_config) 
Example #6
Source File: config.py    From cli_helpers with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def read_config_file(self, f):
        """Read a config file *f*.

        :param str f: The path to a file to read.
        """
        configspec = self.default_file if self.validate else None
        try:
            config = ConfigObj(infile=f, configspec=configspec,
                               interpolation=False, encoding='utf8')
        except ConfigObjError as e:
            logger.warning(
                'Unable to parse line {} of config file {}'.format(
                    e.line_number, f))
            config = e.config

        valid = True
        if self.validate:
            valid = config.validate(Validator(), preserve_errors=True,
                                    copy=True)
        if bool(config):
            self.config_filenames.append(config.filename)

        return config, valid 
Example #7
Source File: Config.py    From pyexperiment with MIT License 6 votes vote down vote up
def validate_config(config):
        """Validate configuration
        """
        validator = validate.Validator()
        result = config.validate(validator,
                                 copy=True,
                                 preserve_errors=True)

        if not isinstance(result, bool):
            raise ValueError("Configuration does not adhere"
                             " to the specification: %s" %
                             configobj.flatten_errors(config, result))
        elif not result:
            # This should never happen
            raise RuntimeError(  # pragma: no cover
                "Configuration validated to false.") 
Example #8
Source File: make_cfgobj_files.py    From drizzlepac with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def dict2cfgfile():
    for inst_det in param_dict.keys():

        cfg_filename = "{}/runhlaprocessing_{}.cfg".format(os.path.join(os.path.dirname(__file__), "pars"),inst_det.replace(" ","_").lower())
        configspec_filename ="{}/runhlaprocessing_config.spec".format(os.path.join(os.path.dirname(__file__),"pars"))
        print("FILENAME: ",cfg_filename)
        print("CONFIGSPEC: ",configspec_filename)
        config = ConfigObj(cfg_filename,configspec=configspec_filename)


        for section in param_dict[inst_det].keys():
            config[section] = {}
            for param in param_dict[inst_det][section].keys():
                print(inst_det, section,param,">>>>>> ",param_dict[inst_det][section][param])
                config[section][param] = param_dict[inst_det][section][param]
            print("\n")

        validator = Validator()
        result = config.validate(validator)
        print("RESULT: ",result)
        config.write()
        print("Wrote "+cfg_filename)
        return()

#============================================================================================================ 
Example #9
Source File: make_cfgobj_files.py    From drizzlepac with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def readcfgfile(inst_det):
    cfg_filename = "{}/runhlaprocessing_{}.cfg".format(os.path.join(os.path.dirname(__file__), "pars"),inst_det.replace(" ", "_").lower())
    configspec_filename = "{}/runhlaprocessing_config.spec".format(os.path.join(os.path.dirname(__file__),"pars"))
    print("Converting config file {} to parameter dictionary".format(cfg_filename))
    newconfig = ConfigObj(cfg_filename,configspec=configspec_filename)
    validator = Validator()
    newconfig.validate(validator)
    param_dict={}
    for section in newconfig.keys():
        param_dict[section]={}
        for parameter in newconfig[section].keys():
            value = newconfig[section][parameter]
            param_dict[section][parameter] = value


    for section in param_dict.keys():
        for parameter in param_dict[section].keys():
            value = param_dict[section][parameter]
            print(section, parameter, value, type(value))
    return(param_dict)


#============================================================================================================ 
Example #10
Source File: helpers.py    From panoptes with Apache License 2.0 5 votes vote down vote up
def parse_config_file(config_file, config_spec_file):

    assert validators.PanoptesValidators.valid_nonempty_string(config_file), u'config_file must be a non-empty str'
    assert validators.PanoptesValidators.valid_nonempty_string(config_spec_file), \
        u'config_spec_file must be a non empty str'

    try:
        config = ConfigObj(config_file, configspec=config_spec_file, interpolation=u'template', file_error=True)
    except IOError as e:
        raise PanoptesConfigurationParsingError(u'Error reading file: %s' % str(e))
    except ConfigObjError as e:
        raise PanoptesConfigurationParsingError(u'Error parsing config file "%s": %s' % (config_file, str(e)))

    validator = Validator()
    result = config.validate(validator, preserve_errors=True)

    if result is not True:
        errors = u''
        for (section_list, key, error) in flatten_errors(config, result):
            if key is None:
                errors += u'Section(s) ' + u','.join(section_list) + u' are missing\n'
            else:
                errors += u'The "' + key + u'" key in section "' + u','.join(section_list) + u'" failed validation\n'

        raise PanoptesConfigurationParsingError(u'Error parsing the configuration file: %s' % errors)

    return config 
Example #11
Source File: config_parser.py    From agentless-system-crawler with Apache License 2.0 5 votes vote down vote up
def parse_crawler_config(config_path='crawler.conf'):
    global _config

    # 1. get configs
    _config = ConfigObj(infile=misc.execution_path(config_path),
                        configspec=misc.execution_path(CONFIG_SPEC_PATH))

    # Configspec is not being used currently
    # but keeping validate() and apply_user_args() for future.
    # Essentially NOP right now

    # 2. apply defaults
    vdt = Validator()
    _config.validate(vdt) 
Example #12
Source File: processor.py    From aws-syndicate with Apache License 2.0 5 votes vote down vote up
def _validate_ini(self):
        # building a validator
        validator = Validator({
            'region_func': _region,
            'account_func': _account,
            'project_func': _project_mapping
        })
        # validate
        param_valid_dict = self._config_dict.validate(validator=validator)
        if not param_valid_dict:
            raise Exception(f'Error while parsing {self._config_path}')
        # check non-required parameters
        prefix_value = self._config_dict.get(RESOURCES_PREFIX_CFG)
        if prefix_value:
            if len(prefix_value) > 5:
                if not isinstance(param_valid_dict, dict):
                    param_valid_dict = {RESOURCES_PREFIX_CFG: False}
                else:
                    param_valid_dict[RESOURCES_PREFIX_CFG] = False

        suffix_value = self._config_dict.get(RESOURCES_SUFFIX_CFG)
        if suffix_value:
            if len(suffix_value) > 5:
                if not isinstance(param_valid_dict, dict):
                    param_valid_dict = {RESOURCES_SUFFIX_CFG: False}
                else:
                    param_valid_dict[RESOURCES_SUFFIX_CFG] = False

        # processing results
        if isinstance(param_valid_dict, dict):
            messages = ''
            for key, value in param_valid_dict.items():
                if not value:
                    messages += '\n{0} {1}'.format(key,
                                                   ERROR_MESSAGE_MAPPING[key])

            if messages:
                raise Exception('Configuration is invalid. ' + messages) 
Example #13
Source File: config.py    From ComicStreamer with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        super(ComicStreamerConfig, self).__init__()
               
        self.csfolder = AppFolders.settings()

        # make sure folder exisits
        if not os.path.exists( self.csfolder ):
            os.makedirs( self.csfolder )

        # set up initial values
        self.filename = os.path.join(self.csfolder, "settings")
        self.configspec=io.StringIO(ComicStreamerConfig.configspec)
        self.encoding="UTF8"
        
        # since some stuff in the configobj has to happen during object initialization,
        # use a temporary delegate,  and them merge it into self
        tmp = ConfigObj(self.filename, configspec=self.configspec, encoding=self.encoding)
        validator = Validator()
        tmp.validate(validator,  copy=True)
       
        # set up the install ID
        if tmp['general']['install_id'] == '':
            tmp['general']['install_id'] = uuid.uuid4().hex
            
        #set up the cookie secret
        if tmp['security']['cookie_secret'] == '':
            tmp['security']['cookie_secret'] = base64.b64encode(uuid.uuid4().bytes + uuid.uuid4().bytes)

        # normalize the folder list
        tmp['general']['folder_list'] = [os.path.abspath(os.path.normpath(unicode(a))) for a in tmp['general']['folder_list']]

        self.merge(tmp)
        if not os.path.exists( self.filename ):
            self.write()
            
        # not sure if this belongs here:
        # if mac app, and no unrar in path, add the one from the app bundle
        if getattr(sys, 'frozen', None) and  platform.system() == "Darwin":
            if which("unrar") is None:
                addtopath(AppFolders.appBase()) 
Example #14
Source File: main.py    From deep_motion_mag with MIT License 4 votes vote down vote up
def main(args):
    configspec = ConfigObj(args.config_spec, raise_errors=True)
    config = ConfigObj(args.config_file,
                       configspec=configspec,
                       raise_errors=True,
                       file_error=True)
    # Validate to get all the default values.
    config.validate(Validator())
    if not os.path.exists(config['exp_dir']):
        # checkpoint directory.
        os.makedirs(os.path.join(config['exp_dir'], 'checkpoint'))
        # Tensorboard logs directory.
        os.makedirs(os.path.join(config['exp_dir'], 'logs'))
        # default output directory for this experiment.
        os.makedirs(os.path.join(config['exp_dir'], 'sample'))
    network_type = config['architecture']['network_arch']
    exp_name = config['exp_name']
    setproctitle.setproctitle('{}_{}_{}' \
                              .format(args.phase, network_type, exp_name))
    tfconfig = tf.ConfigProto(allow_soft_placement=True,
                              log_device_placement=False)
    tfconfig.gpu_options.allow_growth = True

    with tf.Session(config=tfconfig) as sess:
        model = MagNet3Frames(sess, exp_name, config['architecture'])
        checkpoint = config['training']['checkpoint_dir']
        if args.phase == 'train':
            train_config = config['training']
            if not os.path.exists(train_config['checkpoint_dir']):
                os.makedirs(train_config['checkpoint_dir'])
            model.train(train_config)
        elif args.phase == 'run':
            model.run(checkpoint,
                      args.vid_dir,
                      args.frame_ext,
                      args.out_dir,
                      args.amplification_factor,
                      args.velocity_mag)
        elif args.phase == 'run_temporal':
            model.run_temporal(checkpoint,
                               args.vid_dir,
                               args.frame_ext,
                               args.out_dir,
                               args.amplification_factor,
                               args.fl,
                               args.fh,
                               args.fs,
                               args.n_filter_tap,
                               args.filter_type)
        else:
            raise ValueError('Invalid phase argument. '
                             'Expected ["train", "run", "run_temporal"], '
                             'got ' + args.phase)