[uiprops] introduce lazystr to get a chance to change style without having to change all properties
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 08 Jul 2010 12:54:27 +0200
changeset 5938 9cb13d1b2ce4
parent 5937 82cac9383dd8
child 5939 a33402c1aa79
[uiprops] introduce lazystr to get a chance to change style without having to change all properties
web/propertysheet.py
web/test/data/sheet1.py
web/test/unittest_propertysheet.py
--- a/web/propertysheet.py	Thu Jul 08 12:54:25 2010 +0200
+++ b/web/propertysheet.py	Thu Jul 08 12:54:27 2010 +0200
@@ -23,6 +23,13 @@
 import os
 import os.path as osp
 
+class lazystr(object):
+    def __init__(self, string, context):
+        self.string = string
+        self.context = context
+    def __str__(self):
+        return self.string % self.context
+
 
 class PropertySheet(dict):
     def __init__(self, cache_directory, **context):
@@ -30,8 +37,12 @@
         self.context = context
         self.reset()
         context['sheet'] = self
+        context['lazystr'] = self.lazystr
         self._percent_rgx = re.compile('%(?!\()')
 
+    def lazystr(self, str):
+        return lazystr(str, self)
+
     def reset(self):
         self.clear()
         self._ordered_propfiles = []
--- a/web/test/data/sheet1.py	Thu Jul 08 12:54:25 2010 +0200
+++ b/web/test/data/sheet1.py	Thu Jul 08 12:54:27 2010 +0200
@@ -1,3 +1,4 @@
 bgcolor = '#000000'
 stylesheets = ['%s/cubicweb.css' % datadir_url]
 logo = '%s/logo.png' % datadir_url
+lazy = lazystr('%(bgcolor)s')
--- a/web/test/unittest_propertysheet.py	Thu Jul 08 12:54:25 2010 +0200
+++ b/web/test/unittest_propertysheet.py	Thu Jul 08 12:54:27 2010 +0200
@@ -27,6 +27,10 @@
         # defined by sheet1, extended by sheet2
         self.assertEquals(ps['stylesheets'], ['http://cwtest.com/cubicweb.css',
                                               'http://cwtest.com/mycube.css'])
+        # lazy string defined by sheet1
+        self.assertIsInstance(ps['lazy'], lazystr)
+        self.assertEquals(str(ps['lazy']), '#FFFFFF')
+        # test compilation
         self.assertEquals(ps.compile('a {bgcolor: %(bgcolor)s; size: 1%;}'),
                           'a {bgcolor: #FFFFFF; size: 1%;}')
         self.assertEquals(ps.process_resource(DATADIR, 'pouet.css'),