Skip to content

Commit

Permalink
Fix simple errors/warnings, reported by pep8 and pyflakes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Palazhchenko committed Dec 5, 2010
1 parent 595b94a commit cd2847f
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 80 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -31,7 +31,7 @@ flakes:
$(PYFLAKES) txmongo

pep8:
$(PEP8) txmongo
$(PEP8) --ignore=E501 -r txmongo


.PHONY: env docs
5 changes: 5 additions & 0 deletions txmongo/__init__.py
Expand Up @@ -18,6 +18,7 @@
from txmongo._pymongo.objectid import ObjectId
from twisted.internet import task, defer, reactor, protocol


class _offline(object):
def OP_INSERT(self, *args, **kwargs):
pass
Expand Down Expand Up @@ -117,14 +118,18 @@ def _Connection(host, port, reconnect, pool_size, lazy):
reactor.connectTCP(host, port, factory)
return (lazy is True) and factory.API or factory.deferred


def MongoConnection(host="localhost", port=27017, reconnect=True):
return _Connection(host, port, reconnect, pool_size=1, lazy=False)


def lazyMongoConnection(host="localhost", port=27017, reconnect=True):
return _Connection(host, port, reconnect, pool_size=1, lazy=True)


def MongoConnectionPool(host="localhost", port=27017, reconnect=True, pool_size=5):
return _Connection(host, port, reconnect, pool_size, lazy=False)


def lazyMongoConnectionPool(host="localhost", port=27017, reconnect=True, pool_size=5):
return _Connection(host, port, reconnect, pool_size, lazy=True)
10 changes: 5 additions & 5 deletions txmongo/_gridfs/__init__.py
Expand Up @@ -30,6 +30,7 @@
DESCENDING)
from txmongo.database import Database


class GridFS(object):
"""An instance of GridFS on top of a single Database.
"""
Expand All @@ -55,7 +56,7 @@ def __init__(self, database, collection="fs"):
self.__collection = database[collection]
self.__files = self.__collection.files
self.__chunks = self.__collection.chunks
self.__chunks.create_index(filter.sort(ASCENDING("files_id") +ASCENDING("n")),
self.__chunks.create_index(filter.sort(ASCENDING("files_id") + ASCENDING("n")),
unique=True)

def new_file(self, **kwargs):
Expand Down Expand Up @@ -140,15 +141,14 @@ def get_last_version(self, filename):
d.addCallback(self._cb_get_last_version, filename)
return d
# cursor.limit(-1).sort("uploadDate", -1)#DESCENDING)

def _cb_get_last_version(self, docs, filename):
try:
grid_file = docs[0]
return GridOut(self.__collection, grid_file)
except IndexError:
raise NoFile("no file in gridfs with filename %r" % filename)



# TODO add optional safe mode for chunk removal?
def delete(self, file_id):
"""Delete a file from GridFS by ``"_id"``.
Expand Down Expand Up @@ -195,4 +195,4 @@ def remove(self, *args, **kwargs):
The remove method is no longer supported.
"""
raise UnsupportedAPI("The remove method is no longer supported. "
"Please use the delete method instead.")
"Please use the delete method instead.")
4 changes: 3 additions & 1 deletion txmongo/_gridfs/errors.py
Expand Up @@ -14,6 +14,7 @@

"""Exceptions raised by the :mod:`gridfs` package"""


class GridFSError(Exception):
"""Base class for all GridFS exceptions.
Expand All @@ -32,6 +33,7 @@ class NoFile(GridFSError):
.. versionadded:: 1.6
"""


