Skip to content

Commit

Permalink
ruff linter
Browse files Browse the repository at this point in the history
  • Loading branch information
killian-scalian committed Sep 24, 2024
1 parent f08aac1 commit 2c33a22
Show file tree
Hide file tree
Showing 64 changed files with 10,341 additions and 10,419 deletions.
130 changes: 65 additions & 65 deletions src/antares/api_conf/api_conf.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

import requests

from antares.config.base_configuration import BaseConfiguration
from antares.exceptions.exceptions import MissingTokenError


class APIconf(BaseConfiguration):
"""
APIconf defines host and token to be used for API mode
"""

def __init__(self, api_host: str, token: str, verify: bool = True) -> None:
self._api_host: str = api_host
self._token: str = token
self._verify: bool = verify

@property
def token(self) -> str:
return self._token

@token.setter
def token(self, value: str) -> None:
self._token = value

@property
def verify(self) -> bool:
return self._verify

@property
def api_host(self) -> str:
return self._api_host

@api_host.setter
def api_host(self, value: str) -> None:
self._api_host = value

def get_host(self) -> str:
return self._api_host

def get_token(self) -> str:
return self._token

def checks_token(self) -> None:
if self._api_host not in ["localhost", "127.0.0.1"] and self._token is None:
raise MissingTokenError()

def set_up_api_conf(self) -> requests.Session:
self.checks_token()
session = requests.Session()
if self._token:
token_bearer = f"Bearer {self._token}"
session.headers.update({"Authorization": token_bearer})
return session
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

import requests

from antares.config.base_configuration import BaseConfiguration
from antares.exceptions.exceptions import MissingTokenError


class APIconf(BaseConfiguration):
"""
APIconf defines host and token to be used for API mode
"""

def __init__(self, api_host: str, token: str, verify: bool = True) -> None:
self._api_host: str = api_host
self._token: str = token
self._verify: bool = verify

@property
def token(self) -> str:
return self._token

@token.setter
def token(self, value: str) -> None:
self._token = value

@property
def verify(self) -> bool:
return self._verify

@property
def api_host(self) -> str:
return self._api_host

@api_host.setter
def api_host(self, value: str) -> None:
self._api_host = value

def get_host(self) -> str:
return self._api_host

def get_token(self) -> str:
return self._token

def checks_token(self) -> None:
if self._api_host not in ["localhost", "127.0.0.1"] and self._token is None:
raise MissingTokenError()

def set_up_api_conf(self) -> requests.Session:
self.checks_token()
session = requests.Session()
if self._token:
token_bearer = f"Bearer {self._token}"
session.headers.update({"Authorization": token_bearer})
return session
138 changes: 64 additions & 74 deletions src/antares/api_conf/request_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,64 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

import json
from typing import IO, Any, Iterable, Mapping, Optional, Tuple, Union

import requests

from antares.exceptions.exceptions import APIError

DATA_TYPE = Union[
str, bytes, Mapping[str, Any], Iterable[Tuple[str, Optional[str]]], IO
]


def _handle_exceptions(response: requests.Response) -> requests.Response:
"""
If an exception occurred, returns APIError exception containing the AntaresWeb error message.
"""
if response.status_code - 200 < 100:
return response
try:
msg = response.json()["description"]
except (json.decoder.JSONDecodeError, KeyError):
msg = response.reason
raise APIError(msg)


class RequestWrapper:
"""
A wrapper around the requests library
"""

def __init__(self, session: requests.Session):
self.session = session

def get(self, url: str, **kwargs: Any) -> requests.Response:
response = self.session.get(url, **kwargs)
return _handle_exceptions(response)

def post(
self,
url: str,
data: Optional[DATA_TYPE] = None,
json: Optional[Any] = None,
**kwargs: Any
) -> requests.Response:
response = self.session.post(url, data, json, **kwargs)
return _handle_exceptions(response)

def put(
self, url: str, data: Optional[DATA_TYPE] = None, **kwargs: Any
) -> requests.Response:
response = self.session.put(url, data, **kwargs)
return _handle_exceptions(response)

def patch(
self, url: str, data: Optional[DATA_TYPE] = None, **kwargs: Any
) -> requests.Response:
response = self.session.patch(url, data, **kwargs)
return _handle_exceptions(response)

def delete(self, url: str, **kwargs: Any) -> requests.Response:
response = self.session.delete(url, **kwargs)
return _handle_exceptions(response)
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

import json
from typing import IO, Any, Iterable, Mapping, Optional, Tuple, Union

import requests

from antares.exceptions.exceptions import APIError

DATA_TYPE = Union[str, bytes, Mapping[str, Any], Iterable[Tuple[str, Optional[str]]], IO]


def _handle_exceptions(response: requests.Response) -> requests.Response:
"""
If an exception occurred, returns APIError exception containing the AntaresWeb error message.
"""
if response.status_code - 200 < 100:
return response
try:
msg = response.json()["description"]
except (json.decoder.JSONDecodeError, KeyError):
msg = response.reason
raise APIError(msg)


class RequestWrapper:
"""
A wrapper around the requests library
"""

def __init__(self, session: requests.Session):
self.session = session

def get(self, url: str, **kwargs: Any) -> requests.Response:
response = self.session.get(url, **kwargs)
return _handle_exceptions(response)

def post(
self, url: str, data: Optional[DATA_TYPE] = None, json: Optional[Any] = None, **kwargs: Any
) -> requests.Response:
response = self.session.post(url, data, json, **kwargs)
return _handle_exceptions(response)

def put(self, url: str, data: Optional[DATA_TYPE] = None, **kwargs: Any) -> requests.Response:
response = self.session.put(url, data, **kwargs)
return _handle_exceptions(response)

def patch(self, url: str, data: Optional[DATA_TYPE] = None, **kwargs: Any) -> requests.Response:
response = self.session.patch(url, data, **kwargs)
return _handle_exceptions(response)

def delete(self, url: str, **kwargs: Any) -> requests.Response:
response = self.session.delete(url, **kwargs)
return _handle_exceptions(response)
34 changes: 17 additions & 17 deletions src/antares/config/base_configuration.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

from abc import ABC


class BaseConfiguration(ABC):
pass
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

from abc import ABC


class BaseConfiguration(ABC):
pass
58 changes: 29 additions & 29 deletions src/antares/config/local_configuration.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

from pathlib import Path

from antares.config.base_configuration import BaseConfiguration


class LocalConfiguration(BaseConfiguration):
def __init__(self, local_path: Path, study_name: str):
self._local_path = local_path
self._study_name = study_name

@property
def local_path(self) -> Path:
return self._local_path

@property
def study_path(self) -> Path:
return self._local_path / self._study_name
# Copyright (c) 2024, RTE (https://www.rte-france.com)
#
# See AUTHORS.txt
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.

from pathlib import Path

from antares.config.base_configuration import BaseConfiguration


class LocalConfiguration(BaseConfiguration):
def __init__(self, local_path: Path, study_name: str):
self._local_path = local_path
self._study_name = study_name

@property
def local_path(self) -> Path:
return self._local_path

@property
def study_path(self) -> Path:
return self._local_path / self._study_name
Loading

0 comments on commit 2c33a22

Please sign in to comment.