minor changes to appease pychecker
This commit is contained in:
parent
453cc617f5
commit
a6b77c347b
@ -74,12 +74,12 @@ class GridFS(object):
|
||||
|
||||
# convert to _id's so we can uniquely create GridFile instances
|
||||
ids = []
|
||||
for file in self.__database[collection].files.find(spec):
|
||||
ids.append(file["_id"])
|
||||
for grid_file in self.__database[collection].files.find(spec):
|
||||
ids.append(grid_file["_id"])
|
||||
|
||||
# open for writing to remove the chunks for these files
|
||||
for id in ids:
|
||||
f = GridFile({"_id": id}, self.__database, "w", collection)
|
||||
for file_id in ids:
|
||||
f = GridFile({"_id": file_id}, self.__database, "w", collection)
|
||||
f.close()
|
||||
|
||||
self.__database[collection].files.remove(spec)
|
||||
@ -95,6 +95,6 @@ class GridFS(object):
|
||||
if not isinstance(collection, types.StringTypes):
|
||||
raise TypeError("collection must be an instance of (str, unicode)")
|
||||
names = []
|
||||
for file in self.__database[collection].files.find():
|
||||
names.append(file["filename"])
|
||||
for grid_file in self.__database[collection].files.find():
|
||||
names.append(grid_file["filename"])
|
||||
return names
|
||||
|
||||
@ -81,9 +81,9 @@ class GridFile(object):
|
||||
self.__collection = database[collection]
|
||||
self.__collection.chunks.create_index([("files_id", ASCENDING), ("n", ASCENDING)])
|
||||
|
||||
file = self.__collection.files.find_one(file_spec)
|
||||
if file:
|
||||
self.__id = file["_id"]
|
||||
grid_file = self.__collection.files.find_one(file_spec)
|
||||
if grid_file:
|
||||
self.__id = grid_file["_id"]
|
||||
else:
|
||||
if mode == "r":
|
||||
raise IOError("No such file: %r" % file_spec)
|
||||
@ -103,10 +103,10 @@ class GridFile(object):
|
||||
def __erase(self):
|
||||
"""Erase all of the data stored in this GridFile.
|
||||
"""
|
||||
file = self.__collection.files.find_one(self.__id)
|
||||
file["next"] = None
|
||||
file["length"] = 0
|
||||
self.__collection.files.save(file)
|
||||
grid_file = self.__collection.files.find_one(self.__id)
|
||||
grid_file["next"] = None
|
||||
grid_file["length"] = 0
|
||||
self.__collection.files.save(grid_file)
|
||||
|
||||
self.__collection.chunks.remove({"files_id": self.__id})
|
||||
|
||||
@ -119,15 +119,15 @@ class GridFile(object):
|
||||
return self.__mode
|
||||
|
||||
def __create_property(field_name, read_only=False):
|
||||
def get(self):
|
||||
def getter(self):
|
||||
return self.__collection.files.find_one(self.__id).get(field_name, None)
|
||||
def set(self, value):
|
||||
file = self.__collection.files.find_one(self.__id)
|
||||
file[field_name] = value
|
||||
self.__collection.files.save(file)
|
||||
def setter(self, value):
|
||||
grid_file = self.__collection.files.find_one(self.__id)
|
||||
grid_file[field_name] = value
|
||||
self.__collection.files.save(grid_file)
|
||||
if not read_only:
|
||||
return property(get, set)
|
||||
return property(get)
|
||||
return property(getter, setter)
|
||||
return property(getter)
|
||||
|
||||
name = __create_property("filename", True)
|
||||
content_type = __create_property("contentType")
|
||||
@ -146,9 +146,9 @@ class GridFile(object):
|
||||
:Parameters:
|
||||
- `filename`: the new name for this GridFile
|
||||
"""
|
||||
file = self.__collection.files.find_one(self.__id)
|
||||
file["filename"] = filename
|
||||
self.__collection.files.save(file)
|
||||
grid_file = self.__collection.files.find_one(self.__id)
|
||||
grid_file["filename"] = filename
|
||||
self.__collection.files.save(grid_file)
|
||||
|
||||
def __max_chunk(self):
|
||||
return self.__collection.chunks.find_one({"files_id": self.__id, "n": self.__chunk_number})
|
||||
@ -164,18 +164,18 @@ class GridFile(object):
|
||||
"""Write the buffer contents out to chunks.
|
||||
"""
|
||||
while len(self.__buffer):
|
||||
max = self.__max_chunk()
|
||||
if not max:
|
||||
max = self.__new_chunk(self.__chunk_number)
|
||||
max_chunk = self.__max_chunk()
|
||||
if not max_chunk:
|
||||
max_chunk = self.__new_chunk(self.__chunk_number)
|
||||
space = (self.__chunk_number + 1) * self.chunk_size - self.__position
|
||||
if not space:
|
||||
self.__chunk_number += 1
|
||||
max = self.__new_chunk(self.__chunk_number)
|
||||
max_chunk = self.__new_chunk(self.__chunk_number)
|
||||
space = self.chunk_size
|
||||
to_write = len(self.__buffer) > space and space or len(self.__buffer)
|
||||
|
||||
max["data"] = Binary(max["data"] + self.__buffer[:to_write])
|
||||
self.__collection.chunks.save(max)
|
||||
max_chunk["data"] = Binary(max_chunk["data"] + self.__buffer[:to_write])
|
||||
self.__collection.chunks.save(max_chunk)
|
||||
self.__buffer = self.__buffer[to_write:]
|
||||
self.__position += to_write
|
||||
|
||||
@ -188,9 +188,9 @@ class GridFile(object):
|
||||
|
||||
self.__write_buffer_to_chunks()
|
||||
|
||||
file = self.__collection.files.find_one(self.__id)
|
||||
file["length"] = self.__position + len(self.__buffer)
|
||||
self.__collection.files.save(file)
|
||||
grid_file = self.__collection.files.find_one(self.__id)
|
||||
grid_file["length"] = self.__position + len(self.__buffer)
|
||||
self.__collection.files.save(grid_file)
|
||||
|
||||
def close(self):
|
||||
"""Close the GridFile.
|
||||
|
||||
@ -22,7 +22,6 @@ import random
|
||||
import re
|
||||
import datetime
|
||||
import calendar
|
||||
import logging
|
||||
|
||||
from binary import Binary
|
||||
from code import Code
|
||||
@ -37,10 +36,6 @@ try:
|
||||
except ImportError:
|
||||
_use_c = False
|
||||
|
||||
_logger = logging.getLogger("pymongo.bson")
|
||||
# _logger.setLevel(logging.DEBUG)
|
||||
# _logger.addHandler(logging.StreamHandler())
|
||||
|
||||
def _get_int(data):
|
||||
try:
|
||||
value = struct.unpack("<i", data[:4])[0]
|
||||
|
||||
@ -67,7 +67,7 @@ class Collection(object):
|
||||
command = SON({"create": self.__collection_name})
|
||||
command.update(options)
|
||||
|
||||
response = self.__database._command(command)
|
||||
self.__database._command(command)
|
||||
|
||||
def __getattr__(self, name):
|
||||
"""Get a sub-collection of this collection by name.
|
||||
|
||||
@ -101,7 +101,7 @@ class Connection(object):
|
||||
Return a tuple (host, port). Return True if this connection is
|
||||
the master.
|
||||
"""
|
||||
result = self.admin._command({"ismaster": 1})
|
||||
result = self["admin"]._command({"ismaster": 1})
|
||||
|
||||
if result["ismaster"] == 1:
|
||||
return True
|
||||
@ -140,7 +140,7 @@ class Connection(object):
|
||||
self.__socket.settimeout(_TIMEOUT)
|
||||
self.__socket.connect((host, port))
|
||||
master = self._master()
|
||||
if master == True:
|
||||
if master is True:
|
||||
_logger.debug("success")
|
||||
self.__host = host
|
||||
self.__port = port
|
||||
@ -300,7 +300,7 @@ class Connection(object):
|
||||
def __database_info(self):
|
||||
"""Get a dictionary of (database_name: size_on_disk).
|
||||
"""
|
||||
result = self.admin._command({"listDatabases": 1})
|
||||
result = self["admin"]._command({"listDatabases": 1})
|
||||
info = result["databases"]
|
||||
return dict([(db["name"], db["sizeOnDisk"]) for db in info])
|
||||
|
||||
|
||||
@ -47,7 +47,7 @@ class Database(object):
|
||||
self.__manipulators = [ObjectIdInjector(self), ObjectIdShuffler(self)]
|
||||
|
||||
def __check_name(self, name):
|
||||
for invalid_char in " .$/\\":
|
||||
for invalid_char in [" ", ".", "$", "/", "\\"]:
|
||||
if invalid_char in name:
|
||||
raise InvalidName("database names cannot contain the character %r" % name)
|
||||
if not name:
|
||||
@ -160,7 +160,7 @@ class Database(object):
|
||||
def collection_names(self):
|
||||
"""Get a list of all the collection names in this database.
|
||||
"""
|
||||
results = self.system.namespaces.find()
|
||||
results = self["system.namespaces"].find()
|
||||
names = [r["name"] for r in results]
|
||||
names = [n[len(self.__name) + 1:] for n in names
|
||||
if n.startswith(self.__name + ".")]
|
||||
@ -237,7 +237,7 @@ class Database(object):
|
||||
def profiling_info(self):
|
||||
"""Returns a list containing current profiling information.
|
||||
"""
|
||||
return list(self.system.profile.find())
|
||||
return list(self["system.profile"].find())
|
||||
|
||||
def error(self):
|
||||
"""Get a database error if one occured on the last operation.
|
||||
|
||||
@ -102,11 +102,7 @@ class SON(dict):
|
||||
yield k
|
||||
|
||||
def has_key(self, key):
|
||||
try:
|
||||
value = self[key]
|
||||
except KeyError:
|
||||
return False
|
||||
return True
|
||||
return key in self.keys()
|
||||
|
||||
def __contains__(self, key):
|
||||
return self.has_key(key)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user