Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set logger levels through handler #209

Merged
merged 5 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions pynxtools/dataconverter/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
from pynxtools.dataconverter.writer import Writer
from pynxtools.dataconverter.template import Template
from pynxtools.nexus import nexus
from pynxtools.dataconverter.logger import logger as pynx_logger

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

if sys.version_info >= (3, 10):
from importlib.metadata import entry_points
Expand Down Expand Up @@ -145,7 +147,6 @@ def transfer_data_into_template(
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.
Expand All @@ -163,8 +164,6 @@ def transfer_data_into_template(
Root name of nxdl file, e.g. NXmpes from NXmpes.nxdl.xml
nxdl_root : ET.element
Root element of nxdl file, otherwise provide nxdl_name
logger: looging.Logger
Logger to get log massages.

Returns
-------
Expand All @@ -183,7 +182,7 @@ def transfer_data_into_template(

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

Expand All @@ -198,7 +197,7 @@ def transfer_data_into_template(
data = data_reader().read( # type: ignore[operator]
template=Template(template), file_paths=input_file, **kwargs
)
helpers.validate_data_dict(template, data, nxdl_root, logger=logger)
helpers.validate_data_dict(template, data, nxdl_root)
return data


Expand All @@ -211,7 +210,6 @@ def convert(
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.
Expand All @@ -233,8 +231,6 @@ def convert(
in the template.
undocumented : bool, default False
If True, an undocumented warning is given.
logger: looging.Logger
Logger to get log massages.

Returns
-------
Expand All @@ -245,15 +241,14 @@ def convert(
if generate_template:
template = Template()
helpers.generate_template_from_nxdl(nxdl_root, template)
logger.info(template)
print(template)
return

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

Expand All @@ -270,9 +265,7 @@ def convert(
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))
Writer(data=data, nxdl_f_path=nxdl_f_path, output_path=output).write()

logger.info(f"The output file generated: {output}.")
Expand Down Expand Up @@ -351,6 +344,7 @@ def convert_cli(
mapping: str,
):
"""The CLI entrypoint for the convert function"""
logger.addHandler(logging.StreamHandler())
domna marked this conversation as resolved.
Show resolved Hide resolved
if params_file:
try:
convert(**parse_params_file(params_file))
Expand Down
13 changes: 8 additions & 5 deletions pynxtools/dataconverter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import json
import re
import logging
import xml.etree.ElementTree as ET
from datetime import datetime, timezone
from typing import Any, Callable, List, Optional, Tuple, Union
Expand All @@ -28,10 +29,12 @@
from ase.data import chemical_symbols

from pynxtools import get_nexus_version, get_nexus_version_hash
from pynxtools.dataconverter.logger import logger as pynx_logger
from pynxtools.nexus import nexus
from pynxtools.nexus.nexus import NxdlAttributeError

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


def is_a_lone_group(xml_element) -> bool:
"""Checks whether a given group XML element has no field or attributes mentioned"""
Expand Down Expand Up @@ -487,7 +490,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):
"""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 Down Expand Up @@ -544,7 +547,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):
"""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 All @@ -553,7 +556,7 @@ def validate_data_dict(template, data, nxdl_root: ET.Element, logger=pynx_logger
nxdl_path_to_elm: dict = {}

# Make sure all required fields exist.
ensure_all_required_fields_exist(template, data, nxdl_root, logger)
ensure_all_required_fields_exist(template, data, nxdl_root)
try_undocumented(data, nxdl_root)

for path in data.get_documented().keys():
Expand Down Expand Up @@ -650,7 +653,7 @@ def convert_to_hill(atoms_typ):
return atom_list + list(atoms_typ)


def add_default_root_attributes(data, filename, logger=pynx_logger):
def add_default_root_attributes(data, filename):
"""
Takes a dict/Template and adds NXroot fields/attributes that are inherently available
"""
Expand Down
25 changes: 0 additions & 25 deletions pynxtools/dataconverter/logger.py

This file was deleted.

1 change: 0 additions & 1 deletion pynxtools/dataconverter/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

logger = logging.getLogger(__name__) # pylint: disable=C0103
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler(sys.stdout))


def does_path_exist(path, h5py_obj) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion tests/dataconverter/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_cli(caplog, cli_inputs):
result = runner.invoke(dataconverter.convert_cli, cli_inputs)
if "--generate-template" in cli_inputs:
assert result.exit_code == 0
assert '"/ENTRY[entry]/NXODD_name/int_value": "None",' in caplog.text
assert '"/ENTRY[entry]/NXODD_name/int_value": "None",' in result.stdout
elif "--input-file" in cli_inputs:
assert "test_input" in caplog.text
elif result.exit_code == 2:
Expand Down
7 changes: 3 additions & 4 deletions tests/dataconverter/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from setuptools import distutils

from pynxtools.dataconverter import helpers
from pynxtools.dataconverter.logger import logger as pynx_logger
from pynxtools.dataconverter.template import Template


Expand Down Expand Up @@ -394,7 +393,7 @@ def test_validate_data_dict(
"link-dict-instead-of-bool",
"opt-group-completely-removed",
):
helpers.validate_data_dict(template, data_dict, nxdl_root, logger=pynx_logger)
helpers.validate_data_dict(template, data_dict, nxdl_root)
# Missing required fields caught by logger with warning
elif request.node.callspec.id in (
"empty-required-field",
Expand All @@ -406,11 +405,11 @@ def test_validate_data_dict(
assert "" == caplog.text
# logger records
captured_logs = caplog.records
helpers.validate_data_dict(template, data_dict, nxdl_root, pynx_logger)
helpers.validate_data_dict(template, data_dict, nxdl_root)
assert any(error_message in rec.message for rec in captured_logs)
else:
with pytest.raises(Exception) as execinfo:
helpers.validate_data_dict(template, data_dict, nxdl_root, pynx_logger)
helpers.validate_data_dict(template, data_dict, nxdl_root)
assert (error_message) == str(execinfo.value)


Expand Down
8 changes: 5 additions & 3 deletions tests/nexus/test_nexus.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

from pynxtools.nexus import nexus

logger = logging.getLogger(__name__)


def test_get_nexus_classes_units_attributes():
"""Check the correct parsing of a separate list for:
Expand Down Expand Up @@ -53,7 +55,7 @@ def test_nexus(tmp_path):
"""
local_dir = os.path.abspath(os.path.dirname(__file__))
example_data = os.path.join(local_dir, "../data/nexus/201805_WSe2_arpes.nxs")
logger = logging.getLogger(__name__)

logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(os.path.join(tmp_path, "nexus_test.log"), "w")
handler.setLevel(logging.DEBUG)
Expand Down Expand Up @@ -194,7 +196,7 @@ def test_c_option(tmp_path):
path_to_ref_files = os.path.join(local_path, "../data/nexus/")
ref_file = path_to_ref_files + "Ref1_c_option_test.log"
tmp_file = os.path.join(tmp_path, "c_option_1_test.log")
logger = logging.getLogger(__name__)

logger.setLevel(logging.INFO)
handler = logging.FileHandler(tmp_file, "w")

Expand Down Expand Up @@ -247,7 +249,7 @@ def test_d_option(tmp_path):
"""

tmp_file = os.path.join(tmp_path, "d_option_1_test.log")
logger = logging.getLogger(__name__)

logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(tmp_file, "w")

Expand Down
Loading