Skip to content

Commit

Permalink
minor stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
eacharles committed Oct 4, 2023
1 parent 8b667b8 commit 3dd1282
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 24 deletions.
78 changes: 77 additions & 1 deletion src/lsst/cmservice/cli/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,82 @@
]


class DictParamType(click.ParamType):
"""Represents the dictionary type of a CLI parameter.
Validates and converts values from the command line string or Python into
a Python dict.
- All key-value pairs must be separated by one semicolon.
- Key and value must be separated by one equal sign.
- Converts sequences separeted by dots into a list: list value items
must be separated by commas.
- Converts numbers to int.
Usage:
>>> @click.option("--param", default=None, type=DictParamType())
... def command(param):
... ...
CLI: command --param='page=1; name=Items; rules=1, 2, three; extra=A,;'
Example:
>>> param_value = 'page=1; name=Items; rules=1, 2, three; extra=A,;'
>>> DictParamType().convert(param_value, None, None)
{'page': 1, 'name': 'Items', 'rules': [1, 2, 'three'], 'extra': ['A']}`
"""

name = "dictionary"

def convert(self, cli_value, param, ctx):
"""Converts CLI value to the dictionary structure.
Args:
cli_value (Any): The value to convert.
param (click.Parameter | None): The parameter that is using this
type to convert its value.
ctx (click.Context | None): The current context that arrived
at this value.
Returns:
dict: The validated and converted dictionary.
Raises:
click.BadParameter: If the validation is failed.
"""
if isinstance(cli_value, dict):
return cli_value
try:
keyvalue_pairs = cli_value.rstrip(";").split(";")
result_dict = {}
for pair in keyvalue_pairs:
key, values = [item.strip() for item in pair.split("=")]
converted_values = []
for value in values.split(","):
value = value.strip()
if value.isdigit():
value = int(value)
converted_values.append(value)

if len(converted_values) == 1:
result_dict[key] = converted_values[0]
elif len(converted_values) > 1 and converted_values[-1] == "":
result_dict[key] = converted_values[:-1]
else:
result_dict[key] = converted_values
return result_dict
except ValueError:
self.fail(
"All key-value pairs must be separated by one semicolon. "
"Key and value must be separated by one equal sign. "
"List value items must be separated by one comma. "
f"Key-value: {pair}.",
param,
ctx,
)


class EnumChoice(click.Choice):
"""A version of click.Choice specialized for enum types."""

Expand Down Expand Up @@ -114,7 +190,7 @@ class OutputEnum(Enum):

update_dict = PartialOption(
"--update_dict",
type=dict,
type=DictParamType(),
help="Values to update",
)

