Skip to content

Commit 079b7a5

Browse files
author
David Erb
committed
adds new visit methods
1 parent c52b90f commit 079b7a5

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

src/dls_utilpack/visit.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import logging
22
import os
3+
import re
34
from datetime import datetime
5+
from pathlib import Path
46

57

68
class VisitNotFound(Exception):
@@ -30,3 +32,37 @@ def get_visit_year(beamline, visit):
3032
logger.info(f"visit directory determined to be {visit_directory}")
3133

3234
return year
35+
36+
37+
# ----------------------------------------------------------------------------------------
38+
def get_xchem_subdirectory(visit):
39+
40+
# This is the pattern all visits must have.
41+
pattern = r"^([a-z][a-z][0-9][0-9][0-9][0-9][0-9])[-]([0-9]+)$"
42+
43+
match = re.search(pattern, visit)
44+
45+
if not match:
46+
raise RuntimeError(
47+
f'the visit name "{visit}" does not conform to the visit naming convention'
48+
)
49+
50+
part1 = match.group(1)
51+
part2 = match.group(2)
52+
53+
subdirectory = f"{part1}/{part1}-{part2}"
54+
55+
return subdirectory
56+
57+
58+
# ----------------------------------------------------------------------------------------
59+
def get_xchem_directory(parent, visit):
60+
61+
subdirectory = get_xchem_subdirectory(visit)
62+
63+
full_path = Path(parent) / subdirectory
64+
65+
if not full_path.is_dir():
66+
raise RuntimeError(f"the visit directory {str(full_path)} does not exist")
67+
68+
return str(full_path)

tests/test_visit.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import logging
2+
from pathlib import Path
3+
4+
import pytest
5+
6+
from dls_utilpack.visit import get_xchem_directory, get_xchem_subdirectory
7+
8+
# Base class for the tester.
9+
from tests.base_tester import BaseTester
10+
11+
logger = logging.getLogger(__name__)
12+
13+
14+
# ----------------------------------------------------------------------------------------
15+
class TestVisit(BaseTester):
16+
def test(self, constants, logging_setup, output_directory):
17+
""" """
18+
19+
self.main(constants, output_directory)
20+
21+
# ----------------------------------------------------------------------------------------
22+
async def _main_coroutine(
23+
self,
24+
constants,
25+
output_directory,
26+
):
27+
""" """
28+
29+
# Check valid visits.
30+
assert get_xchem_subdirectory("aa12345-1") == "aa12345/aa12345-1"
31+
assert get_xchem_subdirectory("aa12345-1234") == "aa12345/aa12345-1234"
32+
33+
# Check invalid visit formats.
34+
with pytest.raises(RuntimeError) as excinfo:
35+
get_xchem_subdirectory("aa12345")
36+
assert "convention" in str(excinfo.value)
37+
38+
with pytest.raises(RuntimeError) as excinfo:
39+
get_xchem_subdirectory("a12345-1")
40+
assert "convention" in str(excinfo.value)
41+
42+
with pytest.raises(RuntimeError) as excinfo:
43+
get_xchem_subdirectory("[aa12345-1]")
44+
assert "convention" in str(excinfo.value)
45+
46+
# Check good directory.
47+
parent = Path(output_directory) / "processing/lab36"
48+
full_path = parent / "aa12345/aa12345-1"
49+
full_path.mkdir(parents=True, exist_ok=True)
50+
51+
assert get_xchem_directory(str(parent), "aa12345-1") == str(full_path)
52+
53+
# Check invalid directory.
54+
with pytest.raises(RuntimeError) as excinfo:
55+
get_xchem_directory("something", "aa12345-1")
56+
assert "does not exist" in str(excinfo.value)

0 commit comments

Comments
 (0)