Skip to content

Commit 580c8f2

Browse files
author
Mike Dirolf
committed
minor: clean up and docs for gridfs
1 parent d80de9c commit 580c8f2

File tree

3 files changed

+155
-105
lines changed

3 files changed

+155
-105
lines changed

gridfs/__init__.py

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@
1414

1515
"""GridFS is a specification for storing large objects in Mongo.
1616
17-
The `gridfs` package is an implementation of GridFS on top of `pymongo`,
18-
exposing a file-like interface.
17+
The :mod:`gridfs` package is an implementation of GridFS on top of
18+
:mod:`pymongo`, exposing a file-like interface.
1919
"""
2020

2121
from grid_file import GridFile
2222
from pymongo.database import Database
2323

2424
class GridFS(object):
25-
"""An instance of GridFS on top of a single `pymongo.database.Database`.
25+
"""An instance of GridFS on top of a single Database.
2626
"""
2727
def __init__(self, database):
28-
"""Create a new instance of GridFS.
28+
"""Create a new instance of :class:`GridFS`.
2929
30-
Raises TypeError if database is not an instance of
31-
`pymongo.database.Database`.
30+
Raises :class:`TypeError` if `database` is not an instance of
31+
:class:`~pymongo.database.Database`.
3232
3333
:Parameters:
3434
- `database`: database to use
@@ -39,31 +39,41 @@ def __init__(self, database):
3939
self.__database = database
4040

4141
def open(self, filename, mode="r", collection="fs"):
42-
"""Open a GridFile for reading or writing.
42+
"""Open a :class:`~gridfs.grid_file.GridFile` for reading or
43+
writing.
4344
44-
Shorthand method for creating / opening a GridFile from a filename. mode
45-
must be a mode supported by `gridfs.grid_file.GridFile`.
45+
Shorthand method for creating / opening a
46+
:class:`~gridfs.grid_file.GridFile` with name
47+
`filename`. `mode` must be a mode supported by
48+
:class:`~gridfs.grid_file.GridFile`.
4649
47-
Only a single opened GridFile instance may exist for a file in gridfs
48-
at any time. Care must be taken to close GridFile instances when done
49-
using them. GridFiles support the context manager protocol (the "with"
50-
statement).
50+
Only a single opened :class:`~gridfs.grid_file.GridFile`
51+
instance may exist for a file in gridfs at any time. Care must
52+
be taken to close :class:`~gridfs.grid_file.GridFile`
53+
instances when done using
54+
them. :class:`~gridfs.grid_file.GridFile` instances support
55+
the context manager protocol (the "with" statement).
5156
5257
:Parameters:
53-
- `filename`: name of the GridFile to open
58+
- `filename`: name of the :class:`~gridfs.grid_file.GridFile`
59+
to open
5460
- `mode` (optional): mode to open the file in
5561
- `collection` (optional): root collection to use for this file
5662
"""
5763
return GridFile({"filename": filename}, self.__database, mode, collection)
5864

5965
def remove(self, filename_or_spec, collection="fs"):
60-
"""Remove one or more GridFile(s).
66+
"""Remove one or more :class:`~gridfs.grid_file.GridFile`
67+
instances.
6168
6269
Can remove by filename, or by an entire file spec (see
63-
`gridfs.grid_file.GridFile` for documentation on valid fields. Delete
64-
all GridFiles that match filename_or_spec. Raises TypeError if
65-
filename_or_spec is not an instance of (str, unicode, dict, SON) or
66-
collection is not an instance of (str, unicode).
70+
:meth:`~gridfs.grid_file.GridFile` for documentation on valid
71+
fields. Delete all :class:`~gridfs.grid_file.GridFile`
72+
instances that match `filename_or_spec`. Raises
73+
:class:`TypeError` if `filename_or_spec` is not an instance of
74+
(:class:`basestring`, :class:`dict`,
75+
:class:`~pymongo.son.SON`) or collection is not an instance of
76+
:class:`basestring`.
6777
6878
:Parameters:
6979
- `filename_or_spec`: identifier of file(s) to remove
@@ -74,9 +84,9 @@ def remove(self, filename_or_spec, collection="fs"):
7484
spec = {"filename": filename_or_spec}
7585
if not isinstance(spec, dict):
7686
raise TypeError("filename_or_spec must be an "
77-
"instance of (str, dict, SON)")
87+
"instance of (basestring, dict, SON)")
7888
if not isinstance(collection, basestring):
79-
raise TypeError("collection must be an instance of (str, unicode)")
89+
raise TypeError("collection must be an instance of basestring")
8090

8191
# convert to _id's so we can uniquely create GridFile instances
8292
ids = []
@@ -91,15 +101,17 @@ def remove(self, filename_or_spec, collection="fs"):
91101
self.__database[collection].files.remove(spec)
92102

93103
def list(self, collection="fs"):
94-
"""List the names of all GridFiles stored in this instance of GridFS.
104+
"""List the names of all :class:`~gridfs.grid_file.GridFile`
105+
instances stored in this instance of :class:`GridFS`.
95106
96-
Raises TypeError if collection is not an instance of (str, unicode).
107+
Raises :class:`TypeError` if collection is not an instance of
108+
:class:`basestring`.
97109
98110
:Parameters:
99111
- `collection` (optional): root collection to list files from
100112
"""
101113
if not isinstance(collection, basestring):
102-
raise TypeError("collection must be an instance of (str, unicode)")
114+
raise TypeError("collection must be an instance of basestring")
103115
names = []
104116
for grid_file in self.__database[collection].files.find():
105117
names.append(grid_file["filename"])

gridfs/errors.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""Exceptions raised by the `gridfs` package"""
15+
"""Exceptions raised by the :mod:`gridfs` package"""
1616

1717
class CorruptGridFile(Exception):
18-
"""Raised when a GridFS "file" is malformed.
18+
"""Raised when a :class:`~gridfs.grid_file.GridFile` instance is
19+
malformed.
1920
"""

0 commit comments

Comments
 (0)