Skip to content
Merged
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ flake8:
flake8 --ignore E501,E402,F401,W503,C0414 ./tests || true

pylint:
pylint --extension-pkg-whitelist='pydantic' ./lib || true
pylint --extension-pkg-whitelist='pydantic' --disable=E0401,W0621 ./tests || true
pylint ./lib || true
pylint --disable=E0401,W0621 ./tests || true

ruff:
ruff check --fix ./lib || true
Expand Down
14 changes: 9 additions & 5 deletions lib/controllers/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ class EnvController:
"""
Controller for the Environment model.

Init Attributes:
env: models.Env

Enables:
- Simulation of a RocketPy Environment from models.Env
- CRUD operations over models.Env on the database
Expand All @@ -35,6 +32,7 @@ async def create_env(env: Env) -> Union[EnvCreated, HTTPException]:
Returns:
views.EnvCreated
"""
env_repo = None
try:
async with EnvRepository(env) as env_repo:
await env_repo.create_env()
Expand All @@ -58,9 +56,15 @@ async def create_env(env: Env) -> Union[EnvCreated, HTTPException]:
else:
return EnvCreated(env_id=env_repo.env_id)
finally:
logger.info(
f"Call to controllers.environment.create_env completed for Env {env_repo.env_id}"
env_id = (
getattr(env_repo, 'env_id', 'unknown')
if env_repo
else 'unknown'
)
if env_repo:
logger.info(
f"Call to controllers.environment.create_env completed for Env {env_id}"
)

@staticmethod
async def get_env_by_id(env_id: str) -> Union[Env, HTTPException]:
Expand Down
40 changes: 17 additions & 23 deletions lib/controllers/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ class FlightController:
"""
Controller for the Flight model.

Init Attributes:
flight (models.Flight): Flight model object.

Enables:
- Create a RocketPyFlight object from a Flight model object.
- Generate trajectory simulation from a RocketPyFlight object.
Expand All @@ -39,34 +36,24 @@ class FlightController:

"""

def __init__(
self,
flight: Flight,
):
self.guard(flight)
self._flight = flight

@property
def flight(self) -> Flight:
return self._flight

@flight.setter
def flight(self, flight: Flight):
self._flight = flight

@staticmethod
def guard(flight: Flight):
RocketController.guard(flight.rocket)

async def create_flight(self) -> Union[FlightCreated, HTTPException]:
@classmethod
async def create_flight(
cls, flight: Flight
) -> Union[FlightCreated, HTTPException]:
"""
Create a flight in the database.

Returns:
views.FlightCreated
"""
flight_repo = None
try:
async with FlightRepository(self.flight) as flight_repo:
cls.guard(flight)
async with FlightRepository(flight) as flight_repo:
await flight_repo.create_flight()
except PyMongoError as e:
logger.error(f"controllers.flight.create_flight: PyMongoError {e}")
Expand All @@ -86,8 +73,13 @@ async def create_flight(self) -> Union[FlightCreated, HTTPException]:
else:
return FlightCreated(flight_id=flight_repo.flight_id)
finally:
flight_id = (
getattr(flight_repo, 'flight_id', 'unknown')
if flight_repo
else 'unknown'
)
logger.info(
f"Call to controllers.flight.create_flight completed for Flight {flight_repo.flight_id}"
f"Call to controllers.flight.create_flight completed for Flight {flight_id}"
)

@staticmethod
Expand Down Expand Up @@ -189,8 +181,9 @@ async def get_rocketpy_flight_binary(
f"Call to controllers.flight.get_rocketpy_flight_binary completed for Flight {flight_id}"
)

@classmethod
async def update_flight_by_id(
self, flight_id: str
cls, flight: Flight, flight_id: str
) -> Union[FlightUpdated, HTTPException]:
"""
Update a models.Flight in the database.
Expand All @@ -205,7 +198,8 @@ async def update_flight_by_id(
HTTP 404 Not Found: If the flight is not found in the database.
"""
try:
async with FlightRepository(self.flight) as flight_repo:
cls.guard(flight)
async with FlightRepository(flight) as flight_repo:
await flight_repo.update_flight_by_id(flight_id)
except PyMongoError as e:
logger.error(f"controllers.flight.update_flight: PyMongoError {e}")
Expand Down
40 changes: 19 additions & 21 deletions lib/controllers/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,10 @@ class MotorController:
"""
Controller for the motor model.

Init Attributes:
motor (models.Motor): Motor model object.

Enables:
- Create a rocketpy.Motor object from a Motor model object.
"""

def __init__(self, motor: Motor):
self.guard(motor)
self._motor = motor

@property
def motor(self) -> Motor:
return self._motor

@motor.setter
def motor(self, motor: Motor):
self._motor = motor

@staticmethod
def guard(motor: Motor):
if (
Expand All @@ -51,15 +36,20 @@ def guard(motor: Motor):

# TODO: extend guard to check motor kinds and tank kinds specifics

async def create_motor(self) -> Union[MotorCreated, HTTPException]:
@classmethod
async def create_motor(
cls, motor: Motor
) -> Union[MotorCreated, HTTPException]:
"""
Create a models.Motor in the database.

Returns:
views.MotorCreated
"""
motor_repo = None
try:
async with MotorRepository(self.motor) as motor_repo:
cls.guard(motor)
async with MotorRepository(motor) as motor_repo:
await motor_repo.create_motor()
except PyMongoError as e:
logger.error(f"controllers.motor.create_motor: PyMongoError {e}")
Expand All @@ -79,9 +69,15 @@ async def create_motor(self) -> Union[MotorCreated, HTTPException]:
else:
return MotorCreated(motor_id=motor_repo.motor_id)
finally:
logger.info(
f"Call to controllers.motor.create_motor completed for Motor {motor_repo.motor_id}"
motor_id = (
getattr(motor_repo, 'motor_id', 'unknown')
if motor_repo
else 'unknown'
)
if motor_repo:
logger.info(
f"Call to controllers.motor.create_motor completed for Motor {motor_id}"
)

@staticmethod
async def get_motor_by_id(
Expand Down Expand Up @@ -173,8 +169,9 @@ async def get_rocketpy_motor_binary(
f"Call to controllers.motor.get_rocketpy_motor_binary completed for Motor {motor_id}"
)

@classmethod
async def update_motor_by_id(
self, motor_id: str
cls, motor_id: str, motor: Motor
) -> Union[MotorUpdated, HTTPException]:
"""
Update a motor in the database.
Expand All @@ -189,7 +186,8 @@ async def update_motor_by_id(
HTTP 404 Not Found: If the motor is not found in the database.
"""
try:
async with MotorRepository(self.motor) as motor_repo:
cls.guard(motor)
async with MotorRepository(motor) as motor_repo:
await motor_repo.update_motor_by_id(motor_id)
except PyMongoError as e:
logger.error(f"controllers.motor.update_motor: PyMongoError {e}")
Expand Down
43 changes: 19 additions & 24 deletions lib/controllers/rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,28 @@ class RocketController:
"""
Controller for the Rocket model.

Init Attributes:
rocket: models.Rocket.

Enables:
- CRUD operations over models.Rocket on the database.
"""

def __init__(
self,
rocket: Rocket,
):
self.guard(rocket)
self._rocket = rocket

@property
def rocket(self) -> Rocket:
return self._rocket

@rocket.setter
def rocket(self, rocket: Rocket):
self._rocket = rocket

@staticmethod
def guard(rocket: Rocket):
MotorController.guard(rocket.motor)

async def create_rocket(self) -> Union[RocketCreated, HTTPException]:
@classmethod
async def create_rocket(
cls, rocket: Rocket
) -> Union[RocketCreated, HTTPException]:
"""
Create a models.Rocket in the database.

Returns:
views.RocketCreated
"""
rocket_repo = None
try:
async with RocketRepository(self.rocket) as rocket_repo:
cls.guard(rocket)
async with RocketRepository(rocket) as rocket_repo:
await rocket_repo.create_rocket()
except PyMongoError as e:
logger.error(f"controllers.rocket.create_rocket: PyMongoError {e}")
Expand All @@ -76,9 +63,15 @@ async def create_rocket(self) -> Union[RocketCreated, HTTPException]:
else:
return RocketCreated(rocket_id=rocket_repo.rocket_id)
finally:
logger.info(
f"Call to controllers.rocket.create_rocket completed for Rocket {rocket_repo.rocket_id}"
rocket_id = (
getattr(rocket_repo, 'rocket_id', 'unknown')
if rocket_repo
else 'unknown'
)
if rocket_repo:
logger.info(
f"Call to controllers.rocket.create_rocket completed for Rocket {rocket_id}"
)

@staticmethod
async def get_rocket_by_id(
Expand Down Expand Up @@ -175,8 +168,9 @@ async def get_rocketpy_rocket_binary(
f"Call to controllers.rocket.get_rocketpy_rocket_binary completed for Rocket {rocket_id}"
)

@classmethod
async def update_rocket_by_id(
self, rocket_id: str
cls, rocket: Rocket, rocket_id: str
) -> Union[RocketUpdated, HTTPException]:
"""
Update a models.Rocket in the database.
Expand All @@ -191,7 +185,8 @@ async def update_rocket_by_id(
HTTP 404 Not Found: If the rocket is not found in the database.
"""
try:
async with RocketRepository(self.rocket) as rocket_repo:
cls.guard(rocket)
async with RocketRepository(rocket) as rocket_repo:
await rocket_repo.update_rocket_by_id(rocket_id)
except PyMongoError as e:
logger.error(f"controllers.rocket.update_rocket: PyMongoError {e}")
Expand Down
22 changes: 11 additions & 11 deletions lib/models/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,25 @@ class MotorTank(BaseModel):
discretize: int

# Level based tank parameters
liquid_height: Optional[float]
liquid_height: Optional[float] = None

# Mass based tank parameters
liquid_mass: Optional[float]
gas_mass: Optional[float]
liquid_mass: Optional[float] = None
gas_mass: Optional[float] = None

# Mass flow based tank parameters
gas_mass_flow_rate_in: Optional[float]
gas_mass_flow_rate_out: Optional[float]
liquid_mass_flow_rate_in: Optional[float]
liquid_mass_flow_rate_out: Optional[float]
initial_liquid_mass: Optional[float]
initial_gas_mass: Optional[float]
gas_mass_flow_rate_in: Optional[float] = None
gas_mass_flow_rate_out: Optional[float] = None
liquid_mass_flow_rate_in: Optional[float] = None
liquid_mass_flow_rate_out: Optional[float] = None
initial_liquid_mass: Optional[float] = None
initial_gas_mass: Optional[float] = None

# Ullage based tank parameters
ullage: Optional[float]
ullage: Optional[float] = None

# Optional parameters
name: Optional[str]
name: Optional[str] = None

# Computed parameters
tank_kind: TankKinds = TankKinds.MASS_FLOW
Expand Down
4 changes: 2 additions & 2 deletions lib/routes/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async def create_flight(
"""
with tracer.start_as_current_span("create_flight"):
flight.rocket.motor.set_motor_kind(motor_kind)
return await FlightController(flight).create_flight()
return await FlightController.create_flight(flight)


@router.get("/{flight_id}")
Expand Down Expand Up @@ -146,7 +146,7 @@ async def update_flight(
"""
with tracer.start_as_current_span("update_flight"):
flight.rocket.motor.set_motor_kind(motor_kind)
return await FlightController(flight).update_flight_by_id(flight_id)
return await FlightController.update_flight_by_id(flight, flight_id)


@router.get("/{flight_id}/summary")
Expand Down
6 changes: 3 additions & 3 deletions lib/routes/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def create_motor(motor: Motor, motor_kind: MotorKinds) -> MotorCreated:
"""
with tracer.start_as_current_span("create_motor"):
motor.set_motor_kind(motor_kind)
return await MotorController(motor).create_motor()
return await MotorController.create_motor(motor)


@router.get("/{motor_id}")
Expand Down Expand Up @@ -68,11 +68,11 @@ async def update_motor(
"""
with tracer.start_as_current_span("update_motor"):
motor.set_motor_kind(motor_kind)
return await MotorController(motor).update_motor_by_id(motor_id)
return await MotorController.update_motor_by_id(motor_id, motor)


@router.get(
"/rocketpy/{motor_id}",
"/{motor_id}/rocketpy",
responses={
203: {
"description": "Binary file download",
Expand Down
Loading
Loading