This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
glasnost/shared/common/CardsCommon.py

333 lines
13 KiB
Python

# -*- coding: iso-8859-15 -*-
# Glasnost
# By: Odile Bénassy <obenassy@entrouvert.com>
# Romain Chantereau <rchantereau@entrouvert.com>
# Nicolas Clapiès <nclapies@easter-eggs.org>
# Pierre-Antoine Dejace <padejace@entrouvert.be>
# Thierry Dulieu <tdulieu@easter-eggs.com>
# Florent Monnier <monnier@codelutin.com>
# Cédric Musso <cmusso@easter-eggs.org>
# Frédéric Péters <fpeters@entrouvert.be>
# Benjamin Poussin <poussin@codelutin.com>
# Emmanuel Raviart <eraviart@entrouvert.com>
# Sébastien Régnier <regnier@codelutin.com>
# Emmanuel Saracco <esaracco@easter-eggs.com>
#
# Copyright (C) 2000, 2001 Easter-eggs & Emmanuel Raviart
# Copyright (C) 2002 Odile Bénassy, Code Lutin, Thierry Dulieu, Easter-eggs,
# Entr'ouvert, Frédéric Péters, Benjamin Poussin, Emmanuel Raviart,
# Emmanuel Saracco & Théridion
# Copyright (C) 2003 Odile Bénassy, Romain Chantereau, Nicolas Clapiès,
# Code Lutin, Pierre-Antoine Dejace, Thierry Dulieu, Easter-eggs,
# Entr'ouvert, Florent Monnier, Cédric Musso, Ouvaton, Frédéric Péters,
# Benjamin Poussin, Rodolphe Quiédeville, Emmanuel Raviart, Sébastien
# Régnier, Emmanuel Saracco, Théridion & Vecam
#
# 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.
__doc__ = """Glasnost Cards Common Models"""
__version__ = '$Revision$'[11:-2]
from ObjectsCommon import AdminCommon, ObjectCommon, ObjectsCommonMixin
import slots
import tools_new as commonTools
class AdminCardsCommon(AdminCommon):
serverRole = 'cards'
class CardCommon(ObjectCommon):
language_kindName = None
properties = None
properties_kindName = 'Properties'
prototypeIds = None
prototypeIds_kind_itemKind_value_serverRoles = ['cards']
prototypeIds_kind_itemKind_valueName = 'Id'
prototypeIds_kindName = 'Sequence'
serverRole = 'cards'
values = None
views = None
## views_kind_itemKind_value_valueThingCategory = 'widget'
## views_kind_itemKind_value_valueThingName = 'View'
## views_kind_itemKind_valueName = 'Thing'
## views_kindName = 'Sequence'
def getC3Linearization(self):
# An implementation of the C3 class hierarchy Linearization for the
# cards prototypes.
# Cf. http://www.webcom.com/haahr/dylan/linearization-oopsla96.html
assert self.id
return [self.id] + self.getC3PrototypeLinearization()
def getC3PrototypeLinearization(self):
# An implementation of the C3 class hierarchy Linearization for the
# cards prototypes.
# Cf. http://www.webcom.com/haahr/dylan/linearization-oopsla96.html
# Note: Only the prototypes are put in this linearization, not "self"
# itself.
linearization = []
if self.prototypeIds:
prototypeIdsByPrecedenceOrder = self.prototypeIds[:]
prototypeIdsByPrecedenceOrder.reverse()
remainingInputs = [
self.getCard(prototypeId).getC3Linearization()
for prototypeId in self.prototypeIds] \
+ [prototypeIdsByPrecedenceOrder]
while remainingInputs:
# The first class which is not in the tail of each remaining
# input is the next class to append to the linearization.
for remainingInput in remainingInputs:
candidate = remainingInput[0]
for remainingInput2 in remainingInputs:
if candidate in remainingInput2[1:]:
break
else:
break
else:
raise faults.InconsistentPrototypeHierarchy(self)
# A good candidate has been found.
# Add it to the partial linearization and remove it from the
# remaining inputs.
linearization.append(candidate)
newRemainingInputs = []
for remainingInput in remainingInputs:
if remainingInput[0] == candidate:
del remainingInput[0]
if not remainingInput:
continue
newRemainingInputs.append(remainingInput)
remainingInputs = newRemainingInputs
return linearization
def getCard(self, cardId):
raise NotImplementedError
def getDefaultPropertyValue(self, propertyName, propertyKind):
# FIXME: We should use a Python iterator for computing the C3
# linearization, because here we use only the first prototype.
prototypeIds = self.getC3PrototypeLinearization()
for prototypeId in prototypeIds:
prototype = self.getCard(prototypeId)
if prototype.hasDirectPropertyValue(propertyName):
return prototype.getDirectPropertyValue(propertyName)
return None
def getDirectPropertyNames(self):
if self.properties is None:
return []
return [property.name for property in self.properties]
def getDirectPropertyValue(self, propertyName):
if not self.hasDirectPropertyValue(propertyName):
raise faults.MissingItem(propertyName)
return self.values[propertyName]
def getDirectPropertyValueKind(self, propertyName):
if self.properties:
for property in self.properties:
if property.name == propertyName:
return property.kind
return None
# This method has ben commented, because it doesn't work when a property name
# has changed: self.values may contain an item whose name doesn't correspond to
# a property anymore.
## def getExportSlotNames(self, parentSlot = None):
## slotNames = ObjectCommon.getExportSlotNames(
## self, parentSlot = parentSlot)
## if self.values is None:
## return slotNames
## return slotNames + self.values.keys()
def getImportSlotNames(self, parentSlot = None):
names = ObjectCommon.getImportSlotNames(self, parentSlot = parentSlot)
names = names[:]
if 'properties' in names:
# The slot 'properties' is already imported by the method
# importFromXmlRpc.
names.remove('properties')
if 'prototypeIds' in names:
# The slot 'prototypeIds' is already imported by the method
# importFromXmlRpc.
names.remove('prototypeIds')
return names
def getLabel(self):
if self.hasSlotName('title'):
label = self.getSlot('title').getValue()
if label:
return label
return 'Card number %s' % commonTools.extractLocalId(self.id)
def getLabelLanguage(self):
if hasattr(self, 'language') and self.language:
return self.language
return ''
def getOrderedLayoutSlotNames(self, parentSlot = None):
slotNames = ObjectCommon.getOrderedLayoutSlotNames(
self, parentSlot = parentSlot)
slotNames += ['prototypeIds', 'properties']
# If a standard slotName is also a propertyName, display it using the
# property order. For example, if a card has a "language" property,
# this property is not displayed before the other properties.
propertyNames = self.getPropertyNames()
nonPropertyNames = [slotName
for slotName in slotNames
if not slotName in propertyNames]
return nonPropertyNames + propertyNames
def getPropertiesCount(self):
return len(self.getPropertyNames())
def getPropertyNames(self):
propertyNames = []
prototypeIds = self.getC3PrototypeLinearization()
prototypeIds.reverse()
for prototypeId in prototypeIds:
prototype = self.getCard(prototypeId)
prototypePropertyNames = prototype.getDirectPropertyNames()
for propertyName in prototypePropertyNames:
if propertyName not in propertyNames:
propertyNames.append(propertyName)
propertyNames += self.getDirectPropertyNames()
return propertyNames
def getPropertySlot(self, propertyName, parentSlot = None):
propertiesSlot = self.getSlot('properties', parentSlot = parentSlot)
return slots.Property(propertyName, parent = propertiesSlot)
def getPropertyValue(self, propertyName, propertyKind):
if self.hasDirectPropertyValue(propertyName):
return self.getDirectPropertyValue(propertyName)
return self.getDefaultPropertyValue(propertyName, propertyKind)
def getPropertyValueKind(self, propertyName):
kind = self.getDirectPropertyValueKind(propertyName)
if kind is not None:
return kind
prototypeIds = self.getC3PrototypeLinearization()
for prototypeId in prototypeIds:
prototype = self.getCard(prototypeId)
kind = prototype.getDirectPropertyValueKind(propertyName)
if kind is not None:
return kind
raise Exception('Property "%s" of %s has no kind' % (
propertyName, self))
def getPropertyValueSlot(self, propertyName, parentSlot = None):
if parentSlot is None:
container = self
else:
container = None
return slots.PropertyValue(
propertyName, container = container, parent = parentSlot)
def getSlot(self, attributeName, parentSlot = None):
if attributeName in ObjectCommon.getSlotNames(
self, parentSlot = parentSlot):
return ObjectCommon.getSlot(
self, attributeName, parentSlot = parentSlot)
return self.getPropertyValueSlot(
attributeName, parentSlot = parentSlot)
def getSlotNames(self, parentSlot = None):
slotNames = ObjectCommon.getSlotNames(self, parentSlot = parentSlot)
return slotNames + self.getPropertyNames()
def hasDirectPropertyValue(self, propertyName):
return self.values and self.values.has_key(propertyName)
def importFromXmlRpc(self, dataImport, parentSlot = None):
# The slot 'properties' must be imported first, because it is needed
# to compute the slotNames list.
if dataImport.has_key('__noneSlotNames__'):
noneSlotNames = dataImport['__noneSlotNames__']
else:
noneSlotNames = []
for slotName in ['properties', 'prototypeIds']:
if dataImport.has_key(slotName):
exportedValue = dataImport[slotName]
elif slotName in noneSlotNames:
exportedValue = None
else:
continue
slot = self.getSlot(slotName, parentSlot = parentSlot)
kind = slot.getKind()
if not kind.isImportable():
continue
slot.setValue(kind.importValueFromXmlRpc(slot, exportedValue))
return ObjectCommon.importFromXmlRpc(
self, dataImport, parentSlot = parentSlot)
def setDirectPropertyValue(self, propertyName, value):
if self.values is None:
self.values = {}
self.values[propertyName] = value
def setDirectPropertyValueKind(self, propertyName, propertyKind):
if self.properties is None:
self.properties = []
else:
for i in range(len(self.properties)):
if self.properties[i].name == propertyName:
if propertyKind is None:
del self.properties[i]
return
else:
property = self.properties[i]
property.kind = propertyKind
property.kindName = propertyKind.getThingName()
return
if propertyKind is not None:
property = commonTools.newThing('other', 'Property')
property.kind = propertyKind
property.kindName = propertyKind.getThingName()
property.name = propertyName
self.properties.append(property)
def setPropertyValue(self, propertyName, propertyKind, value):
if self.values is None:
self.values = {}
if value == self.getDefaultPropertyValue(propertyName, propertyKind):
if self.values.has_key(propertyName):
del self.values[propertyName]
if not self.values:
del self.values
else:
self.values[propertyName] = value
class CardsCommonMixin(ObjectsCommonMixin):
adminClassName = 'AdminCards'
newObjectNameCapitalized = N_('New Card')
objectClassName = 'Card'
objectName = N_('card')
objectNameCapitalized = N_('Card')
objectsName = N_('cards')
objectsNameCapitalized = N_('Cards')
serverRole = 'cards'