00001
00002
00003
00004 import os.path, sys
00005
00006
00007 register = False
00008 for arg in sys.argv:
00009 if arg.startswith('-register'):
00010 register = True
00011 sarg = arg.split('=')
00012 registry = len(sarg) == 2 and sarg[1] == 'system' and 2 or 1
00013 sys.argv.remove(arg)
00014 break
00015
00016
00017 if len(sys.argv) > 1:
00018 specPath = len(sys.argv) > 1 and os.path.split(sys.argv[1])[1] or 'component.xml'
00019 spath = os.path.split(sys.argv[1])
00020 gpath = os.path.split(sys.argv[0])
00021 genPath = os.path.splitdrive(sys.argv[1])[0] and spath[0] or \
00022 spath[0] and os.path.join(gpath[0], spath[0]) or gpath[0]
00023 else:
00024 specPath = 'component.xml'
00025 genPath = os.path.split(sys.argv[0])[0]
00026
00027
00028 import pythoncom
00029 specDict = {'tooltip' : '', 'iconpath' : '', 'clsid' : str(pythoncom.CreateGuid())}
00030
00031
00032 from xml.dom.minidom import parse, Element
00033 specXML = parse(os.path.join(genPath, specPath))
00034 de = specXML.documentElement
00035 for attr in ('name', 'version', 'type', 'paradigm'):
00036 specDict[attr] = getattr(de.attributes.get(attr), 'value', None)
00037 if specDict[attr] is None:
00038 print "Attribute '%s' is missing from 'component' tag."
00039 if attr == 'type' and specDict[attr] and specDict[attr] not in ('Interpreter', 'Addon'):
00040 print "Unrecognized component type. Must be either 'Interpreter' or 'Addon'."
00041 specDict[attr] = None
00042 if None in specDict.values():
00043 print "Component generation terminated due to specification errors."
00044 else:
00045
00046 if specDict['type'] == 'Interpreter':
00047 for c in de.childNodes:
00048 if isinstance(c, Element):
00049 specDict[c.nodeName] = getattr(c.attributes.get('value'), 'value', None)
00050
00051 if specDict.get('iconpath'):
00052 spath = os.path.split(specDict['iconpath'])
00053 iconBase = os.path.splitdrive(specDict['iconpath'])[0] and spath[0] or \
00054 spath[0] and os.path.join(genPath, spath[0]) or genPath
00055 specDict['iconpath'] = spath[1]
00056 else:
00057 iconBase = ''
00058
00059
00060 from Generator import InterpreterTemplate, AddonTemplate
00061 f = file(os.path.join(genPath, "%s.py" % specDict['name']), "w")
00062 f.write((specDict['type'] == 'Interpreter' and InterpreterTemplate or AddonTemplate) % specDict)
00063 f.close()
00064
00065
00066 if register:
00067 if genPath not in sys.path:
00068 sys.path.insert(0, genPath)
00069 mod = __import__(specDict['name'])
00070 comp = getattr(mod, specDict['name'])
00071 comp.RegisterSelf(registry, specDict['type'] == 'Interpreter' and iconBase or '')