From 785fb6dd64fb90ac4adb2296268bf9912a7af42a Mon Sep 17 00:00:00 2001 From: GabrielBarberini Date: Sat, 16 Sep 2023 12:27:39 -0300 Subject: [PATCH] removes trailing-whitespace from controllers --- lib/controllers/environment.py | 19 +- lib/controllers/flight.py | 354 +++++++++++++++++---------------- lib/controllers/motor.py | 8 +- lib/controllers/rocket.py | 15 +- 4 files changed, 207 insertions(+), 189 deletions(-) diff --git a/lib/controllers/environment.py b/lib/controllers/environment.py index 9440185..e86e6f4 100644 --- a/lib/controllers/environment.py +++ b/lib/controllers/environment.py @@ -1,15 +1,14 @@ from rocketpy import Environment +from fastapi import Response, status +from typing import Dict, Any, Union from lib.models.environment import Env from lib.repositories.environment import EnvRepository from lib.views.environment import EnvSummary, EnvData, EnvPlots -from fastapi import Response, status -from typing import Dict, Any, Union - import jsonpickle -class EnvController(): +class EnvController(): """ Controller for the Environment model. @@ -27,10 +26,10 @@ def __init__(self, env: Env): date=env.date ) rocketpy_env.set_atmospheric_model( - type=env.atmospheric_model_type, + type=env.atmospheric_model_type, file=env.atmospheric_model_file ) - self.rocketpy_env = rocketpy_env + self.rocketpy_env = rocketpy_env self.env = env def create_env(self) -> "Dict[str, str]": @@ -47,6 +46,7 @@ def create_env(self) -> "Dict[str, str]": else: return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) + @staticmethod def get_env(env_id: int) -> "Union[Env, Response]": """ Get a env from the database. @@ -58,7 +58,7 @@ def get_env(env_id: int) -> "Union[Env, Response]": env model object Raises: - HTTP 404 Not Found: If the env is not found in the database. + HTTP 404 Not Found: If the env is not found in the database. """ successfully_read_env = \ EnvRepository(env_id=env_id).get_env() @@ -66,6 +66,7 @@ def get_env(env_id: int) -> "Union[Env, Response]": return Response(status_code=status.HTTP_404_NOT_FOUND) return successfully_read_env + @staticmethod def get_rocketpy_env(env_id: int) -> "Union[Dict[str, Any], Response]": """ Get a rocketpy env object encoded as jsonpickle string from the database. @@ -113,12 +114,13 @@ def update_env(self, env_id: int) -> "Union[Dict[str, Any], Response]": if successfully_updated_env: return { - "message": "Environment successfully updated", + "message": "Environment successfully updated", "new_env_id": str(successfully_updated_env) } else: return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) + @staticmethod def delete_env(env_id: int) -> "Union[Dict[str, str], Response]": """ Delete a env from the database. @@ -144,6 +146,7 @@ def delete_env(env_id: int) -> "Union[Dict[str, str], Response]": else: return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) + @staticmethod def simulate(env_id: int) -> "Union[EnvSummary, Response]": """ Simulate a rocket environment. diff --git a/lib/controllers/flight.py b/lib/controllers/flight.py index a9f4e78..f0fff50 100644 --- a/lib/controllers/flight.py +++ b/lib/controllers/flight.py @@ -2,7 +2,7 @@ from lib.models.flight import Flight from lib.models.environment import Env from lib.views.flight import FlightSummary, SurfaceWindConditions, OutOfRailConditions, BurnoutConditions, ApogeeConditions, MaximumValues, InitialConditions, NumericalIntegrationSettings, ImpactConditions, EventsRegistered, LaunchRailConditions, FlightData, FlightPlots -from lib.repositories.flight import FlightRepository +from lib.repositories.flight import FlightRepository from lib.controllers.environment import EnvController from lib.controllers.rocket import RocketController @@ -35,14 +35,190 @@ def __init__(self, flight: Flight): rocketpy_flight=rocketpy.Flight( rocket=rocketpy_rocket, - inclination=flight.inclination, + inclination=flight.inclination, heading=flight.heading, environment=rocketpy_env, rail_length=flight.rail_length, ) - self.rocketpy_flight = rocketpy_flight + self.rocketpy_flight = rocketpy_flight self.flight = flight + def create_flight(self) -> "Dict[str, str]": + """ + Create a flight in the database. + + Returns: + Dict[str, str]: Flight id. + """ + flight = FlightRepository(flight=self.flight) + successfully_created_flight = flight.create_flight() + if successfully_created_flight: + return { "message": "Flight created", "flight_id": str(flight.flight_id) } + else: + return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) + + @staticmethod + def get_flight(flight_id: int) -> "Union[Flight, Response]": + """ + Get a flight from the database. + + Args: + flight_id (int): Flight id. + + Returns: + Flight model object + + Raises: + HTTP 404 Not Found: If the flight is not found in the database. + """ + successfully_read_flight = \ + FlightRepository(flight_id=flight_id).get_flight() + if not successfully_read_flight: + return Response(status_code=status.HTTP_404_NOT_FOUND) + return successfully_read_flight + + @staticmethod + def get_rocketpy_flight(flight_id: int) -> "Union[Dict[str, Any], Response]": + """ + Get a rocketpy flight object encoded as jsonpickle string from the database. + + Args: + flight_id (int): Flight id. + + Returns: + str: jsonpickle string of the rocketpy flight. + + Raises: + HTTP 404 Not Found: If the flight is not found in the database. + """ + successfully_read_flight = \ + FlightRepository(flight_id=flight_id).get_flight() + if not successfully_read_flight: + return Response(status_code=status.HTTP_404_NOT_FOUND) + + successfully_read_rocketpy_flight = \ + FlightController(successfully_read_flight).rocketpy_flight + + return { "jsonpickle_rocketpy_flight": jsonpickle.encode(successfully_read_rocketpy_flight) } + + def update_flight(self, flight_id: int) -> "Union[Dict[str, Any], Response]": + """ + Update a flight in the database. + + Args: + flight_id (int): Flight id. + + Returns: + Dict[str, Any]: Flight id and message. + + Raises: + HTTP 404 Not Found: If the flight is not found in the database. + """ + successfully_read_flight = \ + FlightRepository(flight_id=flight_id).get_flight() + if not successfully_read_flight: + return Response(status_code=status.HTTP_404_NOT_FOUND) + + successfully_updated_flight = \ + FlightRepository(flight=self.flight, flight_id=flight_id).update_flight() + + if successfully_updated_flight: + return { + "message": "Flight successfully updated", + "new_flight_id": str(successfully_updated_flight) + } + else: + return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) + + @staticmethod + def update_env(flight_id: int, env: Env) -> "Union[Dict[str, Any], Response]": + """ + Update the environment of a flight in the database. + + Args: + flight_id (int): Flight id. + env (models.Env): Environment model object. + + Returns: + Dict[str, Any]: Flight id and message. + + Raises: + HTTP 404 Not Found: If the flight is not found in the database. + """ + successfully_read_flight = \ + FlightRepository(flight_id=flight_id).get_flight() + if not successfully_read_flight: + return Response(status_code=status.HTTP_404_NOT_FOUND) + + flight.env = env + successfully_updated_flight = \ + FlightRepository(flight=flight).update_flight(flight_id) + if successfully_updated_flight: + return { + "message": "Flight updated successfully", + "new_flight_id": str(successfully_updated_flight) + } + else: + return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) + + @staticmethod + def update_rocket(flight_id: int, rocket: Rocket) -> "Union[Dict[str, Any], Response]": + """ + Update the rocket of a flight in the database. + + Args: + flight_id (int): Flight id. + rocket (models.Rocket): Rocket model object. + + Returns: + Dict[str, Any]: Flight id and message. + + Raises: + HTTP 404 Not Found: If the flight is not found in the database. + """ + successfully_read_flight = \ + FlightRepository(flight_id=flight_id).get_flight() + if not successfully_read_flight: + return Response(status_code=status.HTTP_404_NOT_FOUND) + + flight.rocket = rocket + successfully_updated_flight = \ + FlightRepository(flight=flight).update_flight(flight_id) + if successfully_updated_flight: + return { + "message": "Flight updated successfully", + "new_flight_id": str(successfully_updated_flight) + } + else: + return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) + + @staticmethod + def delete_flight(flight_id: int) -> "Union[Dict[str, str], Response]": + """ + Delete a flight from the database. + + Args: + flight_id (int): Flight id. + + Returns: + Dict[str, str]: Flight id and message. + + Raises: + HTTP 404 Not Found: If the flight is not found in the database. + """ + successfully_read_flight = \ + FlightRepository(flight_id=flight_id).get_flight() + if not successfully_read_flight: + return Response(status_code=status.HTTP_404_NOT_FOUND) + + successfully_deleted_flight = \ + FlightRepository(flight_id=flight_id).delete_flight() + if successfully_deleted_flight: + return {"deleted_flight_id": str(flight_id), "message": "Flight successfully deleted"} + else: + return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) + + @staticmethod def simulate(flight_id: int) -> "Union[FlightSummary, Response]": """ Simulate a rocket flight. @@ -198,7 +374,7 @@ def simulate(flight_id: int) -> "Union[FlightSummary, Response]": _flight_data = FlightData( initial_conditions = _initial_conditions, numerical_integration_settings = _numerical_integration_settings, - surface_wind_conditions = _surface_wind_conditions, + surface_wind_conditions = _surface_wind_conditions, launch_rail_conditions = _launch_rail_conditions, out_of_rail_conditions= _out_of_rail_conditions, burnout_conditions = _burnout_conditions, @@ -211,173 +387,3 @@ def simulate(flight_id: int) -> "Union[FlightSummary, Response]": flight_summary = FlightSummary( flight_data = _flight_data ) return flight_summary - - def create_flight(self) -> "Dict[str, str]": - """ - Create a flight in the database. - - Returns: - Dict[str, str]: Flight id. - """ - flight = FlightRepository(flight=self.flight) - successfully_created_flight = flight.create_flight() - if successfully_created_flight: - return { "message": "Flight created", "flight_id": str(flight.flight_id) } - else: - return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) - - def get_flight(flight_id: int) -> "Union[Flight, Response]": - """ - Get a flight from the database. - - Args: - flight_id (int): Flight id. - - Returns: - Flight model object - - Raises: - HTTP 404 Not Found: If the flight is not found in the database. - """ - successfully_read_flight = \ - FlightRepository(flight_id=flight_id).get_flight() - if not successfully_read_flight: - return Response(status_code=status.HTTP_404_NOT_FOUND) - return successfully_read_flight - - def get_rocketpy_flight(flight_id: int) -> "Union[Dict[str, Any], Response]": - """ - Get a rocketpy flight object encoded as jsonpickle string from the database. - - Args: - flight_id (int): Flight id. - - Returns: - str: jsonpickle string of the rocketpy flight. - - Raises: - HTTP 404 Not Found: If the flight is not found in the database. - """ - successfully_read_flight = \ - FlightRepository(flight_id=flight_id).get_flight() - if not successfully_read_flight: - return Response(status_code=status.HTTP_404_NOT_FOUND) - - successfully_read_rocketpy_flight = \ - FlightController(successfully_read_flight).rocketpy_flight - - return { "jsonpickle_rocketpy_flight": jsonpickle.encode(successfully_read_rocketpy_flight) } - - def update_flight(self, flight_id: int) -> "Union[Dict[str, Any], Response]": - """ - Update a flight in the database. - - Args: - flight_id (int): Flight id. - - Returns: - Dict[str, Any]: Flight id and message. - - Raises: - HTTP 404 Not Found: If the flight is not found in the database. - """ - successfully_read_flight = \ - FlightRepository(flight_id=flight_id).get_flight() - if not successfully_read_flight: - return Response(status_code=status.HTTP_404_NOT_FOUND) - - successfully_updated_flight = \ - FlightRepository(flight=self.flight, flight_id=flight_id).update_flight() - - if successfully_updated_flight: - return { - "message": "Flight successfully updated", - "new_flight_id": str(successfully_updated_flight) - } - else: - return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) - - def update_env(flight_id: int, env: Env) -> "Union[Dict[str, Any], Response]": - """ - Update the environment of a flight in the database. - - Args: - flight_id (int): Flight id. - env (models.Env): Environment model object. - - Returns: - Dict[str, Any]: Flight id and message. - - Raises: - HTTP 404 Not Found: If the flight is not found in the database. - """ - successfully_read_flight = \ - FlightRepository(flight_id=flight_id).get_flight() - if not successfully_read_flight: - return Response(status_code=status.HTTP_404_NOT_FOUND) - - flight.env = env - successfully_updated_flight = \ - FlightRepository(flight=flight).update_flight(flight_id) - if successfully_updated_flight: - return { - "message": "Flight updated successfully", - "new_flight_id": str(successfully_updated_flight) - } - else: - return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) - - def update_rocket(flight_id: int, rocket: Rocket) -> "Union[Dict[str, Any], Response]": - """ - Update the rocket of a flight in the database. - - Args: - flight_id (int): Flight id. - rocket (models.Rocket): Rocket model object. - - Returns: - Dict[str, Any]: Flight id and message. - - Raises: - HTTP 404 Not Found: If the flight is not found in the database. - """ - successfully_read_flight = \ - FlightRepository(flight_id=flight_id).get_flight() - if not successfully_read_flight: - return Response(status_code=status.HTTP_404_NOT_FOUND) - - flight.rocket = rocket - successfully_updated_flight = \ - FlightRepository(flight=flight).update_flight(flight_id) - if successfully_updated_flight: - return { - "message": "Flight updated successfully", - "new_flight_id": str(successfully_updated_flight) - } - else: - return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) - - def delete_flight(flight_id: int) -> "Union[Dict[str, str], Response]": - """ - Delete a flight from the database. - - Args: - flight_id (int): Flight id. - - Returns: - Dict[str, str]: Flight id and message. - - Raises: - HTTP 404 Not Found: If the flight is not found in the database. - """ - successfully_read_flight = \ - FlightRepository(flight_id=flight_id).get_flight() - if not successfully_read_flight: - return Response(status_code=status.HTTP_404_NOT_FOUND) - - successfully_deleted_flight = \ - FlightRepository(flight_id=flight_id).delete_flight() - if successfully_deleted_flight: - return {"deleted_flight_id": str(flight_id), "message": "Flight successfully deleted"} - else: - return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/lib/controllers/motor.py b/lib/controllers/motor.py index 1b87dc4..8589ac7 100644 --- a/lib/controllers/motor.py +++ b/lib/controllers/motor.py @@ -54,6 +54,7 @@ def create_motor(self) -> "Dict[str, str]": else: return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) + @staticmethod def get_motor(motor_id: int) -> "Union[Motor, Response]": """ Get a motor from the database. @@ -65,7 +66,7 @@ def get_motor(motor_id: int) -> "Union[Motor, Response]": Motor model object Raises: - HTTP 404 Not Found: If the motor is not found in the database. + HTTP 404 Not Found: If the motor is not found in the database. """ successfully_read_motor = \ MotorRepository(motor_id=motor_id).get_motor() @@ -73,6 +74,7 @@ def get_motor(motor_id: int) -> "Union[Motor, Response]": return Response(status_code=status.HTTP_404_NOT_FOUND) return successfully_read_motor + @staticmethod def get_rocketpy_motor(motor_id: int) -> "Union[Dict[str, Any], Response]": """ Get a rocketpy motor object encoded as jsonpickle string from the database. @@ -120,12 +122,13 @@ def update_motor(self, motor_id: int) -> "Union[Dict[str, Any], Response]": if successfully_updated_motor: return { - "message": "Motor successfully updated", + "message": "Motor successfully updated", "new_motor_id": str(successfully_updated_motor) } else: return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) + @staticmethod def delete_motor(motor_id: int) -> "Union[Dict[str, str], Response]": """ Delete a motor from the database. @@ -151,6 +154,7 @@ def delete_motor(motor_id: int) -> "Union[Dict[str, str], Response]": else: return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) + @staticmethod def simulate(motor_id: int) -> "Union[MotorSummary, Response]": """ Simulate a rocketpy motor. diff --git a/lib/controllers/rocket.py b/lib/controllers/rocket.py index 66182f5..d88d2a6 100644 --- a/lib/controllers/rocket.py +++ b/lib/controllers/rocket.py @@ -58,7 +58,7 @@ def __init__(self, rocket: Rocket): rocketpy_rocket.evaluate_static_margin() #Tail - tail = self.TailController(rocket.tail).rocketpy_tail + tail = self.TailController(rocket.tail).rocketpy_tail rocketpy_rocket.aerodynamic_surfaces.add(tail, tail.position) rocketpy_rocket.evaluate_static_margin() @@ -164,6 +164,7 @@ def __init__(self, parachute: Parachute, p: int): self.rocketpy_parachute = rocketpy_parachute self.parachute = parachute + @staticmethod def check_trigger(expression: str) -> bool: """ Check if the trigger expression is valid. @@ -236,6 +237,7 @@ def create_rocket(self) -> "Dict[str, str]": else: return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) + @staticmethod def get_rocket(rocket_id: int) -> "Union[Rocket, Response]": """ Get a rocket from the database. @@ -247,7 +249,7 @@ def get_rocket(rocket_id: int) -> "Union[Rocket, Response]": rocket model object Raises: - HTTP 404 Not Found: If the rocket is not found in the database. + HTTP 404 Not Found: If the rocket is not found in the database. """ successfully_read_rocket = \ RocketRepository(rocket_id=rocket_id).get_rocket() @@ -255,6 +257,7 @@ def get_rocket(rocket_id: int) -> "Union[Rocket, Response]": return Response(status_code=status.HTTP_404_NOT_FOUND) return successfully_read_rocket + @staticmethod def get_rocketpy_rocket(rocket_id: int) -> "Union[Dict[str, Any], Response]": """ Get a rocketpy rocket object encoded as jsonpickle string from the database. @@ -302,12 +305,13 @@ def update_rocket(self, rocket_id: int) -> "Union[Dict[str, Any], Response]": if successfully_updated_rocket: return { - "message": "Rocket successfully updated", + "message": "Rocket successfully updated", "new_rocket_id": str(successfully_updated_rocket) } else: return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) + @staticmethod def delete_rocket(rocket_id: int) -> "Union[Dict[str, str], Response]": """ Delete a rocket from the database. @@ -333,6 +337,7 @@ def delete_rocket(rocket_id: int) -> "Union[Dict[str, str], Response]": else: return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) + @staticmethod def simulate(rocket_id: int) -> "Union[RocketSummary, Response]": """ Simulate a rocket rocket. @@ -394,7 +399,7 @@ def simulate(rocket_id: int) -> "Union[RocketSummary, Response]": ) ) - _aerodynamics_lift_coefficient_derivatives = {} + _aerodynamics_lift_coefficient_derivatives = {} for surface, position in rocket.aerodynamic_surfaces: name = surface.name _aerodynamics_lift_coefficient_derivatives[name] = [] @@ -412,7 +417,7 @@ def simulate(rocket_id: int) -> "Union[RocketSummary, Response]": ) _rocket_aerodynamics_quantities = RocketAerodynamicsQuantities( - aerodynamics_lift_coefficient_derivatives = _aerodynamics_lift_coefficient_derivatives, + aerodynamics_lift_coefficient_derivatives = _aerodynamics_lift_coefficient_derivatives, aerodynamics_center_of_pressure = _aerodynamics_center_of_pressure, distance_cop_to_codm = "Distance from Center of Pressure to Center of Dry Mass: " + "{:.3f}".format(rocket.center_of_mass(0) - rocket.cp_position) + " m", initial_static_margin = "Initial Static Margin: " + "{:.3f}".format(rocket.static_margin(0)) + " c",