Esempio n. 1
0
def gmpe_list(value, min):
    """
    Checks to see if value is a list of strings at least min elements long.
    The entries are not checked for their validity as GMPEs. Raises a
    ValidateError exception on failure.

    Args:
        value (str): A string representing a list of GMPEs.

    Returns:
        list: The input string converted to a list of GMPEs.

    """

    if value == 'None' or value == '[]':
        value = []
    if isinstance(value, str):
        value = [value]
    if not isinstance(value, list) or len(value) < int(min):
        print("'%s' is not a list of at least %s gmpes" % (value, min))
        raise ValidateError()
    for gmpe in value:
        if not isinstance(gmpe, str):
            print("'%s' is not a list of strings" % (value))
            raise ValidateError()

    return value
Esempio n. 2
0
def annotatedfloat_type(value):
    """
    Checks to see if value is a float, or a float with a 'c', 'm', or 'd'
    appended. Then converts the value to decimal degrees where an unadorned
    float or a float plus 'd' is interpreted as decimal degrees, 'm' is
    interpreted as arc-minutes, and 'c' is interpreted as arc-seconds.
    Raises a ValidateError exception on failure.

    Args:
        value (str): A string representing a float or a float appended
            with 'd', 'm', or 'c' (for degrees, minutes, seconds).

    Returns:
        float: The input value converted to decimal degrees.

    """
    try:
        out = float(value)
    except ValueError:
        try:
            if value.endswith('c'):
                out = float(value.replace('c', '')) / 3600.0
            elif value.endswith('m'):
                out = float(value.replace('m', '')) / 60.0
            elif value.endswith('d'):
                out = float(value.replace('d', ''))
            else:
                raise ValidateError(value)
        except Exception:
            raise ValidateError(value)
    return out
Esempio n. 3
0
def nanfloat_list(value, min):
    """
    Checks to see if value is a list of floats, including NaN and Inf.
    Raises a ValidateError exception on failure.

    Args:
        value (str): A string representing a list of floats.

    Returns:
        list: The input string converted to a list of floats.

    """
    min = int(min)
    if isinstance(value, str) and (value == 'None' or value == '[]'):
        value = []
    if isinstance(value, str):
        value = [value]
    if isinstance(value, list) and not value:
        value = []
    if not isinstance(value, list):
        logging.error("'%s' is not a list" % value)
        raise ValidateError()
    if len(value) < min:
        logging.error("extent list must contain %i entries" % min)
        raise ValidateError()
    try:
        out = [float(a) for a in value]
    except ValueError:
        logging.error("%s is not a list of %i floats" % (value, min))
        raise ValidateError()
    return out
Esempio n. 4
0
def check_config(config, logger):
    """
    Checks that the gmpe, gmice, ipe, and ccf parameters
    in config are defined in their respective sections. Raises a
    ValidateError exception if an error is encountered.

    Args:
        config (ConfigObj): A ConfigObj instance.
        logger (logger): The logger to which to write complaints.

    Returns:
        Nothing: Nothing.

    """
    if config['modeling']['gmpe'] not in config['gmpe_sets']:
        logger.error('Configuration error: gmpe %s not in gmpe_sets' %
                     (config['modeling']['gmpe']))
        raise ValidateError()
    if config['modeling']['gmice'] not in config['gmice_modules']:
        logger.error('Configuration error: gmice %s not in gmice_modules' %
                     (config['modeling']['gmice']))
        raise ValidateError()
    if config['modeling']['ipe'] not in config['ipe_modules']:
        logger.error('Configuration error: ipe %s not in ipe_modules' %
                     (config['modeling']['ipe']))
        raise ValidateError()
    if config['modeling']['ccf'] not in config['ccf_modules']:
        logger.error('Configuration error: ccf %s not in ccf_modules' %
                     (config['modeling']['ccf']))
        raise ValidateError()
