cwxmlgen.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Mac » Lib » mkcwproject » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Mac » Lib » mkcwproject » cwxmlgen.py
# First attempt at automatically generating CodeWarior projects
import os
import MacOS
import string

Error="gencwproject.Error"
#
# These templates are executed in-order.
# 
TEMPLATELIST= [
  ("tmp_allsources", "file", "template-allsources.xml", "sources"),
  ("tmp_linkorder", "file", "template-linkorder.xml", "sources"),
  ("tmp_grouplist", "file", "template-grouplist.xml", "sources"),
  ("tmp_alllibraries", "file", "template-alllibraries.xml", "libraries"),
  ("tmp_linkorderlib", "file", "template-linkorderlib.xml", "libraries"),
  ("tmp_grouplistlib", "file", "template-grouplistlib.xml", "libraries"),
  ("tmp_extrasearchdirs", "file", "template-searchdirs.xml", "extrasearchdirs"),
  ("tmp_projectxmldata", "file", "template.prj.xml", None)
]

class ProjectBuilder:
  def __init__(self, dict, templatelist=TEMPLATELIST, templatename=None):
    if templatename == None:
      if hasattr(MacOS, 'runtimemodel'):
        templatename = 'template-%s'%MacOS.runtimemodel
      else:
        templatename = 'template'
    if os.sep in templatename:
      templatedir = templatename
    else:
      try:
        packagedir = os.path.split(__file__)[0]
      except NameError:
        packagedir = os.curdir
      templatedir = os.path.join(packagedir, templatename)
    if not os.path.exists(templatedir):
      raise Error, "Cannot find templatedir %s"%templatedir
    self.dict = dict
    if not dict.has_key('prefixname'):
      dict['prefixname'] = 'mwerks_plugin_config.h'
    self.templatelist = templatelist
    self.templatedir = templatedir
    
  def generate(self):
    for tmpl in self.templatelist:
      self._generate_one_template(tmpl)
    
  def _generate_one_template(self, tmpl):
    resultname, datasource, dataname, key = tmpl
    result = ''
    if key:
      # This is a multi-element rule. Run for every item in dict[key]
      if self.dict.has_key(key):
        keyvalues = self.dict[key]
        try:
          if not type(keyvalues) in (type(()), type([])):
            raise Error, "List or tuple expected for %s"%key
          for curkeyvalue in keyvalues:
            if string.lower(curkeyvalue[:10]) == '{compiler}':
              curkeyvalue = curkeyvalue[10:]
              self.dict['pathtype'] = 'CodeWarrior'
            elif string.lower(curkeyvalue[:9]) == '{project}':
              curkeyvalue = curkeyvalue[9:]
              self.dict['pathtype'] = 'Project'
            elif curkeyvalue[0] == '{':
              raise Error, "Unknown {} escape in %s"%curkeyvalue
            elif os.path.isabs(curkeyvalue):
              self.dict['pathtype'] = 'Absolute'
            else:
              self.dict['pathtype'] = 'Project'
            if curkeyvalue[-2:] == ':*':
              curkeyvalue = curkeyvalue[:-2]
              self.dict['recursive'] = 'true'
            else:
              self.dict['recursive'] = 'false'
            self.dict[key] = curkeyvalue
            curkeyvalueresult = self._generate_one_value(datasource, dataname)
            result = result + curkeyvalueresult
        finally:
          # Restore the list
          self.dict[key] = keyvalues
          self.dict['pathtype'] = None
          del self.dict['pathtype']
          self.dict['recursive'] = None
          del self.dict['recursive']
    else:
      # Not a multi-element rule. Simply generate
      result = self._generate_one_value(datasource, dataname)
    # And store the result
    self.dict[resultname] = result
    
  def _generate_one_value(self, datasource, dataname):
    if datasource == 'file':
      filepath = os.path.join(self.templatedir, dataname)
      fp = open(filepath, "r")
      format = fp.read()
    elif datasource == 'string':
      format = dataname
    else:
      raise Error, 'Datasource should be file or string, not %s'%datasource
    return format % self.dict
    
def _test():
  dict = {
    "mac_projectxmlname" : "controlstrip.prj.xml",  # The XML filename (full path)
    "mac_exportname" : "controlstrip.prj.exp",  # Export file (relative to project)
    "mac_outputdir" : ":",  # The directory where the DLL is put (relative to project)
    "mac_dllname" : "controlstrip.ppc.slb",  # The DLL filename (within outputdir)
    "mac_targetname" : "controlstrip.ppc",  # The targetname within the project
    "sysprefix" : sys.prefix,  # Where the Python sources live
    "mac_sysprefixtype" : "Absolute",  # Type of previous pathname
    "sources" : ["controlstripmodule.c"],
    "extrasearchdirs": [],  # -I and -L, in unix terms
  }
  pb = ProjectBuilder(dict)
  pb.generate()
  fp = open(dict["mac_projectxmlname"], "w")
  fp.write(dict["tmp_projectxmldata"])
  
if __name__ == '__main__':
  _test()
    
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.