Skip to content

Commit

Permalink
Put Key Validator on Cache (#753)
Browse files Browse the repository at this point in the history
* put validator on Cache

* Remove redundant comment
  • Loading branch information
Oddant1 committed Apr 24, 2024
1 parent 27f5ec0 commit 7b0e435
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions qiime2/core/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,32 @@ def is_cache(cls, path):
version_file = fh.read()
return regex.match(version_file) is not None

@classmethod
def validate_key(cls, key):
"""Validates that the given key is a valid Python idenitifier with the
exception that - is allowed.
Parameters
----------
key : str
The name of the key to validate.
Raises
------
ValueError
If the key passed in is not a valid Python identifier. We enforce
this to ensure no one creates keys that cause issues when we try to
load them.
"""
validation_key = key.replace('-', '_')
if not validation_key.isidentifier():
raise ValueError(f"Key '{key}' is not a valid Python identifier. "
"Keys may contain '-' characters but must "
"otherwise be valid Python identifiers. Python "
"identifier rules may be found here "
"https://www.askpython.com/python/"
"python-identifiers-rules-best-practices")

def _create_cache_contents(self):
"""Create the cache directory, all sub directories, and the version
file.
Expand Down Expand Up @@ -859,24 +885,8 @@ def _register_key(self, key, value, pool=False, collection=None):
The path to the data or pool we are keying.
pool : bool
Whether we are keying a pool or not.
Raises
------
ValueError
If the key passed in is not a valid Python identifier. We enforce
this to ensure no one creates keys that cause issues when we try to
load them.
"""
# We require keys to be valid Python identifiers with the single caveat
# that they may also contain dashes
validation_key = key.replace('-', '_')
if not validation_key.isidentifier():
raise ValueError("Keys may contain '-' characters but must "
"otherwise be valid Python identifiers. Python "
"identifier rules may be found here "
"https://www.askpython.com/python/"
"python-identifiers-rules-best-practices")

self.validate_key(key)
key_fp = self.keys / key

key_dict = {}
Expand Down

0 comments on commit 7b0e435

Please sign in to comment.