Skip to content

Commit

Permalink
Update SDK to version 0.5.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Roboto-Bot-O committed Jul 19, 2024
1 parent fe1e94d commit 890baa0
Show file tree
Hide file tree
Showing 29 changed files with 233 additions and 147 deletions.
22 changes: 11 additions & 11 deletions src/roboto/cli/actions/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def create(
)

print(f"Successfully created action '{action.name}'. Record: ")
print(json.dumps(action.to_dict(), indent=4))
print(json.dumps(action.to_dict(), indent=2))
except RobotoConflictException:
action = actions.Action.from_name(
name=config_with_overrides.name,
Expand All @@ -237,10 +237,10 @@ def create(
if updates:
action.update(**updates)
print(f"Successfully updated action '{action.name}'. Record: ")
print(json.dumps(action.to_dict(), indent=4))
print(json.dumps(action.to_dict(), indent=2))
else:
print(f"Action '{action.name}' is up-to-date. Record: ")
print(json.dumps(action.to_dict(), indent=4))
print(json.dumps(action.to_dict(), indent=2))


def create_parser(parser: argparse.ArgumentParser):
Expand Down Expand Up @@ -283,9 +283,9 @@ def create_parser(parser: argparse.ArgumentParser):
arg_name="inherits_from",
arg_help=(
"Partially or fully qualified reference to action from which to inherit configuration. "
"Inheriting from another action is mutually exclusive with specifying a container image (--image), "
"entrypoint (--entrypoint), command (--command), working directory (--workdir), env vars (--env), "
"or parameter(s) (--parameter). "
"Inheriting from another action is mutually exclusive with specifying a container image (``--image``), "
"entrypoint (``--entrypoint``), command (``--command``), working directory (``--workdir``), "
"env vars (``--env``), or parameter(s) (``--parameter``). "
),
positional=False,
required=False,
Expand All @@ -305,12 +305,12 @@ def create_parser(parser: argparse.ArgumentParser):
action=ActionParameterArg,
help=(
"Zero or more parameters (space-separated) accepted by this action. "
"'name' is the only required attribute. "
"'default' values, if provided, are JSON parsed. "
"``name`` is the only required attribute. "
"``default`` values, if provided, are JSON parsed. "
"This argument can be specified multiple times. "
"Parameters can be modified after creation. "
"Argument values must be wrapped in quotes. E.g.: "
"--put-parameter 'name=my_param|required=true|description=My description of my_param'"
"``--put-parameter 'name=my_param|required=true|description=My description of my_param'``"
),
)
parser.add_argument(
Expand All @@ -320,8 +320,8 @@ def create_parser(parser: argparse.ArgumentParser):
nargs="*",
action=KeyValuePairsAction,
help=(
"Zero or more 'key=value' format key/value pairs which represent action metadata. "
"`value` is parsed as JSON. "
"Zero or more ``<key>=<value>`` format key/value pairs which represent action metadata. "
"``value`` is parsed as JSON. "
"Metadata can be modified after creation."
),
)
Expand Down
19 changes: 8 additions & 11 deletions src/roboto/cli/actions/invoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

import argparse
import textwrap

from ...domain import actions
from ..command import (
Expand Down Expand Up @@ -65,14 +64,12 @@ def invoke_parser(parser: argparse.ArgumentParser) -> None:
type=str,
nargs="+",
action="extend",
# fmt: off
help=textwrap.dedent("""\
One or many file patterns for data to download from the data source. Examples:
front camera images, "--input-data '**/cam_front/*.jpg'";
front and rear camera images, "--input-data '**/cam_front/*.jpg' --input-data '**/cam_rear/*.jpg'";
all data, "--input-data '**/*'"
"""),
# fmt: on
help=(
"One or many file patterns for data to download from the data source. Examples: "
"front camera images, ``--input-data '**/cam_front/*.jpg'``; "
"front and rear camera images, ``--input-data '**/cam_front/*.jpg' --input-data '**/cam_rear/*.jpg'``; "
"all data, ``--input-data '**/*'``."
),
)

parser.add_argument(
Expand Down Expand Up @@ -110,8 +107,8 @@ def invoke_parser(parser: argparse.ArgumentParser) -> None:
nargs="*",
action=KeyValuePairsAction,
help=(
"Zero or more '<parameter_name>=<parameter_value>' pairs. "
"`parameter_value` is parsed as JSON. "
"Zero or more ``<parameter_name>=<parameter_value>`` pairs. "
"``parameter_value`` is parsed as JSON. "
),
)

Expand Down
8 changes: 4 additions & 4 deletions src/roboto/cli/actions/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def search(
owner_org_id=args.org,
roboto_client=context.roboto_client,
)
print(json.dumps([action.to_dict() for action in matching_actions], indent=4))
print(json.dumps([action.to_dict() for action in matching_actions], indent=2))


def search_parser(parser: argparse.ArgumentParser):
Expand All @@ -104,16 +104,16 @@ def search_parser(parser: argparse.ArgumentParser):
nargs="*",
action=KeyValuePairsAction,
help=(
"Zero or more 'key=value' pairs which represent action metadata. "
"`value` is parsed as JSON. E.g.: --metadata foo=bar --metadata baz.nested=200"
"Zero or more ``key=value`` pairs which represent action metadata. "
"``value`` is parsed as JSON. E.g.: ``--metadata foo=bar --metadata baz.nested=200``"
),
)
parser.add_argument(
"--tag",
required=False,
type=str,
nargs="*",
help="One or more tags associated with this action. E.g.: --tag foo --tag bar",
help="One or more tags associated with this action. E.g.: ``--tag foo --tag bar``",
action="extend",
)
add_org_arg(parser=parser)
Expand Down
2 changes: 1 addition & 1 deletion src/roboto/cli/actions/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def show(
owner_org_id=owner_org_id,
roboto_client=context.roboto_client,
)
print(json.dumps(action.to_dict(), indent=4))
print(json.dumps(action.to_dict(), indent=2))


