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

Fix using a field name different from attribute name #45

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
887ec37
added 2to3 for setup.py
Sep 21, 2013
f6372f3
fix for python3
Sep 21, 2013
c08bfa6
fix EmailField bug with trying to validate default=None
Sep 28, 2013
2c13fe8
find and aind_on changes (@gen.engine -> @gen.coroutine)
Dec 26, 2013
161c2ae
all method for Manager
Dec 26, 2013
7ceea34
small refactoring collection.py
Dec 26, 2013
27c4c65
count() over gen.coroutine
Dec 30, 2013
aa97ce2
incr version
Dec 30, 2013
5e65753
fix bug with __collection__ autofill
geerk Mar 3, 2014
b884a4b
add documentation for __collection__ auto-generation
geerk Mar 3, 2014
d3f0a96
add documentation for manager's all method
geerk Mar 3, 2014
b270e3a
add python 2.6+ requirement, change package name back to mongotor
geerk Mar 3, 2014
ed5cfc4
fix bug with endless recursion
geerk Mar 26, 2014
47bdc13
fix collection tests
geerk Mar 26, 2014
c071b59
distinct over gen.coroutine
geerk Mar 29, 2014
1bdf4d4
fix bug (mistype) with max_value validation in NumberField
geerk Mar 29, 2014
086047a
fix bug with NumberField
geerk Mar 29, 2014
4d7cb60
add remove method to collection manager
geerk Mar 30, 2014
c5199bb
complete manager over gen.coroutine
geerk Mar 30, 2014
f8f157e
Merge remote-tracking branch 'marcelnicolay/master' into coroutine-py3
tahajahangir Jun 12, 2014
0ed9a05
Fix wrong tests / use gen.coroutine in tests
tahajahangir Jun 4, 2014
62404ec
Add python 2.6/3.2/3.3/3.4/pypy to travis
tahajahangir Jun 4, 2014
27d5ee2
Compatible with new version of sure
tahajahangir Jun 5, 2014
cb6fd20
Use new except syntax (Python 2.6+/3.x)
tahajahangir Jun 5, 2014
85f9428
Use Exceptions as errors base class (instead of StandardError)
tahajahangir Jun 5, 2014
a58d831
Use six to compatible with python 2,3
tahajahangir Jun 5, 2014
9c0aecb
Use string literals instead of unicode
tahajahangir Jun 5, 2014
dbdd89a
Use unittest instead of sure
tahajahangir Jun 5, 2014
95dbb41
No need to run 2to3.py (Code is compatible using six)
tahajahangir Jun 6, 2014
4d4d5ac
stop testing on python 2.6/pypy for now
tahajahangir Jun 6, 2014
24365d9
Add a failing test about overriding Field name
tahajahangir Jun 6, 2014
bf179d6
Fix overriding Field.name
tahajahangir Jun 6, 2014
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ data
.mr.developer.cfg

#PyCharm
.idea/
.idea/

#Sublime
*.sublime-*
13 changes: 10 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
language: python
python:
- "2.7"
#- 2.6
- 2.7
#- pypy
- 3.2
- 3.3
- 3.4

script:
install:
- if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2; fi
- pip install -r requirements-dev.txt
- make mongo-start
- make mongo-config
- python setup.py build
- make test

script:
- make test
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The next steps are provide support to:
* authentication
* nearest preference in replica sets
* gridfs
* all python versions (2.5, 2.6, 2.7, 3.2 and PyPy), only python 2.7 is tested now
* all python versions (2.6, 2.7, 3.2 and PyPy), only python 2.7 is tested now

## Documentation

Expand Down Expand Up @@ -69,7 +69,7 @@ class Handler(tornado.web.RequestHandler):
def get(self):
user = {'_id': ObjectId(), 'name': 'User Name'}
yield gen.Task(self.db.user.insert, user)

yield gen.Task(self.db.user.update, user['_id'], {"$set": {'name': 'New User Name'}})

user_found = yield gen.Task(self.db.user.find_one, user['_id'])
Expand Down Expand Up @@ -100,10 +100,10 @@ class Handler(tornado.web.RequestHandler):
@gen.engine
def get(self):
user = {'_id': ObjectId()}

