mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2024-12-23 15:40:01 -06:00
47: Added plugable.check_identifier() function; added corresponding unit tests
This commit is contained in:
parent
d134b48306
commit
56fa454fdd
@ -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
|
||||
|
@ -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.
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user