Use regular expression to get content provider name
authorThierry Florac <tflorac@ulthar.net>
Wed, 03 Oct 2018 15:11:04 +0200
changeset 25 9e66e0f019e2
parent 24 986b5f85ecd4
child 26 cb8e6f779fe0
Use regular expression to get content provider name
src/pyams_viewlet/provider.py
--- a/src/pyams_viewlet/provider.py	Mon Jun 11 15:58:05 2018 +0200
+++ b/src/pyams_viewlet/provider.py	Wed Oct 03 15:11:04 2018 +0200
@@ -12,23 +12,27 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
+import re
 
-# import interfaces
-from zope.contentprovider.interfaces import IContentProvider, ContentProviderLookupError, BeforeUpdateEvent
+from chameleon.astutil import Symbol
+from chameleon.tales import StringExpr
+from pyramid_zope_request import PyramidPublisherRequest
+from zope.contentprovider.interfaces import BeforeUpdateEvent, ContentProviderLookupError, IContentProvider
+from zope.contentprovider.tales import addTALNamespaceData
 from zope.location.interfaces import ILocation
 
-# import packages
-from chameleon.astutil import Symbol
-from chameleon.tales import StringExpr
 from pyams_utils.tales import ContextExprMixin
-from pyramid_zope_request import PyramidPublisherRequest
-from zope.contentprovider.tales import addTALNamespaceData
+
+
+CONTENT_PROVIDER_NAME = re.compile('([A-Za-z0-9_\-\.]+)')
 
 
 def render_content_provider(econtext, name):
-    name = name.strip()
+    match = CONTENT_PROVIDER_NAME.match(name.strip())
+    if match:
+        name = match.groups()[0]
+    else:
+        raise ContentProviderLookupError(name)
 
     context = econtext.get('context')
     request = econtext.get('request')