diff --git a/tests/__init__.py b/tests/__init__.py index da81e00..0bfc4c9 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -10,7 +10,7 @@ ''' import vdirsyncer.log -from vdirsyncer.utils import text_type +from vdirsyncer.utils.compat import text_type from vdirsyncer.utils.vobject import normalize_item as _normalize_item vdirsyncer.log.set_level(vdirsyncer.log.logging.DEBUG) diff --git a/tests/storage/__init__.py b/tests/storage/__init__.py index 20803d0..d8147d3 100644 --- a/tests/storage/__init__.py +++ b/tests/storage/__init__.py @@ -13,7 +13,7 @@ import pytest from .. import assert_item_equals, SIMPLE_TEMPLATE import vdirsyncer.exceptions as exceptions from vdirsyncer.storage.base import Item -from vdirsyncer.utils import text_type, iteritems +from vdirsyncer.utils.compat import text_type, iteritems class StorageTests(object): diff --git a/vdirsyncer/storage/base.py b/vdirsyncer/storage/base.py index d6984a0..e85b8f2 100644 --- a/vdirsyncer/storage/base.py +++ b/vdirsyncer/storage/base.py @@ -10,6 +10,7 @@ from .. import exceptions from .. import utils +from ..utils.compat import text_type class Item(object): @@ -33,7 +34,7 @@ class Item(object): This is either the UID or the hash of the item's content.''' def __init__(self, raw): - assert isinstance(raw, utils.text_type) + assert isinstance(raw, text_type) for line in raw.splitlines(): if line.startswith(u'UID:'): diff --git a/vdirsyncer/storage/dav.py b/vdirsyncer/storage/dav.py index ac5cb5a..12725e1 100644 --- a/vdirsyncer/storage/dav.py +++ b/vdirsyncer/storage/dav.py @@ -290,10 +290,10 @@ class DavStorage(Storage): raise ValueError(href) x = utils.urlparse.urljoin(self.session.url, href) assert x.startswith(self.session.url) - return utils.urlunquote_plus(utils.urlparse.urlsplit(x).path) + return utils.compat.urlunquote_plus(utils.urlparse.urlsplit(x).path) def _get_href(self, item): - href = utils.urlunquote_plus(item.ident) + self.fileext + href = utils.compat.urlunquote_plus(item.ident) + self.fileext return self._normalize_href(href) def get(self, href): diff --git a/vdirsyncer/storage/filesystem.py b/vdirsyncer/storage/filesystem.py index 055f74a..6f1c093 100644 --- a/vdirsyncer/storage/filesystem.py +++ b/vdirsyncer/storage/filesystem.py @@ -12,8 +12,8 @@ import os from .base import Item, Storage import vdirsyncer.exceptions as exceptions import vdirsyncer.log as log -from vdirsyncer.utils import expand_path, text_type, safe_write, \ - get_etag_from_file, checkdir +from ..utils import expand_path, safe_write, get_etag_from_file, checkdir +from ..utils.compat import text_type logger = log.get(__name__) diff --git a/vdirsyncer/storage/http.py b/vdirsyncer/storage/http.py index 978b7de..63a65b7 100644 --- a/vdirsyncer/storage/http.py +++ b/vdirsyncer/storage/http.py @@ -8,7 +8,8 @@ ''' from .base import Item, Storage -from ..utils import expand_path, get_password, request, text_type, urlparse +from ..utils import expand_path, get_password, request +from ..utils.compat import text_type, urlparse from ..utils.vobject import split_collection from ..exceptions import NotFoundError diff --git a/vdirsyncer/storage/singlefile.py b/vdirsyncer/storage/singlefile.py index 1ed95b4..ee35d08 100644 --- a/vdirsyncer/storage/singlefile.py +++ b/vdirsyncer/storage/singlefile.py @@ -12,8 +12,9 @@ import collections from .base import Item, Storage import vdirsyncer.exceptions as exceptions import vdirsyncer.log as log -from vdirsyncer.utils import expand_path, safe_write, itervalues, checkfile -from vdirsyncer.utils.vobject import split_collection, join_collection +from ..utils import expand_path, safe_write, checkfile +from ..utils.compat import itervalues +from ..utils.vobject import split_collection, join_collection logger = log.get(__name__) diff --git a/vdirsyncer/sync.py b/vdirsyncer/sync.py index e93525e..2d03549 100644 --- a/vdirsyncer/sync.py +++ b/vdirsyncer/sync.py @@ -18,7 +18,7 @@ import itertools from . import exceptions, log -from .utils import iteritems, text_type +from .utils.compat import iteritems, text_type sync_logger = log.get(__name__) diff --git a/vdirsyncer/utils/__init__.py b/vdirsyncer/utils/__init__.py index c639ae0..246c2ec 100644 --- a/vdirsyncer/utils/__init__.py +++ b/vdirsyncer/utils/__init__.py @@ -8,35 +8,15 @@ ''' import os -import sys import requests from .. import log, exceptions +from .compat import urlparse logger = log.get(__name__) -PY2 = sys.version_info[0] == 2 - - -if PY2: - import urlparse - from urllib import \ - quote_plus as urlquote_plus, \ - unquote_plus as urlunquote_plus - text_type = unicode # flake8: noqa - iteritems = lambda x: x.iteritems() - itervalues = lambda x: x.itervalues() -else: - import urllib.parse as urlparse - urlquote_plus = urlparse.quote_plus - urlunquote_plus = urlparse.unquote_plus - text_type = str - iteritems = lambda x: x.items() - itervalues = lambda x: x.values() - - try: import keyring except ImportError: @@ -284,6 +264,7 @@ def get_class_init_args(cls): return all | s_all, required | s_required + def checkdir(path, create=False): if not os.path.isdir(path): if os.path.exists(path): @@ -296,6 +277,7 @@ def checkdir(path, create=False): 'create it, or create it ' 'yourself.'.format(path)) + def checkfile(path, create=False): checkdir(os.path.dirname(path), create=create) if not os.path.isfile(path): diff --git a/vdirsyncer/utils/compat.py b/vdirsyncer/utils/compat.py new file mode 100644 index 0000000..94dc822 --- /dev/null +++ b/vdirsyncer/utils/compat.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +''' + vdirsyncer.utils.compat + ~~~~~~~~~~~~~~~~~~~~~~~ + + :copyright: (c) 2014 Markus Unterwaditzer & contributors + :license: MIT, see LICENSE for more details. +''' + +import sys + +PY2 = sys.version_info[0] == 2 + + +if PY2: + import urlparse + from urllib import \ + quote_plus as urlquote_plus, \ + unquote_plus as urlunquote_plus + text_type = unicode # flake8: noqa + iteritems = lambda x: x.iteritems() + itervalues = lambda x: x.itervalues() +else: + import urllib.parse as urlparse + urlquote_plus = urlparse.quote_plus + urlunquote_plus = urlparse.unquote_plus + text_type = str + iteritems = lambda x: x.items() + itervalues = lambda x: x.values() diff --git a/vdirsyncer/utils/vobject.py b/vdirsyncer/utils/vobject.py index 9e01272..8ffd4b2 100644 --- a/vdirsyncer/utils/vobject.py +++ b/vdirsyncer/utils/vobject.py @@ -12,7 +12,7 @@ import icalendar.cal import icalendar.parser import icalendar.caselessdict -from . import text_type, itervalues +from .compat import text_type, itervalues def _process_properties(*s):