Commits

Andriy Kornatskyy  committed bfce558

Refactoring builder and widget by moving out some ulility functions to utils module.

  • Participants
  • Parent commits f017c02

Comments (0)

Files changed (3)

File src/wheezy/html/builder.py

 """ ``builder`` module.
 """
 
-from datetime import date
-from datetime import datetime
-
-from wheezy.html.comp import str_type
 from wheezy.html.markup import Tag
 from wheezy.html.widgets import default
+from wheezy.html.utils import format_value
 from wheezy.html.utils import html_escape
 
 
 
     def format(self, format_string=None, format_provider=None):
         """ Formats widget value.
-
-            ``format_provider`` - a callable of the following form::
-
-                def my_formatter(value, format_string):
-                    return value_formatted
-
-            >>> h = WidgetBuilder('date_of_birth', date(2012, 2, 6), None)
-            >>> h.format('%m-%d-%y').formatted
-            '02-06-12'
-            >>> h = WidgetBuilder('date_of_birth', date(2012, 2, 6), None)
-            >>> h.format(format_provider=lambda value, ignore:
-            ...         value.strftime('%m-%d-%y')).formatted
-            '02-06-12'
-            >>> h = WidgetBuilder('pref', [1, 2, 7], None)
-            >>> list(map(str, h.format().formatted))
-            ['1', '2', '7']
-            >>> h = WidgetBuilder('pref', [], None)
-            >>> h.format().formatted
-            ()
         """
         value = self.value
-        # TODO: probably there is better check since attribute check for
-        # __iter__ is not valid in python 3.2, str support it.
-        if isinstance(value, (list, tuple)):
-            try:
-                if format_provider is None:
-                    formatter_name = type(value[0]).__name__
-                    format_provider = format_providers[formatter_name]
-                self.formatted = tuple(format_provider(item, format_string)
-                        for item in value)
-            except IndexError:
-                self.formatted = tuple([])
-        else:
-            if format_provider is None:
-                formatter_name = type(value).__name__
-                format_provider = format_providers[formatter_name]
-            self.formatted = format_provider(self.value, format_string)
+        self.formatted = format_value(value, format_string, format_provider)
         return self
 
     def __repr__(self):
         return Tag('span', self.errors[-1], {
             'class': CSS_CLASS_ERROR
         })
-
-
-str_format_provider = lambda value, format_string: str_type(value)
-
-
-def date_format_provider(value, format_string=None):
-    """ Default format provider for ``datetime.date``.
-
-        >>> date_format_provider(date.min)
-        ''
-        >>> date_format_provider(date(2012, 2, 6))
-        '2012/02/06'
-    """
-    if date.min == value:
-        return ''
-    return value.strftime(str(format_string or '%Y/%m/%d'))
-
-
-def datetime_format_provider(value, format_string=None):
-    """ Default format provider for ``datetime.datetime``.
-
-        >>> datetime_format_provider(datetime.min)
-        ''
-        >>> datetime_format_provider(datetime(2012, 2, 6, 15, 17))
-        '2012/02/06 15:17'
-    """
-    if datetime.min == value:
-        return ''
-    return value.strftime(str(format_string or '%Y/%m/%d %H:%M'))
-
-
-format_providers = {
-        'str': lambda value, format_string: html_escape(str_type(value)),
-        'unicode': lambda value, format_string: html_escape(value),
-        'int': str_format_provider,
-        'Decimal': str_format_provider,
-        'bool': str_format_provider,
-        'float': str_format_provider,
-        'date': date_format_provider,
-        'time': lambda value, format_string: value.strftime(
-            str(format_string or '%H:%M')),
-        'datetime': datetime_format_provider
-}

File src/wheezy/html/utils.py

 """ ``utils`` module.
 """
 
+from datetime import date
+from datetime import datetime
+
+from wheezy.html.comp import str_type
+
 
 def html_escape(s):
     """ Escapes a string so it is valid within HTML.
     """
     return s.replace('&', '&amp;').replace('<', '&lt;'
             ).replace('>', '&gt;').replace('"', '&quot;')
