From d885c3708c9902f6e2c38ce073697b938f80b39a Mon Sep 17 00:00:00 2001 From: Alexander Shorin Date: Fri, 21 Mar 2014 15:00:09 +0400 Subject: [PATCH 1/4] Fix imports Use relative imports for package's modules Fix import urlparse for Python 3.x Remove unused imports --- cloudant/__init__.py | 14 +++++++------- cloudant/account.py | 6 +++++- cloudant/resource.py | 14 ++++++++++---- test/__init__.py | 5 ++--- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/cloudant/__init__.py b/cloudant/__init__.py index 04cacf4..9dfeac4 100644 --- a/cloudant/__init__.py +++ b/cloudant/__init__.py @@ -1,7 +1,7 @@ -from resource import Resource -from account import Account -from database import Database -from document import Document -from design import Design -from attachment import Attachment -from index import Index +from .resource import Resource +from .account import Account +from .database import Database +from .document import Document +from .design import Design +from .attachment import Attachment +from .index import Index diff --git a/cloudant/account.py b/cloudant/account.py index 580b218..4449c8a 100644 --- a/cloudant/account.py +++ b/cloudant/account.py @@ -1,6 +1,10 @@ from .resource import Resource from .database import Database -import urlparse + +try: + import urllib.parse as urlparse +except ImportError: + import urlparse class Account(Resource): diff --git a/cloudant/resource.py b/cloudant/resource.py index cdadc6e..990ad0f 100644 --- a/cloudant/resource.py +++ b/cloudant/resource.py @@ -1,14 +1,20 @@ +import json +import copy + +import requests + + try: from requests_futures.sessions import FuturesSession requests_futures_available = True except ImportError: requests_futures_available = False -import urlparse -import json -import copy -import requests +try: + import urllib.parse as urlparse +except ImportError: + import urlparse class RequestsFutureNotAvailable(Exception): diff --git a/test/__init__.py b/test/__init__.py index bcb70e6..03f9db5 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -1,7 +1,6 @@ import cloudant from collections import defaultdict import unittest -import os class ResourceTest(unittest.TestCase): @@ -137,10 +136,10 @@ class DatabaseTest(ResourceTest): def setUp(self): super(DatabaseTest, self).setUp() - + db_name = '/'.join([self.uri, self.db_name]) self.db = cloudant.Database(db_name) - + response = self.db.put() response.raise_for_status() From ea3f35ca59d3d529af77831f89f25e2a260ea85f Mon Sep 17 00:00:00 2001 From: Alexander Shorin Date: Fri, 21 Mar 2014 15:10:34 +0400 Subject: [PATCH 2/4] Support Python 3.x --- cloudant/account.py | 2 +- cloudant/database.py | 4 ++-- cloudant/design.py | 4 ++-- cloudant/document.py | 2 +- cloudant/index.py | 4 ++-- cloudant/resource.py | 2 +- test/__init__.py | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cloudant/account.py b/cloudant/account.py index 4449c8a..aeee48c 100644 --- a/cloudant/account.py +++ b/cloudant/account.py @@ -41,7 +41,7 @@ def __init__(self, uri="http://localhost:5984", **kwargs): def database(self, name, **kwargs): """Create a `Database` object prefixed with this account's URL.""" - opts = dict(self.opts.items() + kwargs.items()) + opts = dict(self.opts, **kwargs) return Database(self._make_url(name), session=self._session, **opts) def __getitem__(self, name): diff --git a/cloudant/database.py b/cloudant/database.py index e599631..8403215 100644 --- a/cloudant/database.py +++ b/cloudant/database.py @@ -16,7 +16,7 @@ def document(self, name, **kwargs): """ Create a `Document` object from `name`. """ - opts = dict(self.opts.items() + kwargs.items()) + opts = dict(self.opts, **kwargs) return Document(self._make_url(name), session=self._session, **opts) def design(self, name, **kwargs): @@ -26,7 +26,7 @@ def design(self, name, **kwargs): db.design('test') # refers to DB/_design/test """ - opts = dict(self.opts.items() + kwargs.items()) + opts = dict(self.opts, **kwargs) return Design(self._make_url('/'.join(['_design', name])), session=self._session, **opts) def __getitem__(self, name): diff --git a/cloudant/design.py b/cloudant/design.py index b1755b1..1ffc245 100644 --- a/cloudant/design.py +++ b/cloudant/design.py @@ -16,7 +16,7 @@ def index(self, path, **kwargs): index = doc.index('_view/index-name') # refers to /DB/_design/DOC/_view/index-name """ - opts = dict(self.opts.items() + kwargs.items()) + opts = dict(self.opts, **kwargs) return Index(self._make_url(path), session=self._session, **opts) def view(self, function, **kwargs): @@ -65,4 +65,4 @@ def show(self, function, id, **kwargs): For more details on show functions, see [Querying Show Functions](http://docs.cloudant.com/api/design-documents-shows-lists.html#querying-show-functions). """ - return self.get(self._make_url('/'.join(['_show', function, id])), **kwargs) \ No newline at end of file + return self.get(self._make_url('/'.join(['_show', function, id])), **kwargs) diff --git a/cloudant/document.py b/cloudant/document.py index 8749423..6904c57 100644 --- a/cloudant/document.py +++ b/cloudant/document.py @@ -15,7 +15,7 @@ def attachment(self, name, **kwargs): Create an `Attachment` object from `name` and the settings for the current database. """ - opts = dict(self.opts.items() + kwargs.items()) + opts = dict(self.opts, **kwargs) return Attachment(self._make_url(name), session=self._session, **opts) def merge(self, change, **kwargs): diff --git a/cloudant/index.py b/cloudant/index.py index ade7301..8dbd37b 100644 --- a/cloudant/index.py +++ b/cloudant/index.py @@ -31,7 +31,7 @@ def __iter__(self, **kwargs): line = line[:-1] try: yield json.loads(line) - except ValueError: + except (TypeError, ValueError): # if we can't decode a line, ignore it pass @@ -49,4 +49,4 @@ def iter(self, **kwargs): # emits only rows with the key 'thegoodstuff' # with each row's emitting document """ - return self.__iter__(**kwargs) \ No newline at end of file + return self.__iter__(**kwargs) diff --git a/cloudant/resource.py b/cloudant/resource.py index 990ad0f..843ada2 100644 --- a/cloudant/resource.py +++ b/cloudant/resource.py @@ -40,7 +40,7 @@ def __init__(self, uri, **kwargs): if kwargs.get('session'): self._session = kwargs['session'] del kwargs['session'] - elif kwargs.has_key('async'): + elif 'async' in kwargs: if kwargs['async']: if not requests_futures_available: raise RequestsFutureNotAvailable() diff --git a/test/__init__.py b/test/__init__.py index 03f9db5..d87228a 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -10,7 +10,7 @@ def setUp(self): names = cloudant.Account(self.uri).uuids(4).json()['uuids'] # database names must start with a letter - names = map(lambda name: 'a' + name, names) + names = ['a' + name for name in names] self.db_name = names[0] self.otherdb_name = names[1] self.doc_name = names[2] From 14197b16e80e7d38e55f53ecad5df2016cda9b6b Mon Sep 17 00:00:00 2001 From: Alexander Shorin Date: Fri, 21 Mar 2014 15:14:06 +0400 Subject: [PATCH 3/4] Fix too weird formatting --- cloudant/resource.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cloudant/resource.py b/cloudant/resource.py index 843ada2..0d4ae81 100644 --- a/cloudant/resource.py +++ b/cloudant/resource.py @@ -102,12 +102,7 @@ def _make_request(self, method, path='', **kwargs): opts['params'] = params # make the request - future = getattr( - self._session, - method)( - self._make_url( - path), - **opts) + future = getattr(self._session, method)(self._make_url(path), **opts) return future def head(self, path='', **kwargs): From eb4efb40a8bfbd596172e2ff133ff56074d8980c Mon Sep 17 00:00:00 2001 From: Alexander Shorin Date: Fri, 21 Mar 2014 15:31:16 +0400 Subject: [PATCH 4/4] Fix JSON decoding for Python 3.x --- cloudant/index.py | 1 + 1 file changed, 1 insertion(+) diff --git a/cloudant/index.py b/cloudant/index.py index 8dbd37b..017d694 100644 --- a/cloudant/index.py +++ b/cloudant/index.py @@ -26,6 +26,7 @@ def __iter__(self, **kwargs): if hasattr(response, 'result'): response = response.result() for line in response.iter_lines(): + line = line.decode('utf-8') if line: if line[-1] == ',': line = line[:-1]