Skip to content

Commit a58030f

Browse files
authored
Merge pull request #242 from European-XFEL/special-variables
Fix API support for comments and timestamps
2 parents a4a1d8a + 7847673 commit a58030f

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

damnit/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Prototype for extracting and showing metadata (AMORE project)"""
22

3-
__version__ = '0.1.1'
3+
__version__ = '0.1.2'
44

55
from .api import Damnit, RunVariables, VariableData

damnit/api.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,20 @@ def keys(self) -> list:
237237
def _var_titles(self):
238238
result = self._db.conn.execute("SELECT name, title FROM variables").fetchall()
239239
available_vars = self.keys()
240-
return { row[0]: row[1] if row[1] is not None else row[0] for row in result
241-
if row[0] in available_vars }
240+
titles = { row[0]: row[1] if row[1] is not None else row[0] for row in result
241+
if row[0] in available_vars }
242+
243+
# These variables are created automatically, but they aren't included in
244+
# the `variables` table (yet) so we need to explicitly add their titles.
245+
special_vars = {
246+
"start_time": "Timestamp",
247+
"comment": "Comment"
248+
}
249+
for name, title in special_vars.items():
250+
if name in available_vars:
251+
titles[name] = title
252+
253+
return titles
242254

243255
def titles(self) -> list:
244256
"""The titles of available variables.

tests/test_api.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ def test_run_variables(mock_db_with_data, monkeypatch):
6767
assert rv["scalar1"].name == "scalar1"
6868
assert rv["Scalar1"].name == "scalar1"
6969

70+
# Test getting the start_time. This one's a bit special because we create it
71+
# automatically, it's a proper variable except for the fact that we don't
72+
# insert it into the `variables` table.
73+
assert isinstance(rv["start_time"].read(), float)
74+
assert "Timestamp" in rv.titles()
75+
76+
# Test getting comments. This is also special because it doesn't appear in
77+
# the `variables` table.
78+
assert "comment" not in rv.keys()
79+
db.change_run_comment(damnit.proposal, 1, "foo")
80+
assert "Comment" in rv.titles()
81+
assert rv["comment"].read() == "foo"
82+
7083
with pytest.raises(KeyError):
7184
rv["foo"]
7285

0 commit comments

Comments
 (0)