Skip to content

Commit

Permalink
style: reformat source code using iSort and Black
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent-laporte-pro committed Sep 22, 2023
1 parent fbb60e0 commit e243fba
Show file tree
Hide file tree
Showing 51 changed files with 205 additions and 588 deletions.
8 changes: 2 additions & 6 deletions antareslauncher/antares_launcher.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
from dataclasses import dataclass
from typing import Optional

from antareslauncher.use_cases.check_remote_queue.check_queue_controller import (
CheckQueueController,
)
from antareslauncher.use_cases.check_remote_queue.check_queue_controller import CheckQueueController
from antareslauncher.use_cases.create_list.study_list_composer import StudyListComposer
from antareslauncher.use_cases.kill_job.job_kill_controller import JobKillController
from antareslauncher.use_cases.launch.launch_controller import LaunchController
from antareslauncher.use_cases.retrieve.retrieve_controller import RetrieveController
from antareslauncher.use_cases.wait_loop_controller.wait_controller import (
WaitController,
)
from antareslauncher.use_cases.wait_loop_controller.wait_controller import WaitController


@dataclass
Expand Down
28 changes: 6 additions & 22 deletions antareslauncher/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
import yaml

from antareslauncher import __author__, __project_name__, __version__
from antareslauncher.exceptions import (
ConfigFileNotFoundError,
InvalidConfigValueError,
UnknownFileSuffixError,
)
from antareslauncher.exceptions import ConfigFileNotFoundError, InvalidConfigValueError, UnknownFileSuffixError

APP_NAME = __project_name__
APP_AUTHOR = __author__.split(",")[0]
Expand Down Expand Up @@ -119,9 +115,7 @@ def load_config(cls, ssh_config_path: pathlib.Path) -> "SSHConfig":
obj = parse_config(ssh_config_path)
kwargs = {k.lower(): v for k, v in obj.items()}
private_key_file = kwargs.pop("private_key_file", None)
kwargs["private_key_file"] = (
None if private_key_file is None else pathlib.Path(private_key_file)
)
kwargs["private_key_file"] = None if private_key_file is None else pathlib.Path(private_key_file)
try:
return cls(config_path=ssh_config_path, **kwargs)
except TypeError as exc:
Expand All @@ -139,11 +133,7 @@ def save_config(self, ssh_config_path: pathlib.Path) -> None:
"""
obj = dataclasses.asdict(self)
del obj["config_path"]
obj = {
k: v
for k, v in obj.items()
if v or k not in {"private_key_file", "key_password", "password"}
}
obj = {k: v for k, v in obj.items() if v or k not in {"private_key_file", "key_password", "password"}}
if "private_key_file" in obj:
obj["private_key_file"] = obj["private_key_file"].as_posix()
dump_config(ssh_config_path, obj)
Expand Down Expand Up @@ -212,9 +202,7 @@ def load_config(cls, config_path: pathlib.Path) -> "Config":
obj = parse_config(config_path)
kwargs = {k.lower(): v for k, v in obj.items()}
try:
kwargs["remote_solver_versions"] = kwargs.pop(
"antares_versions_on_remote_server"
)
kwargs["remote_solver_versions"] = kwargs.pop("antares_versions_on_remote_server")
# handle paths
for key in [
"log_dir",
Expand All @@ -226,9 +214,7 @@ def load_config(cls, config_path: pathlib.Path) -> "Config":
kwargs[key] = pathlib.Path(kwargs[key])
ssh_configfile_name = kwargs.pop("default_ssh_configfile_name")
except KeyError as exc:
raise InvalidConfigValueError(
config_path, f"missing parameter '{exc}'"
) from None
raise InvalidConfigValueError(config_path, f"missing parameter '{exc}'") from None
# handle SSH configuration
config_dir = config_path.parent
ssh_config_path = config_dir.joinpath(ssh_configfile_name)
Expand Down Expand Up @@ -287,9 +273,7 @@ def get_user_config_dir(system: str = ""):
username = getpass.getuser()
system = system or sys.platform
if system == "win32":
config_dir = pathlib.WindowsPath(
rf"C:\Users\{username}\AppData\Local\{APP_AUTHOR}"
)
config_dir = pathlib.WindowsPath(rf"C:\Users\{username}\AppData\Local\{APP_AUTHOR}")
elif system == "darwin":
config_dir = pathlib.PosixPath("~/Library/Preferences").expanduser()
else:
Expand Down
4 changes: 1 addition & 3 deletions antareslauncher/display/display_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ def generate_progress_bar(
desc=desc,
leave=False,
dynamic_ncols=True,
bar_format="["
+ str(now.strftime(self.format))
+ "] {l_bar}{bar}| {n_fmt}/{total_fmt} ",
bar_format="[" + str(now.strftime(self.format)) + "] {l_bar}{bar}| {n_fmt}/{total_fmt} ",
)
return progress_bar
4 changes: 1 addition & 3 deletions antareslauncher/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ class AntaresLauncherException(Exception):
class ConfigFileNotFoundError(AntaresLauncherException):
"""Configuration file not found."""

def __init__(
self, possible_dirs: Sequence[pathlib.Path], config_name: str, *args
) -> None:
def __init__(self, possible_dirs: Sequence[pathlib.Path], config_name: str, *args) -> None:
super().__init__(possible_dirs, config_name, *args)

@property
Expand Down
28 changes: 7 additions & 21 deletions antareslauncher/file_manager/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ def _get_list_of_files_recursively(self, element_path):
Returns:
List of all the files inside a directory recursively
"""
self.logger.info(
f"Getting list of all files inside the directory {element_path}"
)
self.logger.info(f"Getting list of all files inside the directory {element_path}")
element_file_paths = []
for root, _, files in os.walk(element_path):
for filename in files:
Expand All @@ -71,9 +69,7 @@ def _get_complete_list_of_files_and_dirs_in_list_dir(self, dir_path, list_dir):
file_paths.extend(element_file_paths)
return file_paths

