Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
christophe-david committed Jan 14, 2022
1 parent 0e7c87e commit 3feba43
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 39 deletions.
17 changes: 11 additions & 6 deletions src/fastoad/_utils/resource_management/contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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))
Expand All @@ -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
Expand Down
46 changes: 24 additions & 22 deletions src/fastoad/cmd/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
)

Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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,
)

Expand Down Expand Up @@ -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,
)

Expand All @@ -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)

Expand Down
2 changes: 0 additions & 2 deletions src/fastoad/cmd/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import os.path as pth

import click
import pandas as pd
import tabulate

import fastoad
Expand All @@ -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,
Expand Down
19 changes: 10 additions & 9 deletions src/fastoad/module_management/_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@


class SubPackageNames(Enum):
"""Enumeration of possible plugin subpackages."""

MODELS = "models"
NOTEBOOKS = "notebooks"
CONFIGURATIONS = "configurations"
Expand Down Expand Up @@ -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],
)
)
Expand All @@ -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),
)


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 3feba43

Please sign in to comment.