class UnsupportedAPI(GridFSError):
"""Raised when trying to use the old GridFS API.
Expand All @@ -42,4 +44,4 @@ class UnsupportedAPI(GridFSError):
to use unsupported constructs from the old API.
.. versionadded:: 1.6
"""
"""
71 changes: 32 additions & 39 deletions txmongo/_gridfs/grid_file.py
Expand Up @@ -35,7 +35,7 @@
_SEEK_SET = os.SEEK_SET
_SEEK_CUR = os.SEEK_CUR
_SEEK_END = os.SEEK_END
except AttributeError: # before 2.5
except AttributeError: # before 2.5
_SEEK_SET = 0
_SEEK_CUR = 1
_SEEK_END = 2
Expand All @@ -54,6 +54,7 @@ def getter(self):
raise AttributeError("can only get %r on a closed file" %
field_name)
return self._file.get(field_name, None)

def setter(self, value):
if self._closed:
raise AttributeError("cannot set %r on a closed file" %
Expand Down Expand Up @@ -165,39 +166,38 @@ def __setattr__(self, name, value):
def __flush_data(self, data):
"""Flush `data` to a chunk.
"""
if data:
if data:
assert(len(data) <= self.chunk_size)
_id = self._id
chunk = {"files_id": self._file["_id"],
"n": self._chunk_number,
"data": Binary(data)}

# Continue writing after the insert completes (non-blocking)
yield self._chunks.insert(chunk)
self._chunk_number += 1
self._position += len(data)

@defer.inlineCallbacks
def __flush_buffer(self):
"""Flush the buffer contents out to a chunk.
"""
yield self.__flush_data(self._buffer.getvalue())
self._buffer.close()
self._buffer = StringIO()

@defer.inlineCallbacks
def __flush(self):
"""Flush the file to the database.
"""
yield self.__flush_buffer()

md5 = yield self._coll.filemd5(self._id)

self._file["md5"] = md5
self._file["length"] = self._position
self._file["uploadDate"] = datetime.datetime.utcnow()
yield self._coll.files.insert(self._file)

@defer.inlineCallbacks
def close(self):
"""Flush the file and close it.
Expand Down Expand Up @@ -229,13 +229,13 @@ def write(self, data):
"""
if self._closed:
raise ValueError("cannot write to a closed file")

#NC: Reverse the order of string and file-like from pymongo 1.6.
# It is more likely to call write several times when writing
# It is more likely to call write several times when writing
# strings than to write multiple file-like objects to a
# single concatenated file.
try: # string
# single concatenated file.

try: # string
while data:
space = self.chunk_size - self._buffer.tell()
if len(data) <= space:
Expand All @@ -245,8 +245,8 @@ def write(self, data):
self._buffer.write(data[:space])
self.__flush_buffer()
data = data[space:]
except AttributeError:
try: # file-like
except AttributeError:
try: # file-like
if self._buffer.tell() > 0:
space = self.chunk_size - self._buffer.tell()
self._buffer.write(data.read(space))
Expand All @@ -258,7 +258,6 @@ def write(self, data):
self._buffer.write(to_write)
except AttributeError:
raise TypeError("can only write strings or file-like objects")


def writelines(self, sequence):
"""Write a sequence of strings to the file.
Expand All @@ -279,7 +278,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
Close the file and allow exceptions to propogate.
"""
self.close()
return False # untrue will propogate exceptions
return False # untrue will propogate exceptions


class GridOut(object):
Expand Down Expand Up @@ -325,49 +324,48 @@ def __init__(self, root_collection, doc):
True)
md5 = _create_property("md5", "MD5 of the contents of this file "
"(generated on the server).", True)

def __getattr__(self, name):
if name in self._file:
return self._file[name]
raise AttributeError("GridOut object has no attribute '%s'" % name)

@defer.inlineCallbacks
def read(self, size=-1):
"""Read at most `size` bytes from the file (less if there
isn't enough data).
The bytes are returned as an instance of :class:`str`. If
`size` is negative or omitted all data is read.
:Parameters:
- `size` (optional): the number of bytes to read
- `size` (optional): the number of bytes to read
"""
if size:
remainder = int(self.length) - self.__position
if size < 0 or size > remainder:
size = remainder

data = self.__buffer
chunk_number = (len(data) + self.__position) / self.chunk_size

while len(data) < size:
chunk = yield self.__chunks.find_one({"files_id": self._id,
"n": chunk_number})
if not chunk:
raise CorruptGridFile("no chunk #%d" % chunk_number)

if not data:
data += chunk["data"][self.__position % self.chunk_size:]
else:
data += chunk["data"]

chunk_number += 1

self.__position += size
to_return = data[:size]
self.__buffer = data[size:]
defer.returnValue(to_return)


def tell(self):
"""Return the current position of this file.
Expand Down Expand Up @@ -399,21 +397,15 @@ def seek(self, pos, whence=_SEEK_SET):
raise IOError(22, "Invalid value for `pos` - must be positive")

self.__position = new_pos

def close(self):
self.__buffer = ''
self.__current_chunk = -1

def __iter__(self):
"""Return an iterator over all of this file's data.
"""Deprecated."""
raise UnsupportedAPI("Iterating is deprecated for iterated reading")

The iterator will return chunk-sized instances of
:class:`str`. This can be useful when serving files using a
webserver that handles such an iterator efficiently.
"""
raise NotSupportedError("Iterating is deprecated for iterated reading")
#return GridOutIterator(self, self.__chunks)

def __repr__(self):
return str(self._file)

Expand All @@ -428,7 +420,7 @@ def __init__(self, grid_out, chunks):

def __iter__(self):
return self

@defer.inlineCallbacks
def next(self):
if self.__current_chunk >= self.__max_chunk:
Expand All @@ -440,6 +432,7 @@ def next(self):
self.__current_chunk += 1
defer.returnValue(str(chunk["data"]))


class GridFile(object):
"""No longer supported.
Expand All @@ -448,4 +441,4 @@ class GridFile(object):
"""
def __init__(self, *args, **kwargs):
raise UnsupportedAPI("The GridFile class is no longer supported. "
"Please use GridIn or GridOut instead.")
"Please use GridIn or GridOut instead.")
14 changes: 8 additions & 6 deletions txmongo/_pymongo/bson.py
Expand Up @@ -167,6 +167,7 @@ def _validate_timestamp(data):
assert len(data) >= 8
return data[8:]


