appy.gen: stopped generating properties on wrapppers by setting AbstractWrapper as new-style class and using __getattribute__.

This commit is contained in:
Gaetan Delannay 2011-09-07 01:33:09 +02:00
parent 3c95ac083d
commit b9c6283d9e
3 changed files with 47 additions and 88 deletions

View file

@ -433,19 +433,6 @@ class Generator(AbstractGenerator):
repls['totalNumberOfTests'] = self.totalNumberOfTests repls['totalNumberOfTests'] = self.totalNumberOfTests
self.copyFile('__init__.py', repls) self.copyFile('__init__.py', repls)
def generateWrapperProperty(self, name, type):
'''Generates the getter for attribute p_name.'''
res = ' def get_%s(self):\n ' % name
if name == 'title':
res += 'return self.o.Title()\n'
else:
suffix = ''
if type == 'Ref': suffix = ', noListIfSingleObj=True'
res += 'return self.o.getAppyType("%s").getValue(self.o%s)\n' % \
(name, suffix)
res += ' %s = property(get_%s)\n\n' % (name, name)
return res
def getClasses(self, include=None): def getClasses(self, include=None):
'''Returns the descriptors for all the classes in the generated '''Returns the descriptors for all the classes in the generated
gen-application. If p_include is: gen-application. If p_include is:
@ -511,26 +498,6 @@ class Generator(AbstractGenerator):
wrapperDef = 'class %s_Wrapper(%s):\n' % \ wrapperDef = 'class %s_Wrapper(%s):\n' % \
(c.name, ','.join(parentClasses)) (c.name, ','.join(parentClasses))
wrapperDef += ' security = ClassSecurityInfo()\n' wrapperDef += ' security = ClassSecurityInfo()\n'
titleFound = False
for attrName in c.orderedAttributes:
if attrName == 'title':
titleFound = True
try:
attrValue = getattr(c.klass, attrName)
except AttributeError:
attrValue = getattr(c.modelClass, attrName)
if isinstance(attrValue, Type):
wrapperDef += self.generateWrapperProperty(attrName,
attrValue.type)
# Generate properties for back references
if self.referers.has_key(c.name):
for refDescr, rel in self.referers[c.name]:
attrName = refDescr.appyType.back.attribute
wrapperDef += self.generateWrapperProperty(attrName, 'Ref')
if not titleFound:
# Implicitly, the title will be added by Archetypes. So I need
# to define a property for it.
wrapperDef += self.generateWrapperProperty('title', 'String')
if c.customized: if c.customized:
# For custom tool, add a call to a method that allows to # For custom tool, add a call to a method that allows to
# customize elements from the base class. # customize elements from the base class.

View file

@ -356,17 +356,10 @@ class PloneInstaller:
if not role in data: if not role in data:
data.append(role) data.append(role)
# Add to portal_role_manager # Add to portal_role_manager
# First, try to fetch it. If it's not there, we probaly have no prm = site.acl_users.portal_role_manager
# PAS or another way to deal with roles was configured.
try: try:
prm = site.acl_users.get('portal_role_manager', None) prm.addRole(role, role, 'Added by "%s"' % self.productName)
if prm is not None: except KeyError: # Role already exists
try:
prm.addRole(role, role,
"Added by product '%s'" % self.productName)
except KeyError: # Role already exists
pass
except AttributeError:
pass pass
# If it is a global role, create a specific group and grant him # If it is a global role, create a specific group and grant him
# this role # this role

View file

