Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

specify properties before calendar components (icalendar) #98

Merged
merged 3 commits into from
Sep 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dist/**/*
vobject.egg-info/**/*
venv/**/*
.idea/*
*.sublime-*
2 changes: 1 addition & 1 deletion vobject/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ def defaultSerialize(obj, buf, lineLength):
s.write(";{0}={1}".format(key, paramstr))
try:
s.write(":{0}".format(obj.value))
except (UnicodeDecodeError, UnicodeEncodeError) as e:
except (UnicodeDecodeError, UnicodeEncodeError):
s.write(":{0}".format(obj.value.encode('utf-8')))
if obj.behavior and not startedEncoded:
obj.behavior.decode(obj)
Expand Down
62 changes: 60 additions & 2 deletions vobject/icalendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import print_function

import codecs
import datetime
import logging
import random # for generating a UID
Expand Down Expand Up @@ -32,7 +31,7 @@ class NonExistentTimeError(Exception):
from . import behavior
from .base import (VObjectError, NativeError, ValidateError, ParseError,
Component, ContentLine, logger, registerBehavior,
backslashEscape, foldOneLine, str_)
backslashEscape, foldOneLine)


# ------------------------------- Constants ------------------------------------
Expand Down Expand Up @@ -974,6 +973,65 @@ def findTzids(obj, table):
tzid = toUnicode(tzid)
if tzid != u'UTC' and tzid not in oldtzids:
obj.add(TimezoneComponent(tzinfo=getTzid(tzid)))

@classmethod
def serialize(cls, obj, buf, lineLength, validate=True):
"""
Set implicit parameters, do encoding, return unicode string.

If validate is True, raise VObjectError if the line doesn't validate
after implicit parameters are generated.

Default is to call base.defaultSerialize.

"""

cls.generateImplicitParameters(obj)
if validate:
cls.validate(obj, raiseException=True)
if obj.isNative:
transformed = obj.transformFromNative()
undoTransform = True
else:
transformed = obj
undoTransform = False
out = None
outbuf = buf or six.StringIO()
if obj.group is None:
groupString = ''
else:
groupString = obj.group + '.'
if obj.useBegin:
foldOneLine(outbuf, "{0}BEGIN:{1}".format(groupString, obj.name),
lineLength)

try:
first_props = [s for s in cls.sortFirst if s in obj.contents \
and not isinstance(obj.contents[s][0], Component)]
first_components = [s for s in cls.sortFirst if s in obj.contents \
and isinstance(obj.contents[s][0], Component)]
except Exception:
first_props = first_components = []
# first_components = []

prop_keys = sorted(list(k for k in obj.contents.keys() if k not in first_props \
and not isinstance(obj.contents[k][0], Component)))
comp_keys = sorted(list(k for k in obj.contents.keys() if k not in first_components \
and isinstance(obj.contents[k][0], Component)))

sorted_keys = first_props + prop_keys + first_components + comp_keys
children = [o for k in sorted_keys for o in obj.contents[k]]

for child in children:
# validate is recursive, we only need to validate once
child.serialize(outbuf, lineLength, validate=False)
if obj.useBegin:
foldOneLine(outbuf, "{0}END:{1}".format(groupString, obj.name),
lineLength)
out = buf or outbuf.getvalue()
if undoTransform:
obj.transformToNative()
return out
registerBehavior(VCalendar2_0)


Expand Down