[z3-checkins] r28693 - in z3/bdbxml/trunk: . xmlsite xmlsite/archived

paul at codespeak.net paul at codespeak.net
Mon Jun 12 08:53:01 CEST 2006


Author: paul
Date: Mon Jun 12 08:52:57 2006
New Revision: 28693

Added:
   z3/bdbxml/trunk/
   z3/bdbxml/trunk/xmlsite/
   z3/bdbxml/trunk/xmlsite/__init__.py
   z3/bdbxml/trunk/xmlsite/archived/
   z3/bdbxml/trunk/xmlsite/archived/bdbxmlcontainer.py
   z3/bdbxml/trunk/xmlsite/configure.zcml
   z3/bdbxml/trunk/xmlsite/interfaces.py
   z3/bdbxml/trunk/xmlsite/xmldbcontainer.py
   z3/bdbxml/trunk/xmlsite/xmlpage.py
Log:
Getting this re-organized.  Still a lot to do: everything requires authentication, no actually transforming stuff into HTML, no tests, etc.

Added: z3/bdbxml/trunk/xmlsite/__init__.py
==============================================================================
--- (empty file)
+++ z3/bdbxml/trunk/xmlsite/__init__.py	Mon Jun 12 08:52:57 2006
@@ -0,0 +1 @@
+# Make a Python package

Added: z3/bdbxml/trunk/xmlsite/archived/bdbxmlcontainer.py
==============================================================================
--- (empty file)
+++ z3/bdbxml/trunk/xmlsite/archived/bdbxmlcontainer.py	Mon Jun 12 08:52:57 2006
@@ -0,0 +1,139 @@
+__doc__="""
+Make a Zope 3 container datastructure, ala BTreeContainer, based on Berkeley DB XML.
+
+This module contains no Zope 3 specific code and can run without Zope 3.
+"""
+
+import os
+from bsddb3 import db
+from bsddb3 import *
+from dbxml import *
+#from xmlpage import XMLPage
+
+class BDBXMLContainer:
+    """A class to manage content in Berkeley DB XML databases."""
+
+    def __init__(self, fn):
+        self.mgr = XmlManager()
+        self.container = self.mgr.openContainer(fn)
+    	self.container.addAlias("content")
+        
+        self.__data = self.getAllDocuments()
+
+
+    def xxxgetDatabase(self):
+    	mgr = XmlManager()
+    	container = mgr.openContainer(self.containername, 
+    						db.DB_CREATE)
+    	# Let's add an alias to make XQuery expressions simpler
+    	container.addAlias("content")
+    	
+    	return mgr, container
+
+
+    def get(self, key):
+        return self.__data.get(key)
+        
+    def has_key(self, key):
+        return self.__data.has_key(key)
+        
+    def keys(self):
+        return self.__data.keys()
+        
+    def items(self):
+        return self.__data.items()
+        
+    def values(self):
+        return self.__data.values()
+        
+    def __len__(self):
+        return len(self.__data)
+        
+        
+    #######
+    
+    def getAllIds(self):
+        """Return the id of each document in the database"""
+        mgr, container = self.getDatabase()
+
+        qc = mgr.createQueryContext()
+        querystring = "collection('content')/*"
+        results = mgr.query(querystring, qc)
+        response = []
+        for result in results:
+            doc = result.asDocument()
+            docname = doc.getName()
+    	    response.append(docname)
+
+        
+        container.close()
+        return response
+        
+    def getAllDocuments(self):
+        """Dump of every document in the database, for iterators"""
+
+        qc = self.mgr.createQueryContext()
+        querystring = "collection('content')/*"
+        results = self.mgr.query(querystring, qc)
+        data = {}
+        for result in results:
+            doc = result.asDocument()
+            docname = doc.getName()
+            thispage = result.asString()
+    	    data[docname] = thispage
+
+        return data
+        
+    def xxxgetDocument(self, name):
+        """Retrieve a document with the published name"""
+
+        mgr = XmlManager()
+        uc = mgr.createUpdateContext()
+        container = mgr.openContainer("var/stuff.dbxml")
+        document = container.getDocument(str(name))
+        s = document.getContent()
+        container.close()
+        
+        return s
+
+
+def loadDirectory():
+    dirname = "/Users/paul/zeaprojects/mobility/trunk/src/mobilib/sampledata"
+    containername = "var/stuff.dbxml"
+    
+    
+    environment = db.DBEnv()
+    environment.open(None, DB_CREATE|DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|DB_INIT_TXN, 0)
+    mgr = XmlManager(environment, 0)
+    uc = mgr.createUpdateContext()
+    container = mgr.createContainer(containername, DBXML_TRANSACTIONAL)
+    
+    
+    for fn in os.listdir(dirname):
+        print fn
+        if fn[-4:] != ".xml":
+            continue
+        fullfn = os.path.join(dirname, fn)
+        xtxn = mgr.createTransaction();
+        document = mgr.createDocument()
+        book_content = open(fullfn).read()
+        document.setContent(book_content)
+        document.setName(fn)
+        container.putDocument(xtxn, document, uc)
+        #del document  # holds ref on container
+        xtxn.commit()        
+        del document
+        
+    del container # prevents removal
+    environment.close(0)
+    return
+    
+
+
+def main():
+    #b = BDBXMLContainer("/tmp/z3bdbxml.dbxml")
+    #print b.getAllDocuments()
+    loadDirectory()
+    #b.test()
+
+if __name__ == "__main__": main()

