Skip to content

Commit

Permalink
Ruff formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
domna committed Dec 19, 2023
1 parent 43fe11f commit 1c4d0c3
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 161 deletions.
58 changes: 36 additions & 22 deletions pynxtools/dataconverter/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,14 @@ def get_nxdl_root_and_path(nxdl: str):
return ET.parse(nxdl_f_path).getroot(), nxdl_f_path


def transfer_data_into_template(input_file,
reader, nxdl_name,
nxdl_root: Optional[ET.Element] = None,
logger: logging.Logger = pynx_logger,
**kwargs):
def transfer_data_into_template(
input_file,
reader,
nxdl_name,
nxdl_root: Optional[ET.Element] = None,
logger: logging.Logger = pynx_logger,
**kwargs,
):
"""Transfer parse and merged data from input experimental file, config file and eln.
Experimental and eln files will be parsed and finally will be merged into template.
Expand Down Expand Up @@ -179,8 +182,10 @@ def transfer_data_into_template(input_file,
input_file = (input_file,)

bulletpoint = "\n\u2022 "
logger.info(f"Using {reader} reader reader to convert the given files:"
f" {bulletpoint.join((' ', *input_file))}")
logger.info(
f"Using {reader} reader reader to convert the given files:"
f" {bulletpoint.join((' ', *input_file))}"
)

data_reader = get_reader(reader)
if not (
Expand All @@ -198,15 +203,17 @@ def transfer_data_into_template(input_file,


# pylint: disable=too-many-arguments,too-many-locals,W1203
def convert(input_file: Tuple[str, ...],
reader: str,
nxdl: str,
output: str,
generate_template: bool = False,
fair: bool = False,
undocumented: bool = False,
logger: logging.Logger = pynx_logger,
**kwargs):
def convert(
input_file: Tuple[str, ...],
reader: str,
nxdl: str,
output: str,
generate_template: bool = False,
fair: bool = False,
undocumented: bool = False,
logger: logging.Logger = pynx_logger,
**kwargs,
):
"""The conversion routine that takes the input parameters and calls the necessary functions.
Parameters
Expand Down Expand Up @@ -241,9 +248,14 @@ def convert(input_file: Tuple[str, ...],
logger.info(template)
return

data = transfer_data_into_template(input_file=input_file, reader=reader,
nxdl_name=nxdl, nxdl_root=nxdl_root,
logger=logger, **kwargs)
data = transfer_data_into_template(
input_file=input_file,
reader=reader,
nxdl_name=nxdl,
nxdl_root=nxdl_root,
logger=logger,
**kwargs,
)

if fair and data.undocumented.keys():
logger.warning(
Expand All @@ -255,10 +267,12 @@ def convert(input_file: Tuple[str, ...],
if "/@default" in path:
continue
logger.info(
f"NO DOCUMENTATION: The path, {path}, is being written but has no documentation.")
f"NO DOCUMENTATION: The path, {path}, is being written but has no documentation."
)

helpers.add_default_root_attributes(data=data, filename=os.path.basename(output),
logger=logger)
helpers.add_default_root_attributes(
data=data, filename=os.path.basename(output), logger=logger
)
Writer(data=data, nxdl_f_path=nxdl_f_path, output_path=output).write()

logger.info(f"The output file generated: {output}.")
Expand Down
22 changes: 13 additions & 9 deletions pynxtools/dataconverter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,7 @@ def does_group_exist(path_to_group, data):


# pylint: disable=W1203
def ensure_all_required_fields_exist(template, data,
nxdl_root, logger=pynx_logger):
def ensure_all_required_fields_exist(template, data, nxdl_root, logger=pynx_logger):
"""Checks whether all the required fields are in the returned data object."""
for path in template["required"]:
entry_name = get_name_from_data_dict_entry(path[path.rindex("/") + 1 :])
Expand All @@ -502,16 +501,22 @@ def ensure_all_required_fields_exist(template, data,
if path in template["lone_groups"]:
opt_parent = check_for_optional_parent(path, nxdl_root)
if opt_parent != "<<NOT_FOUND>>":
if does_group_exist(opt_parent, data) and not does_group_exist(renamed_path, data):
logger.warning(f"The required group, {path}, hasn't been supplied"
f" while its optional parent, {opt_parent}, is supplied.")
if does_group_exist(opt_parent, data) and not does_group_exist(
renamed_path, data
):
logger.warning(
f"The required group, {path}, hasn't been supplied"
f" while its optional parent, {opt_parent}, is supplied."
)
continue
if not does_group_exist(renamed_path, data):
raise ValueError(f"The required group, {path}, hasn't been supplied.")
continue
if not is_path_in_data_dict or data[renamed_path] is None:
logger.warning(f"The data entry corresponding to {path} is required "
f"and hasn't been supplied by the reader.",)
logger.warning(
f"The data entry corresponding to {path} is required "
f"and hasn't been supplied by the reader.",
)


def try_undocumented(data, nxdl_root: ET.Element):
Expand Down Expand Up @@ -540,8 +545,7 @@ def try_undocumented(data, nxdl_root: ET.Element):
pass


def validate_data_dict(template, data,
nxdl_root: ET.Element, logger=pynx_logger):
def validate_data_dict(template, data, nxdl_root: ET.Element, logger=pynx_logger):
"""Checks whether all the required paths from the template are returned in data dict."""
assert nxdl_root is not None, "The NXDL file hasn't been loaded."

Expand Down
Loading

0 comments on commit 1c4d0c3

Please sign in to comment.