Browse Source

[minor] unicode fix for frappe/erpnext#7070 (#2478)

version-14
Rushabh Mehta 8 years ago
committed by GitHub
parent
commit
a6d5cec95c
2 changed files with 14 additions and 20 deletions
  1. +12
    -11
      frappe/__init__.py
  2. +2
    -9
      frappe/utils/data.py

+ 12
- 11
frappe/__init__.py View File

@@ -41,22 +41,25 @@ class _dict(dict):
def _(msg, lang=None):
"""Returns translated string in current lang, if exists."""
from frappe.translate import get_full_dict
from frappe.utils import cstr

if not lang:
lang = local.lang

# msg should always be unicode
msg = cstr(msg).strip()
msg = as_unicode(msg).strip()

return get_full_dict(local.lang).get(msg) or msg

def as_unicode(text, encoding='utf-8'):
'''Convert to unicode if required'''
if text and not isinstance(text, unicode):
if isinstance(text, unicode):
return text
elif text==None:
return ''
elif isinstance(text, basestring):
return unicode(text, encoding)
else:
return text or ''
return unicode(text)

def get_lang_dict(fortype, name=None):
"""Returns the translated language dict for the given type and name.
@@ -232,11 +235,11 @@ def errprint(msg):
"""Log error. This is sent back as `exc` in response.

:param msg: Message."""
from utils import cstr
msg = as_unicode(msg)
if not request or (not "cmd" in local.form_dict):
print cstr(msg)
print msg.encode('utf-8')

error_log.append(cstr(msg))
error_log.append(msg)

def log(msg):
"""Add to `debug_log`.
@@ -246,8 +249,7 @@ def log(msg):
if conf.get("logging") or False:
print repr(msg)

from utils import cstr
debug_log.append(cstr(msg))
debug_log.append(as_unicode(msg))

def msgprint(msg, title=None, raise_exception=0, as_table=False, indicator=None, alert=False):
"""Print a message to the user (via HTTP response).
@@ -852,13 +854,12 @@ def get_file_json(path):

def read_file(path, raise_not_found=False):
"""Open a file and return its content as Unicode."""
from frappe.utils import cstr
if isinstance(path, unicode):
path = path.encode("utf-8")

if os.path.exists(path):
with open(path, "r") as f:
return cstr(f.read())
return as_unicode(f.read())
elif raise_not_found:
raise IOError("{} Not Found".format(path))
else:


+ 2
- 9
frappe/utils/data.py View File

@@ -271,15 +271,8 @@ def cint(s):
except: num = 0
return num

def cstr(s):
if isinstance(s, unicode):
return s
elif s==None:
return ''
elif isinstance(s, basestring):
return unicode(s, 'utf-8')
else:
return unicode(s)
def cstr(s, encoding='utf-8'):
return frappe.as_unicode(s, encoding)

def rounded(num, precision=0):
"""round method for round halfs to nearest even algorithm aka banker's rounding - compatible with python3"""


Loading…
Cancel
Save