From a67cef20c36df9534ea08ceafe687d0c04556986 Mon Sep 17 00:00:00 2001 From: Simon Billinge Date: Tue, 31 Dec 2024 09:22:10 -0500 Subject: [PATCH 1/2] strip the prum finisher input before assigning to key --- src/regolith/helpers/u_finishprumhelper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/regolith/helpers/u_finishprumhelper.py b/src/regolith/helpers/u_finishprumhelper.py index dd96078f5..ab1fdc99b 100644 --- a/src/regolith/helpers/u_finishprumhelper.py +++ b/src/regolith/helpers/u_finishprumhelper.py @@ -55,7 +55,7 @@ def construct_global_ctx(self): def db_updater(self): rc = self.rc - key = rc.projectum_id + key = rc.projectum_id.strip() filterid = {"_id": key} found_projectum = rc.client.find_one(rc.database, rc.coll, filterid) if not found_projectum: From 310065dc733e84e74020e6638abe91354690f093 Mon Sep 17 00:00:00 2001 From: Simon Billinge Date: Sat, 4 Jan 2025 06:54:47 -0500 Subject: [PATCH 2/2] more general rc-stripper in runcontrol, draft wip --- news/strip-prum_finisher.rst | 23 +++++++++++++++++++++++ src/regolith/runcontrol.py | 26 ++++++++++++++++++++++++++ tests/test_runcontrol.py | 9 ++++++++- 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 news/strip-prum_finisher.rst diff --git a/news/strip-prum_finisher.rst b/news/strip-prum_finisher.rst new file mode 100644 index 000000000..033d51f5b --- /dev/null +++ b/news/strip-prum_finisher.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* All inputs of `_id`s are now stripped before assigning to variables + +**Security:** + +* diff --git a/src/regolith/runcontrol.py b/src/regolith/runcontrol.py index 51a3fa5f7..c557f50ef 100644 --- a/src/regolith/runcontrol.py +++ b/src/regolith/runcontrol.py @@ -15,6 +15,32 @@ FORBIDDEN_NAMES = frozenset(["del", "global"]) +def strip_rc(rc): + """strip whitespace from all string-like rc members + + Parameters + ---------- + rc : runcontrol object + The rc with unstripped strings + + Return + ------ + rc : runcontrol object + The rc with all strings sstripped of whitespace + """ + stripped_rc = rc.__class__() + for attr in dir(rc): + if not attr.startswith("__"): + value = getattr(rc, attr) + if isinstance(value, str): + print("str", attr, value) + setattr(stripped_rc, attr, value.strip()) + else: + print("non-str", attr, value) + setattr(stripped_rc, attr, value) + return stripped_rc + + def warn_forbidden_name(forname, inname=None, rename=None): """Warns the user that a forbidden name has been found.""" msg = "found forbidden name {0!r}".format(forname) diff --git a/tests/test_runcontrol.py b/tests/test_runcontrol.py index 7e2edc5c4..a2700dcff 100644 --- a/tests/test_runcontrol.py +++ b/tests/test_runcontrol.py @@ -2,7 +2,7 @@ import os from regolith.database import connect -from regolith.runcontrol import DEFAULT_RC, connect_db, filter_databases, load_rcfile +from regolith.runcontrol import DEFAULT_RC, connect_db, filter_databases, load_rcfile, strip_rc def test_connect_db(make_db): @@ -17,3 +17,10 @@ def test_connect_db(make_db): chained_db, dbs = connect_db(rc) assert chained_db == expected_chdb assert dbs == expected_dbs + + +def test_strip_rc(): + rc = copy.copy(DEFAULT_RC) + rc.new_thing = "new_thing " + rc = strip_rc(rc) + assert rc.new_thing == "new_thing"