def pathToCephValidator(value): """Check if value conforms to path to Ceph.""" # check if the value has the expected type stringTypeValidator(value) # construct regexp for path PREFIX = r"archives/compressed/" SELECTOR = r"[0-9a-f]{2}/" UUID = r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/" DATETIME = r"[0-9]{6}/[0-9]{2}/" TARBALL = r"[0-9]+\.tar\.gz" PATH_RE = re.compile(PREFIX + SELECTOR + UUID + DATETIME + TARBALL) if not PATH_RE.fullmatch(value): raise Invalid("wrong path value '{}'".format(value))
def register(self, **kwargs): form_data = cherrypy.request.body.params; # Block creation of any extra users beyond the # first one via this route. if admin.AdminModel().userExists() == True: raise cherrypy.HTTPRedirect("/admin") validation = Schema({ Required('username'): All(str, Length(min=4)), Required('password'): All(str, Length(min=8)) }) try: validation(form_data) if (" " in form_data["username"]) == True: raise Invalid("Username must not contain any spaces.") if not admin.AdminModel().addUser(form_data["username"], form_data["password"]): raise Invalid("Unable to add the user to the database.") except Exception as err: return self.render("setup.html", error=str(err)) # Setup was complete successfully redirect to admin raise cherrypy.HTTPRedirect("/admin")
def f(v): # if the value is already the correct type then just return it if typ and isinstance(v, typ): return v elif converter_function: # convert with the custom function if one is provided if converter_function: try: converted_value = converter_function(v) if return_original: return v return converted_value except Exception as e: raise Invalid(msg) elif typ: # convert by type-casting try: converted_value = typ(v) if return_original: return v return converted_value except ValueError as e: raise Invalid(msg)
def f(value): if value in [None, ''] and empty_to_none: return None if isinstance(value, datetime.date): return value try: casted_value = datetime.datetime.strptime(value, format) except ValueError: raise Invalid(msg or 'Given value cannot be casted to a date.') if cast: return casted_value.date() return value
def check_removal_version(v, version_field, collection_name_field, error_code='invalid-removal-version'): version = v.get(version_field) collection_name = v.get(collection_name_field) if not isinstance(version, string_types) or not isinstance( collection_name, string_types): # If they are not strings, schema validation will have already complained. return v if collection_name == 'ansible.builtin': try: parsed_version = StrictVersion() parsed_version.parse(version) except ValueError as exc: raise _add_ansible_error_code( Invalid('%s (%r) is not a valid ansible-base version: %s' % (version_field, version, exc)), error_code=error_code) return v try: parsed_version = SemanticVersion() parsed_version.parse(version) if parsed_version.major != 0 and (parsed_version.minor != 0 or parsed_version.patch != 0): raise _add_ansible_error_code(Invalid( '%s (%r) must be a major release, not a minor or patch release (see specification at ' 'https://semver.org/)' % (version_field, version)), error_code= 'removal-version-must-be-major') except ValueError as exc: raise _add_ansible_error_code(Invalid( '%s (%r) is not a valid collection version (see specification at https://semver.org/): ' '%s' % (version_field, version, exc)), error_code=error_code) return v
def validator(loc: str) -> Station: loc = loc.upper().split(",") if len(loc) == 1: icao = loc[0] try: return Station.from_icao(icao) except BadStation as exc: # if icao in ICAO_WHITELIST: # return Station(*([None] * 4), "DNE", icao, *([None] * 9)) raise Invalid( f"{icao} is not a valid ICAO station ident") from exc elif len(loc) == 2: try: lat, lon = Latitude(loc[0]), Longitude(loc[1]) if coerce_station: return Station.nearest(lat, lon, is_airport=airport, sends_reports=reporting)[0] return lat, lon except Exception as exc: raise Invalid(f"{loc} is not a valid coordinate pair") from exc else: raise Invalid(f"{loc} is not a valid station/coordinate pair")
def ip_address(ip): """ Verifies that the given IP address is valid. :param ip: An IP address :type ip: str :return: The given value :rtype: str :raises: voluptuous.Invalid """ if isinstance(ipaddr.ip_address(ip), (ipaddr.IPv4Address, ipaddr.IPv6Address)): return ip else: raise Invalid('Invalid IP address')
def author(value): if value is None: return value # let schema checks handle if not is_iterable(value): value = [value] for line in value: if not isinstance(line, string_types): continue # let schema checks handle m = author_line.search(line) if not m: raise Invalid("Invalid author") return value
def expiration_date(date_): """ Verifies that the given object represents a date and that the date has not passed :param date_: A datetime object representing an expiration date :type date_: datetime.datetime :return: Correctly formatted expiration date :rtype: str :raises: voluptuous.Invalid """ date_format = date("%m%y") if date_format(date_) and datetime.today() < date_: return date_format(date_) else: raise Invalid('Invalid expiration date')
def MqttTopicValidator(v, msg=''): if isinstance(v, str): return [(v, 0)] ret = [] for i, val in enumerate(v): qos = 0 if i < len(v) - 1: qos = v[i + 1] if not isinstance(val, str) and not isinstance(val, int): raise Invalid(msg or "Topics must consist of int and string!") if not isinstance(val, str): continue if isinstance(qos, int): if qos not in [0, 1, 2]: raise Invalid(msg or (f"QoS must be 0, 1 or 2")) else: qos = None ret.append((val, qos)) return ret
def converter(value): if value is None: return value import urlparse split_url = list(urlparse.urlsplit(value)) if full and add_prefix and not split_url[0] and not split_url[1] and split_url[2] \ and not split_url[2].startswith(u'/'): split_url = list(urlparse.urlsplit(add_prefix + value)) scheme = split_url[0] if scheme != scheme.lower(): split_url[0] = scheme = scheme.lower() if full and not scheme: raise Invalid('URL must be complete') if scheme and schemes is not None and scheme not in schemes: raise Invalid('Scheme must belong to {0}').format(sorted(schemes)) network_location = split_url[1] if network_location != network_location.lower(): split_url[1] = network_location = network_location.lower() if scheme in ('http', 'https') and not split_url[2]: # By convention a full HTTP URL must always have at least a "/" in its path. split_url[2] = '/' if remove_fragment and split_url[4]: split_url[4] = '' return unicode(urlparse.urlunsplit(split_url))
def datetime_validator(value): """Validate and sanitize a datetime string in ISO format. Args: value: The value to validate Returns: unicode: ISO-formatted datetime string Raises: Invalid """ try: # The dateutil parser defaults empty values to the current day, # which is NOT what we want. if value is None or value == '': raise Invalid(u"Datetime value cannot be \"{val}\"".format(val=value)) # Parse the date and interpret it as UTC value = dateutil.parser.parse(value).replace(tzinfo=utc) return str(value.isoformat()) except (ValueError, TypeError): raise Invalid(u"Could not parse datetime from value \"{val}\"".format(val=value))
def country_code(code): """ Verifies that the given two-letter country code is valid. :param code: A two-letter country code :type code: str :return: The passed value :rtype: str :raises: voluptuous.Invalid """ try: pycountry.countries.get(alpha2=code) except KeyError: raise Invalid('Invalid country code') else: return code
def decimal(number, decimal_re=DECIMAL_RE): """ Verifies that the given number is a valid decimal :param number: A number to check :type number: str or decimal.Decimal :param decimal_re: CONSTANT :type decimal_re: _sre.SRE_Pattern :return: The given value :rtype: str :raises: voluptuous.Invalid """ if re.match(decimal_re, str(number)): return u(str(number)) else: raise Invalid("Not a decimal number")
def edgetpu_device_validator(device): """Check for valid EdgeTPU device name. Valid values are: ":<N>" : Use N-th Edge TPU "usb" : Use any USB Edge TPU "usb:<N>" : Use N-th USB Edge TPU "pci" : Use any PCIe Edge TPU "pci:<N>" : Use N-th PCIe Edge TPU "cpu" : Run on the CPU """ for regex in DEVICE_REGEXES: if regex.match(device): return device raise Invalid( f"EdgeTPU device {device} is invalid. Please check your configuration")
def execute(self): configurator = get_configurator(self.params.config, storage_path=self.params.storage, include_config_dirs=True) cluster_name = self.params.cluster try: cluster = configurator.load_cluster(cluster_name) cluster.update() except (ClusterNotFound, ConfigurationError) as ex: log.error("Setting up cluster %s: %s\n" % (cluster_name, ex)) return if self.params.ssh_to: try: nodes = dict((n.name, n) for n in cluster.get_all_nodes()) frontend = nodes[self.params.ssh_to] except KeyError: raise Invalid("Hostname %s not found in cluster %s" % (self.params.ssh_to, cluster_name)) else: frontend = cluster.get_frontend_node() try: # ensure we can connect to the host if not frontend.preferred_ip: # Ensure we can connect to the node, and save the value of `preferred_ip` ssh = frontend.connect(keyfile=cluster.known_hosts_file) if ssh: ssh.close() cluster.repository.save_or_update(cluster) except NodeNotFound as ex: log.error("Unable to connect to the frontend node: %s" % str(ex)) sys.exit(1) host = frontend.connection_ip() username = frontend.image_user knownhostsfile = cluster.known_hosts_file if cluster.known_hosts_file \ else '/dev/null' ssh_cmdline = [ "ssh", "-i", frontend.user_key_private, "-o", "UserKnownHostsFile=%s" % knownhostsfile, "-o", "StrictHostKeyChecking=yes", '%s@%s' % (username, host) ] ssh_cmdline.extend(self.params.ssh_args) log.debug("Running command `%s`" % str.join(' ', ssh_cmdline)) os.execlp("ssh", *ssh_cmdline)
def validator(config): """Warn if key is present. Replace it if a value is given.""" if key in config: if replacement: print(f"Config option {key} is deprecated. " f"Replace it with {replacement}. " "In the future this will produce an error") value = config[key] config.pop(key) if replacement not in config: config[replacement] = value return config return config raise Invalid(f"Config option {key} is deprecated. " "Please remove it from your configuration") return config
def supported_cache_type(types): """Checks if link type config option consists only of valid values. Args: types (list/string): type(s) of links that dvc should try out. """ if types is None: return None if isinstance(types, str): types = [typ.strip() for typ in types.split(",")] unsupported = set(types) - {"reflink", "hardlink", "symlink", "copy"} if unsupported: raise Invalid("Unsupported cache type(s): {}".format( ", ".join(unsupported))) return types
def BoolGET(val): """ Takes a string value and returns a boolean based on the input, since GET requests always come as strings. '1' and 'true' return True, while '0' and 'false' return False. Any other input raises an Invalid exception. :param val: The value to evaluate. """ if isinstance(val, bool): return val elif isinstance(val, str): if val == '1' or val.lower() == 'true': return True elif val == '0' or val.lower() == 'false': return False raise Invalid( 'boolean must be "1", "true", "0", or "false" (case insensitive)')
def PermissionsListOfUser(perm_list: List[str]) -> List[str]: """ Takes a list of items and asserts that all of them are in the permissions list of a user. :param perm_list: A list of permissions encoded as ``str`` :return: The input perm_list :raises Invalid: If the user does not have a permission in the list """ if isinstance(perm_list, list): for perm in perm_list: if not flask.g.user.has_permission(perm): break else: return perm_list raise Invalid('permissions must be in the user\'s permissions list')
async def _get_request_data(): """ Helper function for ``validate_data``. Depending on the request method, it decides whether or not to process the query_string or form. If the form data is not valid JSON, an Invalid exception will be raised. :return: The request data (empty dict if no data present). :rtype: dict :raises: voluptuous.Invalid """ if quart.request.method == "GET": return quart.request.args.to_dict() try: data = await quart.request.get_data() return json.loads(data) if data else {} except ValueError: # JSONDecodeError subclasses ValueError. raise Invalid("Unable to parse data.")
def accepted_payment_type(credit_card_type): """ Verifies that the given payment type is supported by Lime Light :param credit_card_type: The type of credit card :type credit_card_type: str :return: The given values :rtype: str :raises: voluptuous.Invalid """ if credit_card_type.lower() in { 'amex', 'visa', 'master', 'discover', 'checking', 'offline', 'solo', 'maestro', 'switch', 'boleto', 'paypal', 'diners', 'hipercard', 'aura', 'eft_germany', 'giro' }: return credit_card_type.lower() else: raise Invalid("Invalid payment type")
def fn(value): try: if isinstance(value, basestring): if ',' in value: value = list( map( int, filter( bool, list(map(lambda x: x.strip(), value.split(',')))))) return value else: return [int(value)] else: raise ValueError except ValueError: raise Invalid('<{0}> is not a valid csv of integers'.format(value))
def validate_author(data): warnings = [] if "author" not in data and "authors" not in data: raise Invalid("need either author or authors to be defined") elif "author" in data and "authors" in data: message = ( "data['author'] and data['authors'] are both defined, " "data['author'] should be removed" ) warnings.append(message) elif "author" in data and ("," in data["author"] or "&" in data["author"]): message = ( "data['author'] appears to contain multiple entries, " "move to data['authors']" ) warnings.append(message) return warnings
def utf8_validator(value): """Validate and sanitize unicode strings. If we're given a bytestring, assume that the encoding is UTF-8 Args: value: The value to validate Returns: unicode Raises: Invalid """ try: if isinstance(value, bytes): return value.decode('utf-8') return six.text_type(value) except (ValueError, TypeError): raise Invalid(u"Could not load unicode from value \"{val}\"".format(val=value))
def v(value): """ Trys to validate the value with the given callbacks. Args: value: the item to validate Raises: APIException with the given error code and msg. Returns: The value if the validation callbacks are satisfied. """ for msg, callbacks in callback_tuples: for callback in callbacks: try: result = callback(value) if not result and type(result) == bool: raise Invalid() except Exception: raise WebException(msg) return value
def boolean(value): ''' Convert the content of a string (or a number) to a boolean. Do nothing when input value is already a boolean. This filter accepts usual values for ``True`` and ``False``: "0", "f", "false", "n", etc. ''' if value is None or isinstance(value, bool): return value try: return bool(int(value)) except ValueError: lower_value = value.strip().lower() if not lower_value: return None if lower_value in ('f', 'false', 'n', 'no', 'off'): return False if lower_value in ('on', 't', 'true', 'y', 'yes'): return True raise Invalid('Unable to parse boolean {0}'.format(value))
def valid_datetime(datetime): """Make sure that datetime is parseable. :param datetime: Datetime value to parse :type datetime: str :return: The datetime value after parsing :rtype: :class:`datetime.datetime` """ try: parsed_datetime = parse_datetime(datetime) except Exception: raise Invalid('Datetime parsing error') # Make sure timestamp is in UTC, but doesn't have any timezone info. # Passing timezone aware timestamp to PosgreSQL through SQLAlchemy # doesn't seem to work well in manual tests if parsed_datetime.tzinfo: parsed_datetime = (parsed_datetime.astimezone( pytz.timezone('UTC')).replace(tzinfo=None)) return parsed_datetime
def to_coordinates(values: list[Union[Coord, str]], last_value: Optional[list[Coord]] = None) -> list[Coord]: """Convert any known idents found in a flight path into coordinates""" if not values: return values coord = values[0] if isinstance(coord, str): try: # No IATA due to navaid conflict station = Station.from_icao(coord) coord = (station.latitude, station.longitude) except BadStation: coords = NAVAIDS.get(coord) if coords is None: # pylint: disable=raise-missing-from raise Invalid(f"Could not find coordinates for {coord}") if len(coords) == 1: coord = coords[0] else: new_coords = to_coordinates(values[1:], coords) new_coord = new_coords[0] if new_coords else None coord = _best_coord(last_value, coords, new_coord) return [coord] + new_coords return [coord] + to_coordinates(values[1:], coord)
def check_option_choices(v): # Check whether choices have the correct type v_choices = v.get('choices') if not is_iterable(v_choices): return v if v.get('type') == 'list': # choices for a list type means that every list element must be one of these choices type_checker, type_name = get_type_checker({'type': v.get('elements')}) else: type_checker, type_name = get_type_checker(v) if type_checker is None: return v for value in v_choices: try: type_checker(value) except Exception as exc: raise _add_ansible_error_code( Invalid( 'Argument defines choices as (%r) but this is incompatible with argument type %s: %s' % (value, type_name, exc)), error_code='doc-choices-incompatible-type') return v