def _validate_number_long(data):
assert len(data) >= 8
return data[8:]
Expand Down Expand Up @@ -328,6 +329,7 @@ def _get_timestamp(data):
(inc, data) = _get_int(data)
return ((timestamp, inc), data)


def _get_long(data):
return (struct.unpack("<q", data[:8])[0], data[8:])

Expand All @@ -337,17 +339,17 @@ def _get_long(data):
"\x03": _get_object,
"\x04": _get_array,
"\x05": _get_binary,
"\x06": _get_null, # undefined
"\x06": _get_null, # undefined
"\x07": _get_oid,
"\x08": _get_boolean,
"\x09": _get_date,
"\x0A": _get_null,
"\x0B": _get_regex,
"\x0C": _get_ref,
"\x0D": _get_string, # code
"\x0E": _get_string, # symbol
"\x0D": _get_string, # code
"\x0E": _get_string, # symbol
"\x0F": _get_code_w_scope,
"\x10": _get_int, # number_int
"\x10": _get_int, # number_int
"\x11": _get_timestamp,
"\x12": _get_long,
}
Expand Down Expand Up @@ -435,9 +437,9 @@ def _element_to_bson(key, value, check_keys):
return "\x08" + name + "\x00"
if isinstance(value, (int, long)):
# TODO this is a really ugly way to check for this...
if value > 2**64 / 2 - 1 or value < -2**64 / 2:
if value > 2 ** 64 / 2 - 1 or value < -2 ** 64 / 2:
raise OverflowError("MongoDB can only handle up to 8-byte ints")
if value > 2**32 / 2 - 1 or value < -2**32 / 2:
if value > 2 ** 32 / 2 - 1 or value < -2 ** 32 / 2:
return "\x12" + name + struct.pack("<q", value)
return "\x10" + name + struct.pack("<i", value)
if isinstance(value, datetime.datetime):
Expand Down
4 changes: 1 addition & 3 deletions txmongo/_pymongo/objectid.py
Expand Up @@ -26,7 +26,7 @@
try:
import hashlib
_md5func = hashlib.md5
except: # for Python < 2.5
except: # for Python < 2.5
import md5
_md5func = md5.new

Expand Down Expand Up @@ -171,5 +171,3 @@ def __hash__(self):
.. versionadded:: 1.1
"""
return hash(self.__id)


0 comments on commit cd2847f

Please sign in to comment.