diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2008-08-05 06:33:09 +0000 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2008-08-05 06:33:09 +0000 |
commit | 56fa454fdd229524999127a5b89cc7c9077b9bd6 (patch) | |
tree | 1e3b2ee0e60ccc5d31e109947a332bb682c271bb | |
parent | d134b483066ae9d3a7e76d6e491f0f91eba6a954 (diff) | |
download | freeipa.git-56fa454fdd229524999127a5b89cc7c9077b9bd6.tar.gz freeipa.git-56fa454fdd229524999127a5b89cc7c9077b9bd6.tar.xz freeipa.git-56fa454fdd229524999127a5b89cc7c9077b9bd6.zip |
47: Added plugable.check_identifier() function; added corresponding unit tests
-rw-r--r-- | ipalib/errors.py | 5 | ||||
-rw-r--r-- | ipalib/plugable.py | 11 | ||||
-rw-r--r-- | ipalib/tests/test_plugable.py | 24 |
3 files changed, 39 insertions, 1 deletions
diff --git a/ipalib/errors.py b/ipalib/errors.py index 53a0870e..e9f78477 100644 --- a/ipalib/errors.py +++ b/ipalib/errors.py @@ -56,13 +56,16 @@ class SetError(IPAError): - class RegistrationError(IPAError): """ Base class for errors that occur during plugin registration. """ +class NameSpaceError(RegistrationError): + msg = 'name %r does not re.match %r' + + class SubclassError(RegistrationError): """ Raised when registering a plugin that is not a subclass of one of the diff --git a/ipalib/plugable.py b/ipalib/plugable.py index 70743f5a..8f2cbc27 100644 --- a/ipalib/plugable.py +++ b/ipalib/plugable.py @@ -21,6 +21,7 @@ Utility classes for registering plugins, base classes for writing plugins. """ +import re import inspect import errors @@ -43,6 +44,16 @@ def from_cli(cli_name): return cli_name.replace('-', '_').replace('.', '__') +def check_identifier(name): + """ + Raises errors.NameSpaceError if `name` is not a valid Python identifier + suitable for use in a NameSpace. + """ + regex = r'^[a-z][_a-z0-9]*[a-z0-9]$' + if re.match(regex, name) is None: + raise errors.NameSpaceError(name, regex) + + class Plugin(object): """ Base class for all plugins. diff --git a/ipalib/tests/test_plugable.py b/ipalib/tests/test_plugable.py index 2e87ea2c..1fa34bd5 100644 --- a/ipalib/tests/test_plugable.py +++ b/ipalib/tests/test_plugable.py @@ -41,6 +41,30 @@ def test_from_cli(): assert f('meta-service.do-something') == 'meta_service__do_something' +def test_valid_identifier(): + f = plugable.check_identifier + okay = [ + 'user_add', + 'stuff2junk', + 'sixty9', + ] + nope = [ + '_user_add', + '__user_add', + 'user_add_', + 'user_add__', + '_user_add_', + '__user_add__', + '60nine', + ] + for name in okay: + f(name) + for name in nope: + raises(errors.NameSpaceError, f, name) + for name in okay: + raises(errors.NameSpaceError, f, name.upper()) + + def test_Plugin(): api = 'the api instance' p = plugable.Plugin() |