created cybertools.storage

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1395 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2006-10-08 22:51:37 +00:00
parent 23e8733e16
commit c810ab11af
5 changed files with 158 additions and 0 deletions

16
storage/README.txt Normal file
View file

@ -0,0 +1,16 @@
===============================
Controlling the storage of data
===============================
($Id$)
>>> import os
>>> from cybertools import storage
>>> directory = os.path.dirname(storage.__file__)
>>> from cybertools.storage.filesystem import explicitDirectoryStorage
>>> storage = explicitDirectoryStorage(os.path.sep.join((directory, 'testdata')))
>>> storage.getDir('demo')
'/.../cybertools/storage/testdata/demo'

3
storage/__init__.py Normal file
View file

@ -0,0 +1,3 @@
"""
$Id$
"""

68
storage/filesystem.py Normal file
View file

@ -0,0 +1,68 @@
#
# Copyright (c) 2006 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
Storing data in files in the file system.
$Id$
"""
import os
from zope.interface import implements
from cybertools.storage.interfaces import IExternalStorage
DEFAULT_DIRECTORY = 'extfiles'
class FileSystemStorage(object):
implements(IExternalStorage)
def __init__(self, rootDir, subDir):
self.rootDir = rootDir
self.subDir = subDir
def getDir(self, address, subDir=None):
subDir = subDir or self.subDir
paths = [p for p in (self.rootDir, subDir, address) if p]
return os.path.sep.join(paths)
def setData(self, address, data, params={}):
subDir = params.get('subdirectory')
fn = self.getDir(address, subDir)
f = open(fn, 'wb')
f.write(data)
f.close()
def getData(self, address, params={}):
subDir = params.get('subdirectory')
fn = self.getDir(address, subDir)
f = open(fn, 'rb')
data = f.read()
f.close()
return data
def explicitDirectoryStorage(dirname):
""" This cannot be used as a utility but must be called explicitly."""
return FileSystemStorage('', dirname)
def instanceVarSubdirectoryStorage(dirname=DEFAULT_DIRECTORY):
instanceHome = '';
return FileSystemStorage(instanceHome, dirname)

43
storage/interfaces.py Normal file
View file

@ -0,0 +1,43 @@
#
# Copyright (c) 2006 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
interface definitions for storage utilities.
$Id$
"""
from zope.interface import Interface
class IExternalStorage(Interface):
""" An external storage for data elements.
"""
def setData(address, data, params=None):
""" Store the data given at the address specified, using the
(optional) params argument that may give more information on
where and how to store the data.
"""
def getData(address, params=None):
""" Retrieve the data from the address specified, using the
(optional) params argument that may give more information on
where and how to the data can be found.
"""

28
storage/tests.py Executable file
View file

@ -0,0 +1,28 @@
#! /usr/bin/python
"""
Tests for the 'cybertools.storage' package.
$Id$
"""
import unittest, doctest
from zope.testing.doctestunit import DocFileSuite
from cybertools.text import pdf
class Test(unittest.TestCase):
"Basic tests for the storage package."
def testBasicStuff(self):
pass
def test_suite():
flags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
return unittest.TestSuite((
unittest.makeSuite(Test),
DocFileSuite('README.txt', optionflags=flags),
))
if __name__ == '__main__':
unittest.main(defaultTest='test_suite')