Skip to content

Commit f48d66c

Browse files
author
Engelbert Gruber
committed
add module blobfiles in backends with file access functions. not yet activated.
1 parent 62ae25b commit f48d66c

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ are given with the most recent entry first.
33

44
2002-02-?? - 0.4.1
55
Feature:
6+
. add module blobfiles in backends with file access functions.
67
. roundup db catch only IOError in getfile.
78
. roundup db catches retrieving not existing files.
89
. #503204 ] mailgw needs a default class

roundup/backends/blobfiles.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
'''
23+
24+
import os, os.path
25+
26+
class FileStorage:
27+
"""Store files in some directory structure"""
28+
def __init__(self):
29+
# maybe set "files"
30+
pass
31+
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)
42+
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)
46+
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))
59+
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, )))
70+
71+
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()
85+
86+
def numfiles(self):
87+
'''Get number of files in storage, even across subdirectories.
88+
'''
89+
files_dir = os.path.join(self.dir, 'files')
90+
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
102+
103+
return files_in_dir(files_dir)
104+
105+
106+

0 commit comments

Comments
 (0)