Esempio n. 5
0
def cfg_float_list(value):
    """
    Converts (if possible) the input list (or string) to a list
    of floats. Raises ValidateError if the input can't be
    converted to a list of floats.

    Args:
        value (str or list): A string or list of strings to be
            converted to a list of floats.

    Returns:
        list: The input converted to a list of floats.

    Raises:
        ValidateError
    """
    if not value or value == 'None':
        print("'%s' is not a list of at least 1 float" % (value))
        raise ValidateError()
    if isinstance(value, str):
        value = [value]
    if not isinstance(value, list) or len(value) < 1:
        print("'%s' is not a list of at least 1 float" % (value))
        raise ValidateError()
    fvalue = []
    for val in value:
        try:
            fval = float(val)
        except ValueError:
            print("'%s' is not a list of floats" % (value))
            raise ValidateError()
        fvalue.append(fval)
    return fvalue
Esempio n. 6
0
def command_check(vals):
    """ Validates the commands in the config file """
    #print "Running command check"
    #print vals
    try:
        cmdint = int(vals[0])
    except IndexError:
        raise ValidateError("Command should be list separated "
                            "by comma first value should be integer")

    except ValueError:
        raise ValidateError("Command should be integer")

    try:
        fmtchropts = "xcbB?hHiIlLqQfdspP"
        if not str(vals[1]) in fmtchropts:
            raise ValidateError("Expected format character "
                                "value: %s" % fmtchropts)
        fmtchr = str(vals[1])
    except IndexError:
        raise ValidateError("Command should be list separated "
                            "by comma, second value should be character")
    #print vals

    return cmdint, fmtchr
Esempio n. 7
0
def cfg_bool(value):
    """
    Converts (if possible) the input string to a bool. Raises
    ValidateError if the input can't be converted to a bool.

    Args:
        value (str): A string to be converted to a bool.

    Returns:
        bool: The input converted to a bool.

    Raises:
        ValidateError
    """
    if not isinstance(value, (str, bool)) or not value or value == 'None':
        logging.error("'%s' is not a bool" % (value))
        raise ValidateError()
    try:
        if value.lower() in ['true', 't', 'yes', 'y', '1']:
            bval = True
        else:
            bval = False
    except ValueError:
        logging.error("'%s' is not a bool" % (value))
        raise ValidateError()
    return bval
Esempio n. 8
0
File: utils.py Progetto: pvbl/fpeam
    def __init__(self, value):
        """
        >>> raise VdtPathDoesNotExist('/not/a/path')
        Traceback (most recent call last):
        VdtValueTooSmallError: the path "/not/a/path" does not exist
        """

        ValidateError.__init__(self,
                               'the path "%s" does not exist' % (value, ))
Esempio n. 9
0
def email_check(value):
    if isinstance(value, list):
        raise ValidateError(
            'A list was passed when an email address was expected')
    if email_re.match(value) is None:
        print '"%s" is not an email address' % value
        raise ValidateError('"%s" is not an email address' % value)

    return value
Esempio n. 10
0
File: utils.py Progetto: pvbl/fpeam
    def __init__(self, value, max_length):
        """
        >>> raise VdtPathTooLong('/path/too/long')
        Traceback (most recent call last):
        VdtValueTooSmallError: the path "/path/too/long" exceeds the maximum length of <length> characters
        """

        ValidateError.__init__(
            self, 'the path "%s" exceeds the maximum length of %s characters' %
            (value, max_length))
Esempio n. 11
0
def url_filter_check(value, schemes=('http', 'https'), non_local=True):
    expanded = urlparse(value)

    if expanded.scheme not in schemes:
        raise ValidateError(
            'url not one of the allowed schemes: {0}'.format(schemes))

    if non_local and not expanded.netloc:
        raise ValidateError('url must specify a remote server')

    return value
Esempio n. 12
0
def idt_read_filter_check(value):
    value = ast.literal_eval(value)
    if not isinstance(value, dict):
        raise ValidateError('read_filter must be a dict')

    keys = frozenset(value.keys())
    if not keys.issubset(IDONETHIS_FILTERS):
        raise ValidateError('invalid filter types specified: {0}'.format(
            keys.difference(IDONETHIS_FILTERS)))

    return value
