From 68b5db49a54f59731ee52a966e0241c2ed0e5ba7 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Fri, 24 Jul 2015 06:37:33 +0200 Subject: Utils: Generalize safe_module_name Other plugins that also import user generated python source code, may want to use safe_module_name from TemplateHelper. --- src/lib/Bcfg2/Utils.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/lib/Bcfg2/Utils.py') diff --git a/src/lib/Bcfg2/Utils.py b/src/lib/Bcfg2/Utils.py index 64d0d8b93..2fdc0c3e0 100644 --- a/src/lib/Bcfg2/Utils.py +++ b/src/lib/Bcfg2/Utils.py @@ -321,6 +321,15 @@ def safe_input(msg): return input(msg) +def safe_module_name(prefix, module): + """ Munge the name of a module with prefix to avoid collisions + with other Python modules. E.g., if you want to import user + defined helper modules and someone has a helper named 'ldap.py', + it should not be added to ``sys.modules`` as ``ldap``, but rather + as something more obscure. """ + return '__%s_%s' % (prefix, module) + + class classproperty(object): # pylint: disable=C0103 """ Decorator that can be used to create read-only class properties. """ -- cgit v1.2.3-1-g7c22