From b9c6283d9e4de098168775c55b4071c2bf631fee Mon Sep 17 00:00:00 2001 From: Gaetan Delannay Date: Wed, 7 Sep 2011 01:33:09 +0200 Subject: [PATCH] appy.gen: stopped generating properties on wrapppers by setting AbstractWrapper as new-style class and using __getattribute__. --- gen/plone25/generator.py | 33 ------------ gen/plone25/installer.py | 13 ++--- gen/plone25/wrappers/__init__.py | 89 ++++++++++++++++---------------- 3 files changed, 47 insertions(+), 88 deletions(-) diff --git a/gen/plone25/generator.py b/gen/plone25/generator.py index f862252..96c62e0 100644 --- a/gen/plone25/generator.py +++ b/gen/plone25/generator.py @@ -433,19 +433,6 @@ class Generator(AbstractGenerator): repls['totalNumberOfTests'] = self.totalNumberOfTests 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): '''Returns the descriptors for all the classes in the generated gen-application. If p_include is: @@ -511,26 +498,6 @@ class Generator(AbstractGenerator): wrapperDef = 'class %s_Wrapper(%s):\n' % \ (c.name, ','.join(parentClasses)) 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: # For custom tool, add a call to a method that allows to # customize elements from the base class. diff --git a/gen/plone25/installer.py b/gen/plone25/installer.py index 8e5fb52..29b9f01 100644 --- a/gen/plone25/installer.py +++ b/gen/plone25/installer.py @@ -356,17 +356,10 @@ class PloneInstaller: if not role in data: data.append(role) # Add to portal_role_manager - # First, try to fetch it. If it's not there, we probaly have no - # PAS or another way to deal with roles was configured. + prm = site.acl_users.portal_role_manager try: - prm = site.acl_users.get('portal_role_manager', None) - if prm is not None: - try: - prm.addRole(role, role, - "Added by product '%s'" % self.productName) - except KeyError: # Role already exists - pass - except AttributeError: + prm.addRole(role, role, 'Added by "%s"' % self.productName) + except KeyError: # Role already exists pass # If it is a global role, create a specific group and grant him # this role diff --git a/gen/plone25/wrappers/__init__.py b/gen/plone25/wrappers/__init__.py index 2313ef6..19c4807 100644 --- a/gen/plone25/wrappers/__init__.py +++ b/gen/plone25/wrappers/__init__.py @@ -4,7 +4,7 @@ # ------------------------------------------------------------------------------ import os, os.path, time, mimetypes, random import appy.pod -from appy.gen import Search +from appy.gen import Type, Search, Ref from appy.gen.utils import sequenceTypes from appy.shared.utils import getOsTempFolder, executeCommand, normalizeString from appy.shared.xml_parser import XmlMarshaller @@ -20,7 +20,7 @@ FREEZE_FATAL_ERROR = 'A server error occurred. Please contact the system ' \ 'administrator.' # ------------------------------------------------------------------------------ -class AbstractWrapper: +class AbstractWrapper(object): '''Any real Zope object has a companion object that is an instance of this class.''' def __init__(self, o): self.__dict__['o'] = o @@ -32,6 +32,48 @@ class AbstractWrapper: raise 'Attribute "%s" does not exist.' % name 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): return '<%s appyobj at %s>' % (self.klass.__name__, id(self)) @@ -50,49 +92,6 @@ class AbstractWrapper: if customUser.__dict__.has_key(methodName): 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 link(self, fieldName, obj):