# write on primary
yield gen.Task(self.db.user.insert, user)

# wait for replication
time.sleep(2)

Expand Down
8 changes: 5 additions & 3 deletions docs/source/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ MongoTor supports installation using standard Python "distutils" or
Install via easy_install or pip
-------------------------------

When ``easy_install`` or ``pip`` is available, the distribution can be
When ``easy_install`` or ``pip`` is available, the distribution can be
downloaded from Pypi and installed in one step::

easy_install mongotor
Expand All @@ -39,17 +39,19 @@ Python prompt like this:

.. sourcecode:: python

>>> import mongotor
>>> import mongotor
>>> mongotor.version # doctest: +SKIP

Requirements
------------

Python version 2.6+ is required. But only version 2.7 is tested for now.

The following three python libraries are required.

* `pymongo <http://github.com/mongodb/mongo-python-driver>`_ version 1.9+ for bson library
* `tornado <http://github.com/facebook/tornado>`_

.. note::
The above requirements are automatically managed when installed using
any of the supported installation methods
any of the supported installation methods
2 changes: 1 addition & 1 deletion mongotor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

version = "0.1.0"
version = "0.1.5"
3 changes: 2 additions & 1 deletion mongotor/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
from bson.code import Code
import six
from tornado import gen
from mongotor.node import ReadPreference
from mongotor.cursor import Cursor
Expand Down Expand Up @@ -291,7 +292,7 @@ def group(self, key, condition, initial, reduce, finalize=None,
"""

group = {}
if isinstance(key, basestring):
if isinstance(key, six.string_types):
group["$keyf"] = Code(key)
elif key is not None:
group = {"key": helpers._fields_list_to_dict(key)}
Expand Down
4 changes: 2 additions & 2 deletions mongotor/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _connect(self):
self._stream.set_close_callback(self._socket_close)

self._connected = True
except socket.error, error:
except socket.error as error:
raise InterfaceError(error)

def __repr__(self):
Expand Down Expand Up @@ -156,7 +156,7 @@ def reset(self):
def close_on_error(self):
try:
yield
except DatabaseError, de:
except DatabaseError as de:
logger.error('database error'.format(de))
raise
except Exception:
Expand Down
18 changes: 9 additions & 9 deletions mongotor/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import six
from tornado import gen
from bson import SON
from mongotor import message
Expand Down Expand Up @@ -95,8 +96,8 @@ def find(self, callback=None):
else:
callback((response['data'], None))

@gen.engine
def count(self, callback):
@gen.coroutine
def count(self):
"""Get the size of the results set for this query.

Returns the number of documents in the results set for this query. Does
Expand All @@ -110,19 +111,18 @@ def count(self, callback):
if response and len(response) > 0 and 'n' in response:
total = int(response['n'])

callback(total)
raise gen.Return(total)

@gen.engine
def distinct(self, key, callback):
@gen.coroutine
def distinct(self, key):
"""Get a list of distinct values for `key` among all documents
in the result set of this query.

:Parameters:
- `key`: name of key for which we want to get the distinct values
"""
if not isinstance(key, basestring):
raise TypeError("key must be an instance "
"of %s" % (basestring.__name__,))
if not isinstance(key, six.string_types):
raise TypeError("key must be a string (str/unicode)")

command = {"key": key}
if self._spec:
Expand All @@ -131,7 +131,7 @@ def distinct(self, key, callback):
response, error = yield gen.Task(self._database.command,
'distinct', self._collection, **command)

callback(response['values'])
raise gen.Return(response['values'])

def _query_options(self):
"""Get the query options string to use for this query."""
Expand Down
13 changes: 8 additions & 5 deletions mongotor/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from functools import partial, wraps
from datetime import timedelta
import six
from tornado import gen
from tornado.ioloop import IOLoop
from bson import SON
Expand All @@ -29,7 +30,7 @@
def initialized(fn):
@wraps(fn)
def wrapped(self, *args, **kwargs):
if not hasattr(self, '_initialized'):
if not hasattr(self, '_initialized') or not self._initialized:
raise DatabaseError("you must be initialize database before perform this action")

return fn(self, *args, **kwargs)
Expand All @@ -45,6 +46,7 @@ class Database(object):
def __new__(cls):
if not cls._instance:
cls._instance = super(Database, cls).__new__(cls)
cls._initialized = False

return cls._instance

Expand Down Expand Up @@ -120,10 +122,10 @@ def dbname(self):

@initialized
def get_collection_name(self, collection):
return u'%s.%s' % (self.dbname, collection)
return '%s.%s' % (self.dbname, collection)

def _parse_addresses(self, addresses):
if isinstance(addresses, (str, unicode)):
if isinstance(addresses, six.string_types):
addresses = [addresses]

assert isinstance(addresses, list)
Expand Down Expand Up @@ -152,7 +154,8 @@ def disconnect(cls):
>>> Database.disconnect()

"""
if not cls._instance or not hasattr(cls._instance, '_initialized'):
if (not cls._instance or not hasattr(cls._instance, '_initialized')
or not cls._instance._initialized):
raise ValueError("Database isn't initialized")

for node in cls._instance._nodes:
Expand Down Expand Up @@ -237,7 +240,7 @@ def command(self, command, value=1, read_preference=None,
be added to the command document before it is sent

"""
if isinstance(command, basestring):
if isinstance(command, six.string_types):
command = SON([(command, value)])

command.update(kwargs)
Expand Down
2 changes: 1 addition & 1 deletion mongotor/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.


class Error(StandardError):
class Error(Exception):
"""Base class for all mongotor exceptions.

"""
Expand Down
7 changes: 4 additions & 3 deletions mongotor/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import bson
import struct
import six
from mongotor.errors import (DatabaseError,
InterfaceError, TimeoutError)

Expand Down Expand Up @@ -64,7 +65,7 @@ def _check_command_response(response, msg="%s", allowable_errors=[]):
# Mongos returns the error details in a 'raw' object
# for some errors.
if "raw" in response:
for shard in response["raw"].itervalues():
for shard in six.itervalues(response["raw"]):
if not shard.get("ok"):
# Just grab the first error...
details = shard
Expand Down Expand Up @@ -92,8 +93,8 @@ def _fields_list_to_dict(fields):
"""
as_dict = {}
for field in fields:
if not isinstance(field, basestring):
if not isinstance(field, six.string_types):
raise TypeError("fields must be a list of key names, "
"each an instance of %s" % (basestring.__name__,))
"each an instance of string (str/unicode)")
as_dict[field] = 1
return as_dict
4 changes: 2 additions & 2 deletions mongotor/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from mongotor.errors import InvalidOperationError


__ZERO = "\x00\x00\x00\x00"
__ZERO = b"\x00\x00\x00\x00"


def __last_error(args):
Expand Down Expand Up @@ -57,7 +57,7 @@ def insert(collection_name, docs, check_keys, safe, last_error_args):
"""
data = __ZERO
data += bson._make_c_string(collection_name)
bson_data = "".join([bson.BSON.encode(doc, check_keys) for doc in docs])
bson_data = b"".join([bson.BSON.encode(doc, check_keys) for doc in docs])
if not bson_data:
raise InvalidOperationError("cannot do an empty bulk insert")
data += bson_data
Expand Down
5 changes: 3 additions & 2 deletions mongotor/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import logging
import random
import six
from tornado import gen
from bson import SON
from mongotor.pool import ConnectionPool
Expand All @@ -34,7 +35,7 @@ def __init__(self, host, port, database, pool_kargs=None):
if not pool_kargs:
pool_kargs = {}

assert isinstance(host, (str, unicode))
assert isinstance(host, six.string_types)
assert isinstance(port, int)

self.host = host
Expand Down Expand Up @@ -65,7 +66,7 @@ def config(self, callback=None):
connection=connection)
if not connection._pool: # if connection is created on the fly
connection.close()
except InterfaceError, ie:
except InterfaceError as ie:
logger.error('oops, database node {host}:{port} is unavailable: {error}'
.format(host=self.host, port=self.port, error=ie))

Expand Down
Loading