Code

2080ab450dc8b793bbf2cccb2116cca11dd00eb7
[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.1 2002-02-25 14:25:41 grubert Exp $
19 '''
20 This module exports file storage for roundup backends.
21 Files are stored into a directory hierarchy.
22 '''
24 import os, os.path
26 class FileStorage:
27     """Store files in some directory structure"""
28     def __init__(self):
29         # maybe set "files"
30         pass
32     def filename(self, classname, nodeid, property=None):
33         '''Determine what the filename for the given node and optionally 
34            property is.
35         '''
36         if property:
37             name = '%s%s.%s'%(classname, nodeid, property)
38         else:
39             # roundupdb.FileClass never specified the property name, so don't 
40             # include it
41             name = '%s%s'%(classname, nodeid)
43         # have a separate subdir for every thousand messages
44         subdir = str(int(nodeid) / 1000)
45         return os.path.join(self.dir, 'files', classname, subdir, name)
47     def filename_flat(self, classname, nodeid, property=None):
48         '''Determine what the filename for the given node and optionally 
49            property is.
50         '''
51         if property:
52             return os.path.join(self.dir, 'files', '%s%s.%s'%(classname,
53                 nodeid, property))
54         else:
55             # roundupdb.FileClass never specified the property name, so don't 
56             # include it
57             return os.path.join(self.dir, 'files', '%s%s'%(classname,
58                 nodeid))
60     def storefile(self, classname, nodeid, property, content):
61         '''Store the content of the file in the database. The property may be
62            None, in which case the filename does not indicate which property
63            is being saved.
64         '''
65         name = self.filename(classname, nodeid, property)
66         if not os.path.exists(os.path.dirname(name)):
67             os.makedirs(os.path.dirname(name))
68         open(name + '.tmp', 'wb').write(content)
69         self.transactions.append((self._doStoreFile, (name, )))
72     def getfile(self, classname, nodeid, property):
73         '''Get the content of the file in the database.
74         '''
75         filename = self.filename(classname, nodeid, property)
76         try:
77             return open(filename, 'rb').read()
78         except:
79             try:
80                 return open(filename+'.tmp', 'rb').read()
81             except:
82                 # fallback to flat file storage
83                 filename = self.filename_flat(classname, nodeid, property)
84                 return open(filename, 'rb').read()
86     def numfiles(self):
87         '''Get number of files in storage, even across subdirectories.
88         '''
89         files_dir = os.path.join(self.dir, 'files')
91         def files_in_dir(dir):       
92             if not os.path.exists(dir):
93                 return 0
94             num_files = 0
95             for dir_entry in os.listdir(dir):
96                 full_filename = os.path.join(dir,dir_entry)
97                 if os.path.isfile(full_filename):
98                     num_files = num_files + 1
99                 elif os.path.isdir(full_filename):
100                     num_files = num_files + files_in_dir(full_filename)
101             return num_files
103         return files_in_dir(files_dir)