From 3feba439929d45d748b2a1907d6ce0deb633c8e6 Mon Sep 17 00:00:00 2001 From: Christophe DAVID Date: Fri, 14 Jan 2022 15:11:54 +0100 Subject: [PATCH] Linting --- .../_utils/resource_management/contents.py | 17 ++++--- src/fastoad/cmd/api.py | 46 ++++++++++--------- src/fastoad/cmd/cli.py | 2 - src/fastoad/module_management/_plugins.py | 19 ++++---- 4 files changed, 45 insertions(+), 39 deletions(-) diff --git a/src/fastoad/_utils/resource_management/contents.py b/src/fastoad/_utils/resource_management/contents.py index 9dd36ff41..4264d0f06 100644 --- a/src/fastoad/_utils/resource_management/contents.py +++ b/src/fastoad/_utils/resource_management/contents.py @@ -21,13 +21,9 @@ class PackageReader: Wrapper of `importlib.resources.contents` which handles when `package_name` is not a package or does not exist. - :param package_name: - :return: An iterator if package exists, an empty SET {} if it does not exist, and None - if it is not a package + :param package_name: Name of package to inspect. """ - package_name: str - def __init__(self, package_name: str): """ :param package_name: @@ -36,7 +32,16 @@ def __init__(self, package_name: str): self.is_module = False self.has_error = False self._contents = [] + self.package_name = package_name + + @property + def package_name(self): + """Name of package to inspect.""" + return self._package_name + @package_name.setter + def package_name(self, package_name): + self._package_name = package_name if package_name: try: self._contents = list(contents(package_name)) @@ -45,7 +50,7 @@ def __init__(self, package_name: str): self.is_module = True except ModuleNotFoundError: pass - except Exception: + except Exception: # pylint: disable = W0703 # Here we catch any Python error that may happen when reading the loaded code. # Thus, we ensure to not break the application if a module is incorrectly written. self.has_error = True diff --git a/src/fastoad/cmd/api.py b/src/fastoad/cmd/api.py index 6ab5804a8..625e447a6 100644 --- a/src/fastoad/cmd/api.py +++ b/src/fastoad/cmd/api.py @@ -172,7 +172,7 @@ def generate_configuration_file( # Write file make_parent_dir(configuration_file_path) copy_resource(file_info.package_name, file_info.name, configuration_file_path) - _LOGGER.info('Sample configuration written in "%s".' % configuration_file_path) + _LOGGER.info('Sample configuration written in "%s".', configuration_file_path) return configuration_file_path @@ -199,8 +199,8 @@ def generate_inputs( input_file_path = conf.input_file_path if not overwrite and pth.exists(conf.input_file_path): raise FastPathExistsError( - "Input file %s not written because it already exists. " - "Use overwrite=True to bypass." % input_file_path, + f"Input file {input_file_path} not written because it already exists. " + "Use overwrite=True to bypass.", input_file_path, ) @@ -270,12 +270,12 @@ def list_variables( out = pth.abspath(out) if not overwrite and pth.exists(out): raise FastPathExistsError( - "File %s not written because it already exists. " - "Use overwrite=True to bypass." % out, + f"File {out} not written because it already exists. " + "Use overwrite=True to bypass.", out, ) make_parent_dir(out) - out_file = open(out, "w") + out_file = open(out, "w", encoding="utf-8") else: if out == sys.stdout and InteractiveShell.initialized() and not force_text_output: display(HTML(variables_df.to_html(index=False))) @@ -357,7 +357,7 @@ def list_modules( elif pth.isdir(source_path): RegisterOpenMDAOSystem.explore_folder(source_path) else: - raise FileNotFoundError("Could not find %s" % source_path) + raise FileNotFoundError(f"Could not find {source_path}") elif isinstance(source_path, Iterable): for folder_path in source_path: if not pth.isdir(folder_path): @@ -376,13 +376,13 @@ def list_modules( out = pth.abspath(out) if not overwrite and pth.exists(out): raise FastPathExistsError( - "File %s not written because it already exists. " - "Use overwrite=True to bypass." % out, + f"File {out} not written because it already exists. " + "Use overwrite=True to bypass.", out, ) make_parent_dir(out) - out_file = open(out, "w") + out_file = open(out, "w", encoding="utf-8") else: if ( out == sys.stdout @@ -435,8 +435,10 @@ def _get_detailed_system_list(): component.options.undeclare("distributed") cell_content = ( - " IDENTIFIER: %s\nPATH: %s\nDOMAIN: %s\nDESCRIPTION: %s\n" - % (identifier, path, domain.value, tw.indent(tw.dedent(description), " ")) + f"IDENTIFIER: {identifier}\n" + f"PATH: {path}\n" + f"DOMAIN: {domain.value}\n" + f"DESCRIPTION: {tw.indent(tw.dedent(description), ' ')}\n" ) if len(list(component.options.items())) > 0: cell_content += component.options.to_table(fmt="grid") + "\n" @@ -451,10 +453,10 @@ def _get_detailed_system_list(): if description is None: description = "" - cell_content = " IDENTIFIER: %s\nPATH: %s\nDESCRIPTION: %s\n" % ( - identifier, - path, - tw.indent(tw.dedent(description), " "), + cell_content = ( + f"IDENTIFIER: {identifier}\n" + f"PATH: {path}\n" + f"DESCRIPTION: {tw.indent(tw.dedent(description), ' ')}\n" ) cell_list.append([cell_content]) return cell_list @@ -477,8 +479,8 @@ def write_n2(configuration_file_path: str, n2_file_path: str = None, overwrite: if not overwrite and pth.exists(n2_file_path): raise FastPathExistsError( - "N2-diagram file %s not written because it already exists. " - "Use overwrite=True to bypass." % n2_file_path, + f"N2-diagram file {n2_file_path} not written because it already exists. " + "Use overwrite=True to bypass.", n2_file_path, ) @@ -564,8 +566,8 @@ def _run_problem( outputs_path = pth.normpath(problem.output_file_path) if not overwrite and pth.exists(outputs_path): raise FastPathExistsError( - "Problem not run because output file %s already exists. " - "Use overwrite=True to bypass." % outputs_path, + f"Problem not run because output file {outputs_path} already exists. " + "Use overwrite=True to bypass.", outputs_path, ) @@ -581,9 +583,9 @@ def _run_problem( problem.write_outputs() if problem.optim_failed: - _LOGGER.error("Optimization failed after " + str(computation_time) + " seconds") + _LOGGER.error("Optimization failed after %s seconds", computation_time) else: - _LOGGER.info("Computation finished after " + str(computation_time) + " seconds") + _LOGGER.info("Computation finished after %s seconds", computation_time) _LOGGER.info("Problem outputs written in %s", outputs_path) diff --git a/src/fastoad/cmd/cli.py b/src/fastoad/cmd/cli.py index e2fb951bc..0c074d141 100644 --- a/src/fastoad/cmd/cli.py +++ b/src/fastoad/cmd/cli.py @@ -15,7 +15,6 @@ import os.path as pth import click -import pandas as pd import tabulate import fastoad @@ -25,7 +24,6 @@ overwrite_option, ) from fastoad.cmd.exceptions import FastNoAvailableNotebookError -from fastoad.module_management._plugins import FastoadLoader from fastoad.module_management.exceptions import ( FastNoDistPluginError, FastSeveralConfigurationFilesError, diff --git a/src/fastoad/module_management/_plugins.py b/src/fastoad/module_management/_plugins.py index 3bf8d1e16..0bae9973b 100644 --- a/src/fastoad/module_management/_plugins.py +++ b/src/fastoad/module_management/_plugins.py @@ -46,6 +46,8 @@ class SubPackageNames(Enum): + """Enumeration of possible plugin subpackages.""" + MODELS = "models" NOTEBOOKS = "notebooks" CONFIGURATIONS = "configurations" @@ -193,14 +195,14 @@ def get_notebook_folder_list(self, plugin_name=None) -> List[ResourceInfo]: plugin_names = self.keys() info = [] - for plugin_name in plugin_names: - plugin_def = self[plugin_name] + for name in plugin_names: + plugin_def = self[name] if SubPackageNames.NOTEBOOKS in plugin_def.subpackages: info.append( ResourceInfo( name=SubPackageNames.NOTEBOOKS.value, dist_name=self.dist_name, - plugin_name=plugin_name, + plugin_name=name, package_name=plugin_def.subpackages[SubPackageNames.NOTEBOOKS], ) ) @@ -221,7 +223,7 @@ def to_dict(self) -> Dict: installed_package=self.dist_name, has_models=has_models, has_notebooks=has_notebooks, - configurations=conf_files, + configurations=sorted(conf_files), ) @@ -271,8 +273,7 @@ def get_distribution_plugin_definition( if dist_name is None: if len(self._dist_plugin_definitions) > 1: raise FastSeveralDistPluginsError() - else: - return next(iter(self._dist_plugin_definitions.values())) + return next(iter(self._dist_plugin_definitions.values())) if dist_name not in self._dist_plugin_definitions: raise FastUnknownDistPluginError(dist_name) @@ -315,8 +316,8 @@ def _get_resource_list(self, method: Callable, dist_name: str = None) -> List[Re raise FastNoDistPluginError() dist_names = self._dist_plugin_definitions.keys() - for dist_name in dist_names: - dist_plugin_definitions = self._dist_plugin_definitions[dist_name] + for name in dist_names: + dist_plugin_definitions = self._dist_plugin_definitions[name] infos += method(dist_plugin_definitions) return infos @@ -341,7 +342,7 @@ def load(cls): """ Loads declared plugins. """ - for plugin_dist, dist_plugin_definitions in cls._dist_plugin_definitions.items(): + for dist_plugin_definitions in cls._dist_plugin_definitions.values(): for plugin_name, plugin_def in dist_plugin_definitions.items(): _LOGGER.info("Loading FAST-OAD plugin %s", plugin_name) cls._load_models(plugin_def)