@ -4,7 +4,7 @@
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
import os, os.path, time, mimetypes, random import os, os.path, time, mimetypes, random
import appy.pod import appy.pod
from appy.gen import Search from appy.gen import Type, Search, Ref
from appy.gen.utils import sequenceTypes from appy.gen.utils import sequenceTypes
from appy.shared.utils import getOsTempFolder, executeCommand, normalizeString from appy.shared.utils import getOsTempFolder, executeCommand, normalizeString
from appy.shared.xml_parser import XmlMarshaller from appy.shared.xml_parser import XmlMarshaller
@ -20,7 +20,7 @@ FREEZE_FATAL_ERROR = 'A server error occurred. Please contact the system ' \
'administrator.' 'administrator.'
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
class AbstractWrapper: class AbstractWrapper(object):
'''Any real Zope object has a companion object that is an instance of this '''Any real Zope object has a companion object that is an instance of this
class.''' class.'''
def __init__(self, o): self.__dict__['o'] = o def __init__(self, o): self.__dict__['o'] = o
@ -32,6 +32,48 @@ class AbstractWrapper:
raise 'Attribute "%s" does not exist.' % name raise 'Attribute "%s" does not exist.' % name
appyType.store(self.o, value) appyType.store(self.o, value)
def __getattribute__(self, name):
'''Gets the attribute named p_name. Lot of cheating here.'''
if name == 'o': return object.__getattribute__(self, name)
elif name == 'tool': return self.o.getTool().appy()
elif name == 'request': return self.o.REQUEST
elif name == 'session': return self.o.REQUEST.SESSION
elif name == 'typeName': return self.__class__.__bases__[-1].__name__
elif name == 'id': return self.o.id
elif name == 'uid': return self.o.UID()
elif name == 'title': return self.o.Title()
elif name == 'klass': return self.__class__.__bases__[-1]
elif name == 'url': return self.o.absolute_url()
elif name == 'state': return self.o.getState()
elif name == 'stateLabel':
o = self.o
appName = o.getProductConfig().PROJECTNAME
return o.translate(o.getWorkflowLabel(), domain=appName)
elif name == 'history':
o = self.o
key = o.workflow_history.keys()[0]
return o.workflow_history[key]
elif name == 'user':
return self.o.portal_membership.getAuthenticatedMember()
elif name == 'fields': return self.o.getAllAppyTypes()
# Now, let's try to return a real attribute.
try:
res = object.__getattribute__(self, name)
except AttributeError, ae:
# Maybe a back reference?
res = self.o.getAppyType(name)
if not res:
print "Attribute error", ae
raise ae
# If we got an Appy type, return the value of this type for this object
if isinstance(res, Type):
o = self.o
if isinstance(res, Ref):
return res.getValue(o, noListIfSingleObj=True)
else:
return res.getValue(o)
return res
def __repr__(self): def __repr__(self):
return '<%s appyobj at %s>' % (self.klass.__name__, id(self)) return '<%s appyobj at %s>' % (self.klass.__name__, id(self))
@ -50,49 +92,6 @@ class AbstractWrapper:
if customUser.__dict__.has_key(methodName): if customUser.__dict__.has_key(methodName):
return customUser.__dict__[methodName](self, *args, **kwargs) return customUser.__dict__[methodName](self, *args, **kwargs)
def get_tool(self): return self.o.getTool().appy()
tool = property(get_tool)
def get_request(self): return self.o.REQUEST
request = property(get_request)
def get_session(self): return self.o.REQUEST.SESSION
session = property(get_session)
def get_typeName(self): return self.__class__.__bases__[-1].__name__
typeName = property(get_typeName)
def get_id(self): return self.o.id
id = property(get_id)
def get_uid(self): return self.o.UID()
uid = property(get_uid)
def get_klass(self): return self.__class__.__bases__[-1]
klass = property(get_klass)
def get_url(self): return self.o.absolute_url()
url = property(get_url)
def get_state(self): return self.o.getState()
state = property(get_state)
def get_stateLabel(self):
appName = self.o.getProductConfig().PROJECTNAME
return self.o.translate(self.o.getWorkflowLabel(), domain=appName)
stateLabel = property(get_stateLabel)
def get_history(self):
key = self.o.workflow_history.keys()[0]
return self.o.workflow_history[key]
history = property(get_history)
def get_user(self): return self.o.portal_membership.getAuthenticatedMember()
user = property(get_user)
def get_fields(self): return self.o.getAllAppyTypes()
fields = property(get_fields)
def getField(self, name): return self.o.getAppyType(name) def getField(self, name): return self.o.getAppyType(name)
def link(self, fieldName, obj): def link(self, fieldName, obj):