commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

r5125 - in trunk/gnue-common/src/schema/scripter: . processors


From: johannes
Subject: r5125 - in trunk/gnue-common/src/schema/scripter: . processors
Date: Wed, 18 Feb 2004 12:14:11 -0600 (CST)

Author: johannes
Date: 2004-02-18 12:14:10 -0600 (Wed, 18 Feb 2004)
New Revision: 5125

Added:
   trunk/gnue-common/src/schema/scripter/processors/HTML.py
Modified:
   trunk/gnue-common/src/schema/scripter/Scripter.py
   trunk/gnue-common/src/schema/scripter/processors/Base.py
   trunk/gnue-common/src/schema/scripter/processors/__init__.py
Log:
Added HTML processor and slightly changed the base processor.


Modified: trunk/gnue-common/src/schema/scripter/Scripter.py
===================================================================
--- trunk/gnue-common/src/schema/scripter/Scripter.py   2004-02-18 17:33:46 UTC 
(rev 5124)
+++ trunk/gnue-common/src/schema/scripter/Scripter.py   2004-02-18 18:14:10 UTC 
(rev 5125)
@@ -199,23 +199,19 @@
 
     # Instanciate the given processor and iterate over all schema objects
     aModule = self._PROC_PATH % vendor
-    self.processor = dyn_import (aModule).Processor (self.destination)
+    self.processor = dyn_import (aModule).Processor (self.destination, \
+                                                     self.ARGUMENTS [0])
 
     print _("Writing schema to %s ...") % filename
     self.tables = {}
 
-    for line in self.processor.comment ( \
-      _("\nThis file was generated by %s\n") % self.NAME +
-      _("from %s on %s\n\n") % (self.ARGUMENTS [0], strftime ('%c')) +
-      _("Do not edit manually!\n")):
-      self.destination.write (line.encode ('utf-8') + "\n")
-    self.destination.write ("\n")
-
     self.processor.startDump ()
     self.processor.client_encoding (self.OPTIONS ['encoding'])
 
     self.schema.walk (self.__iterate_objects)
 
+    self.processor.finishDump ()
+
     # and finally close the output file
     self.destination.close () 
 

Modified: trunk/gnue-common/src/schema/scripter/processors/Base.py
===================================================================
--- trunk/gnue-common/src/schema/scripter/processors/Base.py    2004-02-18 
17:33:46 UTC (rev 5124)
+++ trunk/gnue-common/src/schema/scripter/processors/Base.py    2004-02-18 
18:14:10 UTC (rev 5125)
@@ -22,6 +22,7 @@
 
 from string import join
 from types import ListType
+from time import strftime
 
 # =============================================================================
 # Exceptions
@@ -75,8 +76,9 @@
   # ---------------------------------------------------------------------------
   # Constructor
   # ---------------------------------------------------------------------------
-  def __init__ (self, destination):
+  def __init__ (self, destination, source = None):
     self.destination = destination
+    self.source      = source
     self.encoding    = "UTF-8"
 
 
