Skip to content

Commit

Permalink
Merge pull request #70 from eventable/fix-unicode
Browse files Browse the repository at this point in the history
Standardize Unicode outputs
  • Loading branch information
wpercy authored Jun 22, 2017
2 parents 3e3ed81 + 8df21f7 commit d94876e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_unicode(self):
self.assertEqual(str(vevent), str(vevent2))
self.assertEqual(
vevent.summary.value,
'The title こんにちはキティ'
u'The title こんにちはキティ'
)

def test_wrapping(self):
Expand Down
38 changes: 23 additions & 15 deletions vobject/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

def str_(s):
"""
Return string with correct encoding
Return byte string with correct encoding
"""
if type(s) == unicode:
return s.encode('utf-8')
Expand Down Expand Up @@ -76,11 +76,11 @@ def to_basestring(s):
DEBUG = False # Don't waste time on debug calls

# ----------------------------------- Constants --------------------------------
CR = '\r'
LF = '\n'
CRLF = CR + LF
SPACE = ' '
TAB = '\t'
CR = '\r'
LF = '\n'
CRLF = CR + LF
SPACE = ' '
TAB = '\t'
SPACEORTAB = SPACE + TAB

# --------------------------------- Main classes -------------------------------
Expand Down Expand Up @@ -360,8 +360,7 @@ def copy(self, copyit):

def __eq__(self, other):
try:
return (self.name == other.name) and (self.params == other.params)\
and (self.value == other.value)
return (self.name == other.name) and (self.params == other.params) and (self.value == other.value)
except Exception:
return False

Expand Down Expand Up @@ -428,11 +427,17 @@ def valueRepr(self):
return v

def __str__(self):
return "<{0}{1}{2}>".format(self.name, self.params, self.valueRepr())
try:
return "<{0}{1}{2}>".format(self.name, self.params, self.valueRepr())
except UnicodeEncodeError as e:
return "<{0}{1}{2}>".format(self.name, self.params, self.valueRepr().encode('utf-8'))

def __repr__(self):
return self.__str__()

def __unicode__(self):
return u"<{0}{1}{2}>".format(self.name, self.params, self.valueRepr())

def prettyPrint(self, level=0, tabwidth=3):
pre = ' ' * level * tabwidth
print(pre, self.name + ":", self.valueRepr())
Expand Down Expand Up @@ -473,8 +478,8 @@ def __init__(self, name=None, *args, **kwds):
self.autoBehavior()

@classmethod
def duplicate(clz, copyit):
newcopy = clz()
def duplicate(cls, copyit):
newcopy = cls()
newcopy.copy(copyit)
return newcopy

Expand Down Expand Up @@ -946,7 +951,7 @@ def foldOneLine(outbuf, input, lineLength=75):
outbuf.write(bytes(input, 'UTF-8'))
except Exception:
# fall back on py2 syntax
outbuf.write(str_(input))
outbuf.write(input)

else:
# Look for valid utf8 range and write that out
Expand Down Expand Up @@ -1013,12 +1018,15 @@ def defaultSerialize(obj, buf, lineLength):

if obj.group is not None:
s.write(obj.group + '.')
s.write(obj.name.upper())
s.write(str_(obj.name.upper()))
keys = sorted(obj.params.keys())
for key in keys:
paramstr = ','.join(dquoteEscape(p) for p in obj.params[key])
s.write(";{0}={1}".format(key, paramstr))
s.write(":{0}".format(str_(obj.value)))
try:
s.write(":{0}".format(obj.value))
except (UnicodeDecodeError, UnicodeEncodeError) as e:
s.write(":{0}".format(obj.value.encode('utf-8')))
if obj.behavior and not startedEncoded:
obj.behavior.decode(obj)
foldOneLine(outbuf, s.getvalue(), lineLength)
Expand Down Expand Up @@ -1067,7 +1075,7 @@ def readComponents(streamOrString, validate=False, transform=True,
Generate one Component at a time from a stream.
"""
if isinstance(streamOrString, basestring):
stream = six.StringIO(str_(streamOrString))
stream = six.StringIO(streamOrString)
else:
stream = streamOrString

Expand Down
4 changes: 2 additions & 2 deletions vobject/icalendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def getrruleset(self, addRDate=False):

# a Ruby iCalendar library escapes semi-colons in rrules,
# so also remove any backslashes
value = str_(line.value).replace('\\', '')
value = line.value.replace('\\', '')
rule = rrule.rrulestr(
value, dtstart=dtstart,
# If dtstart has no time zone, `until`
Expand Down Expand Up @@ -657,7 +657,7 @@ def encode(cls, line):
if encoding and encoding.upper() == cls.base64string:
line.value = base64.b64encode(line.value.encode('utf-8')).decode('utf-8').replace('\n', '')
else:
line.value = backslashEscape(str_(line.value))
line.value = backslashEscape(line.value)
line.encoded = True


Expand Down

0 comments on commit d94876e

Please sign in to comment.