def set_timeout(timeout: tp.Union[datetime, float]): """ If the argument is a datetime, return that (it has already been converted). If not, check that the float meets timeout range rules (1 minute to 7 days) and then return the UTC datetime for when this request expires. Should equal the current UTC + minutes(float). :timeout: A value indicating the datetime for the case's timeout or minutes until then. """ if isinstance(timeout, datetime): return timeout elif isinstance(timeout, (float, int)): if 10080 >= timeout >= 1: return datetime.utcnow() + timedelta(minutes=timeout) # TODO Adjust according to ArgumentError future build raise errors.ArgumentError({ 'timeout': (ValueError, timeout, 'Minutes outside of accepted range, 1 to 10080') }) elif timeout is None: return None else: raise errors.ArgumentError({ 'timeout': (TypeError, timeout, 'Must be float or datetime, received {dtype}'.format( dtype=type(timeout).__name__)) })
def from_absolute(cls, absolute_path: str): path = pathlib.PurePosixPath(absolute_path) if not path.is_absolute(): raise errors.ArgumentError("Invalid path, should be absolute") parts = path.parts[1:] if len(parts) < 2: raise errors.ArgumentError("Invalid path, missing name of the file") return FilePath(cls.__create_key, parts[0], pathlib.PurePosixPath(*parts))
def assert_data_types(self, **kwargs): """ Ensures given instance attributes meet the data types. Values should be a type subclass or a tuple of acceptable types. Keys should be the attribute name. If any data types are out of order, will raise an ArgumentError. :kwargs: variable name = data type or (type a, type b, ...) """ fails = [] for attr, types in kwargs.items(): value = self.__getattribute__(attr) if not isinstance(value, types) and value is not None: # Fails list should only hold tuples formatted as # (attribute name, data types allowed, current data type) fails.append((attr, value, types, type(value))) # If fails list is not empty if fails: errors_dict = {} for attr, value, types, val_type in fails: if isinstance(types, tuple): # Intelligible string types_string = ', '.join( x.__name__ for x in types[:-1]) + ' or ' + types[-1].__name__ else: types_string = types.__name__ errors_dict[attr] = ( TypeError, value, f"Will only accept {types_string}. Received {val_type.__name__}." ) raise errors.ArgumentError(errors_dict)
def handle(event, context): try: if "userID" not in event: raise errors.ArgumentError("Missing userID argument") if "filePath" not in event: raise errors.ArgumentError("Missing filePath argument") path = FilePath.from_absolute(event["filePath"]) if path.user_id != event["userID"]: raise errors.ForbiddenError( "Trying to manipulate data of another user") create_file(path) return {"filePath": path.absolute} except errors.BackendError: raise except Exception as e: logger.error(f"Unknown exception caught at top level: {e}") raise errors.ServerError("Unexpected error")
def assert_not_null(self, *args): """ Ensures given instance attributes are not null. Arguments should be strings of the attribute's name. :args: Attribute names """ errors_dict = {} for attr in args: if self.__getattribute__(attr) is None: errors_dict[attr] = (errors.NullArgumentError, None, "Argument required. None received.") raise errors.ArgumentError(errors_dict)
def set_webhook(url: str): """ Accepts a url string and checks to see if it meets HTTP/s specifications. If it does, it returns the string. If it does not, it raises an ArgumentError. :url: A string representing the webhook's url """ try: if url is None: return None if utils.is_valid_url(url=url): return url except Exception as e: # Should catch type errors raise errors.ArgumentError( {'webhook': (e.__class__, url, e.__str__())}) # Was something wrong with URL format raise errors.ArgumentError({ 'webhook': (ValueError, url, 'Webhook URL not meet HTTP/s specifications.') })
def __init__(self, manifest_locator_or_text, api_client=None, keep_client=None, num_retries=0): """Instantiate a CollectionReader. This class parses Collection manifests to provide a simple interface to read its underlying files. Arguments: * manifest_locator_or_text: One of a Collection UUID, portable data hash, or full manifest text. * api_client: The API client to use to look up Collections. If not provided, CollectionReader will build one from available Arvados configuration. * keep_client: The KeepClient to use to download Collection data. If not provided, CollectionReader will build one from available Arvados configuration. * num_retries: The default number of times to retry failed service requests. Default 0. You may change this value after instantiation, but note those changes may not propagate to related objects like the Keep client. """ self._api_client = api_client self._keep_client = keep_client self.num_retries = num_retries if re.match(r'[a-f0-9]{32}(\+\d+)?(\+\S+)*$', manifest_locator_or_text): self._manifest_locator = manifest_locator_or_text self._manifest_text = None elif re.match(r'[a-z0-9]{5}-[a-z0-9]{5}-[a-z0-9]{15}$', manifest_locator_or_text): self._manifest_locator = manifest_locator_or_text self._manifest_text = None elif re.match( r'((\S+)( +[a-f0-9]{32}(\+\d+)(\+\S+)*)+( +\d+:\d+:\S+)+$)+', manifest_locator_or_text, re.MULTILINE): self._manifest_text = manifest_locator_or_text self._manifest_locator = None else: raise errors.ArgumentError( "Argument to CollectionReader must be a manifest or a collection UUID" ) self._streams = None