Changeset 12231


Ignore:
Timestamp:
14 Dec 2014, 16:01:35 (10 years ago)
Author:
Henrik Bettermann
Message:

Add helper function html2dict.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/utils
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/helpers.py

    r11824 r12231  
    3939from zope.security.management import getInteraction
    4040from zope.pluggableauth.interfaces import IAuthenticatorPlugin
     41from zope.formlib.widget import renderElement
    4142
    4243BUFSIZE = 8 * 1024
     
    804805        result[key] = value
    805806    return result
     807
     808def html2dict(value=None,portal_language='en'):
     809    """Transforms a localized HTML text string into a dictionary.
     810
     811    Different languages must be separated by `>>xy<<` whereas
     812    xy is the language code. Text parts without correct leading
     813    language separator - usually the first part has no language
     814    descriptor - are interpreted as texts in the portal's language.
     815    The latter can be configured in waeup.srp.utils.utils.IkobaUtils.
     816    """
     817    try:
     818        parts = value.split('>>')
     819    except:
     820        return {}
     821    elements = {}
     822    lang = portal_language
     823    for part in parts:
     824        if part[2:4] == u'<<':
     825            lang = part[0:2].lower()
     826            text = part[4:]
     827            elements[lang] = renderElement(u'div id="html"',
     828                contents=text)
     829        else:
     830            text = part
     831            elements[lang] = renderElement(u'div id="html"',
     832                contents=text)
     833    return elements
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_helpers.py

    r11824 r12231  
    582582        info = helpers.get_meminfo(src="nOt-ExIsTiNg-FiLe")
    583583        assert info is None
     584
     585
     586class Html2dictTestCase(unittest.TestCase):
     587
     588    def test_html2dict(self):
     589        assert helpers.html2dict(None) == {}
     590        assert helpers.html2dict() == {}
     591        assert helpers.html2dict(9) == {}
     592        assert helpers.html2dict('Hello world') == {
     593            'en': u'<div id="html">Hello world</div id="html">'}
     594        assert helpers.html2dict('Hello world>>de<<Hallo Welt') == {
     595            'de': u'<div id="html">Hallo Welt</div id="html">',
     596            'en': u'<div id="html">Hello world</div id="html">'}
    584597
    585598
     
    603616        MemInfoTestCase,
    604617        GetMemInfoTestCase,
     618        Html2dictTestCase,
    605619        ]:
    606620        suite.addTests(
Note: See TracChangeset for help on using the changeset viewer.