Skip to content

Commit

Permalink
reformatting files and correcting minor errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdiwahada committed Nov 8, 2024
1 parent d1a2bd8 commit b49627e
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 79 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ pydantic~=2.7.1
pytest~=7.2.1
python-dateutil~=2.9.0
requests~=2.32.0
antares~=0.3.25.0
requests-mock~=1.12.1
2 changes: 1 addition & 1 deletion src/antares/model/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,4 @@ def create_hydro(
# todo: is it necessary to create allocation or correlation ?
hydro = self._area_service.create_hydro(self.id, properties, matrices)
self._hydro = hydro
return hydro
return hydro
76 changes: 75 additions & 1 deletion src/antares/service/api_services/area_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# This file is part of the Antares project.

from pathlib import PurePosixPath
from typing import Dict, List, Optional, Union
from typing import Dict, List, Optional, Union, Any

import pandas as pd

Expand Down Expand Up @@ -542,3 +542,77 @@ def get_load_matrix(self, area: Area) -> pd.DataFrame:
return self.get_matrix(PurePosixPath("input") / "load" / "series" / f"load_{area.id}")
except APIError as e:
raise LoadMatrixDownloadError(area.id, e.message) from e

def read_areas(self) -> List[Area]:
area_list = []

base_api_url = f"{self._base_url}/studies/{self.study_id}/areas"
ui_url = "ui=true"
url_thermal = "clusters/thermal"
url_renewable = "clusters/renewable"
url_st_storage = "storages"
url_properties_form = "properties/form"

json_resp = self._wrapper.get(base_api_url + "?" + ui_url).json()
for area in json_resp:
thermals = dict() #: t.Dict[any, ThermalCluster]
renewables = dict()
st_storage = dict()

area_url = base_api_url + "/" + f"{area}/"
json_thermal = self._wrapper.get(area_url + url_thermal).json()
json_renewable = self._wrapper.get(area_url + url_renewable).json()
json_st_storage = self._wrapper.get(area_url + url_st_storage).json()
json_properties = self._wrapper.get(area_url + url_properties_form).json()

ui_response = craft_ui(self,f"{base_api_url}?type=AREA&{ui_url}",area)

for thermal in json_thermal:
id_therm = thermal.pop("id")
name = thermal.pop("name")

thermal_props = ThermalClusterProperties(**thermal)
therm_cluster = ThermalCluster(self.thermal_service, area, name, thermal_props)
thermals.update({id_therm: therm_cluster})

for renewable in json_renewable:
id_renew = renewable.pop("id")
name = renewable.pop("name")

renew_props = RenewableClusterProperties(**renewable)
renew_cluster = RenewableCluster(self.renewable_service, area, name, renew_props)
renewables.update({id_renew: renew_cluster})

for storage in json_st_storage:
id_storage = storage.pop("id")
name = storage.pop("name")

storage_props = STStorageProperties(**storage)
st_storage_cl = STStorage(self.storage_service, area, name, storage_props)
st_storage.update({id_storage: st_storage_cl})

area_obj = Area(
area,
self,
self.storage_service,
self.thermal_service,
self.renewable_service,
renewables=renewables,
thermals=thermals,
st_storages=st_storage,
properties=json_properties,
ui=ui_response
)

area_list.append(area_obj)

return area_list

def craft_ui(self, url_str:str, area_id) -> AreaUi:
response = self._wrapper.get(url_str)
json_ui = response.json()[area_id]

ui_response = AreaUiResponse.model_validate(json_ui)
current_ui = AreaUi.model_validate(ui_response.to_craft())

return current_ui
62 changes: 0 additions & 62 deletions src/antares/service/api_services/study_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,65 +109,3 @@ def delete(self, children: bool) -> None:
except APIError as e:
raise StudyDeletionError(self.study_id, e.message) from e

def read_areas(self) -> List:
area_list = []

base_api_url = f"{self._base_url}/studies/{self.study_id}/areas"
ui_url = "?ui=true"
url_thermal = "clusters/thermal"
url_renewable = "clusters/renewable"
url_st_storage = "storages"
url_properties_form = "properties/form"

json_resp = self._wrapper.get(base_api_url + ui_url).json()

for area in json_resp:
thermals = dict() #: t.Dict[any, ThermalCluster]
renewables = dict()
st_storage = dict()

area_url = base_api_url + "/" + f"{area}/"
json_thermal = self._wrapper.get(area_url + url_thermal).json()
json_renewable = self._wrapper.get(area_url + url_renewable).json()
json_st_storage = self._wrapper.get(area_url + url_st_storage).json()
json_properties = self._wrapper.get(area_url + url_properties_form).json()

for therm in json_thermal:
id_therm = therm.pop("id")
name = therm.pop("name")

thermal_props = ThermalClusterProperties(**therm)
therm_cluster = ThermalCluster(self.config, area, name, thermal_props)
thermals.update({id_therm: therm_cluster})

for renew in json_renewable:
id_renew = renew.pop("id")
name = renew.pop("name")

renew_props = RenewableClusterProperties(**renew)
renew_cluster = RenewableCluster(self.config, area, name, renew_props)
renewables.update({id_renew: renew_cluster})

for storage in json_st_storage:
id_storage = storage.pop("id")
name = storage.pop("name")

storage_props = STStorageProperties(**storage)
st_storage_cl = STStorage(self.config, area, name, storage_props)
st_storage.update({id_storage: st_storage_cl})

area_obj = Area(
area,
self.config,
self.config,
self.config,
self.config,
renewables=renewables,
thermals=thermals,
st_storages=st_storage,
properties=json_properties,
)

area_list.append(area_obj)

return area_list
14 changes: 7 additions & 7 deletions src/antares/service/base_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ def create_thermal_cluster(
"""
pass

@abstractmethod
def read_areas(self) -> List[Area]:
"""
Returns: Returns a list of areas
"""
pass

@abstractmethod
def create_thermal_cluster_with_matrices(
self,
Expand Down Expand Up @@ -471,13 +478,6 @@ def delete(self, children: bool) -> None:
"""
pass

@abstractmethod
def read_areas(self) -> List:
"""
Returns: Returns a list of areas
"""
pass


class BaseRenewableService(ABC):
@abstractmethod
Expand Down
3 changes: 3 additions & 0 deletions src/antares/service/local_services/area_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,6 @@ def upload_load_matrix(self, area: Area, load_matrix: pd.DataFrame) -> None:

def get_load_matrix(self, area: Area) -> pd.DataFrame:
raise NotImplementedError

def read_areas(self) -> List[Area]:
raise NotImplementedError
3 changes: 0 additions & 3 deletions src/antares/service/local_services/study_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,3 @@ def delete_binding_constraint(self, constraint: BindingConstraint) -> None:

def delete(self, children: bool) -> None:
raise NotImplementedError

def read_areas(self) -> List:
raise NotImplementedError
7 changes: 3 additions & 4 deletions tests/antares/services/api_services/test_study_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from antares.model.settings.general import GeneralParameters
from antares.model.settings.study_settings import StudySettings
from antares.model.study import Study, create_study_api
from antares.service.api_services.study_api import StudyApiService
from antares.service.service_factory import ServiceFactory


Expand All @@ -38,6 +39,7 @@ class TestCreateAPI:
study_id = "22c52f44-4c2a-407b-862b-490887f93dd8"
antares_web_description_msg = "Mocked Server KO"
study = Study("TestStudy", "880", ServiceFactory(api, study_id))
study_api = StudyApiService(api, study_id)
area = Area(
"area_test",
ServiceFactory(api, study_id).create_area_service(),
Expand Down Expand Up @@ -196,7 +198,4 @@ def test_create_binding_constraint_fails(self):
BindingConstraintCreationError,
match=f"Could not create the binding constraint {constraint_name}: {self.antares_web_description_msg}",
):
self.study.create_binding_constraint(name=constraint_name)

def test_read_areas(self):
pass
self.study.create_binding_constraint(name=constraint_name)

0 comments on commit b49627e

Please sign in to comment.