Code

That's gadfly done, mostly. Things left:
[roundup.git] / roundup / backends / blobfiles.py
1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17
18 #$Id: blobfiles.py,v 1.8 2002-07-19 03:36:34 richard Exp $
19 '''
20 This module exports file storage for roundup backends.
21 Files are stored into a directory hierarchy.
22 '''
24 import os
26 def files_in_dir(dir):       
27     if not os.path.exists(dir):
28         return 0
29     num_files = 0
30     for dir_entry in os.listdir(dir):
31         full_filename = os.path.join(dir,dir_entry)
32         if os.path.isfile(full_filename):
33             num_files = num_files + 1
34         elif os.path.isdir(full_filename):
35             num_files = num_files + files_in_dir(full_filename)
36     return num_files
38 class FileStorage:
39     """Store files in some directory structure"""
40     def filename(self, classname, nodeid, property=None):
41         '''Determine what the filename for the given node and optionally 
42            property is.
43         '''
44         if property:
45             name = '%s%s.%s'%(classname, nodeid, property)
46         else:
47             # roundupdb.FileClass never specified the property name, so don't 
48             # include it
49             name = '%s%s'%(classname, nodeid)
51         # have a separate subdir for every thousand messages
52         subdir = str(int(nodeid) / 1000)
53         return os.path.join(self.dir, 'files', classname, subdir, name)
55     def filename_flat(self, classname, nodeid, property=None):
56         '''Determine what the filename for the given node and optionally 
57            property is.
58         '''
59         if property:
60             return os.path.join(self.dir, 'files', '%s%s.%s'%(classname,
61                 nodeid, property))
62         else:
63             # roundupdb.FileClass never specified the property name, so don't 
64             # include it
65             return os.path.join(self.dir, 'files', '%s%s'%(classname,
66                 nodeid))
68     def storefile(self, classname, nodeid, property, content):
69         '''Store the content of the file in the database. The property may be
70            None, in which case the filename does not indicate which property
71            is being saved.
72         '''
73         # determine the name of the file to write to
74         name = self.filename(classname, nodeid, property)
76         # make sure the file storage dir exists
77         if not os.path.exists(os.path.dirname(name)):
78             os.makedirs(os.path.dirname(name))
80         # open the temp file for writing
81         open(name + '.tmp', 'wb').write(content)
83         # save off the commit action
84         self.transactions.append((self.doStoreFile, (classname, nodeid,
85             property)))
87     def getfile(self, classname, nodeid, property):
88         '''Get the content of the file in the database.
89         '''
90         filename = self.filename(classname, nodeid, property)
91         try:
92             return open(filename, 'rb').read()
93         except:
94             # now try the temp pre-commit filename
95             try:
96                 return open(filename+'.tmp', 'rb').read()
97             except:
98                 # fallback to flat file storage
99                 filename = self.filename_flat(classname, nodeid, property)
100                 return open(filename, 'rb').read()
102     def numfiles(self):
103         '''Get number of files in storage, even across subdirectories.
104         '''
105         files_dir = os.path.join(self.dir, 'files')
106         return files_in_dir(files_dir)
108     def doStoreFile(self, classname, nodeid, property, **databases):
109         '''Store the file as part of a transaction commit.
110         '''
111         # determine the name of the file to write to
112         name = self.filename(classname, nodeid, property)
114         # the file is currently ".tmp" - move it to its real name to commit
115         os.rename(name+".tmp", name)
117         # return the classname, nodeid so we reindex this content
118         return (classname, nodeid)
120     def rollbackStoreFile(self, classname, nodeid, property, **databases):
121         '''Remove the temp file as a part of a rollback
122         '''
123         # determine the name of the file to delete
124         name = self.filename(classname, nodeid, property)
125         if os.path.exists(name+".tmp"):
126             os.remove(name+".tmp")
128 # $Log: not supported by cvs2svn $
129 # Revision 1.7  2002/07/14 06:14:40  richard
130 # Some more TODOs
132 # Revision 1.6  2002/07/09 03:02:52  richard
133 # More indexer work:
134 # - all String properties may now be indexed too. Currently there's a bit of
135 #   "issue" specific code in the actual searching which needs to be
136 #   addressed. In a nutshell:
137 #   + pass 'indexme="yes"' as a String() property initialisation arg, eg:
138 #         file = FileClass(db, "file", name=String(), type=String(),
139 #             comment=String(indexme="yes"))
140 #   + the comment will then be indexed and be searchable, with the results
141 #     related back to the issue that the file is linked to
142 # - as a result of this work, the FileClass has a default MIME type that may
143 #   be overridden in a subclass, or by the use of a "type" property as is
144 #   done in the default templates.
145 # - the regeneration of the indexes (if necessary) is done once the schema is
146 #   set up in the dbinit.
148 # Revision 1.5  2002/07/08 06:58:15  richard
149 # cleaned up the indexer code:
150 #  - it splits more words out (much simpler, faster splitter)
151 #  - removed code we'll never use (roundup.roundup_indexer has the full
152 #    implementation, and replaces roundup.indexer)
153 #  - only index text/plain and rfc822/message (ideas for other text formats to
154 #    index are welcome)
155 #  - added simple unit test for indexer. Needs more tests for regression.
157 # Revision 1.4  2002/06/19 03:07:19  richard
158 # Moved the file storage commit into blobfiles where it belongs.
160 # Revision 1.3  2002/02/27 07:33:34  grubert
161 #  . add, vim line and cvs log key.
164 # vim: set filetype=python ts=4 sw=4 et si