def zip_file_paths_with_rootdir_to_zipfile_path(
self, zipfile_path, file_paths, root_dir
):
def zip_file_paths_with_rootdir_to_zipfile_path(self, zipfile_path, file_paths, root_dir):
"""Zips all the files in file_paths inside zipfile_path
while printing a progress bar on the terminal
Expand All @@ -85,12 +81,8 @@ def zip_file_paths_with_rootdir_to_zipfile_path(
root_dir: Root directory
"""
self.logger.info(f"Zipping list of files to archive {zipfile_path}")
with zipfile.ZipFile(
zipfile_path, "w", compression=zipfile.ZIP_DEFLATED
) as my_zip:
loading_bar = self.display.generate_progress_bar(
file_paths, desc="Compressing files: "
)
with zipfile.ZipFile(zipfile_path, "w", compression=zipfile.ZIP_DEFLATED) as my_zip:
loading_bar = self.display.generate_progress_bar(file_paths, desc="Compressing files: ")
for f in loading_bar:
my_zip.write(f, os.path.relpath(f, root_dir))

Expand All @@ -105,13 +97,9 @@ def zip_dir_excluding_subdir(self, dir_path, zipfile_path, subdir_to_exclude):
subdir_to_exclude: Subdirectory that will not be zipped
"""
list_dir = self._get_list_dir_without_subdir(dir_path, subdir_to_exclude)
file_paths = self._get_complete_list_of_files_and_dirs_in_list_dir(
dir_path, list_dir
)
file_paths = self._get_complete_list_of_files_and_dirs_in_list_dir(dir_path, list_dir)
root_dir = str(Path(dir_path).parent)
self.zip_file_paths_with_rootdir_to_zipfile_path(
zipfile_path, file_paths, root_dir
)
self.zip_file_paths_with_rootdir_to_zipfile_path(zipfile_path, file_paths, root_dir)
return Path(zipfile_path).is_file()

def make_dir(self, directory_name):
Expand All @@ -124,9 +112,7 @@ def convert_json_file_to_dict(self, file_path):
with open(file_path, "r") as readFile:
config = json.load(readFile)
except OSError:
self.logger.error(
f"Unable to convert {file_path} to json (file not found or invalid type)"
)
self.logger.error(f"Unable to convert {file_path} to json (file not found or invalid type)")
config = None
return config

Expand Down
8 changes: 2 additions & 6 deletions antareslauncher/logger_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ def init_logger(self):
Returns:
"""
formatter = logging.Formatter(
"%(asctime)s - %(levelname)s - %(name)s - %(message)s"
)
f_handler = RotatingFileHandler(
self.file_path, maxBytes=200000, backupCount=5, mode="a+"
)
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(name)s - %(message)s")
f_handler = RotatingFileHandler(self.file_path, maxBytes=200000, backupCount=5, mode="a+")
f_handler.setFormatter(formatter)
f_handler.setLevel(logging.DEBUG)
logging.basicConfig(level=logging.INFO, handlers=[f_handler])
37 changes: 9 additions & 28 deletions antareslauncher/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,17 @@
from antareslauncher.file_manager.file_manager import FileManager
from antareslauncher.logger_initializer import LoggerInitializer
from antareslauncher.remote_environnement import ssh_connection
from antareslauncher.remote_environnement.remote_environment_with_slurm import (
RemoteEnvironmentWithSlurm,
)
from antareslauncher.remote_environnement.slurm_script_features import (
SlurmScriptFeatures,
)
from antareslauncher.use_cases.check_remote_queue.check_queue_controller import (
CheckQueueController,
)
from antareslauncher.remote_environnement.remote_environment_with_slurm import RemoteEnvironmentWithSlurm
from antareslauncher.remote_environnement.slurm_script_features import SlurmScriptFeatures
from antareslauncher.use_cases.check_remote_queue.check_queue_controller import CheckQueueController
from antareslauncher.use_cases.check_remote_queue.slurm_queue_show import SlurmQueueShow
from antareslauncher.use_cases.create_list.study_list_composer import (
StudyListComposer,
StudyListComposerParameters,
)
from antareslauncher.use_cases.generate_tree_structure.tree_structure_initializer import (
TreeStructureInitializer,
)
from antareslauncher.use_cases.create_list.study_list_composer import StudyListComposer, StudyListComposerParameters
from antareslauncher.use_cases.generate_tree_structure.tree_structure_initializer import TreeStructureInitializer
from antareslauncher.use_cases.kill_job.job_kill_controller import JobKillController
from antareslauncher.use_cases.launch.launch_controller import LaunchController
from antareslauncher.use_cases.retrieve.retrieve_controller import RetrieveController
from antareslauncher.use_cases.retrieve.state_updater import StateUpdater
from antareslauncher.use_cases.wait_loop_controller.wait_controller import (
WaitController,
)
from antareslauncher.use_cases.wait_loop_controller.wait_controller import WaitController


