gump-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From leosim...@apache.org
Subject cvs commit: jakarta-gump/python gumpcore.py
Date Fri, 25 Apr 2003 15:59:44 GMT
leosimons    2003/04/25 08:59:44

  Modified:    python   gumpcore.py
  Log:
  docs
  
  Revision  Changes    Path
  1.6       +97 -24    jakarta-gump/python/gumpcore.py
  
  Index: gumpcore.py
  ===================================================================
  RCS file: /home/cvs/jakarta-gump/python/gumpcore.py,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- gumpcore.py	24 Apr 2003 23:20:24 -0000	1.5
  +++ gumpcore.py	25 Apr 2003 15:59:44 -0000	1.6
  @@ -1,7 +1,7 @@
   #!/usr/bin/python
   """
   	Gump core functionality. It contains a sax dispatcher tool, a dependency
  -	walker, and an object model which is built from an xmlfile using
  +	walker, and an object model (GOM) which is built from an xmlfile using
   	the sax dispatcher.
   
       The idea is that a subclass of GumpBase is used for each of the various
  @@ -18,30 +18,42 @@
   from xml.sax.handler import ContentHandler
   from gumputil import *
   from gumpconf import *
  -#from Cheetah.Template import Template
     
  +#########################################################################
  +#	  Global Settings                                                   #
  +#########################################################################
  +
   # output debug messages or not
   debug = False #True
     
  +# right now
  +DATE=time.strftime('%Y%m%d')
  +
   #########################################################################
  -#	  SAX Dispatcher: maintain a stack of active elements		#
  +#	  SAX Dispatcher Mechanism                                          #
   #########################################################################
  -
  +
  +# a stack of active xml elements
   class SAXDispatcher(ContentHandler):
     def __init__(self,file,name,cls):
       self.topOfStack=DocRoot(name,cls)
       self.elementStack=[self.topOfStack]
       parse(file,self)
  -    self.docElement=self.topOfStack.element
  +    self.docElement=self.topOfStack.element
  +    
     def startElement (self, name, attrs):
       if self.topOfStack: self.topOfStack=self.topOfStack.startElement(name,attrs)
  -    self.elementStack.append(self.topOfStack);
  +    self.elementStack.append(self.topOfStack);
  +    
     def characters(self, string):
  -    if self.topOfStack: self.topOfStack.characters(string)
  +    if self.topOfStack: self.topOfStack.characters(string)
  +    
     def endElement (self, name):
       del self.elementStack[-1]
       self.topOfStack=self.elementStack[-1]
   
  +# Run a file through a saxdispatcher, building a GOM in memory from
  +# the xml file. Return the generated GOM
   def load(file):
     if not os.path.exists(file):
       gumpMessage('Error',
  @@ -61,20 +73,32 @@
     return workspace
   
   #########################################################################
  -#		  Base classes for the Gump object model		#
  +#		  Base classes for the Gump object model		                #
  +#                                                                       #
  +# This is actually where most of the logic and complexity is handled,   #
  +# allowing the actual model to be rather simple and compact. All        #
  +# elements of the GOM should extend GumpBase or a subclass of GumpBase. #
   #########################################################################
   
  -DATE=time.strftime('%Y%m%d')
  -
  -# base class for the entire Gump object model.  Attributes become
  -# properties.  Characters become the string value of the element.
  -class GumpBase(object):
  -  def __init__(self,attrs):
  +# Base class for the entire Gump object model.  Attributes become
  +# properties.  Characters become the string value of the element.
  +#
  +# An internal attribute with name '@text' is used for storing all
  +# the characters (as opposed to xml elements and xml attributes).
  +class GumpBase(object):
  +  def __init__(self,attrs):
  +    # parse out '@@DATE@@'
       for (name,value) in attrs.items():
  -      self.__dict__[name]=value.replace('@@DATE@@',DATE)
  +      self.__dict__[name]=value.replace('@@DATE@@',DATE)
  +    
  +    # setup internal character field
       if not '@text' in self.__dict__: self.init()
       self.__dict__['@text']=''
  -  def startElement(self, name, attrs):
  +  
  +  def startElement(self, name, attrs):
  +    # possibility to customize behaviour based on
  +    # type of the element
  +    # TODO: can this difference just go here?
       try:
         attr=self.__getattribute__(name)
         if isinstance(attr,Single): return attr(attrs)
  @@ -82,26 +106,33 @@
       except:
         # print self.__class__, name
         pass
  +  
     def characters(self,string):
       self.__dict__['@text']+=string
  +  
     def __setitem__(self,name,value): 
       self.__dict__[name]=value
  +  
     def __getitem__(self,name): 
       if name in self.__dict__: return self.__dict__[name]
  +  
     def __getattr__(self,name): 
       pass
  +  
     def __str__(self): 
       return self.__dict__['@text'].strip()
  +  
     def init(self):
       pass
   
   # Document root: workspaces and targets of hrefs
  -class DocRoot(GumpBase):
  +class DocRoot(GumpBase):
     def __init__(self,name,cls):
       GumpBase.__init__(self,{})
       self.name=name
       self.cls=cls
       self.element=None
  +  
     def startElement(self, name, attrs):
       if name<>self.name: 
         raise "Incorrect element, expected %s, found %s" % (self.name,name)
  @@ -133,21 +164,25 @@
     def __init__(self,cls=GumpBase):
       self.delegate=None
       self.cls=cls
  +
     def __call__(self,attrs):
       if self.delegate: 
         self.delegate.__dict__.update(dict(attrs))
       else:
         self.delegate=self.cls(attrs)
       return self.delegate
  +
     def __getattr__(self,name):
       if self.delegate: 
         try:
           return self.delegate.__getattribute__(name)
         except:
           return self.delegate[name]
  +
     def __str__(self):
       if self.delegate: return self.delegate.__str__()
       return ''
  +
     def __nonzero__(self):
       return self.delegate
   
  @@ -156,15 +191,20 @@
     def __init__(self,cls=GumpBase):
       list.__init__(self)
       self.cls=cls
  +
     def __call__(self,attrs):
       result=self.cls(attrs)
       self.append(result)
       return result
   
   #########################################################################
  -#			    Gump Object Model				#
  +#			    Gump Object Model				                        #
  +#                                                                       #
  +# All intelligence and functionality is provided in the base classes    #
  +# above, allowing the actual model to be rather simple and compact.     #
   #########################################################################
   
  +# represents a <workspace/> element
   class Workspace(GumpBase):
     def init(self): 
       self.property=Multiple(Property)
  @@ -172,6 +212,8 @@
       self.module=Multiple(Module)
       self.repository=Multiple(Repository)
       self.profile=Multiple(Profile)
  +
  +  # provide default elements when not defined in xml
     def complete(self):
       if not self['banner-image']:
         self['banner-image']="http://jakarta.apache.org/images/jakarta-logo.gif"
  @@ -181,46 +223,58 @@
       if not self.pkgdir: self.pkgdir=self.basedir
       if self.deliver:
         if not self.scratchdir: self.scratchdir=self.basedir+"/scratch"
  -
  +
  +# represents a <profile/> element
   class Profile(Named):
     list={}
  +
     def init(self): 
       self.project=Multiple(Project)
       self.module=Multiple(Module)
       self.repository=Multiple(Repository)
   
  +# represents a <module/> element
   class Module(Named):
     list={}
  +
     def init(self): 
       self.cvs=Single()
       self.url=Single()
       self.description=Single()
       self.redistributable=Single()
       self.project=Multiple(Project)
  +
  +  # provide default elements when not defined in xml
     def complete(self,workspace):
       self.srcdir=os.path.join(str(workspace.basedir),self.srcdir or self.name)
       for project in self.project: 
         if not project.module: project.module=self.name
  -
  +
  +# represents a <repository/> element
   class Repository(Named):
     list={}
  +
     def init(self): 
       self['home-page']=Single()
       self.title=Single()
       self.cvsweb=Single()
       self.root=Single(RepositoryRoot)
       self.redistributable=Single()
  -
  +
  +# represents a <root/> element within a <repository/> element
   class RepositoryRoot(GumpBase):
  +
     def init(self): 
       self.method=Single()
       self.user=Single()
       self.password=Single()
       self.hostname=Single()
       self.path=Single()
  -
  +
  +# represents a <project/> element
   class Project(Named):
     list={}
  +
     def init(self): 
       self.ant=Single(Ant)
       self.script=Single()
  @@ -239,6 +293,8 @@
       self.work=Multiple(Work)
       self.mkdir=Multiple(Mkdir)
       self.redistributable=Single()
  +
  +  # provide default elements when not defined in xml
     def complete(self,workspace):
   
       # compute home directory
  @@ -250,43 +306,60 @@
       # complete properties
       if self.ant: self.ant.complete(self)
   
  +# represents an <ant/> element
   class Ant(GumpBase): 
     def init(self): 
       self.depend=Multiple(Depend)
       self.property=Multiple(Property)
       self.jvmarg=Multiple()
  +
  +  # provide default elements when not defined in xml
     def complete(self,project):
       for property in self.property: property.complete(project)
   
  +# represents a <nag/> element
   class Nag(GumpBase): 
     def init(self): 
       self.regexp=Multiple()
   
  +# represents a <javadoc/> element
   class Javadoc(GumpBase): 
     def init(self): 
       self.description=Multiple()
   
  +# represents a <property/> element
   class Property(GumpBase): 
  +
  +  # provide default elements when not defined in xml
     def complete(self,project):
       if self.reference=='home':
         self.value=Project.list[self.project].home
       if self.reference=='srcdir':
         module=Project.list[self.project].module
         self.value=Module.list[module].srcdir
  +
  +# TODO: set up the below elements with defaults using complete()
   
  +# represents a <depend/> element
   class Depend(GumpBase): pass
  +# represents a <description/> element
   class Description(GumpBase): pass
  +# represents a <home/> element
   class Home(GumpBase): pass
  +# represents a <jar/> element
   class Jar(GumpBase): pass
  +# represents a <junitreport/> element
   class JunitReport(GumpBase): pass
  +# represents a <mkdir/> element
   class Mkdir(GumpBase): pass
  +# represents a <work/> element
   class Work(GumpBase): pass
   
   #########################################################################
   #                     Utility functions                                 #
   #########################################################################
   
  -#sort project dependencies of a project and returns build sequence
  +# sort project dependencies of a project and returns build sequence
   def dependencies(root, #string
            projects, #hashtable
            state = {}, #hashtable
  @@ -355,7 +428,7 @@
           return ret
         
   #########################################################################
  -#			    Demonstration code				#
  +#			    Demonstration code				                        #
   #########################################################################
   
   if __name__=='__main__':
  
  
  

Mime
View raw message