|
From: <de...@us...> - 2003-09-10 02:07:17
|
Update of /cvsroot/pymerase/pymerase/pymerase
In directory sc8-pr-cvs1:/tmp/cvs-serv10258
Modified Files:
ClassMembers.py
Log Message:
Support one to one associations.
Make class attributes private
Index: ClassMembers.py
===================================================================
RCS file: /cvsroot/pymerase/pymerase/pymerase/ClassMembers.py,v
retrieving revision 1.27
retrieving revision 1.28
diff -C2 -d -r1.27 -r1.28
*** ClassMembers.py 9 Sep 2003 23:14:12 -0000 1.27
--- ClassMembers.py 10 Sep 2003 02:07:13 -0000 1.28
***************
*** 115,130 ****
self.setName(name) # setName calls NameMangler.mangle
#self.name = name
! self.friendlyName = None
! self.description = None
! self.type = None
! self.uuid = None
# utility
! self.location = []
! self.defined = 0
# ER related
! self.indexed = 0
! self.primaryKey = 0
def setName(self, value):
--- 115,130 ----
self.setName(name) # setName calls NameMangler.mangle
#self.name = name
! self.__friendlyName = None
! self.__description = None
! self.__type = None
! self.__uuid = None
# utility
! self.__location = []
! self.__defined = 0
# ER related
! self.__indexed = 0
! self.__primaryKey = 0
def setName(self, value):
***************
*** 135,157 ****
def setFriendlyName(self, value):
! self.friendlyName = value
def getFriendlyName(self):
! if self.friendlyName is None:
return self.name
else:
! return self.friendlyName
def setDescription(self, value):
! self.description = value
def getDescription(self):
! return self.description
def setType(self, value):
! self.type = value
def getType(self):
! return self.type
def setUUID(self, uuid):
--- 135,157 ----
def setFriendlyName(self, value):
! self.__friendlyName = value
def getFriendlyName(self):
! if self.__friendlyName is None:
return self.name
else:
! return self.__friendlyName
def setDescription(self, value):
! self.__description = value
def getDescription(self):
! return self.__description
def setType(self, value):
! self.__type = value
def getType(self):
! return self.__type
def setUUID(self, uuid):
***************
*** 159,176 ****
raise RuntimeError("UUID must not be None")
! self.uuid = uuid
def getUUID(self):
! return self.uuid
def appendLocation(self, location):
"""Append a location describing where we saw this Element
"""
! self.location.append(location)
def getLocations(self):
"""Return list of locations describing where we saw this element
"""
! return self.location
def setDefined(self, flag):
--- 159,176 ----
raise RuntimeError("UUID must not be None")
! self.__uuid = uuid
def getUUID(self):
! return self.__uuid
def appendLocation(self, location):
"""Append a location describing where we saw this Element
"""
! self.__location.append(location)
def getLocations(self):
"""Return list of locations describing where we saw this element
"""
! return self.__location
def setDefined(self, flag):
***************
*** 179,188 ****
As opposed to just having been created by a dangling association reference.
"""
! self.defined = flag
def getDefined(self):
"""
"""
! return self.defined
def __str__(self):
--- 179,188 ----
As opposed to just having been created by a dangling association reference.
"""
! self.__defined = flag
def getDefined(self):
"""
"""
! return self.__defined
def __str__(self):
***************
*** 217,233 ****
# add base class vars, name, type, description, friendlyName
ModelElement.__init__(self, pymeraseConfig, name)
! self.required = 0
! self.unique = 0
! self.primaryKey = 0
! self.foreignKey = 0
def setRequired(self, value):
! self.required = parseBoolValue(value)
def isRequired(self):
! return self.required
def setUnique(self, value):
! self.unique = parseBoolValue(value)
def isUnique(self):
--- 217,235 ----
# add base class vars, name, type, description, friendlyName
ModelElement.__init__(self, pymeraseConfig, name)
! self.__required = 0
! self.__unique = 0
! self.__primaryKey = 0
! self.__foreignKey = 0
!
! self.__indexed = 0
def setRequired(self, value):
! self.__required = parseBoolValue(value)
def isRequired(self):
! return self.__required
def setUnique(self, value):
! self.__unique = parseBoolValue(value)
def isUnique(self):
***************
*** 236,246 ****
NOTE: primary keys are unique so return true in that case to
"""
! return self.unique or self.primaryKey
def setIndexed(self, value):
! self.indexed = parseBoolValue(value)
def isIndexed(self):
! return self.indexed
# FIXME: can something be a primary key and a foreign key?
--- 238,248 ----
NOTE: primary keys are unique so return true in that case to
"""
! return self.__unique or self.__primaryKey
def setIndexed(self, value):
! self.__indexed = parseBoolValue(value)
def isIndexed(self):
! return self.__indexed
# FIXME: can something be a primary key and a foreign key?
***************
*** 253,262 ****
raise ValueError("An attribute cannot be both a primary key and a foreign key")
else:
! self.primaryKey = flag
def isPrimaryKey(self):
"""Does this attribute model an ER Primary key relationship
"""
! return self.primaryKey
def setForeignKey(self, value):
--- 255,264 ----
raise ValueError("An attribute cannot be both a primary key and a foreign key")
else:
! self.__primaryKey = flag
def isPrimaryKey(self):
"""Does this attribute model an ER Primary key relationship
"""
! return self.__primaryKey
def setForeignKey(self, value):
***************
*** 267,281 ****
raise ValueError("An attribute cannot be both a primary key and a foreign key")
else:
! self.foreignKey = flag
def isForeignKey(self):
"""Does this attribute model an ER foreign key relationship
"""
! return self.foreignKey
def isKey(self):
"""Does this attribute model an ER key relationship
"""
! return (self.primaryKey or self.foreignKey)
def createAssociation(pymeraseConfig, thisEnd, otherEnd, associationName=None, associationUUID=None):
--- 269,283 ----
raise ValueError("An attribute cannot be both a primary key and a foreign key")
else:
! self.__foreignKey = flag
def isForeignKey(self):
"""Does this attribute model an ER foreign key relationship
"""
! return self.__foreignKey
def isKey(self):
"""Does this attribute model an ER key relationship
"""
! return (self.__primaryKey or self.__foreignKey)
def createAssociation(pymeraseConfig, thisEnd, otherEnd, associationName=None, associationUUID=None):
***************
*** 308,312 ****
else:
# everything is already set up
! pass
return association
--- 310,315 ----
else:
# everything is already set up
! assert thisEnd.getAssociation() == otherEnd.getAssociation()
! association = thisEnd.getAssociation()
return association
***************
*** 323,341 ****
self.name = name
! self.associationEnds = []
def addAssociationEnd(self, associationEnd):
! if len(self.associationEnds) <= 2:
# mark the AssociationEnd as being owned by this Association
# we were using a dictionary to prevent multiple references
# from being added to the associationEnd list
! #self.associationEnds[associationEnd] = associationEnd
! self.associationEnds.append(associationEnd)
else:
raise IndexError("Only allowed to have 2 AssociationEnd per Association")
def removeAssociationEnd(self, associationEnd):
! if associationEnd in self.associationEnds:
! self.associationEnds.remove(associationEnd)
associationEnd.setAssociation(None)
else:
--- 326,344 ----
self.name = name
! self.__associationEnds = []
def addAssociationEnd(self, associationEnd):
! if len(self.__associationEnds) <= 2:
# mark the AssociationEnd as being owned by this Association
# we were using a dictionary to prevent multiple references
# from being added to the associationEnd list
! #self.__associationEnds[associationEnd] = associationEnd
! self.__associationEnds.append(associationEnd)
else:
raise IndexError("Only allowed to have 2 AssociationEnd per Association")
def removeAssociationEnd(self, associationEnd):
! if associationEnd in self.__associationEnds:
! self.__associationEnds.remove(associationEnd)
associationEnd.setAssociation(None)
else:
***************
*** 346,354 ****
"""Return the list of association ends attached to this association
"""
! #return self.associationEnds.values()
! return self.associationEnds
def __len__(self):
! return len(self.associationEnds)
def __str__(self):
--- 349,357 ----
"""Return the list of association ends attached to this association
"""
! #return self.__associationEnds.values()
! return self.__associationEnds
def __len__(self):
! return len(self.__associationEnds)
def __str__(self):
***************
*** 356,363 ****
firstEndName = "--"
secondEndName = "--"
! if len(self.associationEnds) > 0:
! firstEndName = self.association[0].getName(None)
! elif len(self.associationEnds) > 1:
! secondEndName = self.association[1].getName(None)
return "Association: %s (%s, %s)" % (associationName,
firstEndName,
--- 359,366 ----
firstEndName = "--"
secondEndName = "--"
! if len(self.__associationEnds) > 0:
! firstEndName = self.__association[0].getName(None)
! elif len(self.__associationEnds) > 1:
! secondEndName = self.__association[1].getName(None)
return "Association: %s (%s, %s)" % (associationName,
firstEndName,
***************
*** 377,392 ****
# point to our containing association
! self.association = None
- # names
- self.attributeName = "unamed_attribute"
-
# class references
! self.type = None
# status information
! self.navigable = 0
! self.multiplicity = None
! self.aggregation = 0
def __str__(self):
--- 380,398 ----
# point to our containing association
! self.__association = None
# class references
! self.__type = None
# status information
! self.__navigable = 0
! self.__multiplicity = None
! self.__aggregation = 0
!
! # things to make ER easier
! # name of attribute providing the link
! self.__attributeName = "unamed_attribute"
! # define which association end has the foreign key
! self.__hasForeignKey = 0
def __str__(self):
***************
*** 397,404 ****
"""
if value is None:
! self.association = None
elif isinstance(value, Association):
! if self.association != value:
! self.association = value
else:
raise ValueError("expected Association type")
--- 403,410 ----
"""
if value is None:
! self.__association = None
elif isinstance(value, Association):
! if self.__association != value:
! self.__association = value
else:
raise ValueError("expected Association type")
***************
*** 407,414 ****
"""return the Association this AssociationEnd is attached to
"""
! return self.association
def setAttributeName(self, name):
! self.attributeName = name
def getAttributeName(self, translatorName):
--- 413,420 ----
"""return the Association this AssociationEnd is attached to
"""
! return self.__association
def setAttributeName(self, name):
! self.__attributeName = name
def getAttributeName(self, translatorName):
***************
*** 418,432 ****
"""
mangler = self.config.getNameMangler(translatorName)
! return mangler.mangle(self.attributeName)
def setType(self, value):
"""Set type of this association (usually a class)
"""
! self.type = value
def getType(self):
"""return type of this association (usually a class)
"""
! return self.type
def getClassName(self, translatorName):
--- 424,438 ----
"""
mangler = self.config.getNameMangler(translatorName)
! return mangler.mangle(self.__attributeName)
def setType(self, value):
"""Set type of this association (usually a class)
"""
! self.__type = value
def getType(self):
"""return type of this association (usually a class)
"""
! return self.__type
def getClassName(self, translatorName):
***************
*** 434,464 ****
"""
# FIXME: should we check to see if it exits?
! return self.type.getName(translatorName)
def setMultiplicity(self, value):
! self.multiplicity = value
def getMultiplicity(self):
! return self.multiplicity
def setNavigable(self, value):
! self.navigable = parseBoolValue(value)
def isNavigable(self):
! return self.navigable
def getOppositeEnd(self):
! if self.association is None or len(self.association) == 1:
return None
else:
! otherEnd = filter(lambda x: x != self, self.association.getLinks())
if len(otherEnd) == 1:
return otherEnd[0]
! elif len(otherEnd) == 0 and len(self.association.getLinks()) == 2:
# we have a self referental (object where both ends are the same
# object. So it doesn't matter which one we return
! return self.association.getLinks()[0]
else:
! raise RuntimeError("association had the wrong number of links %d instead of 2" % len(self.association.getLinks()))
--- 440,484 ----
"""
# FIXME: should we check to see if it exits?
! return self.__type.getName(translatorName)
!
! def setHasForeignKey(self, value):
! """set to true if the class this association end is the one containing
! the foreign key
! """
! # FIXME: how can we check to make sure that only one side has hasForeignKey
! # FIXME: set?
! self.__hasForeignKey = parseBoolValue(value)
+ def hasForeignKey(self):
+ """set to true if the class this association end is the one containing
+ the foreign key
+ """
+ return self.__hasForeignKey
+
def setMultiplicity(self, value):
! self.__multiplicity = value
def getMultiplicity(self):
! return self.__multiplicity
def setNavigable(self, value):
! self.__navigable = parseBoolValue(value)
def isNavigable(self):
! return self.__navigable
def getOppositeEnd(self):
! if self.__association is None or len(self.__association) == 1:
return None
else:
! otherEnd = filter(lambda x: x != self, self.__association.getLinks())
if len(otherEnd) == 1:
return otherEnd[0]
! elif len(otherEnd) == 0 and len(self.__association.getLinks()) == 2:
# we have a self referental (object where both ends are the same
# object. So it doesn't matter which one we return
! return self.__association.getLinks()[0]
else:
! raise RuntimeError("association had the wrong number of links %d instead of 2" % len(self.__association.getLinks()))
***************
*** 497,537 ****
# source file for class meta info
! self.path = None
! self.filename = None
# Information about this class,
# base classes, elements contained, linked to by this class
! self.packageName = pymeraseConfig.getDefaultPackage()
! self.attributes = {}
! self.attributes_order = []
! self.associationEnds = {}
# inheritance info
! self.abstract = 0
! self.baseClasses = []
! self.rootClassName = None
# things useful for ER/SQL Modules
! self.primaryKeyName = None
! self.primaryKeyConstructed = 0
! self.foreignKeyName = None
! self.security = []
! self.indices = []
def setFilename(self, pathname):
"""Set source filename for the definition of this object
"""
! self.path, self.filename = os.path.split(pathname)
def getFilename(self):
"""Get source filename for the definition of this object
"""
! return self.filename
def setAbstract(self, value):
! self.abstract = parseBoolValue(value)
def isAbstract(self):
! return self.abstract
def setAssociationEnd(self, value):
--- 517,557 ----
# source file for class meta info
! self.__path = None
! self.__filename = None
# Information about this class,
# base classes, elements contained, linked to by this class
! self.__packageName = pymeraseConfig.getDefaultPackage()
! self.__attributes = {}
! self.__attributes_order = []
! self.__associationEnds = {}
# inheritance info
! self.__abstract = 0
! self.__baseClasses = []
! self.__rootClassName = None
# things useful for ER/SQL Modules
! self.__primaryKeyName = None
! self.__primaryKeyConstructed = 0
! self.__foreignKeyName = None
! # self.__security = []
! # self.__indices = []
def setFilename(self, pathname):
"""Set source filename for the definition of this object
"""
! self.__path, self.__filename = os.path.split(pathname)
def getFilename(self):
"""Get source filename for the definition of this object
"""
! return self.__filename
def setAbstract(self, value):
! self.__abstract = parseBoolValue(value)
def isAbstract(self):
! return self.__abstract
def setAssociationEnd(self, value):
***************
*** 539,543 ****
"""
if isinstance(value, AssociationEnd):
! self.associationEnds[value.getUUID()] = value
else:
raise ValueError("expected AssociationEnd type")
--- 559,563 ----
"""
if isinstance(value, AssociationEnd):
! self.__associationEnds[value.getUUID()] = value
else:
raise ValueError("expected AssociationEnd type")
***************
*** 546,550 ****
"""Return named association end attached to this object
"""
! return self.associationEnds[uuid]
def getAssociationEnds(self):
--- 566,570 ----
"""Return named association end attached to this object
"""
! return self.__associationEnds[uuid]
def getAssociationEnds(self):
***************
*** 555,559 ****
"""
! return self.associationEnds
--- 575,579 ----
"""
! return self.__associationEnds
***************
*** 562,566 ****
"""
mangler = self.config.getNameMangler(translatorName)
! return self.attributes.get(mangler.mangle(name), None)
def getAttributes(self):
--- 582,586 ----
"""
mangler = self.config.getNameMangler(translatorName)
! return self.__attributes.get(mangler.mangle(name), None)
def getAttributes(self):
***************
*** 568,572 ****
definition.
"""
! return map(lambda x: self.attributes[x], self.attributes_order)
def getAttributeNames(self, translatorName):
--- 588,592 ----
definition.
"""
! return map(lambda x: self.__attributes[x], self.__attributes_order)
def getAttributeNames(self, translatorName):
***************
*** 584,597 ****
# addition, as when we return things in the order added
# we'll return multiple instances
! if self.attributes.has_key(class_attribute.getName(None)):
return
if not insert:
! self.attributes_order.append(class_attribute.getName(None))
else:
! self.attributes_order.insert(0, class_attribute.getName(None))
! self.attributes[class_attribute.getName(None)] = class_attribute
def appendBaseClass(self, classToAdd):
"""append the name of super class to the list of classes
--- 604,622 ----
# addition, as when we return things in the order added
# we'll return multiple instances
! if self.__attributes.has_key(class_attribute.getName(None)):
return
if not insert:
! self.__attributes_order.append(class_attribute.getName(None))
else:
! self.__attributes_order.insert(0, class_attribute.getName(None))
! self.__attributes[class_attribute.getName(None)] = class_attribute
+ def removeAttributeByName(self, attributeName):
+ """Remove attribute from class meta info
+ """
+ del self.__attributes[attributeName]
+ self.__attributes_order.remove(attributeName)
def appendBaseClass(self, classToAdd):
"""append the name of super class to the list of classes
***************
*** 599,603 ****
#FIXME: should we be following the MAGE convention of using 'add'?
if isinstance(classToAdd, ClassMetaInfo):
! self.baseClasses.append(classToAdd)
else:
raise ValueError("appendBaseClass requires an object of type"\
--- 624,628 ----
#FIXME: should we be following the MAGE convention of using 'add'?
if isinstance(classToAdd, ClassMetaInfo):
! self.__baseClasses.append(classToAdd)
else:
raise ValueError("appendBaseClass requires an object of type"\
***************
*** 610,639 ****
raise ValueError("setBaseClass requires list")
! self.baseClasses = None
for c in classListToReplace:
! self.appendBaseClass(c)
def getBaseClasses(self):
"""Return list of super class references
"""
! return self.baseClasses
def getBaseClassNames(self, translatorName):
"""Returns list of super class names
"""
! return map(lambda x: x.getName(translatorName), self.baseClasses)
def setPackage(self, packageName):
if type(packageName) == types.StringType or \
type(packageName) == types.UnicodeType:
! self.packageName = packageName
def getPackage(self):
! return self.packageName
def isRootClass(self):
"""Return true if we don't inherit from any other class.
"""
! return not len(self.baseClasses)
def getRootClass(self):
--- 635,664 ----
raise ValueError("setBaseClass requires list")
! self.__baseClasses = None
for c in classListToReplace:
! self.__appendBaseClass(c)
def getBaseClasses(self):
"""Return list of super class references
"""
! return self.__baseClasses
def getBaseClassNames(self, translatorName):
"""Returns list of super class names
"""
! return map(lambda x: x.getName(translatorName), self.__baseClasses)
def setPackage(self, packageName):
if type(packageName) == types.StringType or \
type(packageName) == types.UnicodeType:
! self.__packageName = packageName
def getPackage(self):
! return self.__packageName
def isRootClass(self):
"""Return true if we don't inherit from any other class.
"""
! return not len(self.__baseClasses)
def getRootClass(self):
***************
*** 645,649 ****
return self
! for c in self.baseClasses:
rootClass = c.getRootClass()
if rootClass is not None:
--- 670,674 ----
return self
! for c in self.__baseClasses:
rootClass = c.getRootClass()
if rootClass is not None:
***************
*** 690,708 ****
# FIXME: we also need to allow the user some method of defining
# FIXME: the primary key name
! if self.primaryKeyName is None:
# Construct a primary key object
! self.primaryKeyName = self.name + "_pk"
! primaryKey = ClassAttribute(self.config, self.primaryKeyName)
primaryKey.setType(PymeraseType('serial'))
self.addAttribute(primaryKey, insert=1)
else:
! self.primaryKeyName = name
! warn("Setting primary_key_name: %s" % ( self.primaryKeyName ),
DebugWarning)
# well actually this indicates that we tried to construct a key,
# if it's not needed we don't bother trying again.
! self.primaryKeyConstructed = 1
--- 715,733 ----
# FIXME: we also need to allow the user some method of defining
# FIXME: the primary key name
! if self.__primaryKeyName is None:
# Construct a primary key object
! self.__primaryKeyName = self.name + "_pk"
! primaryKey = ClassAttribute(self.config, self.__primaryKeyName)
primaryKey.setType(PymeraseType('serial'))
self.addAttribute(primaryKey, insert=1)
else:
! self.__primaryKeyName = name
! warn("Setting primary_key_name: %s" % ( self.__primaryKeyName ),
DebugWarning)
# well actually this indicates that we tried to construct a key,
# if it's not needed we don't bother trying again.
! self.__primaryKeyConstructed = 1
***************
*** 714,743 ****
"""
! if self.primaryKeyName is not None:
! primaryKeyName = self.primaryKeyName
! elif self.isRootClass() and not self.primaryKeyConstructed:
self.setPrimaryKeyName()
! primaryKeyName = self.primaryKeyName
else:
return self.getBasePrimaryKeyName(translatorName)
mangler = self.config.getNameMangler(translatorName)
! return mangler.mangle(self.primaryKeyName)
def setForeignKeyName(self, keyName=None):
"""Set the foreign key name that should be used for this class.
"""
if keyName is None:
! if self.foreignKeyName is None:
# Construct a primary key object
! self.foreignKeyName = self.name + "_fk"
! self.foreignKeyNameConstructed = 1
else:
! self.foreignKeyName = keyName
! warn("Setting foreign_key_name: %s" % ( self.foreignKeyName ),
DebugWarning)
! return self.foreignKeyName
def getForeignKeyName(self, translatorName):
--- 739,777 ----
"""
! if self.__primaryKeyName is not None:
! primaryKeyName = self.__primaryKeyName
! elif self.isRootClass() and not self.__primaryKeyConstructed:
self.setPrimaryKeyName()
! primaryKeyName = self.__primaryKeyName
else:
return self.getBasePrimaryKeyName(translatorName)
mangler = self.config.getNameMangler(translatorName)
! return mangler.mangle(self.__primaryKeyName)
+ def setPrimaryKeyConstructed(self, value):
+ """set if primary key has been constructed
+ """
+ self.__primaryKeyConstructed = parseBoolValue(value)
+ def isPrimaryKeyConstructed(self):
+ """indicate state of primary key construction
+ """
+ return self.__primaryKeyConstructed
+
def setForeignKeyName(self, keyName=None):
"""Set the foreign key name that should be used for this class.
"""
if keyName is None:
! if self.__foreignKeyName is None:
# Construct a primary key object
! self.__foreignKeyName = self.name + "_fk"
! self.__foreignKeyNameConstructed = 1
else:
! self.__foreignKeyName = keyName
! warn("Setting foreign_key_name: %s" % ( self.__foreignKeyName ),
DebugWarning)
! return self.__foreignKeyName
def getForeignKeyName(self, translatorName):
***************
*** 746,760 ****
mangler = self.config.getNameMangler(translatorName)
! if self.foreignKeyName is not None:
! return mangler.mangle(self.foreignKeyName)
elif not self.isRootClass():
return mangler.mangle(self.setForeignKeyName())
else:
primaryKeyName = self.getPrimaryKeyName(translatorName)
! self.foreignKeyName = pymerase.util.NameMangling.RelationalKey().getForeignKey(primaryKeyName)
warn("No foreign key name set for %s, making one up %s" % (self.name,
! self.foreignKeyName),
InfoWarning)
! return self.foreignKeyName
--- 780,794 ----
mangler = self.config.getNameMangler(translatorName)
! if self.__foreignKeyName is not None:
! return mangler.mangle(self.__foreignKeyName)
elif not self.isRootClass():
return mangler.mangle(self.setForeignKeyName())
else:
primaryKeyName = self.getPrimaryKeyName(translatorName)
! self.__foreignKeyName = pymerase.util.NameMangling.RelationalKey().getForeignKey(primaryKeyName)
warn("No foreign key name set for %s, making one up %s" % (self.name,
! self.__foreignKeyName),
InfoWarning)
! return self.__foreignKeyName
|