class NoJsonConfigFileError(Exception):
Expand Down Expand Up @@ -88,9 +75,7 @@ class MainParameters:
quality_of_service: str = ""


def run_with(
arguments: argparse.Namespace, parameters: MainParameters, show_banner=False
):
def run_with(arguments: argparse.Namespace, parameters: MainParameters, show_banner=False):
"""Instantiates all the objects necessary to antares-launcher, and runs the program"""
if arguments.version:
print(f"Antares_Launcher v{__version__}")
Expand All @@ -113,9 +98,7 @@ def run_with(
)

tree_structure_initializer.init_tree_structure()
logger_initializer = LoggerInitializer(
str(Path(arguments.log_dir) / "antares_launcher.log")
)
logger_initializer = LoggerInitializer(str(Path(arguments.log_dir) / "antares_launcher.log"))
logger_initializer.init_logger()

# connection
Expand All @@ -133,9 +116,7 @@ def run_with(
quality_of_service=parameters.quality_of_service,
)
environment = RemoteEnvironmentWithSlurm(connection, slurm_script_features)
data_repo = DataRepoTinydb(
database_file_path=db_json_file_path, db_primary_key=parameters.db_primary_key
)
data_repo = DataRepoTinydb(database_file_path=db_json_file_path, db_primary_key=parameters.db_primary_key)
study_list_composer = StudyListComposer(
repo=data_repo,
display=display,
Expand Down
14 changes: 3 additions & 11 deletions antareslauncher/main_option_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ def __init__(self, parameters: ParserParameters) -> None:
def parse_args(self, args: t.Union[t.Sequence[str], None]) -> argparse.Namespace:
return self.parser.parse_args(args)

def add_basic_arguments(
self, *, antares_versions: t.Sequence[str] = ()
) -> MainOptionParser:
def add_basic_arguments(self, *, antares_versions: t.Sequence[str] = ()) -> MainOptionParser:
"""Adds to the parser all the arguments for the light mode"""
self.parser.add_argument(
"-w",
Expand Down Expand Up @@ -234,15 +232,9 @@ def look_for_default_ssh_conf_file(
Returns:
path to the ssh config file is it exists, None otherwise
"""
if (
parameters.ssh_configfile_path_alternate1
and parameters.ssh_configfile_path_alternate1.is_file()
):
if parameters.ssh_configfile_path_alternate1 and parameters.ssh_configfile_path_alternate1.is_file():
return parameters.ssh_configfile_path_alternate1
elif (
parameters.ssh_configfile_path_alternate2
and parameters.ssh_configfile_path_alternate2.is_file()
):
elif parameters.ssh_configfile_path_alternate2 and parameters.ssh_configfile_path_alternate2.is_file():
return parameters.ssh_configfile_path_alternate2
else:
return None
Expand Down
4 changes: 1 addition & 3 deletions antareslauncher/parameters_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,5 @@ def _get_ssh_dict_from_json(self) -> t.Dict[str, t.Any]:
with open(self.json_ssh_conf) as ssh_connection_json:
ssh_dict = json.load(ssh_connection_json)
if "private_key_file" in ssh_dict:
ssh_dict["private_key_file"] = os.path.expanduser(
ssh_dict["private_key_file"]
)
ssh_dict["private_key_file"] = os.path.expanduser(ssh_dict["private_key_file"])
return ssh_dict
Loading

0 comments on commit e243fba

Please sign in to comment.