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

feat: save warnings and errors in second file with loguru (DEV-3406) #883

Merged
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a20386b
add packages
Nora-Olivia-Ammann Mar 14, 2024
bb127da
Create logger_config.yml
Nora-Olivia-Ammann Mar 14, 2024
49a7425
Update .gitignore
Nora-Olivia-Ammann Mar 14, 2024
d176732
Update entry_point.py
Nora-Olivia-Ammann Mar 14, 2024
9d136f1
remove get_logger
Nora-Olivia-Ammann Mar 15, 2024
ee5dfa6
Delete create_logger.py
Nora-Olivia-Ammann Mar 15, 2024
fa2fd36
fix logfiles import
Nora-Olivia-Ammann Mar 15, 2024
d1617a8
Update test_excel2xml_cli.py
Nora-Olivia-Ammann Mar 15, 2024
84475b5
Merge branch 'wip/chore-refactor-excel2xml-test' into wip/dev-3406-sa…
Nora-Olivia-Ammann Mar 15, 2024
f0cf69f
Update test_excel2xml_cli.py
Nora-Olivia-Ammann Mar 15, 2024
b11641d
Merge branch 'wip/chore-refactor-excel2xml-test' into wip/dev-3406-sa…
Nora-Olivia-Ammann Mar 15, 2024
23dc78d
Update logger_config.yml
Nora-Olivia-Ammann Mar 15, 2024
9b389f0
Update logger_config.yml
Nora-Olivia-Ammann Mar 15, 2024
9de58ba
changes
Nora-Olivia-Ammann Mar 15, 2024
1f763ef
Merge branch 'wip/chore-refactor-excel2xml-test' into wip/dev-3406-sa…
Nora-Olivia-Ammann Mar 15, 2024
b503d20
Update logger_config.yml
Nora-Olivia-Ammann Mar 15, 2024
bec399a
Merge branch 'main' into wip/dev-3406-save-errors-in-second-file-with…
Nora-Olivia-Ammann Mar 15, 2024
9e6c92e
Merge branch 'wip/dev-3406-save-errors-in-second-file-with-loguru' of…
Nora-Olivia-Ammann Mar 15, 2024
a9c22df
Update pyproject.toml
Nora-Olivia-Ammann Mar 15, 2024
e7654ed
Update poetry.lock
Nora-Olivia-Ammann Mar 15, 2024
6523416
option in code
Nora-Olivia-Ammann Mar 18, 2024
6ceba40
change time formatting
Nora-Olivia-Ammann Mar 18, 2024
9c9ba17
Update .gitignore
Nora-Olivia-Ammann Mar 18, 2024
fa700bf
remove loguru config
Nora-Olivia-Ammann Mar 18, 2024
77e22d9
remove config yml
Nora-Olivia-Ammann Mar 18, 2024
51207c6
Update logger_config.py
Nora-Olivia-Ammann Mar 18, 2024
61bd496
remove exc_info
Nora-Olivia-Ammann Mar 18, 2024
366b01a
Update logger_config.py
Nora-Olivia-Ammann Mar 18, 2024
3693d97
Update logger_config.py
Nora-Olivia-Ammann Mar 18, 2024
83b3ddc
move logger to correct location
Nora-Olivia-Ammann Mar 18, 2024
205b54c
Update logger_config.py
Nora-Olivia-Ammann Mar 19, 2024
aed1974
Update entry_point.py
Nora-Olivia-Ammann Mar 19, 2024
eba24aa
fix exc_info
Nora-Olivia-Ammann Mar 19, 2024
3f6bf71
change path
Nora-Olivia-Ammann Mar 19, 2024
5ecea6a
change logfiles
Nora-Olivia-Ammann Mar 19, 2024
197cf47
Update logger_config.py
Nora-Olivia-Ammann Mar 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ testdata/preprocessed_files/
testdata/bitstreams/nested
processing_result_*.pkl
processed_files.txt
/warnings-*.log
Nora-Olivia-Ammann marked this conversation as resolved.
Show resolved Hide resolved
183 changes: 113 additions & 70 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ homepage = "https://www.dasch.swiss/"
repository = "https://github.com/dasch-swiss/dsp-tools"
include = [
"src/dsp_tools/resources/*",
"src/dsp_tools/resources/logger_config.yml",
Nora-Olivia-Ammann marked this conversation as resolved.
Show resolved Hide resolved
"src/dsp_tools/py.typed" # see https://mypy.readthedocs.io/en/stable/installed_packages.html#making-pep-561-compatible-packages
]

Expand All @@ -36,6 +37,7 @@ pyyaml = "^6.0.1"
rustworkx = "^0.14.0"
termcolor = "^2.4.0"
packaging = "^23.2"
loguru = "^0.7.2"


[tool.poetry.group.dev.dependencies]
Expand Down
5 changes: 2 additions & 3 deletions src/dsp_tools/cli/call_action.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import argparse
from pathlib import Path

from loguru import logger

from dsp_tools.commands.excel2json.lists import excel2lists
from dsp_tools.commands.excel2json.lists import validate_lists_section_with_schema
from dsp_tools.commands.excel2json.project import excel2json
Expand All @@ -20,11 +22,8 @@
from dsp_tools.commands.template import generate_template_repo
from dsp_tools.commands.xmlupload.upload_config import UploadConfig
from dsp_tools.commands.xmlupload.xmlupload import xmlupload
from dsp_tools.utils.create_logger import get_logger
from dsp_tools.utils.xml_validation import validate_xml

logger = get_logger(__name__)


def call_requested_action(args: argparse.Namespace) -> bool:
"""
Expand Down
12 changes: 6 additions & 6 deletions src/dsp_tools/cli/entry_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import regex
import requests
from loguru import logger
from packaging.version import parse
from termcolor import colored

Expand All @@ -17,9 +18,7 @@
from dsp_tools.models.exceptions import BaseError
from dsp_tools.models.exceptions import InternalError
from dsp_tools.models.exceptions import UserError
from dsp_tools.utils.create_logger import get_logger

logger = get_logger(__name__)
from dsp_tools.utils.logger_config import logger_config


def main() -> None:
Expand Down Expand Up @@ -67,12 +66,12 @@ def run(args: list[str]) -> None:
)
success = call_requested_action(parsed_arguments)
except BaseError as err:
logger.exception("The process was terminated because of an Error:")
logger.error("The process was terminated because of an Error:")
Nora-Olivia-Ammann marked this conversation as resolved.
Show resolved Hide resolved
print("\nThe process was terminated because of an Error:")
print(err.message)
sys.exit(1)
except Exception as err:
logger.exception(err)
except Exception as err: # noqa: BLE001 (blind-except)
logger.error(err)
Nora-Olivia-Ammann marked this conversation as resolved.
Show resolved Hide resolved
print(InternalError())
sys.exit(1)

Expand Down Expand Up @@ -284,4 +283,5 @@ def _derive_sipi_url(


if __name__ == "__main__":
logger_config()
Nora-Olivia-Ammann marked this conversation as resolved.
Show resolved Hide resolved
main()
3 changes: 1 addition & 2 deletions src/dsp_tools/commands/excel2xml/excel2xml_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,7 @@ def excel2xml(
dataframe=dataframe,
max_num_of_props=max_num_of_props,
)
for resource in resources:
root.append(resource)
root.extend(resources)
write_xml(root, output_file)
if len(catched_warnings) > 0:
success = False
Expand Down
4 changes: 1 addition & 3 deletions src/dsp_tools/commands/id2iri.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
from pathlib import Path

import regex
from loguru import logger
from lxml import etree

from dsp_tools.models.exceptions import UserError
from dsp_tools.utils.create_logger import get_logger
from dsp_tools.utils.xml_utils import parse_and_clean_xml_file

logger = get_logger(__name__)


def _check_input_parameters(
xml_file: str,
Expand Down
4 changes: 1 addition & 3 deletions src/dsp_tools/commands/ingest_xmlupload/apply_ingest_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
from typing import cast

import pandas as pd
from loguru import logger
from lxml import etree

from dsp_tools.commands.ingest_xmlupload.user_information import IngestInformation
from dsp_tools.models.exceptions import InputError
from dsp_tools.utils.create_logger import get_logger

logger = get_logger(__name__)


def get_mapping_dict_from_file(shortcode: str) -> dict[str, str]:
Expand Down
4 changes: 1 addition & 3 deletions src/dsp_tools/commands/ingest_xmlupload/upload_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

from pathlib import Path

from loguru import logger
from lxml import etree

from dsp_tools.commands.ingest_xmlupload.apply_ingest_id import get_mapping_dict_from_file
from dsp_tools.commands.ingest_xmlupload.apply_ingest_id import replace_filepath_with_sipi_id
from dsp_tools.commands.xmlupload.upload_config import UploadConfig
from dsp_tools.commands.xmlupload.xmlupload import xmlupload
from dsp_tools.models.exceptions import InputError
from dsp_tools.utils.create_logger import get_logger
from dsp_tools.utils.xml_utils import remove_comments_from_element_tree

logger = get_logger(__name__)


def ingest_xmlupload(
xml_file: Path,
Expand Down
48 changes: 23 additions & 25 deletions src/dsp_tools/commands/project/create/project_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import cast

import regex
from loguru import logger

from dsp_tools.commands.excel2json.lists import expand_lists_from_excel
from dsp_tools.commands.project.create.project_create_lists import create_lists_on_server
Expand All @@ -27,11 +28,8 @@
from dsp_tools.models.langstring import LangString
from dsp_tools.utils.connection import Connection
from dsp_tools.utils.connection_live import ConnectionLive
from dsp_tools.utils.create_logger import get_logger
from dsp_tools.utils.shared import parse_json_input

logger = get_logger(__name__)


def _create_project_on_server(
project_definition: ProjectDefinition,
Expand Down Expand Up @@ -79,7 +77,7 @@ def _create_project_on_server(
f"Cannot create project '{project_definition.shortname}' "
f"({project_definition.shortcode}) on DSP server."
)
logger.error(err_msg, exc_info=True)
logger.error(err_msg)
raise UserError(err_msg) from None
print(f" Created project '{project_remote.shortname}' ({project_remote.shortcode}).")
logger.info(f"Created project '{project_remote.shortname}' ({project_remote.shortcode}).")
Expand Down Expand Up @@ -119,7 +117,7 @@ def _create_groups(
"not possible to retrieve the remote groups from the DSP server."
)
print(f"WARNING: {err_msg}")
logger.warning(err_msg, exc_info=True)
logger.warning(err_msg)
remote_groups = []
overall_success = False

Expand All @@ -131,7 +129,7 @@ def _create_groups(
current_project_groups[group_name] = remotely_existing_group[0]
err_msg = f"Group name '{group_name}' already exists on the DSP server. Skipping..."
print(f" WARNING: {err_msg}")
logger.warning(err_msg, exc_info=True)
logger.warning(err_msg)
overall_success = False
continue

Expand All @@ -149,7 +147,7 @@ def _create_groups(
except BaseError:
err_msg = "Unable to create group '{group_name}'."
print(f" WARNING: {err_msg}")
logger.warning(err_msg, exc_info=True)
logger.warning(err_msg)
overall_success = False
continue

Expand Down Expand Up @@ -198,7 +196,7 @@ def _get_group_iris_for_user(
)
if ":" not in full_group_name and full_group_name != "SystemAdmin":
print(f" WARNING: {inexisting_group_msg}")
logger.warning(inexisting_group_msg, exc_info=True)
logger.warning(inexisting_group_msg)
success = False
continue

Expand All @@ -215,7 +213,7 @@ def _get_group_iris_for_user(
# full_group_name refers to a group inside the same project
if group_name not in current_project_groups:
print(f" WARNING: {inexisting_group_msg}")
logger.warning(inexisting_group_msg, exc_info=True)
logger.warning(inexisting_group_msg)
success = False
continue
group = current_project_groups[group_name]
Expand All @@ -230,13 +228,13 @@ def _get_group_iris_for_user(
f"exists on the DSP server, but no groups could be retrieved from the DSP server."
)
print(f" WARNING: {err_msg}")
logger.warning(err_msg, exc_info=True)
logger.warning(err_msg)
success = False
continue
existing_group = [g for g in remote_groups if g.project == current_project.iri and g.name == group_name]
if not existing_group:
print(f" WARNING: {inexisting_group_msg}")
logger.warning(inexisting_group_msg, exc_info=True)
logger.warning(inexisting_group_msg)
Nora-Olivia-Ammann marked this conversation as resolved.
Show resolved Hide resolved
success = False
continue
group = existing_group[0]
Expand Down Expand Up @@ -278,7 +276,7 @@ def _get_projects_where_user_is_admin(
if ":" not in full_project_name:
err_msg = "Provided project '{full_project_name}' for user '{username}' is not valid. Skipping..."
print(f" WARNING: {err_msg}")
logger.warning(err_msg, exc_info=True)
logger.warning(err_msg)
success = False
continue

Expand All @@ -297,14 +295,14 @@ def _get_projects_where_user_is_admin(
f"because the projects cannot be retrieved from the DSP server."
)
print(f" WARNING: {err_msg}")
logger.warning(err_msg, exc_info=True)
logger.warning(err_msg)
success = False
continue
in_project_list = [p for p in remote_projects if p.shortname == project_name]
if not in_project_list:
msg = f"Provided project '{full_project_name}' for user '{username}' is not valid. Skipping..."
print(f" WARNING: {msg}")
logger.warning(msg, exc_info=True)
logger.warning(msg)
success = False
continue
in_project = in_project_list[0]
Expand Down Expand Up @@ -349,7 +347,7 @@ def _create_users(
if json_user_definition["email"] in [user.email for user in all_users]:
err_msg = f"User '{username}' already exists on the DSP server. Skipping..."
print(f" WARNING: {err_msg}")
logger.warning(err_msg, exc_info=True)
logger.warning(err_msg)
overall_success = False
continue
# add user to the group(s)
Expand Down Expand Up @@ -391,7 +389,7 @@ def _create_users(
user_local.create()
except BaseError:
print(f" WARNING: Unable to create user '{username}'.")
logger.warning(f"Unable to create user '{username}'.", exc_info=True)
logger.warning(f"Unable to create user '{username}'.")
overall_success = False
continue
print(f" Created user '{username}'.")
Expand Down Expand Up @@ -506,7 +504,7 @@ def _create_ontology(
if onto_name in [onto.name for onto in project_ontologies]:
err_msg = f"Ontology '{onto_name}' already exists on the DSP server. Skipping..."
print(f" WARNING: {err_msg}")
logger.warning(err_msg, exc_info=True)
logger.warning(err_msg)
return None

print(f"Create ontology '{onto_name}'...")
Expand All @@ -522,7 +520,7 @@ def _create_ontology(
ontology_remote = ontology_local.create()
except BaseError:
# if ontology cannot be created, let the error escalate
logger.error(f"ERROR while trying to create ontology '{onto_name}'.", exc_info=True)
logger.error(f"ERROR while trying to create ontology '{onto_name}'.")
raise UserError(f"ERROR while trying to create ontology '{onto_name}'.") from None

if verbose:
Expand Down Expand Up @@ -583,7 +581,7 @@ def _create_ontologies(
except BaseError:
err_msg = "Unable to retrieve remote ontologies. Cannot check if your ontology already exists."
print("WARNING: {err_msg}")
logger.warning(err_msg, exc_info=True)
logger.warning(err_msg)
project_ontologies = []

for ontology_definition in ontology_definitions:
Expand Down Expand Up @@ -698,7 +696,7 @@ def _add_resource_classes_to_remote_ontology(
except BaseError:
err_msg = f"Unable to create resource class '{res_class['name']}'."
print(f"WARNING: {err_msg}")
logger.warning(err_msg, exc_info=True)
logger.warning(err_msg)
overall_success = False

return last_modification_date, new_res_classes, overall_success
Expand Down Expand Up @@ -794,7 +792,7 @@ def _add_property_classes_to_remote_ontology(
except BaseError:
err_msg = f"Unable to create property class '{prop_class['name']}'."
print(f"WARNING: {err_msg}")
logger.warning(f"Unable to create property class '{prop_class['name']}'.", exc_info=True)
logger.warning(f"Unable to create property class '{prop_class['name']}'.")
overall_success = False

return last_modification_date, overall_success
Expand Down Expand Up @@ -842,7 +840,7 @@ def _add_cardinalities_to_resource_classes(
f"This class doesn't exist on the DSP server."
)
print(f"WARNINIG: {msg}")
logger.warning(msg, exc_info=True)
logger.warning(msg)
overall_success = False
continue
for card_info in res_class.get("cardinalities", []):
Expand All @@ -865,7 +863,7 @@ def _add_cardinalities_to_resource_classes(
except BaseError:
err_msg = f"Unable to add cardinality '{qualified_propname}' to resource class {res_class['name']}."
print(f"WARNING: {err_msg}")
logger.warning(err_msg, exc_info=True)
logger.warning(err_msg)
overall_success = False

ontology_remote.lastModificationDate = last_modification_date
Expand Down Expand Up @@ -918,7 +916,7 @@ def _rectify_hlist_of_properties(
f"which is not a valid list name. "
f"Assuming that you meant '{deduced_list_name}' instead."
)
logger.warning(msg, exc_info=True)
logger.warning(msg)
print(msg)
else:
msg = f"Property '{prop['name']}' references an unknown list: '{prop['gui_attributes']['hlist']}'"
Expand Down Expand Up @@ -1062,7 +1060,7 @@ def create_project(
f"but during the creation process, some problems occurred. Please carefully check the console output."
)
print(f"========================================================\nWARNING: {msg}")
logger.warning(msg, exc_info=True)
logger.warning(msg)

return overall_success

Expand Down