Skip to content

Commit

Permalink
mypy is happy, and it runsmypy src/lsst/cmservice/
Browse files Browse the repository at this point in the history
  • Loading branch information
eacharles committed Oct 25, 2023
1 parent d842d8e commit 34c7653
Show file tree
Hide file tree
Showing 24 changed files with 108 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/lsst/cmservice/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Configuration(BaseSettings):
)

arq_redis_url: RedisDsn = Field(
defuault=RedisDsn("redis://localhost:6379/1", scheme="redis"),
default=RedisDsn("redis://localhost:6379/1", scheme="redis"),
title="The URL for the cm-service arq redis database",
env="CM_ARQ_REDIS_URL",
)
Expand Down
6 changes: 4 additions & 2 deletions src/lsst/cmservice/db/script_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
from typing import TYPE_CHECKING, Any, Optional

import yaml
from sqlalchemy import JSON
from sqlalchemy.ext.asyncio import async_scoped_session
from sqlalchemy.orm import Mapped, mapped_column, relationship
Expand Down Expand Up @@ -57,6 +58,7 @@ async def load(
cls,
session: async_scoped_session,
name: str,
spec_id: int,
spec_name: str,
file_path: str,
) -> ScriptTemplate:
Expand All @@ -83,9 +85,9 @@ async def load(
"""
full_file_path = os.path.abspath(os.path.expandvars(file_path))
with open(full_file_path, "r") as fin:
data = fin.read()
data = yaml.safe_load(fin)

new_row = cls.create_row(session, name=name, spec_name=spec_name, data=data)
new_row = await cls.create_row(session, name=name, spec_id=spec_id, spec_name=spec_name, data=data)
if TYPE_CHECKING:
assert isinstance(new_row, ScriptTemplate)
return new_row
2 changes: 2 additions & 0 deletions src/lsst/cmservice/handlers/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ async def load_spec_block(
data=block_data.get("data"),
collections=block_data.get("collections"),
child_config=block_data.get("child_config"),
scripts=block_data.get("scripts"),
)
if TYPE_CHECKING:
assert isinstance(new_spec_block, SpecBlock)
Expand All @@ -81,6 +82,7 @@ async def load_script_template(
new_script_template = await ScriptTemplate.load(
session,
spec_name=specification.name,
spec_id=specification.id,
name=key,
file_path=config_values["file_path"],
)
Expand Down
13 changes: 10 additions & 3 deletions src/lsst/cmservice/handlers/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,14 @@ async def _write_script(
config_url = os.path.abspath(os.path.expandvars(f"{prod_area}/{script.fullname}_bps_config.yaml"))
log_url = os.path.abspath(os.path.expandvars(f"{prod_area}/{script.fullname}.log"))

bps_script_template = await specification.get_script_template(data_dict["bps_script_template"])
bps_yaml_template = await specification.get_script_template(data_dict["bps_yaml_template"])
bps_script_template = await specification.get_script_template(
session,
data_dict["bps_script_template"],
)
bps_yaml_template = await specification.get_script_template(
session,
data_dict["bps_yaml_template"],
)

command = f"bps --log-file {json_url} --no-log-tty submit {os.path.abspath(config_url)} > {log_url}"

Expand Down Expand Up @@ -303,7 +309,8 @@ async def _write_script(
report_url = os.path.expandvars(f"{prod_area}/{script.fullname}/submit/manifest_report.yaml")

manifest_script_template = await specification.get_script_template(
data_dict["manifest_script_template"]
session,
data_dict["manifest_script_template"],
)
prepend = manifest_script_template.data["text"].replace("{lsst_version}", lsst_version)

Expand Down
2 changes: 2 additions & 0 deletions src/lsst/cmservice/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
pipetask_error_types,
productions,
queries,
script_templates,
scripts,
spec_blocks,
steps,
Expand Down Expand Up @@ -117,6 +118,7 @@
app.include_router(steps.router, prefix=config.prefix)
app.include_router(groups.router, prefix=config.prefix)
app.include_router(scripts.router, prefix=config.prefix)
app.include_router(script_templates.router, prefix=config.prefix)
app.include_router(jobs.router, prefix=config.prefix)
app.include_router(pipetask_error_types.router, prefix=config.prefix)
app.include_router(spec_blocks.router, prefix=config.prefix)
Expand Down
11 changes: 5 additions & 6 deletions src/lsst/cmservice/models/campaign.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from .element import ElementBase, ElementCreateMixin, ElementMixin
from .element import ElementCreateMixin, ElementMixin


class CampaignBase(ElementBase):
class CampaignCreate(ElementCreateMixin):
pass


class CampaignCreate(CampaignBase, ElementCreateMixin):
class Campaign(ElementMixin):
pass


class Campaign(CampaignBase, ElementMixin):
pass
class Config:
orm_mode = True
2 changes: 1 addition & 1 deletion src/lsst/cmservice/models/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ class Dependency(DependencyBase):
id: int

class Config:
from_attributes = True
orm_mode = True
9 changes: 6 additions & 3 deletions src/lsst/cmservice/models/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ class ElementBase(BaseModel):
handler: str | None = None


class ElementCreateMixin:
class ElementCreateMixin(ElementBase):
spec_block_name: str
parent_name: str


class ElementMixin:
class ElementMixin(ElementBase):
id: int
spec_block_id: int
parent_id: int
fullname: str
status: StatusEnum = StatusEnum.waiting
superseded: bool = False

class Config:
orm_mode = True

class Element(ElementBase, ElementMixin):

class Element(ElementMixin):
pass
10 changes: 3 additions & 7 deletions src/lsst/cmservice/models/group.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
from .element import ElementBase, ElementCreateMixin, ElementMixin
from .element import ElementCreateMixin, ElementMixin


class GroupBase(ElementBase):
class GroupCreate(ElementCreateMixin):
pass


class GroupCreate(GroupBase, ElementCreateMixin):
pass


class Group(GroupBase, ElementMixin):
class Group(ElementMixin):
pass
2 changes: 1 addition & 1 deletion src/lsst/cmservice/models/pipetask_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ class PipetaskError(PipetaskErrorBase):
id: int

class Config:
from_attributes = True
orm_mode = True
2 changes: 1 addition & 1 deletion src/lsst/cmservice/models/pipetask_error_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ class PipetaskErrorType(PipetaskErrorTypeBase):
id: int

class Config:
from_attributes = True
orm_mode = True
2 changes: 1 addition & 1 deletion src/lsst/cmservice/models/product_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ class ProductSet(ProductSetBase):
n_missing: int = 0

class Config:
from_attributes = True
orm_mode = True
2 changes: 1 addition & 1 deletion src/lsst/cmservice/models/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ class Production(ProductionBase):
id: int

class Config:
from_attributes = True
orm_mode = True
2 changes: 1 addition & 1 deletion src/lsst/cmservice/models/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ class Queue(QueueBase):
time_finished: datetime | None

class Config:
from_attributes = True
orm_mode = True
2 changes: 1 addition & 1 deletion src/lsst/cmservice/models/row.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ class RowData(BaseModel):
data: dict

class Config:
from_attributes = False
orm_mode = False
2 changes: 1 addition & 1 deletion src/lsst/cmservice/models/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ class Script(ScriptBase):
superseded: bool = False

class Config:
from_attributes = True
orm_mode = True
2 changes: 1 addition & 1 deletion src/lsst/cmservice/models/script_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ class ScriptError(ScriptErrorBase):
id: int

class Config:
from_attributes = True
orm_mode = True
2 changes: 1 addition & 1 deletion src/lsst/cmservice/models/script_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ class ScriptTemplate(ScriptTemplateBase):
fullname: str

class Config:
from_attributes = True
orm_mode = True
4 changes: 2 additions & 2 deletions src/lsst/cmservice/models/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SpecBlock(SpecBlockBase):
fullname: str

class Config:
from_attributes = True
orm_mode = True


class SpecificationBase(BaseModel):
Expand All @@ -39,7 +39,7 @@ class Specification(SpecificationBase):
id: int

class Config:
from_attributes = True
orm_mode = True


class SpecificationLoad(BaseModel):
Expand Down
10 changes: 3 additions & 7 deletions src/lsst/cmservice/models/step.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
from .element import ElementBase, ElementCreateMixin, ElementMixin
from .element import ElementCreateMixin, ElementMixin


class StepBase(ElementBase):
class StepCreate(ElementCreateMixin):
pass


class StepCreate(StepBase, ElementCreateMixin):
pass


class Step(StepBase, ElementMixin):
class Step(ElementMixin):
pass
2 changes: 1 addition & 1 deletion src/lsst/cmservice/models/task_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ class TaskSet(TaskSetBase):
n_failed_upstream: int = 0

class Config:
from_attributes = True
orm_mode = True
2 changes: 1 addition & 1 deletion src/lsst/cmservice/models/wms_task_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ class WmsTaskReport(WmsTaskReportBase):
id: int

class Config:
from_attributes = True
orm_mode = True
2 changes: 2 additions & 0 deletions src/lsst/cmservice/routers/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async def load_specification(
session: async_scoped_session = Depends(db_session_dependency),
) -> db.Specification:
result = await interface.load_specification(session, **query.dict())
await session.commit()
return result


Expand All @@ -38,6 +39,7 @@ async def load_and_create_campaign(
session: async_scoped_session = Depends(db_session_dependency),
) -> db.Campaign:
result = await interface.load_and_create_campaign(session, **query.dict())
await session.commit()
return result


Expand Down
56 changes: 56 additions & 0 deletions src/lsst/cmservice/routers/script_templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from typing import Sequence

from fastapi import APIRouter, Depends
from safir.dependencies.db_session import db_session_dependency
from sqlalchemy.ext.asyncio import async_scoped_session

from .. import db, models

response_model_class = models.ScriptTemplate
create_model_class = models.ScriptTemplateCreate
db_class = db.ScriptTemplate
class_string = "script_template"
tag_string = "ScriptTemplates"


router = APIRouter(
prefix=f"/{class_string}s",
tags=[tag_string],
)


@router.get(
"",
response_model=list[response_model_class],
summary=f"List {class_string}s",
)
async def get_rows(
parent_id: int | None = None,
parent_name: str | None = None,
skip: int = 0,
limit: int = 100,
session: async_scoped_session = Depends(db_session_dependency),
) -> Sequence[db_class]:
result = await db_class.get_rows(
session,
parent_id=parent_id,
skip=skip,
limit=limit,
parent_name=parent_name,
parent_class=db.Specification,
)
return result


@router.get(
"/{row_id}",
response_model=response_model_class,
summary=f"Retrieve a {class_string}",
)
async def get_row(
row_id: int,
session: async_scoped_session = Depends(db_session_dependency),
) -> db_class:
result = await db_class.get_row(session, row_id)
assert isinstance(result, db_class)
return result

0 comments on commit 34c7653

Please sign in to comment.