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..aeee48c 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): @@ -37,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..017d694 100644 --- a/cloudant/index.py +++ b/cloudant/index.py @@ -26,12 +26,13 @@ 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] try: yield json.loads(line) - except ValueError: + except (TypeError, ValueError): # if we can't decode a line, ignore it pass @@ -49,4 +50,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 cdadc6e..0d4ae81 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): @@ -34,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() @@ -96,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): diff --git a/test/__init__.py b/test/__init__.py index bcb70e6..d87228a 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): @@ -11,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] @@ -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()