Changeset 1666
- Timestamp:
- 07/10/08 13:37:49 (21 months ago)
- Location:
- trunk/pylucid
- Files:
-
- 3 modified
-
PyLucid/template_addons/lucidTag.py (modified) (3 diffs)
-
PyLucid/tools/utils.py (modified) (3 diffs)
-
tests/test_plugin_api.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/pylucid/PyLucid/template_addons/lucidTag.py
r1634 r1666 26 26 from PyLucid.system.context_processors import add_css_tag 27 27 from PyLucid.tools.data_eval import data_eval, DataEvalError 28 from PyLucid.tools.utils import make_kwagrs 28 29 29 30 from django.conf import settings … … 96 97 97 98 99 100 98 101 def lucidTag(parser, token): 99 102 """ … … 114 117 method_name = "lucidTag" 115 118 116 # print "1", content117 # method_kwargs = {}118 # if content:119 # raw_kwargs = content[0]120 # kwargs = raw_kwargs.replace("=", ":")121 # kwargs = u"{ %s }" % kwargs122 # print "2", kwargs123 # try:124 # method_kwargs = data_eval(kwargs)125 # except DataEvalError, err:126 # return lucidTagNodeError(127 # plugin_name, method_name,128 # msg = "Tag args error: %s" % err129 # )130 #131 # print "3", method_kwargs132 133 method_kwargs = {}134 119 if content: 135 kwargs = content[0] 136 kwargs = KWARGS_REGEX.findall(kwargs) 137 for key, value in kwargs: 138 # method Keywords must be Strings 139 key = key.encode(settings.DEFAULT_CHARSET) 140 value_lower = value.lower() 141 if value_lower in ("true", "on"): 142 value = True 143 elif value_lower in ("false", "off"): 144 value = False 145 method_kwargs[key] = value 120 raw_kwargs = content[0] 121 method_kwargs = make_kwagrs(raw_kwargs) 122 else: 123 method_kwargs = {} 146 124 147 125 return lucidTagNode(plugin_name, method_name, method_kwargs) -
trunk/pylucid/PyLucid/tools/utils.py
r1657 r1666 18 18 """ 19 19 20 import shlex 20 21 from xml.sax.saxutils import escape as sax_escape 21 22 … … 31 32 Escape "&", "<", ">" and django template tags chars like "{" and "}" 32 33 defined in ENTITIES to the HTML character entity. 34 >>> escape("<test1> & {{ test2 }} {% test3 %}") 35 '<test1> & {{ test2 }} {% test3 %}' 33 36 """ 34 37 return sax_escape(txt, entities=ENTITIES) … … 38 41 Escape only django template tags chars like "{" and "}" defined in ENTITIES 39 42 to the HTML character entity. 43 44 >>> escape_django_tags("<test1> &") 45 '<test1> &' 46 47 >>> escape_django_tags("{{ test2 }} {% test3 %}") 48 '{{ test2 }} {% test3 %}' 40 49 """ 41 50 for source, dest in ENTITIES.iteritems(): 42 51 txt = txt.replace(source, dest) 43 52 return txt 53 54 55 # For make_kwagrs() 56 KEYWORD_MAP = { 57 "True": True, 58 "False": False, 59 "None": None, 60 } 61 62 def make_kwagrs(raw_content, encoding="UTF-8"): 63 """ 64 convert a string into a dictionary. e.g.: 65 66 >>> make_kwagrs('key1="value1" key2="value2"') 67 {'key2': 'value2', 'key1': 'value1'} 68 69 >>> make_kwagrs('A="B" C=1 D=1.1 E=True F=False G=None') 70 {'A': 'B', 'C': 1, 'E': True, 'D': '1.1', 'G': None, 'F': False} 71 72 >>> make_kwagrs(u'unicode=True') 73 {'unicode': True} 74 """ 75 if isinstance(raw_content, unicode): 76 # shlex.split doesn't work with unicode?!? 77 raw_content = raw_content.encode(encoding) 78 79 parts = shlex.split(raw_content) 80 81 result = {} 82 for part in parts: 83 key, value = part.split("=", 1) 84 85 if value in KEYWORD_MAP: 86 # True False or None 87 value = KEYWORD_MAP[value] 88 else: 89 # A number? 90 try: 91 value = int(value) 92 except ValueError: 93 pass 94 95 result[key] = value 96 97 return result 98 99 100 101 if __name__ == "__main__": 102 import doctest 103 doctest.testmod( 104 # verbose=True 105 verbose=False 106 ) 107 print "DocTest end." -
trunk/pylucid/tests/test_plugin_api.py
r1646 r1666 204 204 # plugin model tables should be re-created, too. 205 205 install_plugin( 206 package_name, plugin_name, page_msg, verbosity=2, active=True 206 package_name, plugin_name, 207 page_msg, verbosity=2, user=None, active=True 207 208 ) 208 209 … … 281 282 ) 282 283 self.assertEqual2(args, "()") 283 self.assertEqual2(kwargs, "{'arg1': u'test1'}")284 285 def test_ tag_more_args(self):286 """ 287 More string kwargs284 self.assertEqual2(kwargs, "{'arg1': 'test1'}") 285 286 def test_numbers(self): 287 """ 288 Test string and numbers 288 289 """ 289 290 args, kwargs = self._get_args_info( 290 '{% lucidTag unittest_plugin a="0" b= "1" c="2"%}'291 '{% lucidTag unittest_plugin a="0" b=1 c=2 %}' 291 292 ) 292 293 self.assertEqual2(args, "()") 293 self.assertEqual2(kwargs, "{'a': u'0', 'b': u'1', 'c': u'2'}") 294 295 def test_tag_bool_args1(self): 296 """ 297 The current implementation converts strings to bool if it found 298 the bool words. 294 self.assertEqual2(kwargs, "{'a': 0, 'b': 1, 'c': 2}") 295 296 def test_keywords(self): 297 """ 298 Test True, False and None 299 299 """ 300 300 args, kwargs = self._get_args_info( 301 '{% lucidTag unittest_plugin' 302 ' t1="True" f1="False" t2="true" f2="false" %}' 301 '{% lucidTag unittest_plugin a=True b=False c=None %}' 303 302 ) 304 303 self.assertEqual2(args, "()") 305 304 self.assertEqual2(kwargs, 306 "{'f1': False, 'f2': False, 't1': True, 't2': True}" 307 ) 308 309 #__________________________________________________________________________ 310 # Problems with the current implementation. 311 # We can't pass other things than string. 312 """ 313 Importtant: All follow test will failed! 314 See: 315 http://pylucid.net:8080/pylucid/ticket/202 316 http://pylucid.org/phpBB2/viewtopic.php?t=228 317 """ 318 def test_tag_bool_args2(self): 319 """ 320 Faild in the current implementation! 321 """ 322 args, kwargs = self._get_args_info( 323 '{% lucidTag unittest_plugin t1=True f1=False %}' 324 ) 325 self.assertEqual2(args, "()") 326 self.assertEqual2(kwargs, 327 "{'f1': False, 'f2': False}", 328 error_msg="Faild in the current implementation!" 329 ) 330 331 def test_non_string_args(self): 332 """ 333 Faild in the current implementation! 334 """ 335 args, kwargs = self._get_args_info( 336 '{% lucidTag unittest_plugin arg1=1 arg2=2.0 %}' 337 ) 338 self.assertEqual2(args, "()") 339 self.assertEqual2(kwargs, 340 "{'arg1': 1, 'args2': 2.0}", 341 error_msg="Faild in the current implementation!" 342 ) 343 344 345 305 "{'a': True, 'b': False, 'c': None}" 306 ) 307 308 309 class PluginPreferencesTest(PluginAPI_Base): 310 """ 311 Test the preferences API. 312 313 more info about plugin preferences: 314 http://www.pylucid.org/_goto/153/preferences/ 315 """ 316 def test(self): 317 url = self.command % "test_preferences" 318 content = self._get_plugin_content(url, debug=False) 319 self.assertEqual2( 320 content, 321 "{'index': u'Index', 'print_index': False, 'print_last_page': True," 322 " 'index_url': u'/'}" 323 ) 346 324 347 325