-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor object_model/tipping_point.py and interface/tipping_points.py
- Loading branch information
1 parent
914708f
commit 509f586
Showing
4 changed files
with
167 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import Any | ||
|
||
from flood_adapt.dbs_controller import Database | ||
from flood_adapt.object_model.interface.tipping_points import ITipPoint | ||
from flood_adapt.object_model.tipping_point import TippingPoint | ||
|
||
|
||
def get_tipping_points() -> dict[str, Any]: | ||
# sorting and filtering either with PyQt table or in the API | ||
return Database().tipping_points.list_objects() | ||
|
||
|
||
def get_tipping_point(name: str) -> ITipPoint: | ||
return Database().tipping_points.get(name) | ||
|
||
|
||
def create_tipping_point(attrs: dict[str, Any]) -> ITipPoint: | ||
return TippingPoint.load_dict(attrs, Database().input_path) | ||
|
||
|
||
def save_tipping_point(tipping_point: ITipPoint) -> None: | ||
Database().tipping_points.save(tipping_point) | ||
|
||
|
||
def edit_tipping_point(tipping_point: ITipPoint) -> None: | ||
Database().tipping_points.edit(tipping_point) | ||
|
||
|
||
def delete_tipping_point(name: str) -> None: | ||
Database().tipping_points.delete(name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import shutil | ||
|
||
from flood_adapt.dbs_classes.dbs_template import DbsTemplate | ||
from flood_adapt.object_model.interface.tipping_points import ITipPoint | ||
from flood_adapt.object_model.tipping_point import TippingPoint | ||
|
||
|
||
class DbsTippingPoint(DbsTemplate): | ||
_type = "tipping_point" | ||
_folder_name = "tipping_points" | ||
_object_model_class = TippingPoint | ||
|
||
def save(self, tipping_point: ITipPoint, overwrite: bool = False): | ||
"""Save a tipping point object in the database. | ||
Parameters | ||
---------- | ||
tipping_point : ITipPoint | ||
object of tipping point type | ||
overwrite : bool, optional | ||
whether to overwrite existing tipping point with same name, by default False | ||
Raises | ||
------ | ||
ValueError | ||
Raise error if name is already in use. Names of tipping points should be unique. | ||
""" | ||
# Save the tipping point | ||
super().save(tipping_point, overwrite=overwrite) | ||
|
||
def delete(self, name: str, toml_only: bool = False): | ||
"""Delete an already existing tipping point in the database. | ||
Parameters | ||
---------- | ||
name : str | ||
name of the tipping point | ||
toml_only : bool, optional | ||
whether to only delete the toml file or the entire folder. If the folder is empty after deleting the toml, | ||
it will always be deleted. By default False | ||
""" | ||
# First delete the tipping point | ||
super().delete(name, toml_only=toml_only) | ||
|
||
# Delete output if edited | ||
output_path = ( | ||
self._database.tipping_points.get_database_path(get_input_path=False) / name | ||
) | ||
|
||
if output_path.exists(): | ||
shutil.rmtree(output_path) | ||
|
||
def edit(self, tipping_point: ITipPoint): | ||
"""Edit an already existing tipping point in the database. | ||
Parameters | ||
---------- | ||
tipping_point : ITipPoint | ||
object of tipping point type | ||
""" | ||
# Edit the tipping point | ||
super().edit(tipping_point) | ||
|
||
# Delete output if edited | ||
output_path = ( | ||
self._database.tipping_points.get_database_path(get_input_path=False) | ||
/ tipping_point.attrs.name | ||
) | ||
|
||
if output_path.exists(): | ||
shutil.rmtree(output_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters