Skip to content

Commit b205725

Browse files
committed
fist docs and logger.
1 parent a33c830 commit b205725

File tree

4 files changed

+221
-106
lines changed

4 files changed

+221
-106
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import logging
2+
from typing import Union
3+
4+
def setup_logger(name: str,
5+
log_file: str,
6+
level: Union[int, str] = logging.INFO,
7+
existing_logger: logging.Logger = None
8+
) -> tuple[logging.Logger, logging.FileHandler]:
9+
"""Set up a named logger that writes to a specific file.
10+
if existing_logger is provided, it will be used instead of creating a new one,
11+
and another handler will be added to it.
12+
"""
13+
logger = existing_logger
14+
if not logger:
15+
logger = logging.getLogger(name)
16+
logger.setLevel(level)
17+
18+
# # Check if the logger already has a handler
19+
# if not logger.handlers:
20+
21+
file_handeler = logging.FileHandler(log_file)
22+
file_handeler.setLevel(level)
23+
24+
formatter = logging.Formatter(
25+
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
26+
)
27+
file_handeler.setFormatter(formatter)
28+
29+
logger.addHandler(file_handeler)
30+
logger.info(f"Test Logger {name} set up with file handler for {log_file}")
31+
return logger, file_handeler

src/pynxtools_spm/nomad_uploader/nomad_upload_api.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ def upload_to_NOMAD(nomad_url, token, upload_file):
6969
print("something went wrong uploading to NOMAD")
7070
return
7171

72+
def trigger_reprocess_upload(nomad_url, token, upload_id):
73+
"""Trigger reprocessing of an upload"""
74+
try:
75+
response = requests.post(
76+
f"{nomad_url}uploads/{upload_id}/action/process",
77+
headers={"Authorization": f"Bearer {token}", "Accept": "application/json"},
78+
timeout=30,
79+
)
80+
return response
81+
except Exception:
82+
print("something went wrong trying to reprocess upload: " + upload_id)
83+
return
84+
7285

7386
def check_upload_status(nomad_url, token, upload_id):
7487
"""

src/pynxtools_spm/nomad_uploader/reader_config_setup.py

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Optional
44
from dataclasses import dataclass, asdict
55
import zipfile
6+
import logging
67

78

89
@dataclass
@@ -11,7 +12,7 @@ class SPMConvertInputParameters:
1112
eln: str | Path
1213
expriement_type: str
1314
reader: str = "spm"
14-
output: str = None
15+
output: Optional[str | Path] = None
1516
nxdl: Optional[str] = None
1617
create_zip: Optional[bool] = True
1718
zip_file_path: Optional[str | Path] = None
@@ -22,6 +23,9 @@ class SPMConvertInputParameters:
2223

2324
def convert_spm_experiments(
2425
input_params: SPMConvertInputParameters,
26+
converter_logger: Optional[logging.Logger],
27+
converter_handeler: Optional[logging.Handler] = None,
28+
2529
):
2630
"""Convert SPM (STS, STM and AFM) experirment data files to NeXus format.
2731
Later, the input files and generated output file are zipped together to
@@ -38,19 +42,19 @@ def convert_spm_experiments(
3842
"""
3943

4044
if not isinstance(input_params, SPMConvertInputParameters):
41-
raise ValueError(
45+
converter_logger.error(
4246
"Input parameters must be an instance of SPMConvertInputParameters"
4347
)
4448

4549
if not input_params.input_file:
46-
raise ValueError("Input files are required to run an SPM experiment")
50+
converter_logger.error("Input files are required to run an SPM experiment")
4751
if not input_params.eln:
48-
raise ValueError(
49-
f"ELN is required to run an {input_params.expriement_type} experiment."
50-
)
52+
converter_logger.error(f"ELN file is requred to run an {input_params.expriement_type} experiment")
5153

5254
if not input_params.expriement_type:
53-
raise ValueError("Experiment type is required to run an SPM experiment")
55+
converter_logger.error(
56+
"Experiment type is required to run an SPM experiment"
57+
)
5458

5559
input_params.input_file = (*input_params.input_file, input_params.eln)
5660
input_params.input_file = tuple(
@@ -67,6 +71,7 @@ def convert_spm_experiments(
6771
for file in input_params.input_file:
6872
if file.suffix in (
6973
input_params.raw_extension,
74+
# TODO remoce the following line
7075
f".{input_params.raw_extension}",
7176
):
7277
if input_params.output is None:
@@ -78,27 +83,37 @@ def convert_spm_experiments(
7883
"Valid raw files and extension is required to run an SPM experiment"
7984
)
8085
# TODO Try with input_file as tuple of Path objects
81-
input_params.input_file = [str(file) for file in input_params.input_file]
82-
input_params.output = str(input_params.output)
86+
# Use handler only for conver function. Do not close the handler
87+
# after the function call as it will be used again and again
88+
if converter_handeler not in converter_logger.handlers:
89+
converter_logger.addHandler(converter_handeler)
8390
try:
84-
convert(**asdict(input_params))
85-
91+
kwargs = asdict(input_params)
92+
print("#### kwargs:", kwargs)
93+
kwargs["input_file"] = tuple(map(str, input_params.input_file))
94+
kwargs["output"] = str(input_params.output)
95+
# with converter_logger:
96+
convert(**kwargs)
97+
print("#### kwargs after conversion:", kwargs)
8698
if input_params.create_zip:
8799
with zipfile.ZipFile(zip_file, "w") as zipf:
88100
zipf.write(
89-
input_params.output, arcname=input_params.output.split("/")[-1]
101+
str(input_params.output),
102+
arcname=str(input_params.output).split("/")[-1],
90103
)
91-
for file in input_params.input_file:
104+
for file in map(str, input_params.input_file):
92105
zipf.write(file, arcname=file.split("/")[-1])
93106
input_params.zip_file_path = Path(zip_file)
94-
input_params.output = Path(input_params.output)
107+
95108
except Exception as e:
96109
print("NeXusConverterError:", e)
97110
finally:
98-
input_params.input_file = tuple(
99-
Path(file) if isinstance(file, str) else file
100-
for file in input_params.input_file
101-
)
111+
converter_logger.removeHandler(converter_handeler)
112+
# finally:
113+
# input_params.input_file = tuple(
114+
# Path(file) if isinstance(file, str) else file
115+
# for file in input_params.input_file
116+
# )
102117

103118
return input_params
104119

0 commit comments

Comments
 (0)