47: Added plugable.check_identifier() function; added corresponding unit tests

This commit is contained in:
Jason Gerard DeRose 2008-08-05 06:33:09 +00:00
parent d134b48306
commit 56fa454fdd
3 changed files with 39 additions and 1 deletions

View File

@ -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

View File

@ -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.

View File

@ -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()