[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/schemagen - modulegen.py:1.1 interfaces.py:1.2

Martijn Faassen m.faassen@vet.uu.nl
Wed, 11 Dec 2002 14:07:23 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/schemagen
In directory cvs.zope.org:/tmp/cvs-serv11175

Modified Files:
	interfaces.py 
Added Files:
	modulegen.py 
Log Message:
Added code that can generate module source from field information
automatically. The generated source contains an interface based on the 
provided fields, as well as a class implementing that schema using
FieldProperty.


=== Added File Zope3/lib/python/Zope/App/schemagen/modulegen.py ===
from typereg import fieldRegistry

def generateModuleSource(schema_name, fields, class_name,
                         schema_history=None):
    if schema_history is None:
        schema_version = 0
    else:
        # XXX get it from schema history
        pass

    import_list = []
    field_text_list = []
    property_text_list = []
    for field_name, field in fields:
        r = fieldRegistry.represent(field)
        field_text_list.append('    %s = %s' % (field_name, r.text))
        property_text_list.append(
            "    %s = "
            "FieldProperty(%s['%s'])" % (
            field_name, schema_name, field_name))
        for import_entry in r.importList:
            import_list.append("from %s import %s" % import_entry)
    
    import_text = '\n'.join(import_list)
    field_text = '\n'.join(field_text_list)
    property_text = '\n'.join(property_text_list)
         
    text = '''\
from Interface import Interface
from Persistence import Persistent
from Zope.Schema.FieldProperty import FieldProperty

# field imports
%(import_text)s

class %(schema_name)s(Interface):
    """Autogenerated schema."""
%(field_text)s
    
class %(class_name)s(Persistent):
    """Autogenerated class for %(schema_name)s."""
    __implements__ = %(schema_name)s
    __schema_version__ = %(schema_version)s

%(property_text)s
''' % vars()
    
    return text



=== Zope3/lib/python/Zope/App/schemagen/interfaces.py 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/App/schemagen/interfaces.py:1.1	Wed Dec 11 11:11:14 2002
+++ Zope3/lib/python/Zope/App/schemagen/interfaces.py	Wed Dec 11 14:07:22 2002
@@ -21,8 +21,26 @@
 from Interface import Interface
 from Interface.Attribute import Attribute
 
+class IModuleGenerator:
+    def generateModuleSource(schema_name, fields, class_name,
+                             schema_history=None):
+        """Generate module source containing an interface and class.
+
+        schema_name the name of the schema to generate, fields is the
+        fields in the schema (in order), class_name the name of the
+        class that implements the schema.  schema_history gives the
+        history of the schema up till now. This will be used
+        eventually in order to create class transformation code so its
+        contents can be updated to a newer version of the schema.
+        """
+
+    
 class ITypeRepresentation(Interface):
+    """Provide a textual representation of object
 
+    The goal is to have a textual representation (text) that can be
+    'eval'ed again so it becomes an object.
+    """
     importList = Attribute('List of two-string tuples for use in '
                            'from X import Y')