Esempio n. 1
0
 def check(self, value):
     """Checks if the value is of type UUID."""
     try:
         click.UUID(value)
         return True
     except:
         return False
Esempio n. 2
0
def click_type(object: typing.Union[type, tuple],
               default=None) -> typing.Union[type, click.types.ParamType]:
    if isinstance(object, type):
        if issubclass(object, datetime.datetime):
            return click.DateTime()
        if issubclass(object, typing.Tuple):
            return click.Tuple(object.__args__)
        if issubclass(object, uuid.UUID):
            return click.UUID(default)
        if object is list:
            return
        if issubclass(object, typing.List):
            return click_type(object.__args__[0], default)
        if issubclass(object, set):
            return click.Choice(object)

        if issubclass(object, pathlib.Path):
            return click.Path()
        return object
    else:
        if isinstance(object, tuple):
            if all(isinstance(x, int) for x in object[:2]):
                return click.IntRange(*object)
            if all(isinstance(x, float) for x in object[:2]):
                return click.FloatRange(*object)
Esempio n. 3
0
 def check(self, value):
     """Check if the value is of type UUID."""
     try:
         click.UUID(value)
         return True
     except click.exceptions.BadParameter:
         return False
Esempio n. 4
0
def click_type(object: typing.Union[type, tuple],
               default=None) -> typing.Union[type, click.types.ParamType]:
    """Translate python types to click's subset of types."""
    if isinstance(object, typing._GenericAlias):
        return click_type(object.__args__[0], default)
    elif isinstance(object, type):
        if issubclass(object, datetime.datetime):
            return click.DateTime()
        if issubclass(object, typing.Tuple):
            return click.Tuple(object.__args__)
        if issubclass(object, uuid.UUID):
            return click.UUID(default)
        if object is list:
            return
        if issubclass(object, set):
            return click.Choice(object)

        if issubclass(object, pathlib.Path):
            return click.Path()
        if object in {builtins.object, typing.Any}:
            return
        return object
    else:
        if isinstance(object, tuple):
            if all(isinstance(x, int) for x in object[:2]):
                return click.IntRange(*object)
            if all(isinstance(x, float) for x in object[:2]):
                return click.FloatRange(*object)
Esempio n. 5
0
    def convert(self, value, param, ctx):
        """
        ParamType.convert() is the actual processing method that takes a
        provided parameter and parses it.
        """
        # passthrough conditions: None or already processed
        if value is None or isinstance(value, tuple):
            return value

        # split the value on the first colon, leave the rest intact
        splitval = value.split(":", 1)
        # first element is the endpoint_id
        endpoint_id = click.UUID(splitval[0])

        # get the second element, defaulting to `None` if there was no colon in
        # the original value
        try:
            path = splitval[1]
        except IndexError:
            path = None
        # coerce path="" to path=None
        # means that we treat "enpdoint_id" and "endpoint_id:" equivalently
        path = path or None

        if path is None and self.path_required:
            self.fail("The path component is required", param=param)

        return (endpoint_id, path)
Esempio n. 6
0
def from_entry(ctx, definition, entry, path_query, destination, discard_paths, follow):
    """
    Start recovery process for DEFINITION and recover data from ENTRY or `latest`.

    If [-d/--destination] is set, all files will be recovered under the provided directory.

    When [-d/--destination] is set and [--discard-paths] is provided, files will be stored
    directly under the specified destination directory. Otherwise, the destination directory
    will be used as a parent and the original file directory structure will be preserved.

    If [-d/--destination] is NOT set, all files will be recovered at their original paths.

    Example: \n
        Original File:               /home/user/.bashrc             \n
        Destination:                 /tmp/recover                   \n
        Recovered:                   /tmp/recover/home/user/.bashrc \n
        Recovered (paths discarded): /tmp/recover/.bashrc           \n
    """
    # pylint: disable=too-many-arguments

    if entry.lower() == 'latest':
        response = ctx.obj.api.recover_from_latest(
            definition=definition,
            path_query=path_query,
            destination=destination,
            discard_paths=discard_paths
        )
    else:
        response = ctx.obj.api.recover_from(
            definition=definition,
            entry=click.UUID(entry),
            path_query=path_query,
            destination=destination,
            discard_paths=discard_paths
        )

    click.echo(ctx.obj.rendering.render_operation_response(response))

    if follow:
        ctx.invoke(follow_operation, operation=response['operation'])
Esempio n. 7
0
 def parse(self, value):
     try:
         c = click.UUID(value)
         return str(c)
     except:
         log.error("Incorrect UUID %s", value)
Esempio n. 8
0
 def parse(self, value):
     try:
         c = click.UUID(value)
         return str(c)
     except click.exceptions.BadParameter:
         log.error("Incorrect UUID %s", value)