Esempio n. 13
0
def list3ints_check(vals):
    """ Validates that we recieve a list of 3 ints """
    # print "Running list of 3 ints validation"
    if len(vals) != 3:
        raise ValidateError("Expecting a list of 3 integers. "
                            "Did not receive 3 integers.")

    try:
        idxs = [int(val) for val in vals]
    except ValueError:
        raise ValidateError("List should be of integers!")

    return idxs
 def webhook_header_check(value, *args, **kwargs):
     token = value.split('Token ')[-1]
     if not token:
         return None
     url = 'https://app.parkpow.com/api/v1/parking-list'
     headers = {'Authorization': f'Token {token}'}
     try:
         response = requests.get(url, headers=headers, timeout=10)
     except (requests.Timeout, requests.ConnectionError):
         raise ValidateError('Please check your internet connection.')
     if response.status_code != 200:
         raise ValidateError('Wrong token.')
     return value
Esempio n. 15
0
def mechType(value):
    """
    Checks that the value is in the mechType enum and returns
    the appropriate enum.
    """
    if not isinstance(value, str):
        print('Config error: Unknown value "%s" for "mech"' % (value))
        raise ValidateError()
    for thing in Mechanism:
        if value == thing.value:
            return thing
    print('Config error: Invalid value "%s" for "mech"' % (value))
    print('Config error: "mech" must be in %s' %
          ([x.value for x in list(Mechanism)]))
    raise ValidateError()
Esempio n. 16
0
def magScalingType(value):
    """
    Checks that the value is in the magScalingType enum and returns
    the appropriate enum.
    """
    if not isinstance(value, str):
        print('Config error: Unknown value "%s" for "rup_dim_model"' % (value))
        raise ValidateError()
    for thing in MagScaling:
        if value == thing.value:
            return thing
    print('Config error: Invalid value "%s" for "rup_dim_model"' % (value))
    print('Config error: "rup_dim_model" must be in %s' %
          ([x.value for x in list(MagScaling)]))
    raise ValidateError()
def send_request(section):
    if not section.get('webhook_target') or not section.get('webhook_header'):
        return None
    if '/api/v1/webhook-receiver' not in section['webhook_target']:
        return None
    headers = {
        'Authorization':
        'Token %s' % section['webhook_header'].split('Token ')[-1]
    }
    url = section['webhook_target'].replace('webhook-receiver', 'parking-list')
    try:
        response = requests.get(url, headers=headers, timeout=10)
    except (requests.Timeout, requests.ConnectionError):
        raise ValidateError('Please check your internet connection.')
    if response.status_code != 200:
        raise ValidateError('Wrong token.')
Esempio n. 18
0
def email(value: str):
    """validator for emails"""
    if len(value) > 7:
        if re.match("^.+@([?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$",
                    value) is not None:
            return value
    raise ValidateError(f'"{value}" is not an email address')
Esempio n. 19
0
def is_directory(value, *args, exists=True, allow_empty=False, **kwargs):
    """
    Checks whether the value is a valid directory path (and optionally whether
    it exists).

    Parameters
    ----------
    value : object
        The value to validate
    exists : bool
        whether the directory is required to exist to pass the validation check
    allow_empty : bool
        whether empty strings are allowed

    Returns
    -------
    IntEnum
        the int enum instance if successfully validated or None

    Raises
    ------
    ValidateError
        if the the value can't be validated
    """
    if allow_empty and value == '':
        return ''
    else:
        _exists(value, exists)
        if not os.path.isdir(value):
            raise ValidateError('"%s" is not a directory.' % value)
        return value