+
+
+html_id = lambda name: name.replace('_', '-')
+
+
+def format_value(value, format_spec=None, format_provider=None):
+    """ Formats widget value.
+
+        ``format_provider`` - a callable of the following form::
+
+            def my_formatter(value, format_spec):
+                return value_formatted
+
+        >>> format_value(date(2012, 2, 6), '%m-%d-%y')
+        '02-06-12'
+        >>> format_value(date(2012, 2, 6),
+        ...         format_provider=lambda value, ignore:
+        ...         value.strftime('%m-%d-%y'))
+        '02-06-12'
+        >>> list(map(str, format_value([1, 2, 7])))
+        ['1', '2', '7']
+        >>> format_value([])
+        ()
+    """
+    # TODO: probably there is better check since attribute check for
+    # __iter__ is not valid in python 3.2, str support it.
+    if isinstance(value, (list, tuple)):
+        try:
+            if format_provider is None:
+                formatter_name = type(value[0]).__name__
+                format_provider = format_providers[formatter_name]
+            return tuple(format_provider(item, format_spec)
+                    for item in value)
+        except IndexError:
+            return tuple([])
+    else:
+        if format_provider is None:
+            formatter_name = type(value).__name__
+            format_provider = format_providers[formatter_name]
+        return format_provider(value, format_spec)
+
+
+str_format_provider = lambda value, format_spec: str_type(value)
+
+
+def date_format_provider(value, format_spec=None):
+    """ Default format provider for ``datetime.date``.
+
+        >>> date_format_provider(date.min)
+        ''
+        >>> date_format_provider(date(2012, 2, 6))
+        '2012/02/06'
+    """
+    if date.min == value:
+        return ''
+    return value.strftime(str(format_spec or '%Y/%m/%d'))
+
+
+def datetime_format_provider(value, format_spec=None):
+    """ Default format provider for ``datetime.datetime``.
+
+        >>> datetime_format_provider(datetime.min)
+        ''
+        >>> datetime_format_provider(datetime(2012, 2, 6, 15, 17))
+        '2012/02/06 15:17'
+    """
+    if datetime.min == value:
+        return ''
+    return value.strftime(str(format_spec or '%Y/%m/%d %H:%M'))
+
+
+format_providers = {
+        'str': lambda value, format_spec: html_escape(str_type(value)),
+        'unicode': lambda value, format_spec: html_escape(value),
+        'int': str_format_provider,
+        'Decimal': str_format_provider,
+        'bool': str_format_provider,
+        'float': str_format_provider,
+        'date': date_format_provider,
+        'time': lambda value, format_spec: value.strftime(
+            str(format_spec or '%H:%M')),
+        'datetime': datetime_format_provider
+}

File src/wheezy/html/widgets.py

 
 from wheezy.html.markup import Fragment
 from wheezy.html.markup import Tag
-
-
-id = lambda name: name.replace('_', '-')
+from wheezy.html.utils import html_id
 
 
 def hidden(name, value, attrs=None):
             name="zip_code" />
     """
     tag_attrs = {
-            'id': id(name),
+            'id': html_id(name),
             'name': name,
             'type': 'text',
             'value': value
             name="passwd" />
     """
     tag_attrs = {
-            'id': id(name),
+            'id': html_id(name),
             'name': name,
             'type': 'password',
             'value': value
             id="message-text"></textarea>
     """
     tag_attrs = {
-            'id': id(name),
+            'id': html_id(name),
             'name': name,
             'rows': '9',
             'cols': '40'
             value="1" class="b" />
      """
     tag_attrs = {
-            'id': id(name),
+            'id': html_id(name),
             'name': name,
             'type': 'checkbox',
             'value': '1'
         <label class="inline" for="zip-code">Zip Code</label>
     """
     tag_attrs = {
-            'for': id(name)
+            'for': html_id(name)
     }
     if attrs:
         tag_attrs.update(attrs)
             }
         append(Tag('option', text, tag_attrs))
     tag_attrs = {
-            'id': id(name),
+            'id': html_id(name),
             'name': name
     }
     if attrs:
             }
         append(Tag('option', text, tag_attrs))
     tag_attrs = {
-            'id': id(name),
+            'id': html_id(name),
             'name': name,
             'multiple': 'multiple'
     }