Expand Down
23 changes: 12 additions & 11 deletions src/lsst/cmservice/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def get_job_errors(
fullname=fullname,
)
query = "get/job/errors"
results = self._client.get(f"{query}", params=params.dict()).json()
results = self._client.get(f"{query}", parans=params.dict()).json()
try:
return parse_obj_as(list[models.ErrorInstance], results)
except ValidationError as msg:
Expand All @@ -229,7 +229,7 @@ def update_status(
) -> StatusEnum:
query = "update/status"
params = models.UpdateStatusQuery(**kwargs)
results = self._client.post(f"{query}", params=params.dict()).json()
results = self._client.post(f"{query}", data=params.dict()).json()
try:
return parse_obj_as(StatusEnum, results)
except ValidationError as msg:
Expand All @@ -242,7 +242,7 @@ def update_collections(
query = "update/collections"
kwargs["node_type"] = kwargs["node_type"].value
params = models.UpdateNodeQuery(**kwargs)
results = self._client.post(f"{query}", params=params.dict()).json()
results = self._client.post(f"{query}", data=params.dict()).json()
try:
return parse_obj_as(dict, results)
except ValidationError as msg:
Expand All @@ -255,7 +255,7 @@ def update_data_dict(
query = "update/data_dict"
kwargs["node_type"] = kwargs["node_type"].value
params = models.UpdateNodeQuery(**kwargs)
results = self._client.post(f"{query}", params=params.dict()).json()
results = self._client.post(f"{query}", data=params.dict()).json()
try:
return parse_obj_as(dict, results)
except ValidationError as msg:
Expand All @@ -268,7 +268,8 @@ def update_child_config(
query = "update/child_config"
kwargs["node_type"] = kwargs["node_type"].value
params = models.UpdateNodeQuery(**kwargs)
results = self._client.post(f"{query}", params=params.dict()).json()
print(params.dict())
results = self._client.post(f"{query}", data=params.json()).json()
try:
return parse_obj_as(dict, results)
except ValidationError as msg:
Expand All @@ -280,7 +281,7 @@ def add_groups(
) -> list[models.Group]:
query = "add/groups"
params = models.AddGroups(**kwargs)
results = self._client.post(f"{query}", params=params.dict()).json()
results = self._client.post(f"{query}", data=params.dict()).json()
try:
return parse_obj_as(list[models.Group], results)
except ValidationError as msg:
Expand All @@ -292,7 +293,7 @@ def add_steps(
) -> list[models.Group]:
query = "add/groups"
params = models.AddSteps(**kwargs)
results = self._client.post(f"{query}", params=params.dict()).json()
results = self._client.post(f"{query}", data=params.dict()).json()
try:
return parse_obj_as(list[models.Group], results)
except ValidationError as msg:
Expand All @@ -304,7 +305,7 @@ def add_campaign(
) -> models.Campaign:
query = "add/campaign"
params = models.CampaignCreate(**kwargs)
results = self._client.post(f"{query}", params=params.dict()).json()
results = self._client.post(f"{query}", data=params.dict()).json()
try:
return parse_obj_as(models.Campaign, results)
except ValidationError as msg:
Expand All @@ -316,7 +317,7 @@ def load_specification(
) -> models.Specification:
query = "load/specification"
params = models.SpecificationLoad(**kwargs)
results = self._client.post(f"{query}", params=params.dict()).json()
results = self._client.post(f"{query}", data=params.dict()).json()
try:
return parse_obj_as(models.Specification, results)
except ValidationError as msg:
Expand All @@ -328,7 +329,7 @@ def load_campaign(
) -> models.Campaign:
query = "load/campaign"
params = models.LoadAndCreateCampaign(**kwargs)
results = self._client.post(f"{query}", params=params.dict()).json()
results = self._client.post(f"{query}", data=params.dict()).json()
try:
return parse_obj_as(models.Campaign, results)
except ValidationError as msg:
Expand All @@ -340,7 +341,7 @@ def load_error_types(
) -> list[models.ErrorType]:
query = "load/error_types"
params = models.YamlFileQuery(**kwargs)
results = self._client.post(f"{query}", params=params.dict()).json()
results = self._client.post(f"{query}", data=params.dict()).json()
try:
return parse_obj_as(list[models.ErrorType], results)
except ValidationError as msg:
Expand Down
2 changes: 1 addition & 1 deletion src/lsst/cmservice/common/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class StatusEnum(enum.Enum):
accepted = 5 # Completed, reviewed and accepted
rescued = 6 # Rescueable and rescue created
rescued = 6 # Rescueable and rescued
"""

# note that ordering of these Enums matters within the
Expand Down
4 changes: 1 addition & 3 deletions src/lsst/cmservice/db/error_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.schema import ForeignKey

from ..common.enums import ErrorAction, ErrorFlavor, ErrorSource
from ..common.enums import ErrorSource
from .base import Base
from .job import Job
from .row import RowMixin
Expand All @@ -25,8 +25,6 @@ class ErrorInstance(Base, RowMixin):
job_id: Mapped[int | None] = mapped_column(ForeignKey("job.id", ondelete="CASCADE"), index=True)
script_id: Mapped[int | None] = mapped_column(ForeignKey("script.id", ondelete="CASCADE"), index=True)
source: Mapped[ErrorSource] = mapped_column()
flavor: Mapped[ErrorFlavor] = mapped_column()
action: Mapped[ErrorAction] = mapped_column()
diagnostic_message: Mapped[str] = mapped_column()
data: Mapped[Optional[dict | list]] = mapped_column(type_=JSON)

Expand Down
2 changes: 1 addition & 1 deletion src/lsst/cmservice/db/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ async def update_child_config(
self.child_config.update(**kwargs)
else:
self.child_config = kwargs.copy()
await session.refresh(self)
await session.refresh(self, attribute_names=["child_config"])
return self.child_config

async def update_collections(
Expand Down
4 changes: 1 addition & 3 deletions src/lsst/cmservice/models/error_instance.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from pydantic import BaseModel

from ..common.enums import ErrorAction, ErrorFlavor, ErrorSource
from ..common.enums import ErrorSource


class ErrorInstanceBase(BaseModel):
error_type_id: int | None
source: ErrorSource
flavor: ErrorFlavor
action: ErrorAction
diagnostic_message: str
data: dict

Expand Down
9 changes: 5 additions & 4 deletions src/lsst/cmservice/routers/updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from lsst.cmservice.db import interface

from .. import models
from ..common.enums import NodeTypeEnum

router = APIRouter(
prefix="/update",
Expand All @@ -25,7 +26,7 @@ async def update_status(
result = await interface.update_status(
session,
query.fullname,
query.node_type,
NodeTypeEnum(query.node_type),
query.status,
)
return result
Expand All @@ -44,7 +45,7 @@ async def update_collections(
result = await interface.update_collections(
session,
query.fullname,
query.node_type,
NodeTypeEnum(query.node_type),
**query.update_dict,
)
return result
Expand All @@ -63,7 +64,7 @@ async def update_child_config(
result = await interface.update_child_config(
session,
query.fullname,
query.node_type,
NodeTypeEnum(query.node_type),
**query.update_dict,
)
return result
Expand All @@ -82,7 +83,7 @@ async def update_data_dict(
result = await interface.update_data_dict(
session,
query.fullname,
query.node_type,
NodeTypeEnum(query.node_type),
**query.update_dict,
)
return result

0 comments on commit 3dd1282

Please sign in to comment.