[Zope-CVS] CVS: Products/Ape/lib/apelib/fs - base.py:1.1 classification.py:1.2 properties.py:1.2 security.py:1.2 structure.py:1.2
Shane Hathaway
shane@zope.com
Tue, 29 Apr 2003 18:12:21 -0400
Update of /cvs-repository/Products/Ape/lib/apelib/fs
In directory cvs.zope.org:/tmp/cvs-serv9561/fs
Modified Files:
classification.py properties.py security.py structure.py
Added Files:
base.py
Log Message:
- Added IGatewayEvent, the base interface for ILoadEvent and
IStoreEvent.
- Filesystem gateways no longer refer directly to a connection. They
get the connection from the event. This is to permit replacing the
connection with a zip/tar file reader/writer or some other interesting
thing.
- Added checkConnection() to gateways for checking the connection
configuration early.
- Added ConfigurationError and changed some places that were raising
RuntimeError to raise ConfigurationError instead.
- Changed some calls to getKeyChain()[-1] to simply getKey()
- Updated module import style in some places
- Various other style improvements
=== Added File Products/Ape/lib/apelib/fs/base.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Base implementation for FS gateways.
$Id: base.py,v 1.1 2003/04/29 22:11:50 shane Exp $
"""
from interfaces import IFSConnection
class FSGatewayBase:
"""Base implementation for FS gateways."""
schema = None
def __init__(self, conn_name='fs'):
self.conn_name = conn_name
def getSchema(self):
return self.schema
def checkConnection(self, event):
conn = self.getConnection(event)
if not IFSConnection.isImplementedBy(conn):
raise ValueError(
"%s does not implement IFSConnection" % repr(conn))
def getConnection(self, event):
return event.getConnection(self.conn_name)
=== Products/Ape/lib/apelib/fs/classification.py 1.1 => 1.2 ===
--- Products/Ape/lib/apelib/fs/classification.py:1.1 Wed Apr 9 23:09:55 2003
+++ Products/Ape/lib/apelib/fs/classification.py Tue Apr 29 18:11:50 2003
@@ -19,23 +19,19 @@
from apelib.core.interfaces import IGateway
from apelib.core.schemas import FieldSchema
+from base import FSGatewayBase
-class FSClassificationSection:
+
+class FSClassificationSection(FSGatewayBase):
"""Gateway for storing classification data."""
__implements__ = IGateway
schema = FieldSchema('classification', 'classification')
- def __init__(self, fs_conn):
- self.fs_conn = fs_conn
-
- def getSchema(self):
- return self.schema
-
def load(self, event):
- c = self.fs_conn
- p = event.getKeychain()[-1]
+ c = self.getConnection(event)
+ p = event.getKey()
classification = {'node_type': c.readNodeType(p)}
text = c.readSection(p, 'classification', '')
if text:
@@ -49,15 +45,16 @@
def store(self, event, state):
# state is a classification
- p = event.getKeychain()[-1]
+ fs_conn = self.getConnection(event)
+ p = event.getKey()
items = state.items()
items.sort()
text = []
for k, v in items:
if k == 'extension':
- self.fs_conn.suggestExtension(p, v)
+ fs_conn.suggestExtension(p, v)
else:
text.append('%s=%s' % (k, v))
text = '\n'.join(text)
- self.fs_conn.writeSection(p, 'classification', text)
+ fs_conn.writeSection(p, 'classification', text)
return text.strip()
=== Products/Ape/lib/apelib/fs/properties.py 1.1 => 1.2 ===
--- Products/Ape/lib/apelib/fs/properties.py:1.1 Wed Apr 9 23:09:55 2003
+++ Products/Ape/lib/apelib/fs/properties.py Tue Apr 29 18:11:50 2003
@@ -21,6 +21,8 @@
from apelib.core.interfaces import IGateway
from apelib.core.schemas import FieldSchema, RowSequenceSchema
+from base import FSGatewayBase
+
token_replacements = {
'\\\\': '\\',
@@ -55,7 +57,7 @@
return ''.join(res)
-class FSProperties:
+class FSProperties (FSGatewayBase):
"""Simple properties to filesystem property section gateway."""
__implements__ = IGateway
@@ -65,16 +67,14 @@
schema.addField('type', 'string')
schema.addField('data', 'string')
- def __init__(self, fs_conn, section='properties'):
- self.fs_conn = fs_conn
+ def __init__(self, section='properties', conn_name='fs'):
self.section = section
-
- def getSchema(self):
- return self.schema
+ FSGatewayBase.__init__(self, conn_name)
def load(self, event):
- p = event.getKeychain()[-1]
- text = self.fs_conn.readSection(p, self.section, '')
+ p = event.getKey()
+ fs_conn = self.getConnection(event)
+ text = fs_conn.readSection(p, self.section, '')
res = []
if text:
lines = text.split('\n')
@@ -95,38 +95,38 @@
lines.append('%s:%s=%s' % (k, t, escape_string(v)))
lines.sort()
text = '\n'.join(lines)
- p = event.getKeychain()[-1]
- self.fs_conn.writeSection(p, self.section, text)
+ p = event.getKey()
+ fs_conn = self.getConnection(event)
+ fs_conn.writeSection(p, self.section, text)
state = list(state)
state.sort()
return tuple(state)
-class FSSectionData:
+class FSSectionData (FSGatewayBase):
"""Text to filesystem property section gateway."""
__implements__ = IGateway
schema = FieldSchema('data', 'string')
- def __init__(self, fs_conn, section):
- self.fs_conn = fs_conn
+ def __init__(self, section, conn_name='fs'):
self.section = section
-
- def getSchema(self):
- return self.schema
+ FSGatewayBase.__init__(self, conn_name)
def load(self, event):
+ fs_conn = self.getConnection(event)
p = event.getKey()
- state = self.fs_conn.readSection(p, self.section, '').strip()
+ state = fs_conn.readSection(p, self.section, '').strip()
return state, state
def store(self, event, state):
if not isinstance(state, StringType):
- raise RuntimeError('Not a string: %s' % repr(state))
+ raise ValueError('Not a string: %s' % repr(state))
state = state.strip()
if state:
p = event.getKey()
- self.fs_conn.writeSection(p, self.section, state)
+ fs_conn = self.getConnection(event)
+ fs_conn.writeSection(p, self.section, state)
return state
=== Products/Ape/lib/apelib/fs/security.py 1.1 => 1.2 ===
--- Products/Ape/lib/apelib/fs/security.py:1.1 Wed Apr 9 23:09:55 2003
+++ Products/Ape/lib/apelib/fs/security.py Tue Apr 29 18:11:50 2003
@@ -21,8 +21,10 @@
from apelib.core.exceptions import MappingError
from params import stringToParams, paramsToString
+from base import FSGatewayBase
-class FSSecurityAttributes:
+
+class FSSecurityAttributes (FSGatewayBase):
"""Gateway for storing security attributes."""
__implements__ = IGateway
@@ -33,16 +35,14 @@
schema.addField('permission', 'string')
schema.addField('username', 'string')
- def __init__(self, fs_conn, section='security'):
- self.fs_conn = fs_conn
+ def __init__(self, section='security', conn_name='fs'):
self.section = section
-
- def getSchema(self):
- return self.schema
+ FSGatewayBase.__init__(self, conn_name)
def load(self, event):
key = event.getKey()
- text = self.fs_conn.readSection(key, self.section, '')
+ fs_conn = self.getConnection(event)
+ text = fs_conn.readSection(key, self.section, '')
res = []
if text:
lines = text.split('\n')
@@ -89,14 +89,15 @@
if lines:
lines.sort()
text = '\n'.join(lines)
- self.fs_conn.writeSection(event.getKey(), self.section, text)
+ fs_conn = self.getConnection(event)
+ fs_conn.writeSection(event.getKey(), self.section, text)
state = list(state)
state.sort()
return tuple(state)
-class FSUserList:
+class FSUserList (FSGatewayBase):
"""User list gateway, where the user list is stored in a flat file."""
__implements__ = IGateway
@@ -107,15 +108,9 @@
schema.addField('roles', 'string:list')
schema.addField('domains', 'string:list')
- def __init__(self, fs_conn):
- self.fs_conn = fs_conn
-
- def getSchema(self):
- return self.schema
-
def load(self, event):
- c = self.fs_conn
- p = event.getKeychain()[-1]
+ c = self.getConnection(event)
+ p = event.getKey()
assert c.readNodeType(p) == 'f'
text = c.readData(p)
res = []
@@ -163,9 +158,10 @@
domainlist = self._joinList(domains)
to_write = '%s:%s:%s:%s' % (id, password, rolelist, domainlist)
replace_lines[id] = to_write
- p = event.getKeychain()[-1]
- self.fs_conn.writeNodeType(p, 'f')
- text = self.fs_conn.readData(p, allow_missing=1)
+ p = event.getKey()
+ fs_conn = self.getConnection(event)
+ fs_conn.writeNodeType(p, 'f')
+ text = fs_conn.readData(p, allow_missing=1)
if text is None:
text = ''
new_lines = []
@@ -187,7 +183,7 @@
new_lines.append(line)
# Write it
text = '\n'.join(new_lines)
- self.fs_conn.writeData(p, text)
+ fs_conn.writeData(p, text)
serial = list(state)
serial.sort()
return text
=== Products/Ape/lib/apelib/fs/structure.py 1.1 => 1.2 ===
--- Products/Ape/lib/apelib/fs/structure.py:1.1 Wed Apr 9 23:09:55 2003
+++ Products/Ape/lib/apelib/fs/structure.py Tue Apr 29 18:11:50 2003
@@ -21,8 +21,10 @@
from apelib.core.interfaces import IGateway
from apelib.core.schemas import FieldSchema, RowSequenceSchema
+from base import FSGatewayBase
-class FSFileData:
+
+class FSFileData (FSGatewayBase):
"""File data gateway, where data is a string.
"""
@@ -30,43 +32,37 @@
schema = FieldSchema('data', 'string')
- def __init__(self, fs_conn, text=0):
- self.fs_conn = fs_conn
+ def __init__(self, text=0, conn_name='fs'):
self.text = text
-
- def getSchema(self):
- return self.schema
+ FSGatewayBase.__init__(self, conn_name)
def load(self, event):
- c = self.fs_conn
- p = event.getKeychain()[-1]
+ c = self.getConnection(event)
+ p = event.getKey()
assert c.readNodeType(p) == 'f'
state = c.readData(p, as_text=self.text)
return state, state
def store(self, event, state):
if not isinstance(state, StringType):
- raise RuntimeError('Not a string: %s' % repr(state))
- c = self.fs_conn
- p = event.getKeychain()[-1]
+ raise ValueError('Not a string: %s' % repr(state))
+ c = self.getConnection(event)
+ p = event.getKey()
c.writeNodeType(p, 'f')
c.writeData(p, state, as_text=self.text)
return state
-class FSAutoId:
- """Automatic ID gateway based on the keychain of the item."""
+class FSAutoId (FSGatewayBase):
+ """Automatic ID gateway based on the key of the item."""
__implements__ = IGateway
schema = FieldSchema('id', 'string')
- def getSchema(self):
- return self.schema
-
def getIdFrom(self, event):
- path = event.getKeychain()[-1]
+ path = event.getKey()
pos = path.rfind('/')
if pos >= 0:
return path[pos + 1:]
@@ -85,7 +81,7 @@
-class FSDirectoryItems:
+class FSDirectoryItems (FSGatewayBase):
"""Read/write objects in a filesystem directory."""
__implements__ = IGateway
@@ -93,15 +89,9 @@
schema = RowSequenceSchema()
schema.addField('id', 'string', 1)
- def __init__(self, fs_conn):
- self.fs_conn = fs_conn
-
- def getSchema(self):
- return self.schema
-
def load(self, event):
- p = event.getKeychain()[-1]
- c = self.fs_conn
+ p = event.getKey()
+ c = self.getConnection(event)
assert c.readNodeType(p) == 'd'
names = c.readData(p)
names.sort()
@@ -109,8 +99,8 @@
return res, res
def store(self, event, state):
- p = event.getKeychain()[-1]
- c = self.fs_conn
+ p = event.getKey()
+ c = self.getConnection(event)
c.writeNodeType(p, 'd')
state = list(state)
state.sort()
@@ -119,22 +109,17 @@
return tuple(state)
-class FSModTime:
+class FSModTime (FSGatewayBase):
"""Reads the modification time of a file."""
__implements__ = IGateway
schema = FieldSchema('mtime', 'int')
- def __init__(self, fs_conn):
- self.fs_conn = fs_conn
-
- def getSchema(self):
- return self.schema
-
def load(self, event):
p = event.getKey()
- state = long(self.fs_conn.getModTime(p))
+ fs_conn = self.getConnection(event)
+ state = long(fs_conn.getModTime(p))
return state, None # Use None as the hash (see store())
def store(self, event, state):