Esempio n. 20
0
def attr_triple(value):
    """
    Check that interprets the value as `urwid.AttrSpec` triple for the colour
    modes 1,16 and 256.  It assumes a <6 tuple of attribute strings for
    mono foreground, mono background, 16c fg, 16c bg, 256 fg and 256 bg
    respectively. If any of these are missing, we downgrade to the next
    lower available pair, defaulting to 'default'.

    :raises: VdtValueTooLongError, VdtTypeError
    :rtype: triple of `urwid.AttrSpec`
    """
    keys = ['dfg', 'dbg', '1fg', '1bg', '16fg', '16bg', '256fg', '256bg']
    acc = {}
    if not isinstance(value, (list, tuple)):
        value = value,
    if len(value) > 6:
        raise VdtValueTooLongError(value)
    # ensure we have exactly 6 attribute strings
    attrstrings = (value + (6 - len(value)) * [None])[:6]
    # add fallbacks for the empty list
    attrstrings = (2 * ['default']) + attrstrings
    for i, value in enumerate(attrstrings):
        if value:
            acc[keys[i]] = value
        else:
            acc[keys[i]] = acc[keys[i - 2]]
    try:
        mono = AttrSpec(acc['1fg'], acc['1bg'], 1)
        normal = AttrSpec(acc['16fg'], acc['16bg'], 16)
        high = AttrSpec(acc['256fg'], acc['256bg'], 256)
    except AttrSpecError as e:
        raise ValidateError(str(e))
    return mono, normal, high
Esempio n. 21
0
def validate_config(mydict, install_path):
    """Recursively validate select.conf.

    Args:
        mydict (dict): Full or partial config dictionary.
        install_path (str):

    """
    for key in mydict:
        if isinstance(mydict[key], dict):
            validate_config(mydict[key], install_path)
            continue
        if key == 'horizontal_buffer' or key == 'vertical_buffer':
            mydict[key] = config.cfg_float(mydict[key])
        elif key == 'gmpe':
            mydict[key] = config.gmpe_list(mydict[key], 1)
        elif key == 'min_depth' or key == 'max_depth':
            mydict[key] = config.cfg_float_list(mydict[key])
        elif key == 'layer_dir':
            mydict[key] = path_macro_sub(mydict[key], ip=install_path)
        elif key in ('x1', 'x2', 'p1', 'p2', 'p_kagan_default',
                     'default_slab_depth'):
            mydict[key] = float(mydict[key])
        else:
            raise ValidateError('Invalid entry in config: "%s"' % (key))
    return
def send_request(section):
    if not section.get('webhook_target') or not section.get('webhook_header'):
        return
    if '/api/v1/webhook-receiver' not in section['webhook_target']:
        return
    headers = {
        'Authorization':
        'Token %s' % section['webhook_header'].split('Token ')[-1]
    }
    url = section['webhook_target'].replace('webhook-receiver', 'parking-list')
    try:
        response = requests.get(url, headers=headers, timeout=10)
    except (requests.Timeout, requests.ConnectionError):
        raise ValidateError('Connection to webhook_target %s failed.' % url)
    if response.status_code != 200:
        raise ValidateError('The token in webhook_header is invalid.')
Esempio n. 23
0
    def get_string_dict(self, plugin_info):
        """
        Convert a section with information of priorities to a string dict.
        
        To avoid typos we make all letters lower case when comparing
        
        Arguments:
            plugin_info (dict): A dictionary with plugin information
        
        Return:
            string_dict (dict): A dictionary with strings as keys and integer
                                that specifies their priorities as values
        """
        string_info = []
        string_dict = {}

        for key in plugin_info:
            try:
                string_info.append(dict(plugin_info[key]))
            except ValueError:
                pass

        string_rules = {}

        for raw_info in string_info:
            try:
                string = raw_info['string']
            except KeyError:
                raise ValidateError(
                    "String information has to have a 'string'")
            try:
                priority = raw_info['priority']
            except KeyError:
                raise ValidateError(
                    "String information has to have a 'priority'")
            try:
                priority = int(priority)
            except ValueError:
                raise ValidateError("'priority' has to be an integer")

            string_dict[string] = priority

        if len(string_dict) == 0:
            raise ValidateError(
                "'string' entrys must have string rules defined")

        return string_dict
Esempio n. 24
0
def gpg_key(value):
    """
    test if value points to a known gpg key
    and return that key as a gpg key object.
    """
    try:
        return crypto.get_key(value)
    except GPGProblem as e:
        raise ValidateError(str(e))
