From c810ab11af59be78bddd0562456cb2395eb0a4c0 Mon Sep 17 00:00:00 2001 From: helmutm Date: Sun, 8 Oct 2006 22:51:37 +0000 Subject: [PATCH] created cybertools.storage git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1395 fd906abe-77d9-0310-91a1-e0d9ade77398 --- storage/README.txt | 16 ++++++++++ storage/__init__.py | 3 ++ storage/filesystem.py | 68 +++++++++++++++++++++++++++++++++++++++++++ storage/interfaces.py | 43 +++++++++++++++++++++++++++ storage/tests.py | 28 ++++++++++++++++++ 5 files changed, 158 insertions(+) create mode 100644 storage/README.txt create mode 100644 storage/__init__.py create mode 100644 storage/filesystem.py create mode 100644 storage/interfaces.py create mode 100755 storage/tests.py diff --git a/storage/README.txt b/storage/README.txt new file mode 100644 index 0000000..2260d04 --- /dev/null +++ b/storage/README.txt @@ -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' + + diff --git a/storage/__init__.py b/storage/__init__.py new file mode 100644 index 0000000..38314f3 --- /dev/null +++ b/storage/__init__.py @@ -0,0 +1,3 @@ +""" +$Id$ +""" diff --git a/storage/filesystem.py b/storage/filesystem.py new file mode 100644 index 0000000..6017410 --- /dev/null +++ b/storage/filesystem.py @@ -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) diff --git a/storage/interfaces.py b/storage/interfaces.py new file mode 100644 index 0000000..3a5e520 --- /dev/null +++ b/storage/interfaces.py @@ -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. + """ + diff --git a/storage/tests.py b/storage/tests.py new file mode 100755 index 0000000..b52c8e6 --- /dev/null +++ b/storage/tests.py @@ -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')