Added: z3/bdbxml/trunk/xmlsite/configure.zcml
==============================================================================
--- (empty file)
+++ z3/bdbxml/trunk/xmlsite/configure.zcml	Mon Jun 12 08:52:57 2006
@@ -0,0 +1,90 @@
+<configure
+    xmlns="http://namespaces.zope.org/zope"
+	xmlns:browser="http://namespaces.zope.org/browser">
+
+  <interface 
+      interface="xmlsite.interfaces.IXMLSite" 
+      type="zope.app.content.interfaces.IContentType"
+      /> 
+
+  <content class="xmlsite.xmldbcontainer.XMLSite">
+    <implements
+        interface="zope.app.container.interfaces.IContentContainer" 
+        />
+    <factory
+        id="xmlsite.xmldbcontainer.XMLSite"
+        description="XML Site" 
+        />
+    <require
+        permission="zope.ManageContent"
+        interface="xmlsite.interfaces.IXMLSite"
+        />
+    <require
+        permission="zope.ManageContent"
+        set_schema="xmlsite.interfaces.IXMLSite"
+        />
+	 <require
+	     permission="zope.Public"
+	     attributes="docname"
+	     />
+  </content>
+
+	<content class="xmlsite.xmlpage.XMLPage">
+	 <require
+	     permission="zope.Public"
+	     attributes="xmlstring"
+	     />
+	</content>
+
+<browser:addform
+    label="Add XML Site"
+    name="AddXMLSite.html"
+    schema="xmlsite.interfaces.IXMLSite"
+    content_factory="xmlsite.xmldbcontainer.XMLSite"
+    fields="description"
+    permission="zope.ManageContent"
+    />
+
+<browser:addMenuItem
+    class="xmlsite.xmldbcontainer.XMLSite"
+    title="XML Site"
+    description="A container that gets content from an XML database"
+    permission="zope.ManageContent"
+    view="AddXMLSite.html" 
+    />
+
+<browser:editform
+    schema="xmlsite.interfaces.IXMLSite"
+    for="xmlsite.interfaces.IXMLSite"
+    label="Change XML Site"
+    name="edit.html"
+    permission="zope.ManageContent"
+    menu="zmi_views" title="Edit" 
+    />
+
+<browser:containerViews
+    for="xmlsite.interfaces.IXMLSite"
+    index="zope.View"
+    contents="zope.View"
+    add="zope.ManageContent"
+    />
+
+<utility
+	factory="xmlsite.xmldbcontainer.BDBXMLConnectionFactory"
+	name="xmlsite.BDBXMLConnection"
+	permission="zope.Public"
+	/>
+
+  <browser:page
+    	for="xmlsite.interfaces.IXMLPage"
+    	name="index.html"
+  		class=".xmlpage.XMLPage"
+  		permission="zope.Public"
+  		/>
+
+  <browser:defaultView
+	  for=".interfaces.IXMLPage"
+	  name="index.html"/>
+
+</configure>
+

Added: z3/bdbxml/trunk/xmlsite/interfaces.py
==============================================================================
--- (empty file)
+++ z3/bdbxml/trunk/xmlsite/interfaces.py	Mon Jun 12 08:52:57 2006
@@ -0,0 +1,26 @@
+
+from zope.interface import Interface
+from zope.schema import Text
+from zope.app.container.interfaces import IContained, IContainer 
+
+class IXMLSite(IContainer):
+    """The XMLSite folder is the base object for our package."""
+    
+    description = Text(
+        title=u"Description",
+        description=u"A detailed description of the content of the xml site.",
+        default=u"",
+        required=False)
+
+class IXMLPage(IContained):
+    """Take an XML document from DBXML and make a component"""
+
+    xmlstring = Text(
+        title=u"xmlstring",
+        description=u"A blog of XML.",
+        default=u"",
+        required=False)
+
+class IBDBXMLConnection(Interface):
+    """Fake stuff"""
+

