Changeset 1666

Show
Ignore:
Timestamp:
07/10/08 13:37:49 (21 months ago)
Author:
JensDiemer
Message:

Fix for ticket:202 - kwargs in template tag {% lucidTag ... %}
Discuss in:  http://pylucid.org/phpBB2/viewtopic.php?t=228

Location:
trunk/pylucid
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid/PyLucid/template_addons/lucidTag.py

    r1634 r1666  
    2626from PyLucid.system.context_processors import add_css_tag 
    2727from PyLucid.tools.data_eval import data_eval, DataEvalError 
     28from PyLucid.tools.utils import make_kwagrs 
    2829 
    2930from django.conf import settings 
     
    9697 
    9798 
     99 
     100 
    98101def lucidTag(parser, token): 
    99102    """ 
     
    114117        method_name = "lucidTag" 
    115118 
    116 #    print "1", content 
    117 #    method_kwargs = {} 
    118 #    if content: 
    119 #        raw_kwargs = content[0] 
    120 #        kwargs = raw_kwargs.replace("=", ":") 
    121 #        kwargs = u"{ %s }" % kwargs 
    122 #        print "2", kwargs 
    123 #        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" % err 
    129 #            ) 
    130 # 
    131 #        print "3", method_kwargs 
    132  
    133     method_kwargs = {} 
    134119    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 = {} 
    146124 
    147125    return lucidTagNode(plugin_name, method_name, method_kwargs) 
  • trunk/pylucid/PyLucid/tools/utils.py

    r1657 r1666  
    1818""" 
    1919 
     20import shlex 
    2021from xml.sax.saxutils import escape as sax_escape 
    2122 
     
    3132    Escape "&", "<", ">" and django template tags chars like "{" and "}" 
    3233    defined in ENTITIES to the HTML character entity. 
     34    >>> escape("<test1> & {{ test2 }} {% test3 %}") 
     35    '&lt;test1&gt; &amp; &#x7B;&#x7B; test2 &#x7D;&#x7D; &#x7B;% test3 %&#x7D;' 
    3336    """ 
    3437    return sax_escape(txt, entities=ENTITIES) 
     
    3841    Escape only django template tags chars like "{" and "}" defined in ENTITIES 
    3942    to the HTML character entity. 
     43 
     44    >>> escape_django_tags("<test1> &") 
     45    '<test1> &' 
     46 
     47    >>> escape_django_tags("{{ test2 }} {% test3 %}") 
     48    '&#x7B;&#x7B; test2 &#x7D;&#x7D; &#x7B;% test3 %&#x7D;' 
    4049    """ 
    4150    for source, dest in ENTITIES.iteritems(): 
    4251        txt = txt.replace(source, dest) 
    4352    return txt 
     53 
     54 
     55# For make_kwagrs() 
     56KEYWORD_MAP = { 
     57    "True": True, 
     58    "False": False, 
     59    "None": None, 
     60} 
     61 
     62def 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 
     101if __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  
    204204        # plugin model tables should be re-created, too. 
    205205        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 
    207208        ) 
    208209 
     
    281282        ) 
    282283        self.assertEqual2(args, "()") 
    283         self.assertEqual2(kwargs, "{'arg1': u'test1'}") 
    284  
    285     def test_tag_more_args(self): 
    286         """ 
    287         More string kwargs 
     284        self.assertEqual2(kwargs, "{'arg1': 'test1'}") 
     285 
     286    def test_numbers(self): 
     287        """ 
     288        Test string and numbers 
    288289        """ 
    289290        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 %}' 
    291292        ) 
    292293        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 
    299299        """ 
    300300        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 %}' 
    303302        ) 
    304303        self.assertEqual2(args, "()") 
    305304        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 
     309class 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        ) 
    346324 
    347325