def show_parser(parser: argparse.ArgumentParser):
Expand Down
18 changes: 9 additions & 9 deletions src/roboto/cli/actions/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def update(
action.update(**updates)

print(f"Successfully updated action '{action.name}'. Record: ")
print(json.dumps(action.to_dict(), indent=4))
print(json.dumps(action.to_dict(), indent=2))


def update_parser(parser: argparse.ArgumentParser):
Expand All @@ -111,15 +111,15 @@ def update_parser(parser: argparse.ArgumentParser):
action="store",
type=lambda s: s if s != "null" else null,
default=NotSet,
help="Optional description of action. Specify 'null' to unset existing description.",
help="Optional description of action. Specify ``null`` to unset existing description.",
)
parser.add_argument(
"--short-description",
required=False,
action="store",
type=lambda s: s if s != "null" else null,
default=NotSet,
help="Optional short description of an action. Specify 'null' to unset existing description.",
help="Optional short description of an action. Specify ``null`` to unset existing description.",
)
parser.add_argument(
"--image",
Expand Down Expand Up @@ -148,7 +148,7 @@ def update_parser(parser: argparse.ArgumentParser):
help=(
"Add parameter(s) or overwrite existing parameter(s) with the same name. "
"Argument values must be wrapped in quotes. E.g.: "
"--put-parameter 'name=my_param|required=true|description=My description of my_param'"
"``--put-parameter 'name=my_param|required=true|description=My description of my_param'``"
),
)
parser.add_argument(
Expand Down Expand Up @@ -178,12 +178,12 @@ def update_parser(parser: argparse.ArgumentParser):
nargs="*",
action=KeyValuePairsAction,
help=(
"Zero or more 'key_path=value' formatted pairs. "
"An attempt is made to parse `value` as JSON; if this fails, `value` is stored as a string. "
"If `key_path` already exists, existing value will be overwritten. "
"Zero or more ``key_path=value`` formatted pairs. "
"An attempt is made to parse ``value`` as JSON; if this fails, ``value`` is stored as a string. "
"If ``key_path`` already exists, existing value will be overwritten. "
"Dot notation is supported for nested keys. "
"Examples: "
"--put-metadata 'key1=value1' 'key2.subkey1=value2' 'key3.sublist1=[\"a\",\"b\",\"c\"]'" # noqa: E501
"``--put-metadata 'key1=value1' 'key2.subkey1=value2' 'key3.sublist1=[\"a\",\"b\",\"c\"]'``" # noqa: E501
),
)

Expand All @@ -194,7 +194,7 @@ def update_parser(parser: argparse.ArgumentParser):
nargs="*",
help=(
"Remove each key from dataset metadata if it exists. "
"Dot notation is supported for nested keys. E.g.: --remove-metadata key1 key2.subkey3"
"Dot notation is supported for nested keys. E.g.: ``--remove-metadata key1 key2.subkey3``"
),
)
add_org_arg(parser=parser)
Expand Down
2 changes: 1 addition & 1 deletion src/roboto/cli/collections/changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def show(args, context: CLIContext, parser: argparse.ArgumentParser):
for change in collection.changes(
from_version=args.from_version, to_version=args.to_version
):
print(change.json())
print(change.model_dump_json(indent=2))


def show_setup_parser(parser):
Expand Down
2 changes: 1 addition & 1 deletion src/roboto/cli/collections/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def create(args, context: CLIContext, parser: argparse.ArgumentParser):
caller_org_id=args.org,
roboto_client=context.roboto_client,
)
print(collection.record.json())
print(collection.record.model_dump_json(indent=2))


def create_setup_parser(parser):
Expand Down
2 changes: 1 addition & 1 deletion src/roboto/cli/collections/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def list(args, context: CLIContext, parser: argparse.ArgumentParser):
owner_org_id=args.org,
roboto_client=context.roboto_client,
):
print(collection.record.json())
print(collection.record.model_dump_json(indent=2))