Esempio n. 25
0
def gpg_key(value):
    """
    test if value points to a known gpg key
    and return that key as :class:`pyme.pygpgme._gpgme_key`.
    """
    try:
        return crypto.get_key(value)
    except GPGProblem, e:
        raise ValidateError(e.message)
Esempio n. 26
0
    def _is_dict(self, value, min=None, max=None):
        """
        Validation function for dict values.

        Reference: https://github.com/DiffSK/configobj/blob/master/src/configobj/validate.py
        """
        # Get boundary values
        (min_val, max_val) = _is_num_param(('min', 'max'), (min, max))

        # Check if value is string
        if isinstance(value, str):
            # Convert string value
            try:
                value = ast.literal_eval(value)
            except ValueError:
                if value == 'dict()' or value == 'dict':
                    value = dict()
                else:
                    raise ValidateError(
                        'A value "{}" could not be converted to a dict!'.
                        format(value))
            except Exception as e:
                raise e

            # Validate whether the value is a dict
            if not isinstance(value, dict):
                raise ValidateError(
                    'A value of type {} was passed when a dict was expected!'.
                    format(type(value)))

        elif isinstance(value, dict):
            pass

        else:
            raise VdtTypeError(value)

        # Validate whether the value is within the boundaries
        if (min_val is not None) and (len(value) < min_val):
            raise VdtValueTooShortError(value)

        if (max_val is not None) and (len(value) > max_val):
            raise VdtValueTooLongError(value)

        return value
Esempio n. 27
0
    def __call__(self, value):
        """
        Parameters
        ----------
        value : object
            The value to validate

        Returns
        -------
        IntEnum
            the int enum instance if successfully validated or None

        Raises
        ------
        ValidateError
            if the the value can't be validated
        """
        if isinstance(value, str):
            try:
                value = int(value)
            except ValueError:
                # If the value was stored, it will be stored as
                # MyEnum.attribute
                #
                # This will get rid of the class portion for casting if present
                if value.startswith(self._enum.__name__ + '.'):
                    value = value[len(self._enum.__name__) + 1:]
                try:
                    value = getattr(self._enum, value)
                except AttributeError:
                    raise ValidateError(
                        'The value is neither an integer or a valid %s ' \
                        'attribute' % self._enum.__name__
                    )
            else:
                value = self._get_enum_from_val(value)
        elif isinstance(value, int):
            value = self._get_enum_from_val(value)
        elif value is None:
            return self._default
        else:
            raise ValidateError('Invalid type')

        return value
Esempio n. 28
0
def list_check(value):
    if not isinstance(value, list):
        if value.lower() == 'none':
            return ['NONE','NONE']
        if isinstance(value, str):
            return [value,'NONE']
        else:
            raise ValidateError('"%s" is not a list' % value)

    return value
Esempio n. 29
0
 def _output_vdterror(self, error_key):
     for (section_list, key, _) in self.vdt_errors:
         if key is None:
             pass
             #print 'The following sections "%s" is(are) missing in the %s configuration' % ('.'.join(section_list), self.cfgtype)
         else:
             msg = mcode.ERR_011 % (key, ','.join(section_list))
             if key in error_key:
                 raise ValidateError(msg)
             else:
                 print '[Warning]' + msg
Esempio n. 30
0
    def version_check(self):
        """
        Check if the version entry is in the proper format
        """
        try:
            version_info = self['Version']
        except KeyError:
            raise ValidateError('Config file has to have a Version section')
        try:
            float(version_info['version'])
        except KeyError:
            raise ValidateError('Config file has to have a version section')
        except ValueError:
            raise ValidateError('Version has to be a float.')

        try:
            version_info['name']
        except KeyError:
            raise ValidateError("Config file has to have a name")
        return
Esempio n. 31
0
 def __init__(self, value, reason):
     ValidateError.__init__(self, "the value '%s' is unacceptable.\n"
             "Reason: %s" % (value, reason))