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

Added python3 compatibility for base64 encoding #24

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 13 additions & 2 deletions vobject/icalendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

from dateutil import rrule, tz

import sys

if sys.version_info >= (3,0):
from base64 import b64encode

from . import behavior
from .base import (VObjectError, NativeError, ValidateError, ParseError,
Component, ContentLine, logger, registerBehavior,
Expand Down Expand Up @@ -606,7 +611,10 @@ def decode(cls, line):
if line.encoded:
encoding = getattr(line, 'encoding_param', None)
if encoding and encoding.upper() == cls.base64string:
line.value = line.value.decode('base64')
if sys.version_info >= (3,0):
line.value = b46encode(bytes(line.value, 'utf-8'))
else:
line.value = line.value.decode('base64')
else:
line.value = stringToTextValues(line.value)[0]
line.encoded=False
Expand All @@ -619,7 +627,10 @@ def encode(cls, line):
if not line.encoded:
encoding = getattr(line, 'encoding_param', None)
if encoding and encoding.upper() == cls.base64string:
line.value = line.value.encode('base64').replace('\n', '')
if sys.version_info >= (3,0):
line.value = b46encode(bytes(line.value.replace('\n', ''), 'utf-8'))
else:
line.value = line.value.encode('base64').replace('\n', '')
else:
line.value = backslashEscape(str_(line.value))
line.encoded=True
Expand Down
14 changes: 12 additions & 2 deletions vobject/vcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from .base import ContentLine, registerBehavior, backslashEscape
from .icalendar import stringToTextValues

import sys

if sys.version_info >= (3,0):
from base64 import b64encode

# Python 3 no longer has a basestring type, so....
try:
Expand Down Expand Up @@ -134,7 +138,10 @@ def decode(cls, line):
line.encoding_param = cls.base64string
encoding = getattr(line, 'encoding_param', None)
if encoding:
line.value = line.value.decode('base64')
if sys.version_info >= (3,0):
line.value = b46encode(bytes(line.value, 'utf-8'))
else:
line.value = line.value.decode('base64')
else:
line.value = stringToTextValues(line.value)[0]
line.encoded=False
Expand All @@ -147,7 +154,10 @@ def encode(cls, line):
if not line.encoded:
encoding = getattr(line, 'encoding_param', None)
if encoding and encoding.upper() == cls.base64string:
line.value = line.value.encode('base64').replace('\n', '')
if sys.version_info >= (3,0):
line.value = b46encode(bytes(line.value.replace('\n', ''), 'utf-8'))
else:
line.value = line.value.encode('base64').replace('\n', '')
else:
line.value = backslashEscape(line.value)
line.encoded=True
Expand Down