@@ -407,6 +409,24 @@
     This method is called by the scripter on start of a dump. Use it to do 
     'per-generation' actions, e.g. set encoding 
     """
+    self._writeText ("\n")
+    self._writeText (self.comment ([ \
+      _("This file was generated by gnue-schema"),
+      _("from %s on %s") % (self.source, strftime ('%c')),
+      "",
+      _("Do NOT edit manually!")]))
+    self._writeText ("\n")
+
+
+
+  # ---------------------------------------------------------------------------
+  # Virtual: called on end of a schema dump
+  # ---------------------------------------------------------------------------
+
+  def finishDump (self):
+    """
+    This method is called by the scripter at the end of a dump.
+    """
     pass
 
 

Added: trunk/gnue-common/src/schema/scripter/processors/HTML.py
===================================================================
--- trunk/gnue-common/src/schema/scripter/processors/HTML.py    2004-02-18 
17:33:46 UTC (rev 5124)
+++ trunk/gnue-common/src/schema/scripter/processors/HTML.py    2004-02-18 
18:14:10 UTC (rev 5125)
@@ -0,0 +1,343 @@
+#
+# This file is part of GNU Enterprise.
+#
+# GNU Enterprise is free software; you can redistribute it
+# and/or modify it under the terms of the GNU General Public
+# License as published by the Free Software Foundation; either
+# version 2, or (at your option) any later version.
+#
+# GNU Enterprise is distributed in the hope that it will be
+# useful, but WITHOUT ANY WARRANTY; without even the implied
+# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+# PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with program; see the file COPYING. If not,
+# write to the Free Software Foundation, Inc., 59 Temple Place
+# - Suite 330, Boston, MA 02111-1307, USA.
+#
+# Copyright 2001-2004 Free Software Foundation
+#
+# $Id$
+
+from gnue.common.schema.scripter.processors.Base import BaseProcessor
+from string import join
+from time import strftime
+
+name        = "HTML"
+description = "HTML 4.01"
+
+# =============================================================================
+# HTML processor for GNUe Schema Definitions
+# =============================================================================
+
+class Processor (BaseProcessor):
+
+  END_COMMAND = ""
+  END_BATCH   = ""
+
+  COMMENT_BEGIN      = "<!-- "
+  COMMENT_END        = "-->"
+  COMMENT_SINGLELINE = False
+
+  # ---------------------------------------------------------------------------
+  # Constructor
+  # ---------------------------------------------------------------------------
+
+  def __init__ (self, destination, source):
+    """
+    Since this class generates a list of tables at the top of the page, we need
+    another private property.
+    """
+    BaseProcessor.__init__ (self, destination, source)
+    self.__tables = []
+
+
+  # ---------------------------------------------------------------------------
+  # Process all fields
+  # ---------------------------------------------------------------------------
+
+  def _processFields (self, tableDef):
+    pro = tableDef.prologue
+    hdr = tableDef.header
+
+    pro.append ("")
+    pro.extend (self.comment (_("Table definition '%s'") % tableDef.name))
+
+    pro.append ('<H2 class="tabledef"><A name="%s">%s</A></H2>' % \
+        (tableDef.name, _("Table definition of '%s'") % tableDef.name))
+
+    hdr.append ('<H3 class="fieldlist">%s</H3>' % _("List of fields"))
+    hdr.append ('<TABLE class="tabledef" width="90%" border="0" ' + \
+                'cellpadding="3" cellspacing="1">')
+    hdr.extend (self._tableHeader ())
+
+    tableDef.footer.append ('</TABLE>')
+
+    BaseProcessor._processFields (self, tableDef)
+
+
+  # ---------------------------------------------------------------------------
+  # Translate all fields into a single row
+  # ---------------------------------------------------------------------------
+
+  def _processField (self, tableDef, gsField, isLast):
+    tableDef.body.extend (self._fieldRow (gsField))
+
+
+  # ---------------------------------------------------------------------------
+  # Process a primary key definition
+  # ---------------------------------------------------------------------------
+
+  def _processPrimaryKey (self, tableDef):
+    pkDef = tableDef.primaryKey
+    flist = join ([pkf.name for pkf in pkDef.fields], ", ")
+
+    epi = tableDef.epilogue
+
+    epi.append ("")
+    epi.extend (self.comment (_("Primary key '%s'") % pkDef.name))
+
+    epi.append ('<H3 class="primarykey">%s</H3>' % \
+      _("Primary Key: %s") % pkDef.name)
+
+    epi.append ('<UL>')
+    epi.extend (["  <LI>%s</LI>" % pkf.name for pkf in pkDef.fields])
+    epi.append ('</UL>')
+
+
+      
+  # ---------------------------------------------------------------------------
+  # Process an IndexDefinition
+  # ---------------------------------------------------------------------------
+
+  def _processIndex (self, tableDef, indexDef):
+    if indexDef.unique:
+      uniq = _("Unique Index")
+    else:
+      uniq = _("Index")
+
+    epi = tableDef.epilogue
+
+    epi.append ("")
+    epi.extend (self.comment (_("Index '%s'") % indexDef.name))
+    epi.append ('<H3 class="index">%s: %s</H3>' % (uniq, indexDef.name))
+
+    epi.append ('<UL>')
+    epi.extend (["  <LI>%s</LI>" % ixf.name for ixf in indexDef.fields])
+    epi.append ('</UL>')
+
+
+
+
+  # ---------------------------------------------------------------------------
+  # Create a sequence with a header row for field tables
+  # ---------------------------------------------------------------------------
+
+  def _tableHeader (self):
+    """
+    Create a table header for field rows
+    """
+    res = []
+
+    res.append ('<TR>')
+    res.append ('  <TH class="fields">%s</TH>' % "Field")
+    res.append ('  <TH class="fields">%s</TH>' % "Type")
+    res.append ('  <TH class="fields">%s</TH>' % "Nullable")
+    res.append ('  <TH class="fields">%s</TH>' % "Default")
+    res.append ('</TR>')
+
+    return res
+
+
+  # ---------------------------------------------------------------------------
+  # Create a sequence for a table row, describing a single field
+  # ---------------------------------------------------------------------------
+
+  def _fieldRow (self, gsField):
+    """
+    Create a HTML sequence describing the given gsField instance.
+    """
+    res = []
+
+    if gsField.nullable:
+      null = _("yes")
+    else:
+      null = _("no")
+
+    if gsField.defaultwith in ["serial", "timestamp"]:
+      default = gsField.defaultwith
+
+    elif hasattr (gsField, "default"):
+      default = gsField.default
+
+    else:
+      default = "&nbsp;"
+
+    res.append ('<TR>')
+    res.append ('  <TD class="fields">%s</TH>' % gsField.name)
+    res.append ('  <TD class="fields">%s</TH>' % \
+                self._translateType (gsField))
+    res.append ('  <TD class="fields">%s</TH>' % null)
+    res.append ('  <TD class="fields">%s</TH>' % default)
+    res.append ('</TR>')
+
+    return res
+
+
+  # ---------------------------------------------------------------------------
+  # start a new file
+  # ---------------------------------------------------------------------------
+
+  def startDump (self):
+    """
+    On start of a dump just create the HTML- and HEAD-tags
+    """
+    self._writeText (['<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 ' + \
+       'Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd";>',
+      '<HTML>', 
+      ' <HEAD>', 
+      '  <TITLE>GNUe Schema Definition: %s</TITLE>' % self.source,
+      '  <META name="author" content="gnue-schema">'])
+
+
+  # ---------------------------------------------------------------------------
+  # Set the client encoding in the html head
+  # ---------------------------------------------------------------------------
+
+  def client_encoding (self, encoding):
+    """
+    Set the client encoding in the html head
+    """
+    self.encoding = encoding
+    self._writeText ([ \
+      '  <META HTTP-EQUIV="content-type" content="text/html; charset=%s">' % \
+      self.encoding])
+
+
+  # ---------------------------------------------------------------------------
+  # Starting a schema dump means finishing the head and starting the body
+  # ---------------------------------------------------------------------------
+
+  def startSchema (self):
+    """
+    Close the HEAD section and start with the BODY.
+    """
+    self._writeText (['<style type="text/css">',
+      '  BODY { font-family: helvetica, arial, sans-serif; }',
+      '  TH { text-align: left; background: #AAAAAA; }',
+      '  TD { text-align: left; background: #CCCCCC; }',
+      '</style>'])
+
+    self._writeText (['</HEAD>', '<BODY>'])
+
+
+  # ---------------------------------------------------------------------------
+  # stop the dump
+  # ---------------------------------------------------------------------------
+
+  def finishDump (self):
+    """
+    Create the table of contents section and write all definitions to the
+    destination.
+    """
+    self._writeText (self.comment ('List of tables'))
+    self._writeText ('<H1 class="toc">%s</H1>' % \
+      _("Table definitions in '%s'") % self.source)
+
+    self._writeText ('<UL>\n')
+
+    for table in self.__tables:
+      self._writeText ('  <LI><A HREF="#%s">%s</A></LI>\n' % \
+        (table.name, table.name))
+
+    self._writeText ('</UL>\n')
+
+    for table in self.__tables:
+      table.writeDefinition (self.destination, self.encoding)
+      self._writeText ('<HR>')
+
+    self._writeText (['<P>%s %s</P>' % (_("Generated on"), strftime ('%c'))])
+    self._writeText (['</BODY>'])
+
+
+  # ---------------------------------------------------------------------------
+  # write a table definition
+  # ---------------------------------------------------------------------------
+
+  def writeTable (self, tableDef):
+    """
+    This function call all _process* () functions on the table definition given
+    and adds the processed definition to the internal list of tables.
+    """
+    self._processFields (tableDef)
+
+    if tableDef.primaryKey is not None:
+      self._processPrimaryKey (tableDef)
+
+    if len (tableDef.indices.keys ()):
+      self._processIndices (tableDef)
+
+    if len (tableDef.constraints.keys ()):
+      self._processConstraints (tableDef)
+
+    if len (self.END_BATCH):
+      tableDef.epilogue.append (self.END_BATCH)
+
+    # Note: we won't write the table definition right now, but append it to the
+    # list of tables. We would let stopDump () do the write out instead.
+    self.__tables.append (tableDef)
+
+
+  # ---------------------------------------------------------------------------
+  # Datatype translation methods
+  # ---------------------------------------------------------------------------
+
+  def string (self, gsField):
+    if hasattr (gsField, "length"):
+      return "string (%s)" % gsField.length
+    else:
+      return "string"
+
+  # ---------------------------------------------------------------------------
+  # Datatype translation methods
+  # ---------------------------------------------------------------------------
+
+  def date (self, gsField):
+    return "date"
+
+  # ---------------------------------------------------------------------------
+  # Datatype translation methods
+  # ---------------------------------------------------------------------------
+
+  def time (self, gsField):
+    return "time"
+
+  # ---------------------------------------------------------------------------
+  # Datatype translation methods
+  # ---------------------------------------------------------------------------
+
+  def datetime (self, gsField):
+    return "datetime"
+
+  # ---------------------------------------------------------------------------
+  # Datatype translation methods
+  # ---------------------------------------------------------------------------
+
+  def boolean (self, gsField):
+    return "boolean"
+
+  # ---------------------------------------------------------------------------
+  # Datatype translation methods
+  # ---------------------------------------------------------------------------
+
+  def key (self, gsField):
+    return "key"
+
+  # ---------------------------------------------------------------------------
+  # Datatype translation methods
+  # ---------------------------------------------------------------------------
+
+  def number (self, gsField):
+    return "number (%s, %s)" % (gsField.length + gsField.precision, \
+                                gsField.precision)


Property changes on: trunk/gnue-common/src/schema/scripter/processors/HTML.py
___________________________________________________________________
Name: svn:keywords
   + Id

Modified: trunk/gnue-common/src/schema/scripter/processors/__init__.py
===================================================================
--- trunk/gnue-common/src/schema/scripter/processors/__init__.py        
2004-02-18 17:33:46 UTC (rev 5124)
+++ trunk/gnue-common/src/schema/scripter/processors/__init__.py        
2004-02-18 18:14:10 UTC (rev 5125)
@@ -1 +1 @@
-vendors = ['postgresql','oracle','interbase','mysql','mssql']
+vendors = ['postgresql','oracle','interbase','mysql','mssql', 'HTML']





reply via email to

[Prev in Thread] Current Thread [Next in Thread]