14
14
15
15
"""GridFS is a specification for storing large objects in Mongo.
16
16
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.
19
19
"""
20
20
21
21
from grid_file import GridFile
22
22
from pymongo .database import Database
23
23
24
24
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.
26
26
"""
27
27
def __init__ (self , database ):
28
- """Create a new instance of GridFS.
28
+ """Create a new instance of :class:` GridFS` .
29
29
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`.
32
32
33
33
:Parameters:
34
34
- `database`: database to use
@@ -39,31 +39,41 @@ def __init__(self, database):
39
39
self .__database = database
40
40
41
41
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.
43
44
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`.
46
49
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).
51
56
52
57
:Parameters:
53
- - `filename`: name of the GridFile to open
58
+ - `filename`: name of the :class:`~gridfs.grid_file.GridFile`
59
+ to open
54
60
- `mode` (optional): mode to open the file in
55
61
- `collection` (optional): root collection to use for this file
56
62
"""
57
63
return GridFile ({"filename" : filename }, self .__database , mode , collection )
58
64
59
65
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.
61
68
62
69
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`.
67
77
68
78
:Parameters:
69
79
- `filename_or_spec`: identifier of file(s) to remove
@@ -74,9 +84,9 @@ def remove(self, filename_or_spec, collection="fs"):
74
84
spec = {"filename" : filename_or_spec }
75
85
if not isinstance (spec , dict ):
76
86
raise TypeError ("filename_or_spec must be an "
77
- "instance of (str , dict, SON)" )
87
+ "instance of (basestring , dict, SON)" )
78
88
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 " )
80
90
81
91
# convert to _id's so we can uniquely create GridFile instances
82
92
ids = []
@@ -91,15 +101,17 @@ def remove(self, filename_or_spec, collection="fs"):
91
101
self .__database [collection ].files .remove (spec )
92
102
93
103
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`.
95
106
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`.
97
109
98
110
:Parameters:
99
111
- `collection` (optional): root collection to list files from
100
112
"""
101
113
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 " )
103
115
names = []
104
116
for grid_file in self .__database [collection ].files .find ():
105
117
names .append (grid_file ["filename" ])
0 commit comments