Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dbd9945

Browse files
committedMay 15, 2024
Initial commit to load datetime automatically with datetime function.
1 parent c45022d commit dbd9945

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed
 

‎src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ def get_args(override_cli_inputs=None):
8989
"For example, facility='NSLS II', 'facility=NSLS II', beamline=28ID-2, "
9090
"'beamline'='28ID-2', 'favorite color'=blue, are all valid key=value items. ",
9191
)
92+
p.add_argument(
93+
"-t"
94+
"--is-test",
95+
action="store_true",
96+
help="If this flag is specified, the app would be run in test mode. No need for general users to specify this "
97+
"flag",
98+
)
9299
args = p.parse_args(override_cli_inputs)
93100
return args
94101

‎src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import re
33
from pathlib import Path
44

5+
import freezegun
6+
from freezegun import freeze_time
7+
from datetime import datetime
58
import pytest
69

710
from diffpy.labpdfproc.labpdfprocapp import get_args
@@ -12,6 +15,7 @@
1215
set_input_lists,
1316
set_output_directory,
1417
set_wavelength,
18+
load_datetime
1519
)
1620

1721
# Use cases can be found here: https://github.com/diffpy/diffpy.labpdfproc/issues/48
@@ -153,7 +157,7 @@ def test_set_wavelength(inputs, expected):
153157
params3 = [
154158
(
155159
["--anode-type", "invalid"],
156-
[f"Anode type not recognized. Please rerun specifying an anode_type from {*known_sources, }."],
160+
[f"Anode type not recognized. Please rerun specifying an anode_type from {*known_sources,}."],
157161
),
158162
(
159163
["--wavelength", "0"],
@@ -232,3 +236,18 @@ def test_load_user_metadata_bad(inputs, msg):
232236
actual_args = get_args(cli_inputs)
233237
with pytest.raises(ValueError, match=msg[0]):
234238
actual_args = load_user_metadata(actual_args)
239+
240+
241+
params7 = [(["--user-metadata", "facility=NSLS II", "beamline=28ID-2", "favorite color=blue"],
242+
[["facility", "NSLS II"], ["beamline", "28ID-2"], ["favorite color", "blue"],
243+
["datetime", "2014-02-01 12:34:56"]])]
244+
245+
246+
@freezegun.freeze_time("2014-02-01 12:34:56")
247+
@pytest.mark.parametrize("inputs, expected", params7)
248+
def test_load_datetime(inputs, expected):
249+
actual_inputs = ["2.5", "data.xy"] + inputs
250+
actual_args = get_args(actual_inputs)
251+
time_loaded_args = load_datetime(actual_args)
252+
actual_args = load_user_metadata(time_loaded_args)
253+
assert actual_args == expected

‎src/diffpy/labpdfproc/tools.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from datetime import datetime
12
from pathlib import Path
23

34
WAVELENGTHS = {"Mo": 0.71, "Ag": 0.59, "Cu": 1.54}
@@ -165,3 +166,24 @@ def load_user_metadata(args):
165166
setattr(args, key, value)
166167
delattr(args, "user_metadata")
167168
return args
169+
170+
171+
def load_datetime(args):
172+
"""
173+
Load datetime into the provided argparse Namespace.
174+
175+
Parameters
176+
----------
177+
args argparse.Namespace
178+
the arguments from the parser
179+
180+
Returns
181+
-------
182+
the updated argparse Namespace with datetime inserted as key-value pairs
183+
"""
184+
if args.user_metadata:
185+
curr_time = datetime.now()
186+
setattr(args, "datetime", curr_time.strftime("%Y-%m-%d %H:%M:%S"))
187+
return args
188+
189+

0 commit comments

Comments
 (0)
Please sign in to comment.