[Zope-Checkins]
SVN: Zope/branches/jim-fix-zclasses/lib/python/ZClasses/
Integrated persistent metaclass with ZClasses and got basic ZClass
Jim Fulton
jim at zope.com
Sun Feb 13 11:37:33 EST 2005
Log message for revision 29136:
Integrated persistent metaclass with ZClasses and got basic ZClass
test to pass.
Changed:
U Zope/branches/jim-fix-zclasses/lib/python/ZClasses/ZClass.py
U Zope/branches/jim-fix-zclasses/lib/python/ZClasses/ZClass.txt
U Zope/branches/jim-fix-zclasses/lib/python/ZClasses/_pmc.py
U Zope/branches/jim-fix-zclasses/lib/python/ZClasses/tests.py
-=-
Modified: Zope/branches/jim-fix-zclasses/lib/python/ZClasses/ZClass.py
===================================================================
--- Zope/branches/jim-fix-zclasses/lib/python/ZClasses/ZClass.py 2005-02-13 16:15:15 UTC (rev 29135)
+++ Zope/branches/jim-fix-zclasses/lib/python/ZClasses/ZClass.py 2005-02-13 16:37:33 UTC (rev 29136)
@@ -22,6 +22,7 @@
from Products.PythonScripts.PythonScript import PythonScript
from zExceptions import BadRequest, Redirect
import webdav.Collection
+import ZClasses._pmc
import marshal
@@ -91,8 +92,19 @@
p_.ZClass_Icon=Globals.ImageFile('class.gif', globals())
class PersistentClass(Base, webdav.Collection.Collection ):
- def __class_init__(self): pass
+ __metaclass__ = ZClasses._pmc.ZClassPersistentMetaClass
+
+ # We need this class to be treated as a normal global class, even
+ # though it is an instance of ZClassPersistentMetaClass.
+ # Subclasses should be stored in the database. See
+ # _pmc._p_DataDescr.__get__.
+
+ __global_persistent_class_not_stored_in_DB__ = True
+
+ def __class_init__(self):
+ pass
+
manage_addZClassForm=Globals.DTMLFile(
'dtml/addZClass', globals(),
default_class_='OFS.SimpleItem Item',
Modified: Zope/branches/jim-fix-zclasses/lib/python/ZClasses/ZClass.txt
===================================================================
--- Zope/branches/jim-fix-zclasses/lib/python/ZClasses/ZClass.txt 2005-02-13 16:15:15 UTC (rev 29135)
+++ Zope/branches/jim-fix-zclasses/lib/python/ZClasses/ZClass.txt 2005-02-13 16:37:33 UTC (rev 29136)
@@ -6,65 +6,86 @@
To do anything, we need a working Zope object space:
- >>> from ZODB.DemoStorage import DemoStorage
- >>> s = DemoStorage()
- >>> import ZODB.DB
- >>> db = ZODB.DB(s)
- >>> conn = db.open()
- >>> from OFS.Application import Application
- >>> app = Application()
- >>> conn.root()['Application'] = app
- >>> from OFS.Application import initialize
- >>> initialize(app)
+ >>> conn = some_database.open()
+ >>> from OFS.Application import Application
+ >>> app = Application()
+ >>> conn.root()['Application'] = app
+ >>> from OFS.Application import initialize
+ >>> initialize(app)
Once we have an object space, we need to create a product to hold the ZClass:
- >>> app.Control_Panel.Products.manage_addProduct('test', '')
- >>> test = app.Control_Panel.Products['test']
+ >>> app.Control_Panel.Products.manage_addProduct('test', '')
+ >>> test = app.Control_Panel.Products['test']
Then we can create the ZClass in the product:
- >>> test.manage_addZClass('C', zope_object=True, CreateAFactory=True)
+ >>> test.manage_addZClass('C', zope_object=True, CreateAFactory=True)
Having create a ZClass, we can create an instance:
- >>> c = test.C()
- >>> c._setId('c')
- >>> app._setObject('c', c)
- 'c'
+ >>> c = test.C()
+ >>> c._setId('c')
+ >>> app._setObject('c', c)
+ 'c'
Now, ZClass instances aren't very interesting by themselves. We can
give them data by defining property sheets:
- >>> test.C.propertysheets.common.manage_addCommonSheet('basic', '')
- >>> test.C.propertysheets.common['basic'].manage_addProperty(
- ... 'x', 'hee ', 'string')
- >>> app.c.x
- 'hee '
- >>> test.C.propertysheets.common['basic'].manage_addProperty('y', 42, 'int')
- >>> app.c.y
- 42
+ >>> test.C.propertysheets.common.manage_addCommonSheet('basic', '')
+ >>> test.C.propertysheets.common['basic'].manage_addProperty(
+ ... 'x', 'hee ', 'string')
+ >>> app.c.x
+ 'hee '
+ >>> test.C.propertysheets.common['basic'].manage_addProperty(
+ ... 'y', 42, 'int')
+ >>> app.c.y
+ 42
Of course, we can change the data:
- >>> app.c.x = 'hi '
- >>> app.c.y = 3
- >>> app.c.x, app.c.y
- ('hi ', 3)
+ >>> app.c.x = 'hi '
+ >>> app.c.y = 3
+ >>> app.c.x, app.c.y
+ ('hi ', 3)
We can also add methods, such as Python scripts:
- >>> test.C.propertysheets.methods.manage_addProduct[
- ... 'PythonScripts'].manage_addPythonScript('eek')
- ''
- >>> test.C.propertysheets.methods['eek'].ZPythonScript_edit('',
- ... 'return container.x * container.y')
+ >>> test.C.propertysheets.methods.manage_addProduct[
+ ... 'PythonScripts'].manage_addPythonScript('eek')
+ ''
+ >>> test.C.propertysheets.methods['eek'].ZPythonScript_edit('',
+ ... 'return container.x * container.y')
- >>> app.c.eek()
- 'hi hi hi '
+ >>> app.c.eek()
+ 'hi hi hi '
-We're done, so clean up:
+Let's commit our changes:
- >>> import transaction
- >>> transaction.commit()
- >>> db.close()
+ >>> import transaction
+ >>> transaction.commit()
+
+We can access the class in another connection:
+
+ >>> import threading
+ >>> def run(func):
+ ... thread = threading.Thread(target=func)
+ ... thread.start()
+ ... thread.join()
+
+ >>> def read_class():
+ ... connection = some_database.open()
+ ... app = connection.root()['Application']
+ ... test = app.Control_Panel.Products['test']
+ ... c2 = test.C()
+ ... c2._setId('c')
+ ... app._setObject('c2', c2)
+ ... app.c2.x = '*'
+ ... print app.c2.x, app.c2.y, app.c2.eek(), '!'
+ ... print app.c.x, app.c.y, app.c.eek(), '!'
+ ... connection.close()
+
+
+ ... run(read_class)
+ hee 42 ****************************************** !
+ hi 3 hi hi hi !
Modified: Zope/branches/jim-fix-zclasses/lib/python/ZClasses/_pmc.py
===================================================================
--- Zope/branches/jim-fix-zclasses/lib/python/ZClasses/_pmc.py 2005-02-13 16:15:15 UTC (rev 29135)
+++ Zope/branches/jim-fix-zclasses/lib/python/ZClasses/_pmc.py 2005-02-13 16:37:33 UTC (rev 29136)
@@ -57,6 +57,9 @@
def __get__(self, inst, cls):
if inst is None:
return self
+
+ if '__global_persistent_class_not_stored_in_DB__' in inst.__dict__:
+ raise AttributeError, self.__name__
return inst._p_class_dict.get(self.__name__)
def __set__(self, inst, v):
Modified: Zope/branches/jim-fix-zclasses/lib/python/ZClasses/tests.py
===================================================================
--- Zope/branches/jim-fix-zclasses/lib/python/ZClasses/tests.py 2005-02-13 16:15:15 UTC (rev 29135)
+++ Zope/branches/jim-fix-zclasses/lib/python/ZClasses/tests.py 2005-02-13 16:37:33 UTC (rev 29136)
@@ -44,11 +44,13 @@
return unittest.TestSuite((
# To do:
- # - test integration: doctest.DocFileSuite("ZClass.txt"),
+ # - Beef up basic test
# - Test working with old pickles
+ # - Test proper handling of __of__
# - Test export/import
doctest.DocFileSuite("_pmc.txt", setUp=setUp, tearDown=tearDown),
+ doctest.DocFileSuite("ZClass.txt", setUp=setUp, tearDown=tearDown),
))
if __name__ == '__main__':
More information about the Zope-Checkins
mailing list