1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
from OFS.PropertySheets import PropertySheet
from Providers import Provider
from Globals import default__class_init__
from Products.PlugIns import defaultConstructors
psKey = 'ZPatterns.SheetProviders', 'PropertySheets'
class SheetProvider(Provider):
"""
Sheet provider example class. This sheet provider uses the 'slot'
facility of Racks/Rackmountables to provide arbitrary propertysheets
stored persistently. All of its methods would need to be redefined
for SheetProviders that implement fixed-schema and/or non-slot-based
property sheets.
"""
__plugin_kind__ = 'Property Sheet Provider'
def namesForRegistration(self,container):
"""
Return a tuple (names,namespaces) where names is a sequence of
sheet names, and namespaces a sequence of XML namespaces, which this sheet
provider supports. '*' may be included in either or both sequences, which
means the provider will be called if a more specific provider could not
be found for a given sheet. This is mainly so that sheet providers can
potentially create propertysheets dynamically based on the requested name
or namespace.
"""
return {
'provides': ('sheets',),
'sheet_names': self.Sheet_Names,
'sheet_xmlns': self.Sheet_Namespaces
}
def _PropertySheetFor(self,client,name='',xmlns=''):
"""
Return client's propertysheet matching name or xmlns, if available.
Otherwise return None.
"""
pslist = client._v_readableSlot.get(psKey,())
for ps in pslist:
if (name and ps.id==name) or (xmlns and ps.xml_namespace()==xmlns):
return ps
def _addPropertySheetFor(self,client,name='',xmlns=''):
"""
Return client's new propertysheet matching name or xmlns, if possible.
Otherwise return None.
"""
ps=PropertySheet(name, {'xmlns':xmlns})
pslist = client._v_readableSlot.get(psKey,())+(ps,)
client._v_writeableSlot[psKey] = pslist
return ps
def _delPropertySheetFor(self,client,name='',xmlns=''):
"""
Delete client's propertysheet matching name or xmlns, if present.
Always returns None (unless an unexpected error occurs, of course)
"""
pslist = client._v_readableSlot.get(psKey,())
newlist = tuple(filter(
lambda ps,name=name,xmlns=xmlns:
(not name or ps.id!=name) and
(not xmlns or ps.xml_namespace()!=xmlns),
pslist
))
if newlist!=pslist:
client._v_writeableSlot[psKey] = newlist
def _PropertySheetsFor(self, client):
"""Return a (possibly empty) list of PropertySheet objects for client"""
# Note: this could include duplicate sheets if persistent sheets
# are defined by more than one provider!
return client._v_readableSlot.get(psKey,())
# Class Metadata
meta_type='Persistent Sheet Provider'
Sheet_Names = ('*',)
Sheet_Namespaces = ('*',)
_properties=(
{'id':'title', 'type': 'string', 'mode': 'w'},
{'id':'Sheet_Names', 'type': 'tokens', 'mode': 'w'},
{'id':'Sheet_Namespaces', 'type': 'lines', 'mode': 'w'},
)
def initialize(context):
context.registerPlugInClass(
SheetProvider,
permission = 'Add Sheet Providers',
constructors = defaultConstructors(SheetProvider,globals()),
icon = 'www/sheetprov.gif'
)
|