Anonymous avatar Anonymous committed 37784ed

Initial py3k porting effort.

Comments (0)

Files changed (11)

webhelpers/constants.py

-# -*- encoding: latin-1 -*-
+y# -*- encoding: latin-1 -*-
 # Latin-1 encoding needed for countries list.
 """Place names and other constants often used in web forms.
 """
 
+# hide from 2to3
+exec('''
+def isstring(obj):
+    return isinstance(obj, basestring)
+''')
+try:
+    isstring("")
+except NameError:
+    def isstring(obj):
+        return isinstance(obj, str) or isinstance(obj, bytes)
+
+
+def _ucs(string, charset=None):                            # ||:fnc:||
+    return unicode(string, charset or 'utf-8')
+try:
+    _ucs("")
+except NameError:
+    _ucs = lambda s, c=None: s.decode(c or 'utf-8')
+
+uc_type = type(_ucs(b""))
+
+def ucs(value, charset=None):                              # ||:fnc:||
+    """\
+    Convert `value` to unicode string using charset or UTF-8, if
+    `value` is a string.
+
+    If `value` is not a string, or if it is already a unicode string,
+    return it unmodified.
+    """
+    if isstring(value) and not isinstance(value, uc_type):
+        return _ucs(value, charset)
+    return value
+
+def u8s(string):                                           # ||:fnc:||
+    """\
+    Convert `string` to UTF-8-encoded byte string, if `string` is a
+    unicode string.
+
+    If `string` is not a unicode string, return it unmodified.
+    """
+    if isinstance(string, uc_type):
+        return string.encode('utf-8')
+    return string
+
+def nts(string):                                           # ||:fnc:||
+    """\
+    Convert string to native string, if applicable.
+
+    Python2 native strings are byte strings, while Python3 native
+    strings are unicode.
+    """
+    # for python3, unicode strings have type str
+    if isinstance(string, str):
+        return string
+    # for python2, encode unicode strings to utf-8 strings
+    if isinstance(string, uc_type):
+        return string.encode('utf-8')
+    if isstring(string):
+        try:
+            return str(string.decode('utf-8'))
+        except UnicodeDecodeError:
+            #return str(string.decode('latin1'))
+            pass
+    return string
+
+# Aliases (old py_b.strings)
+# uc = ucs
+# utf8str = u8s
+# nativestr = nts
+
+
 def uk_counties():
     """\
     Return a list of UK county names.
         return _country_codes
     else:
         
-        text_directly_from_iso_website = u"""
+        text_directly_from_iso_website = ucs("""
 A   	  
 AFGHANISTAN 	AF
 ÅLAND ISLANDS 	AX
 ZAIRE 	see CONGO, THE DEMOCRATIC REPUBLIC OF THE
 ZAMBIA 	ZM
 ZIMBABWE 	ZW
-""".replace('\t','    ').split('\n')
+""").replace('\t','    ').split('\n')
     e = []
     for item in text_directly_from_iso_website:
         if len(item) > 1:

webhelpers/feedgenerator.py

 import datetime
 from webhelpers.util import SimplerXMLGenerator, iri_to_uri
 
+
+# hide from 2to3
+exec('''
+def isstring(obj):
+    return isinstance(obj, basestring)
+''')
+try:
+    isstring("")
+except NameError:
+    def isstring(obj):
+        return isinstance(obj, str) or isinstance(obj, bytes)
+
+
+def _ucs(string, charset=None):                            # ||:fnc:||
+    return unicode(string, charset or 'utf-8')
+try:
+    _ucs("")
+except NameError:
+    _ucs = lambda s, c=None: s.decode(c or 'utf-8')
+
+uc_type = type(_ucs(b""))
+
+def ucs(value, charset=None):                              # ||:fnc:||
+    """\
+    Convert `value` to unicode string using charset or UTF-8, if
+    `value` is a string.
+
+    If `value` is not a string, or if it is already a unicode string,
+    return it unmodified.
+    """
+    if isstring(value) and not isinstance(value, uc_type):
+        return _ucs(value, charset)
+    return value
+
+def u8s(string):                                           # ||:fnc:||
+    """\
+    Convert `string` to UTF-8-encoded byte string, if `string` is a
+    unicode string.
+
+    If `string` is not a unicode string, return it unmodified.
+    """
+    if isinstance(string, uc_type):
+        return string.encode('utf-8')
+    return string
+
+def nts(string):                                           # ||:fnc:||
+    """\
+    Convert string to native string, if applicable.
+
+    Python2 native strings are byte strings, while Python3 native
+    strings are unicode.
+    """
+    # for python3, unicode strings have type str
+    if isinstance(string, str):
+        return string
+    # for python2, encode unicode strings to utf-8 strings
+    if isinstance(string, uc_type):
+        return string.encode('utf-8')
+    if isstring(string):
+        try:
+            return str(string.decode('utf-8'))
+        except UnicodeDecodeError:
+            #return str(string.decode('latin1'))
+            pass
+    return string
+
 #### The following code comes from ``django.utils.feedgenerator`` ####
 
 def rfc2822_date(date):
     if date is not None:
         tag = re.sub('/', ',%s:/' % date.strftime('%Y-%m-%d'), tag, 1)
     tag = re.sub('#', '/', tag)
-    return u'tag:' + tag
+    return ucs('tag:') + ucs(tag)
 
 class SyndicationFeed(object):
     "Base class for all syndication feeds. Subclasses should provide write()"
 
 class RssFeed(SyndicationFeed):
     mime_type = 'application/rss+xml'
-    _version = u"?"
+    _version = ucs("?")
     def write(self, outfile, encoding):
         handler = SimplerXMLGenerator(outfile, encoding)
         handler.startDocument()
-        handler.startElement(u"rss", self.rss_attributes())
-        handler.startElement(u"channel", self.root_attributes())
+        handler.startElement(ucs("rss"), self.rss_attributes())
+        handler.startElement(ucs("channel"), self.root_attributes())
         self.add_root_elements(handler)
         self.write_items(handler)
         self.endChannelElement(handler)
-        handler.endElement(u"rss")
+        handler.endElement(ucs("rss"))
 
     def rss_attributes(self):
-        return {u"version": self._version}
+        return {ucs("version"): self._version}
 
     def write_items(self, handler):
         for item in self.items:
-            handler.startElement(u'item', self.item_attributes(item))
+            handler.startElement(ucs('item'), self.item_attributes(item))
             self.add_item_elements(handler, item)
-            handler.endElement(u"item")
+            handler.endElement(ucs("item"))
 
     def add_root_elements(self, handler):
-        handler.addQuickElement(u"title", self.feed['title'])
-        handler.addQuickElement(u"link", self.feed['link'])
-        handler.addQuickElement(u"description", self.feed['description'])
+        handler.addQuickElement(ucs("title"), self.feed['title'])
+        handler.addQuickElement(ucs("link"), self.feed['link'])
+        handler.addQuickElement(ucs("description"), self.feed['description'])
         if self.feed['language'] is not None:
-            handler.addQuickElement(u"language", self.feed['language'])
+            handler.addQuickElement(ucs("language"), self.feed['language'])
         for cat in self.feed['categories']:
-            handler.addQuickElement(u"category", cat)
+            handler.addQuickElement(ucs("category"), cat)
         if self.feed['feed_copyright'] is not None:
-            handler.addQuickElement(u"copyright", self.feed['feed_copyright'])
-        handler.addQuickElement(u"lastBuildDate", rfc2822_date(self.latest_post_date()).decode('utf-8'))
+            handler.addQuickElement(ucs("copyright"), self.feed['feed_copyright'])
+        handler.addQuickElement(ucs("lastBuildDate"), rfc2822_date(self.latest_post_date()).decode('utf-8'))
         if self.feed['ttl'] is not None:
-            handler.addQuickElement(u"ttl", self.feed['ttl'])
+            handler.addQuickElement(ucs("ttl"), self.feed['ttl'])
 
     def endChannelElement(self, handler):
-        handler.endElement(u"channel")
+        handler.endElement(ucs("channel"))
 
 class RssUserland091Feed(RssFeed):
-    _version = u"0.91"
+    _version = ucs("0.91")
     def add_item_elements(self, handler, item):
-        handler.addQuickElement(u"title", item['title'])
-        handler.addQuickElement(u"link", item['link'])
+        handler.addQuickElement(ucs("title"), item['title'])
+        handler.addQuickElement(ucs("link"), item['link'])
         if item['description'] is not None:
-            handler.addQuickElement(u"description", item['description'])
+            handler.addQuickElement(ucs("description"), item['description'])
 
 class Rss201rev2Feed(RssFeed):
     # Spec: http://blogs.law.harvard.edu/tech/rss
-    _version = u"2.0"
+    _version = ucs("2.0")
     def add_item_elements(self, handler, item):
-        handler.addQuickElement(u"title", item['title'])
-        handler.addQuickElement(u"link", item['link'])
+        handler.addQuickElement(ucs("title"), item['title'])
+        handler.addQuickElement(ucs("link"), item['link'])
         if item['description'] is not None:
-            handler.addQuickElement(u"description", item['description'])
+            handler.addQuickElement(ucs("description"), item['description'])
 
         # Author information.
         if item["author_name"] and item["author_email"]:
-            handler.addQuickElement(u"author", "%s (%s)" % \
+            handler.addQuickElement(ucs("author"), "%s (%s)" % \
                 (item['author_email'], item['author_name']))
         elif item["author_email"]:
-            handler.addQuickElement(u"author", item["author_email"])
+            handler.addQuickElement(ucs("author"), item["author_email"])
         elif item["author_name"]:
-            handler.addQuickElement(u"dc:creator", item["author_name"], {"xmlns:dc": u"http://purl.org/dc/elements/1.1/"})
+            handler.addQuickElement(ucs("dc:creator"), item["author_name"], {"xmlns:dc": ucs("http://purl.org/dc/elements/1.1/")})
 
         if item['pubdate'] is not None:
-            handler.addQuickElement(u"pubDate", rfc2822_date(item['pubdate']).decode('utf-8'))
+            handler.addQuickElement(ucs("pubDate"), rfc2822_date(item['pubdate']).decode('utf-8'))
         if item['comments'] is not None:
-            handler.addQuickElement(u"comments", item['comments'])
+            handler.addQuickElement(ucs("comments"), item['comments'])
         if item['unique_id'] is not None:
-            handler.addQuickElement(u"guid", item['unique_id'])
+            handler.addQuickElement(ucs("guid"), item['unique_id'])
         if item['ttl'] is not None:
-            handler.addQuickElement(u"ttl", item['ttl'])
+            handler.addQuickElement(ucs("ttl"), item['ttl'])
 
         # Enclosure.
         if item['enclosure'] is not None:
-            handler.addQuickElement(u"enclosure", '',
-                {u"url": item['enclosure'].url, u"length": item['enclosure'].length,
-                    u"type": item['enclosure'].mime_type})
+            handler.addQuickElement(ucs("enclosure"), '',
+                {ucs("url"): item['enclosure'].url, ucs("length"): item['enclosure'].length,
+                    ucs("type"): item['enclosure'].mime_type})
 
         # Categories.
         for cat in item['categories']:
-            handler.addQuickElement(u"category", cat)
+            handler.addQuickElement(ucs("category"), cat)
 
 class Atom1Feed(SyndicationFeed):
     # Spec: http://atompub.org/2005/07/11/draft-ietf-atompub-format-10.html
     mime_type = 'application/atom+xml'
-    ns = u"http://www.w3.org/2005/Atom"
+    ns = ucs("http://www.w3.org/2005/Atom")
 
     def write(self, outfile, encoding):
         handler = SimplerXMLGenerator(outfile, encoding)
         handler.startDocument()
-        handler.startElement(u'feed', self.root_attributes())
+        handler.startElement(ucs('feed'), self.root_attributes())
         self.add_root_elements(handler)
         self.write_items(handler)
-        handler.endElement(u"feed")
+        handler.endElement(ucs("feed"))
 
     def root_attributes(self):
         if self.feed['language'] is not None:
-            return {u"xmlns": self.ns, u"xml:lang": self.feed['language']}
+            return {ucs("xmlns"): self.ns, ucs("xml:lang"): self.feed['language']}
         else:
-            return {u"xmlns": self.ns}
+            return {ucs("xmlns"): self.ns}
 
     def add_root_elements(self, handler):
-        handler.addQuickElement(u"title", self.feed['title'])
-        handler.addQuickElement(u"link", "", {u"rel": u"alternate", u"href": self.feed['link']})
+        handler.addQuickElement(ucs("title"), self.feed['title'])
+        handler.addQuickElement(ucs("link"), "", {ucs("rel"): ucs("alternate"), ucs("href"): self.feed['link']})
         if self.feed['feed_url'] is not None:
-            handler.addQuickElement(u"link", "", {u"rel": u"self", u"href": self.feed['feed_url']})
-        handler.addQuickElement(u"id", self.feed['id'])
-        handler.addQuickElement(u"updated", rfc3339_date(self.latest_post_date()).decode('utf-8'))
+            handler.addQuickElement(ucs("link"), "", {ucs("rel"): ucs("self"), ucs("href"): self.feed['feed_url']})
+        handler.addQuickElement(ucs("id"), self.feed['id'])
+        handler.addQuickElement(ucs("updated"), rfc3339_date(self.latest_post_date()).decode('utf-8'))
         if self.feed['author_name'] is not None:
-            handler.startElement(u"author", {})
-            handler.addQuickElement(u"name", self.feed['author_name'])
+            handler.startElement(ucs("author"), {})
+            handler.addQuickElement(ucs("name"), self.feed['author_name'])
             if self.feed['author_email'] is not None:
-                handler.addQuickElement(u"email", self.feed['author_email'])
+                handler.addQuickElement(ucs("email"), self.feed['author_email'])
             if self.feed['author_link'] is not None:
-                handler.addQuickElement(u"uri", self.feed['author_link'])
-            handler.endElement(u"author")
+                handler.addQuickElement(ucs("uri"), self.feed['author_link'])
+            handler.endElement(ucs("author"))
         if self.feed['subtitle'] is not None:
-            handler.addQuickElement(u"subtitle", self.feed['subtitle'])
+            handler.addQuickElement(ucs("subtitle"), self.feed['subtitle'])
         for cat in self.feed['categories']:
-            handler.addQuickElement(u"category", "", {u"term": cat})
+            handler.addQuickElement(ucs("category"), "", {ucs("term"): cat})
         if self.feed['feed_copyright'] is not None:
-            handler.addQuickElement(u"rights", self.feed['feed_copyright'])
+            handler.addQuickElement(ucs("rights"), self.feed['feed_copyright'])
 
     def write_items(self, handler):
         for item in self.items:
-            handler.startElement(u"entry", self.item_attributes(item))
+            handler.startElement(ucs("entry"), self.item_attributes(item))
             self.add_item_elements(handler, item)
-            handler.endElement(u"entry")
+            handler.endElement(ucs("entry"))
 
     def add_item_elements(self, handler, item):
-        handler.addQuickElement(u"title", item['title'])
-        handler.addQuickElement(u"link", u"", {u"href": item['link'], u"rel": u"alternate"})
+        handler.addQuickElement(ucs("title"), item['title'])
+        handler.addQuickElement(ucs("link"), ucs(""), {ucs("href"): item['link'], ucs("rel"): ucs("alternate")})
         if item['pubdate'] is not None:
-            handler.addQuickElement(u"updated", rfc3339_date(item['pubdate']).decode('utf-8'))
-            handler.addQuickElement(u"published", rfc3339_date(item['pubdate']).decode('utf-8'))
+            handler.addQuickElement(ucs("updated"), rfc3339_date(item['pubdate']).decode('utf-8'))
+            handler.addQuickElement(ucs("published"), rfc3339_date(item['pubdate']).decode('utf-8'))
 
         # Author information.
         if item['author_name'] is not None:
-            handler.startElement(u"author", {})
-            handler.addQuickElement(u"name", item['author_name'])
+            handler.startElement(ucs("author"), {})
+            handler.addQuickElement(ucs("name"), item['author_name'])
             if item['author_email'] is not None:
-                handler.addQuickElement(u"email", item['author_email'])
+                handler.addQuickElement(ucs("email"), item['author_email'])
             if item['author_link'] is not None:
-                handler.addQuickElement(u"uri", item['author_link'])
-            handler.endElement(u"author")
+                handler.addQuickElement(ucs("uri"), item['author_link'])
+            handler.endElement(ucs("author"))
 
         # Unique ID.
         if item['unique_id'] is not None:
             unique_id = item['unique_id']
         else:
             unique_id = get_tag_uri(item['link'], item['pubdate'])
-        handler.addQuickElement(u"id", unique_id)
+        handler.addQuickElement(ucs("id"), unique_id)
 
         # Summary.
         if item['description'] is not None:
-            handler.addQuickElement(u"summary", item['description'], {u"type": u"html"})
+            handler.addQuickElement(ucs("summary"), item['description'], {ucs("type"): ucs("html")})
 
         # Enclosure.
         if item['enclosure'] is not None:
-            handler.addQuickElement(u"link", '',
-                {u"rel": u"enclosure",
-                 u"href": item['enclosure'].url,
-                 u"length": item['enclosure'].length,
-                 u"type": item['enclosure'].mime_type})
+            handler.addQuickElement(ucs("link"), '',
+                {ucs("rel"): ucs("enclosure"),
+                 ucs("href"): item['enclosure'].url,
+                 ucs("length"): item['enclosure'].length,
+                 ucs("type"): item['enclosure'].mime_type})
 
         # Categories.
         for cat in item['categories']:
-            handler.addQuickElement(u"category", u"", {u"term": cat})
+            handler.addQuickElement(ucs("category"), ucs(""), {ucs("term"): cat})
 
         # Rights.
         if item['item_copyright'] is not None:
-            handler.addQuickElement(u"rights", item['item_copyright'])
+            handler.addQuickElement(ucs("rights"), item['item_copyright'])
 
 # This isolates the decision of what the system default is, so calling code can
 # do "feedgenerator.DefaultFeed" instead of "feedgenerator.Rss201rev2Feed".
         a unicode GeoRSS representation.
         """
         if self.is_input_latitude_first:
-            return u' '.join([u'%f %f' % x for x in coords])
+            return ucs(' ').join([ucs('%f %f') % x for x in coords])
         else:
-            return u' '.join([u'%f %f' % (x[1], x[0]) for x in coords])
+            return ucs(' ').join([ucs('%f %f') % (x[1], x[0]) for x in coords])
 
     def add_georss_point(self, handler, coords, w3c_geo=False):
         """
                 lat, lon = coords[:2]
             else:
                 lon, lat = coords[:2]
-            handler.addQuickElement(u'geo:lat', u'%f' % lat)
-            handler.addQuickElement(u'geo:lon', u'%f' % lon)
+            handler.addQuickElement(ucs('geo:lat'), ucs('%f') % lat)
+            handler.addQuickElement(ucs('geo:lon'), ucs('%f') % lon)
         else:
-            handler.addQuickElement(u'georss:point', self.georss_coords((coords,)))
+            handler.addQuickElement(ucs('georss:point'), self.georss_coords((coords,)))
 
     def add_georss_element(self, handler, item, w3c_geo=False):
         """
                 # If a GeoRSS box was given via tuple.
                 if not box_coords is None:
                     if w3c_geo: raise ValueError('Cannot use simple GeoRSS box in W3C Geo feeds.')
-                    handler.addQuickElement(u'georss:box', self.georss_coords(box_coords))
+                    handler.addQuickElement(ucs('georss:box'), self.georss_coords(box_coords))
             else:
                 # Getting the lower-case geometry type.
                 gtype = str(geom.geom_type).lower()
                     # For formatting consistent w/the GeoRSS simple standard:
                     # http://georss.org/1.0#simple
                     if gtype in ('linestring', 'linearring'):
-                        handler.addQuickElement(u'georss:line', self.georss_coords(geom.coords))
+                        handler.addQuickElement(ucs('georss:line'), self.georss_coords(geom.coords))
                     elif gtype in ('polygon',):
                         # Only support the exterior ring.
-                        handler.addQuickElement(u'georss:polygon', self.georss_coords(geom[0].coords))
+                        handler.addQuickElement(ucs('georss:polygon'), self.georss_coords(geom[0].coords))
                     else:
                         raise ValueError('Geometry type "%s" not supported.' % geom.geom_type)
 
 class GeoRSSFeed(Rss201rev2Feed, GeoFeedMixin):
     def rss_attributes(self):
         attrs = super(GeoRSSFeed, self).rss_attributes()
-        attrs[u'xmlns:georss'] = u'http://www.georss.org/georss'
+        attrs[ucs('xmlns:georss')] = ucs('http://www.georss.org/georss')
         return attrs
 
     def add_item_elements(self, handler, item):
 class GeoAtom1Feed(Atom1Feed, GeoFeedMixin):
     def root_attributes(self):
         attrs = super(GeoAtom1Feed, self).root_attributes()
-        attrs[u'xmlns:georss'] = u'http://www.georss.org/georss'
+        attrs[ucs('xmlns:georss')] = ucs('http://www.georss.org/georss')
         return attrs
 
     def add_item_elements(self, handler, item):
 class W3CGeoFeed(Rss201rev2Feed, GeoFeedMixin):
     def rss_attributes(self):
         attrs = super(W3CGeoFeed, self).rss_attributes()
-        attrs[u'xmlns:geo'] = u'http://www.w3.org/2003/01/geo/wgs84_pos#'
+        attrs[ucs('xmlns:geo')] = ucs('http://www.w3.org/2003/01/geo/wgs84_pos#')
         return attrs
 
     def add_item_elements(self, handler, item):

webhelpers/html/builder.py

 from urllib import quote as url_escape
 from UserDict import DictMixin
 
+
+# hide from 2to3
+exec('''
+def isstring(obj):
+    return isinstance(obj, basestring)
+''')
+try:
+    isstring("")
+except NameError:
+    def isstring(obj):
+        return isinstance(obj, str) or isinstance(obj, bytes)
+
+
+def _ucs(string, charset=None):                            # ||:fnc:||
+    return unicode(string, charset or 'utf-8')
+try:
+    _ucs("")
+except NameError:
+    _ucs = lambda s, c=None: s.decode(c or 'utf-8')
+
+uc_type = type(_ucs(b""))
+
+def ucs(value, charset=None):                              # ||:fnc:||
+    """\
+    Convert `value` to unicode string using charset or UTF-8, if
+    `value` is a string.
+
+    If `value` is not a string, or if it is already a unicode string,
+    return it unmodified.
+    """
+    if isstring(value) and not isinstance(value, uc_type):
+        return _ucs(value, charset)
+    return value
+
+def u8s(string):                                           # ||:fnc:||
+    """\
+    Convert `string` to UTF-8-encoded byte string, if `string` is a
+    unicode string.
+
+    If `string` is not a unicode string, return it unmodified.
+    """
+    if isinstance(string, uc_type):
+        return string.encode('utf-8')
+    return string
+
+def nts(string):                                           # ||:fnc:||
+    """\
+    Convert string to native string, if applicable.
+
+    Python2 native strings are byte strings, while Python3 native
+    strings are unicode.
+    """
+    # for python3, unicode strings have type str
+    if isinstance(string, str):
+        return string
+    # for python2, encode unicode strings to utf-8 strings
+    if isinstance(string, uc_type):
+        return string.encode('utf-8')
+    if isstring(string):
+        try:
+            return str(string.decode('utf-8'))
+        except UnicodeDecodeError:
+            #return str(string.decode('latin1'))
+            pass
+    return string
+
 import markupsafe
 try:
     from markupsafe import escape_silent as escape
     >>> format_attrs(p=None)
     literal(u'')
     """
-    strings = [u' %s="%s"' % (_attr_decode(attr), escape(value))
+    strings = [ucs(' %s="%s"') % (_attr_decode(attr), escape(value))
         for attr, value in sorted(attrs.iteritems())
         if value is not None]
     return literal("".join(strings))
 HTML = HTMLBuilder()
 
 # Constants depending on ``literal()`` and/or ``HTML``.
-NL = literal(u"\n")
-EMPTY = literal(u"")
+NL = literal(ucs("\n"))
+EMPTY = literal(ucs(""))
 BR = HTML.br(_nl=True)
-_CDATA_START = literal(u"<![CDATA[") 
-_CDATA_END = literal(u"]]>")
+_CDATA_START = literal(ucs("<![CDATA[")) 
+_CDATA_END = literal(ucs("]]>"))

webhelpers/html/grid_demo.py

     return urlpath + "?" + urllib.urlencode(params)
 
 def write_file(dir, filename, content):
-    print "... writing '%s'" % filename
+    print("... writing '%s'" % filename)
     path = os.path.join(dir, filename)
     f = open(path, "w")
     f.write(content)
     dir = args[0]
     if not os.path.exists(dir):
         os.makedirs(dir)
-    print "Putting output in directory '%s'" % dir
+    print("Putting output in directory '%s'" % dir)
     write_file(dir, "demo.css", STYLESHEET)
     for class_ in demos:
         d = class_()

webhelpers/html/render.py

         prog = os.path.basename(sys.argv[0])
         sys.exit("usage: %s <HTML_FILE" % prog)
     html = sys.stdin.read()
-    print render(html)
+    print(render(html))
 
 if __name__ == "__main__":  main()

webhelpers/html/tools.py

 import urllib
 import warnings
 
+
+# hide from 2to3
+exec('''
+def isstring(obj):
+    return isinstance(obj, basestring)
+''')
+try:
+    isstring("")
+except NameError:
+    def isstring(obj):
+        return isinstance(obj, str) or isinstance(obj, bytes)
+
+
+def _ucs(string, charset=None):                            # ||:fnc:||
+    return unicode(string, charset or 'utf-8')
+try:
+    _ucs("")
+except NameError:
+    _ucs = lambda s, c=None: s.decode(c or 'utf-8')
+
+uc_type = type(_ucs(b""))
+
+def ucs(value, charset=None):                              # ||:fnc:||
+    """\
+    Convert `value` to unicode string using charset or UTF-8, if
+    `value` is a string.
+
+    If `value` is not a string, or if it is already a unicode string,
+    return it unmodified.
+    """
+    if isstring(value) and not isinstance(value, uc_type):
+        return _ucs(value, charset)
+    return value
+
+def u8s(string):                                           # ||:fnc:||
+    """\
+    Convert `string` to UTF-8-encoded byte string, if `string` is a
+    unicode string.
+
+    If `string` is not a unicode string, return it unmodified.
+    """
+    if isinstance(string, uc_type):
+        return string.encode('utf-8')
+    return string
+
+def nts(string):                                           # ||:fnc:||
+    """\
+    Convert string to native string, if applicable.
+
+    Python2 native strings are byte strings, while Python3 native
+    strings are unicode.
+    """
+    # for python3, unicode strings have type str
+    if isinstance(string, str):
+        return string
+    # for python2, encode unicode strings to utf-8 strings
+    if isinstance(string, uc_type):
+        return string.encode('utf-8')
+    if isstring(string):
+        try:
+            return str(string.decode('utf-8'))
+        except UnicodeDecodeError:
+            #return str(string.decode('latin1'))
+            pass
+    return string
+
 from webhelpers.html import HTML, literal, lit_sub, escape
 import webhelpers.html.tags as tags
 
         
     """
     if not text:
-        return literal(u"")
+        return literal(ucs(""))
     text = escape(text)
     if link == "all":
         return _auto_link_urls(_auto_link_email_addresses(text), **href_attrs)

webhelpers/markdown.py

 """
 
 
+# hide from 2to3
+exec('''
+def isstring(obj):
+    return isinstance(obj, basestring)
+''')
+try:
+    isstring("")
+except NameError:
+    def isstring(obj):
+        return isinstance(obj, str) or isinstance(obj, bytes)
+
+
+def _ucs(string, charset=None):                            # ||:fnc:||
+    return unicode(string, charset or 'utf-8')
+try:
+    _ucs("")
+except NameError:
+    _ucs = lambda s, c=None: s.decode(c or 'utf-8')
+
+uc_type = type(_ucs(b""))
+
+def ucs(value, charset=None):                              # ||:fnc:||
+    """\
+    Convert `value` to unicode string using charset or UTF-8, if
+    `value` is a string.
+
+    If `value` is not a string, or if it is already a unicode string,
+    return it unmodified.
+    """
+    if isstring(value) and not isinstance(value, uc_type):
+        return _ucs(value, charset)
+    return value
+
+def u8s(string):                                           # ||:fnc:||
+    """\
+    Convert `string` to UTF-8-encoded byte string, if `string` is a
+    unicode string.
+
+    If `string` is not a unicode string, return it unmodified.
+    """
+    if isinstance(string, uc_type):
+        return string.encode('utf-8')
+    return string
+
+def nts(string):                                           # ||:fnc:||
+    """\
+    Convert string to native string, if applicable.
+
+    Python2 native strings are byte strings, while Python3 native
+    strings are unicode.
+    """
+    # for python3, unicode strings have type str
+    if isinstance(string, str):
+        return string
+    # for python2, encode unicode strings to utf-8 strings
+    if isinstance(string, uc_type):
+        return string.encode('utf-8')
+    if isstring(string):
+        try:
+            return str(string.decode('utf-8'))
+        except UnicodeDecodeError:
+            #return str(string.decode('latin1'))
+            pass
+    return string
+
 import re, sys, codecs
 
 from logging import getLogger, StreamHandler, Formatter, \
 SMART_EMPHASIS = 1        # this_or_that does not become this<i>or</i>that
 HTML_REMOVED_TEXT = "[HTML_REMOVED]" # text used instead of HTML in safe mode
 
-RTL_BIDI_RANGES = ( (u'\u0590', u'\u07FF'),
+RTL_BIDI_RANGES = ( (ucs('\u0590'), ucs('\u07FF')),
                     # from Hebrew to Nko (includes Arabic, Syriac and Thaana)
-                    (u'\u2D30', u'\u2D7F'),
+                    (ucs('\u2D30'), ucs('\u2D7F')),
                     # Tifinagh
                     )
 
             self.source = source
 
         if not self.source:
-            return u""
+            return ucs("")
 
         try:
             self.source = unicode(self.source)
         except UnicodeDecodeError:
             message(CRITICAL, 'UnicodeDecodeError: Markdown only accepts unicode or ascii  input.')
-            return u""
+            return ucs("")
 
         for pp in self.textPreprocessors:
             self.source = pp.run(self.source)
                     'encoding': None }
 
         else:
-            print OPTPARSE_WARNING
+            print(OPTPARSE_WARNING)
             return None
 
     parser = optparse.OptionParser(usage="%prog INPUTFILE [options]")

webhelpers/media.py

         sys.exit("usage: %s FILES ...\nPrints dimensions of each image")
     for file in files:
         apath = os.path.abspath(file)
-        print "%s:" % apath,
+        print("%s:" % apath,)
         if not os.path.isfile(file):
-            print "does not exist or is not a plain file"
+            print("does not exist or is not a plain file")
             continue
         width, height = get_dimensions(file)
         if width is None and height is None:
-            print "could not get dimensions"
+            print("could not get dimensions")
         else:
             if width is None:
                 width = "UNKNOWN"
             if height is None:
                 height = "UNKNOWN"
-            print "%s x %s" % (width, height)
+            print("%s x %s" % (width, height))
             
         
 if __name__ == "__main__":  test_get_dimensions()

webhelpers/paginate.py

 
     def __getitem__(self, range):
         if not isinstance(range, slice):
-            raise Exception, "__getitem__ without slicing not supported"
+            raise Exception("__getitem__ without slicing not supported")
         offset = range.start
         limit = range.stop - range.start
         select = self.obj.offset(offset).limit(limit)
 
     def __getitem__(self, range):
         if not isinstance(range, slice):
-            raise Exception, "__getitem__ without slicing not supported"
+            raise Exception("__getitem__ without slicing not supported")
         return self.obj[range]
 
     def __len__(self):
                     first = self.first_item - 1
                     last = self.last_item
                     self.items = list(self.collection[first:last])
-                except TypeError, e:
+                except TypeError:
+                    (t, e, tb) = sys.exc_info()
+                    del(tb)
                     if str(e) == "unhashable type":
                         # Assume this means collection is unsliceable.
                         raise TypeError(INCOMPATIBLE_COLLECTION_TYPE)

webhelpers/pylonslib/minify.py

 import StringIO
 import warnings
 
+
+# hide from 2to3
+exec('''
+def isstring(obj):
+    return isinstance(obj, basestring)
+''')
+try:
+    isstring("")
+except NameError:
+    def isstring(obj):
+        return isinstance(obj, str) or isinstance(obj, bytes)
+
+
+def _ucs(string, charset=None):                            # ||:fnc:||
+    return unicode(string, charset or 'utf-8')
+try:
+    _ucs("")
+except NameError:
+    _ucs = lambda s, c=None: s.decode(c or 'utf-8')
+
+uc_type = type(_ucs(b""))
+
+def ucs(value, charset=None):                              # ||:fnc:||
+    """\
+    Convert `value` to unicode string using charset or UTF-8, if
+    `value` is a string.
+
+    If `value` is not a string, or if it is already a unicode string,
+    return it unmodified.
+    """
+    if isstring(value) and not isinstance(value, uc_type):
+        return _ucs(value, charset)
+    return value
+
+def u8s(string):                                           # ||:fnc:||
+    """\
+    Convert `string` to UTF-8-encoded byte string, if `string` is a
+    unicode string.
+
+    If `string` is not a unicode string, return it unmodified.
+    """
+    if isinstance(string, uc_type):
+        return string.encode('utf-8')
+    return string
+
+def nts(string):                                           # ||:fnc:||
+    """\
+    Convert string to native string, if applicable.
+
+    Python2 native strings are byte strings, while Python3 native
+    strings are unicode.
+    """
+    # for python3, unicode strings have type str
+    if isinstance(string, str):
+        return string
+    # for python2, encode unicode strings to utf-8 strings
+    if isinstance(string, uc_type):
+        return string.encode('utf-8')
+    if isstring(string):
+        try:
+            return str(string.decode('utf-8'))
+        except UnicodeDecodeError:
+            #return str(string.decode('latin1'))
+            pass
+    return string
+
 from webhelpers.html.tags import javascript_link as __javascript_link
 from webhelpers.html.tags import stylesheet_link as __stylesheet_link
 
             def do_css_CSSStyleDeclaration(self, style, separator=None):
                 try:
                     color = style.getPropertyValue('color')
-                    if color and color is not u'':
+                    if color and color is not ucs(''):
                         color = self.change_colors(color)
                         style.setProperty('color', color)
                 except:

webhelpers/textile.py

 
 
 if __name__ == '__main__':
-    print textile('tell me about textile.', head_offset=1)
+    print(textile('tell me about textile.', head_offset=1))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.