Added: z3/bdbxml/trunk/xmlsite/xmldbcontainer.py
==============================================================================
--- (empty file)
+++ z3/bdbxml/trunk/xmlsite/xmldbcontainer.py	Mon Jun 12 08:52:57 2006
@@ -0,0 +1,159 @@
+from zope.app.container.contained import Contained
+
+from xmlpage import XMLPage
+from interfaces import IXMLSite, IBDBXMLConnection
+from bsddb3 import *
+from dbxml import *
+
+from zope.component import getUtility
+from zope.component.interfaces import IFactory
+from zope.interface import implementedBy, implements
+
+
+class BDBXMLConnectionFactory:
+    implements(IFactory)
+    title = u"Re-use database connections"
+    description = u"This factory instantiates"
+    
+    def __init__(self):
+        fn = "var/stuff.dbxml"
+        self.mgr = XmlManager()
+        self.container = self.mgr.openContainer(fn)
+    	self.container.addAlias("content")        
+    
+    def __call__(self):
+        return BDBXMLConnection(self.mgr, self.container)
+        
+    def getInterfaces(self):
+        return implementedBy(BDBXMLConnection)
+
+
+class BDBXMLConnection(object):
+    implements(IBDBXMLConnection)
+
+    def __init__(self, mgr, container):
+        self.mgr = mgr
+        self.container = container
+        
+    def listDocumentIDs(self):
+        """Provide a list of the ids in the database"""
+
+        qc = self.mgr.createQueryContext()  
+        querystring = "collection('content')/*"
+        results = self.mgr.query(querystring, qc)
+        response = [unicode(result.asDocument().getName()) for result in results]
+        return response
+        
+    def countDocuments(self):
+        """Provide a count of documents in the database"""
+
+        qc = self.mgr.createQueryContext()  
+        querystring = "collection('content')/*"
+        results = self.mgr.query(querystring, qc)
+        return results.size()
+
+    def getDocument(self, name):
+        """Given an document identifier, get a string representation"""
+
+        document = self.container.getDocument(str(name))
+        xmlstring = unicode(document.getContent())
+        return xmlstring
+
+    def getAllDocuments(self):
+        """Provide a mapping of all documents in the database"""
+
+        qc = self.mgr.createQueryContext()  
+        querystring = "collection('content')/*"
+        results = self.mgr.query(querystring, qc)
+        data = {}
+        for result in results:
+            doc = result.asDocument()
+            docname = doc.getName()
+            thispage = result.asString()
+    	    data[docname] = thispage
+
+        return data
+
+class XMLSite(Contained):
+    """A very simple implementation of an xmlsite using Berkeley DB XML.
+    """
+    
+    implements(IXMLSite)
+
+    description = u''
+    docname = u"Some Document Here"
+
+    def getConnection(self):
+        bdbxml_factory = getUtility(IFactory, u"xmlsite.BDBXMLConnection")
+        connection = bdbxml_factory()
+        return connection
+
+    def keys(self):
+        '''See interface `IReadContainer`'''
+        connection = self.getConnection()
+        return connection.listDocumentIDs()
+
+    def __iter__(self):
+        connection = self.getConnection()
+        return iter(connection.getAllDocuments())
+
+    def __getitem__(self, key):
+        '''See interface `IReadContainer`'''
+
+        connection = self.getConnection()
+        xmlstring = connection.getDocument(key)
+
+        page = XMLPage()
+        page.__parent__ = self
+        page.__name__ = key
+        page.init(xmlstring)
+        return page                
+
+    def get(self, key, default=None):
+        '''See interface `IReadContainer`'''
+        
+        return self.__getitem__(key)
+
+    def values(self):
+        '''See interface `IReadContainer`'''
+        connection = self.getConnection()
+        return connection.getAllDocuments().values()
+
+    def __len__(self):
+        '''See interface `IReadContainer`'''
+        connection = self.getConnection()
+        return connection.countDocuments()
+
+    def items(self):
+        '''See interface `IReadContainer`'''
+        connection = self.getConnection()
+        return connection.getAllDocuments().items()
+
+    def __contains__(self, key):
+        '''See interface `IReadContainer`'''
+        
+        # TODO: Make a bettery XQuery than this brute force
+        connection = self.getConnection()        
+        return connection.getAllDocuments().has_key(key)
+
+    has_key = __contains__
+
+    def __setitem__(self, key, object):
+        '''See interface `IWriteContainer`'''
+        x127
+        setitem(self, self.__data.__setitem__, key, object)
+
+    def __delitem__(self, key):
+        '''See interface `IWriteContainer`'''
+        
+        x128
+        uncontained(self.__data[key], self, key)
+        del self.__data[key]
+
+def main():
+    
+    xmlsite = XMLSite()
+    connection = xmlsite.getConnection()
+    connection.listDocumentIDs()
+
+if __name__ == "__main__": main()
\ No newline at end of file

Added: z3/bdbxml/trunk/xmlsite/xmlpage.py
==============================================================================
--- (empty file)
+++ z3/bdbxml/trunk/xmlsite/xmlpage.py	Mon Jun 12 08:52:57 2006
@@ -0,0 +1,22 @@
+"""
+Simple representation of a page to return to the publisher.
+
+"""
+
+from zope.app.container.contained import Contained
+from interfaces import IXMLPage
+from zope.interface import implements
+
+
+class XMLPage(Contained):
+
+    implements(IXMLPage)
+    xmlstring = u"<h1>Default from the class...</h1>"
+
+    def init(self, xmlstring):
+        self.xmlstring = xmlstring
+        
+    def __call__(self):
+        response = self.context.xmlstring
+        return '<html><body><big>%s</big></body></html>' % response
+


More information about the z3-checkins mailing list