list_command = RobotoCommand(
Expand Down
2 changes: 1 addition & 1 deletion src/roboto/cli/collections/list_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def list_all(args, context: CLIContext, parser: argparse.ArgumentParser):
roboto_client=context.roboto_client,
content_mode=args.content_mode,
):
print(collection.record.json())
print(collection.record.model_dump_json(indent=2))


def list_all_setup_parser(parser: argparse.ArgumentParser):
Expand Down
6 changes: 3 additions & 3 deletions src/roboto/cli/collections/shared_helpdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
+ " Allows caller to ensure that they're referencing an immutable representation of a given collection."
)
CONTENT_MODE_HELP = (
"The type of content to return for a collection or set of collections. 'summary_only' returns "
+ "only metadata, 'references' will return each id in the collection, and 'full' will retrieve the full content "
+ "of each resource in the collection."
"The type of content to return for a collection or set of collections. ``summary_only`` returns "
+ "only metadata, ``references`` will return each id in the collection, "
+ "and ``full`` will retrieve the full content of each resource in the collection."
)
2 changes: 1 addition & 1 deletion src/roboto/cli/collections/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def show(args, context: CLIContext, parser: argparse.ArgumentParser):
roboto_client=context.roboto_client,
content_mode=content_mode,
)
print(collection.record.json())
print(collection.record.model_dump_json(indent=2))


def show_setup_parser(parser):
Expand Down
2 changes: 1 addition & 1 deletion src/roboto/cli/collections/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def update(args, context: CLIContext, parser: argparse.ArgumentParser):
remove_resources=NotSet if len(remove_resources) == 0 else remove_resources,
)

print(collection.record.json())
print(collection.record.model_dump_json(indent=2))


def update_setup_parser(parser):
Expand Down
5 changes: 4 additions & 1 deletion src/roboto/cli/command/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
JsonFileOrStrType,
KeyValuePairsAction,
)
from .model import RobotoCommand, RobotoCommandSet
from .roboto_command import (
RobotoCommand,
RobotoCommandSet,
)

__all__ = [
"ExistingPathlibPath",
Expand Down
106 changes: 106 additions & 0 deletions src/roboto/cli/command/roboto_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Copyright (c) 2024 Roboto Technologies, Inc.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

import argparse
from typing import Any, Callable, Optional

from ...exceptions import (
RobotoDomainException,
RobotoNoOrgProvidedException,
)
from ..argparse import SortingHelpFormatter
from ..context import CLIContext
from ..terminal import print_error_and_exit


class RobotoCommand(object):
_name: str
_logic: Callable[
[Any, CLIContext, argparse.ArgumentParser], None
] # Args, CLIContext
_inner_setup_parser: Optional[Callable[[Any], None]] # Parser
_command_kwargs: Optional[dict[str, Any]]

def __init__(
self,
name: str,
logic: Callable[[Any, CLIContext, argparse.ArgumentParser], None],
command_kwargs: Optional[dict],
setup_parser: Optional[Callable[[Any], None]] = None,
):
self._name = name
self._logic = logic
self._inner_setup_parser = setup_parser
self._command_kwargs = command_kwargs

@property
def name(self):
return self._name

@property
def command_kwargs(self):
if self._command_kwargs is not None:
return self._command_kwargs
return {}

def setup_parser(
self, parser: argparse.ArgumentParser, context: Optional[CLIContext]
):
def context_aware_logic(args):
if not context:
raise ValueError(
"CLIContext is not set. "
"This is a programming error, and should be fixed by the Roboto development team."
)
try:
return self._logic(args, context, parser)
except RobotoNoOrgProvidedException:
parser.error(
"User with 0 or 2+ orgs must explicitly provide --org to org-bound operations. "
+ "This can also be achieved by setting a 'ROBOTO_ORG_ID' environment variable, "
+ "whose value will be used by default."
)
except RobotoDomainException as exc:
print_error_and_exit(f"{exc.__class__.__name__}: {exc}")

parser.set_defaults(func=context_aware_logic)
if self._inner_setup_parser is not None:
self._inner_setup_parser(parser)


class RobotoCommandSet(object):
commands: list[RobotoCommand]
name: str

__help: str
__context: Optional[CLIContext]

def __init__(self, name, help, commands):
self.name = name
self.__help = help
self.commands = commands

def add_to_subparsers(
self,
parent_subparsers: Any,
context: Optional[CLIContext],
) -> None:
self.__context = context

command_set_parser = parent_subparsers.add_parser(self.name, help=self.__help)
subparsers = command_set_parser.add_subparsers()
subparsers.required = True

for command in self.commands:
command_parser = subparsers.add_parser(
command.name,
formatter_class=SortingHelpFormatter,
**command.command_kwargs,
)
command.setup_parser(command_parser, context)

def sort_commands(self):
self.commands.sort(key=lambda command: command.name)
Loading

0 comments on commit 890baa0

Please sign in to comment.