Verified Commit e92b9569 authored by Štěpán Henek's avatar Štěpán Henek 🌩

python3: unicode cleanup

parent 472108bc
......@@ -98,16 +98,12 @@ def safeunicode(obj, encoding='utf-8'):
u'\u1234'
"""
t = type(obj)
if t is unicode:
if isinstance(t, str):
return obj
elif t is str:
if isinstance(t, bytes):
return obj.decode(encoding)
elif t in [int, float, bool]:
return unicode(obj)
elif hasattr(obj, '__unicode__') or isinstance(obj, unicode):
return unicode(obj)
else:
return str(obj).decode(encoding)
return str(obj)
def safestr(obj, encoding='utf-8'):
......@@ -121,11 +117,9 @@ def safestr(obj, encoding='utf-8'):
>>> safestr(2)
'2'
"""
if isinstance(obj, unicode):
return obj.encode(encoding)
elif isinstance(obj, str):
return obj
elif hasattr(obj, 'next'): # iterator
if isinstance(obj, bytes):
return obj.decode(encoding)
elif hasattr(obj, 'next'): # iterator
return itertools.imap(safestr, obj)
else:
return str(obj)
......@@ -158,11 +152,11 @@ u'\u203d'
u'\u203d'
"""
if val is None:
return u''
elif isinstance(val, str):
return ''
elif isinstance(val, bytes):
val = val.decode('utf-8')
elif not isinstance(val, unicode):
val = unicode(val)
elif not isinstance(val, str):
val = str(val)
return htmlquote(val)
......@@ -170,7 +164,7 @@ u'\u203d'
def attrget(obj, attr, value=None):
try:
if hasattr(obj, 'has_key') and obj.has_key(attr):
if isinstance(obj, dict) and attr in obj:
return obj[attr]
except TypeError:
# Handle the case where has_key takes different number of arguments.
......
......@@ -285,7 +285,7 @@ class LenRange(Validator):
self.js_validator_params = "[%s,%s]" % (low, high)
def valid(self, value):
return self._low <= len(unicode(value.decode("utf8"))) <= self._high
return self._low <= len(value) <= self._high
class ByteLenRange(Validator):
......
......@@ -442,7 +442,7 @@ def makelocale(lang, plural_forms=None):
l18n.set_language("en")
for luci_tz, country, city, zoneinfo in tzdata:
regions.add(luci_tz.split("/")[0])
countries.add(unicode(l18n.territories[country]))
countries.add(l18n.territories[country])
cities.add(city)
po.metadata = {
......@@ -462,7 +462,7 @@ def makelocale(lang, plural_forms=None):
translations = {}
for part in (regions, cities, countries):
for x in part:
translations[x] = unicode(l18n.translation.L18NLazyString(x))
translations[x] = l18n.translation.L18NLazyString(x)
for msgid, msgstr in translations.items():
po.append(polib.POEntry(msgid=msgid, msgstr=msgstr))
......@@ -477,7 +477,7 @@ def maketzdata():
for luci_tz, zoneinfo in luci_TZ:
country = l18n.utils.get_country_code_from_tz(luci_tz.replace(" ", "_"))
city = unicode(l18n.tz_cities[luci_tz.replace(" ", "_")])
city = l18n.tz_cities[luci_tz.replace(" ", "_")]
tzdata.append((luci_tz, country, city, zoneinfo))
return tzdata
......@@ -493,7 +493,7 @@ def gencountries():
countries = {}
l18n.set_language("en")
for luci_tz, country, city, zoneinfo in tzdata:
countries[country] = unicode(l18n.territories[country])
countries[country] = l18n.territories[country]
pickle.dump(countries, file("foris/utils/countries.pickle2", "wb"), protocol=2)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment