Fixed event inheritance code (which didn't work at all).

Do two passes over the suites so we can figure out dependencies for enums and such. The manual method was getting too cumbersome for Finder suites.
This commit is contained in:
Jack Jansen 2000-08-20 19:30:56 +00:00
parent f58bbbe139
commit 12b2b76608
1 changed files with 239 additions and 117 deletions

View File

@ -247,10 +247,18 @@ def compileaete(aete, resinfo, fname):
basepackage = None basepackage = None
macfs.SetFolder(pathname) macfs.SetFolder(pathname)
suitelist = [] suitelist = []
allprecompinfo = []
allsuites = []
for suite in suites: for suite in suites:
code, modname = compilesuite(suite, major, minor, language, script, fname, basepackage) code, suite, fss, modname, precompinfo = precompilesuite(suite, basepackage)
if modname: if not code:
continue
allprecompinfo = allprecompinfo + precompinfo
suiteinfo = suite, fss, modname
suitelist.append((code, modname)) suitelist.append((code, modname))
allsuites.append(suiteinfo)
for suiteinfo in allsuites:
compilesuite(suiteinfo, major, minor, language, script, fname, basepackage, allprecompinfo)
fss, ok = macfs.StandardPutFile('Package module', '__init__.py') fss, ok = macfs.StandardPutFile('Package module', '__init__.py')
if not ok: if not ok:
return return
@ -269,20 +277,22 @@ def compileaete(aete, resinfo, fname):
fp.write("}\n\n") fp.write("}\n\n")
fp.write("\n\n_code_to_fullname = {\n") fp.write("\n\n_code_to_fullname = {\n")
for code, modname in suitelist: for code, modname in suitelist:
fp.write("\t'%s' : '%s.%s',\n"%(code, packagename, modname)) fp.write("\t'%s' : ('%s.%s', '%s'),\n"%(code, packagename, modname, modname))
fp.write("}\n\n") fp.write("}\n\n")
for code, modname in suitelist: for code, modname in suitelist:
fp.write("from %s import *\n"%modname) fp.write("from %s import *\n"%modname)
if suitelist: if suitelist:
fp.write("\n\nclass %s(%s_Events"%(packagename, suitelist[0][1])) fp.write("\n\nclass %s(%s_Events"%(packagename, suitelist[0][1]))
for code, modname in suitelist[1:]: for code, modname in suitelist[1:]:
fp.write(",\n\t%s_Events"%modname) fp.write(",\n\t\t%s_Events"%modname)
fp.write(",\n\taetools.TalkTo):\n") fp.write(",\n\t\taetools.TalkTo):\n")
fp.write("\t_signature = '%s'\n\n"%creatorsignature) fp.write("\t_signature = '%s'\n\n"%creatorsignature)
fp.close() fp.close()
def compilesuite(suite, major, minor, language, script, fname, basepackage=None): def precompilesuite(suite, basepackage=None):
"""Generate code for a single suite""" """Parse a single suite without generating the output. This step is needed
so we can resolve recursive references by suites to enums/comps/etc declared
in other suites"""
[name, desc, code, level, version, events, classes, comps, enums] = suite [name, desc, code, level, version, events, classes, comps, enums] = suite
modname = identify(name) modname = identify(name)
@ -290,9 +300,47 @@ def compilesuite(suite, major, minor, language, script, fname, basepackage=None)
modname = modname[:27] modname = modname[:27]
fss, ok = macfs.StandardPutFile('Python output file', modname+'.py') fss, ok = macfs.StandardPutFile('Python output file', modname+'.py')
if not ok: if not ok:
return None, None return None, None, None, None, None
pathname = fss.as_pathname() pathname = fss.as_pathname()
modname = os.path.splitext(os.path.split(pathname)[1])[0] modname = os.path.splitext(os.path.split(pathname)[1])[0]
if basepackage and basepackage._code_to_module.has_key(code):
# We are an extension of a baseclass (usually an application extending
# Standard_Suite or so). Import everything from our base module
basemodule = basepackage._code_to_module[code]
else:
# We are not an extension.
basemodule = None
enumsneeded = {}
for event in events:
findenumsinevent(event, enumsneeded)
objc = ObjectCompiler(None, basemodule)
for cls in classes:
objc.compileclass(cls)
for cls in classes:
objc.fillclasspropsandelems(cls)
for comp in comps:
objc.compilecomparison(comp)
for enum in enums:
objc.compileenumeration(enum)
for enum in enumsneeded.keys():
objc.checkforenum(enum)
objc.dumpindex()
precompinfo = objc.getprecompinfo(modname)
return code, suite, fss, modname, precompinfo
def compilesuite((suite, fss, modname), major, minor, language, script, fname, basepackage, precompinfo):
"""Generate code for a single suite"""
[name, desc, code, level, version, events, classes, comps, enums] = suite
pathname = fss.as_pathname()
fp = open(fss.as_pathname(), 'w') fp = open(fss.as_pathname(), 'w')
fss.SetCreatorType('Pyth', 'TEXT') fss.SetCreatorType('Pyth', 'TEXT')
@ -309,7 +357,7 @@ def compilesuite(suite, major, minor, language, script, fname, basepackage=None)
if basepackage and basepackage._code_to_module.has_key(code): if basepackage and basepackage._code_to_module.has_key(code):
# We are an extension of a baseclass (usually an application extending # We are an extension of a baseclass (usually an application extending
# Standard_Suite or so). Import everything from our base module # Standard_Suite or so). Import everything from our base module
fp.write('from %s import *\n'%basepackage._code_to_fullname[code]) fp.write('from %s import *\n'%basepackage._code_to_fullname[code][0])
basemodule = basepackage._code_to_module[code] basemodule = basepackage._code_to_module[code]
else: else:
# We are not an extension. # We are not an extension.
@ -323,7 +371,7 @@ def compilesuite(suite, major, minor, language, script, fname, basepackage=None)
else: else:
fp.write("\tpass\n\n") fp.write("\tpass\n\n")
objc = ObjectCompiler(fp, basemodule) objc = ObjectCompiler(fp, basemodule, precompinfo)
for cls in classes: for cls in classes:
objc.compileclass(cls) objc.compileclass(cls)
for cls in classes: for cls in classes:
@ -343,8 +391,10 @@ def compilesuite(suite, major, minor, language, script, fname, basepackage=None)
def compileclassheader(fp, name, module=None): def compileclassheader(fp, name, module=None):
"""Generate class boilerplate""" """Generate class boilerplate"""
classname = '%s_Events'%name classname = '%s_Events'%name
if module and hasattr(module, classname): if module:
fp.write("class %s(%s):\n\n"%(classname, classname)) modshortname = string.split(module.__name__, '.')[-1]
baseclassname = '%s_Events'%modshortname
fp.write("class %s(%s):\n\n"%(classname, baseclassname))
else: else:
fp.write("class %s:\n\n"%classname) fp.write("class %s:\n\n"%classname)
@ -457,69 +507,128 @@ def compileargument(arg):
[name, keyword, what] = arg [name, keyword, what] = arg
print "# %s (%s)" % (name, `keyword`), compiledata(what) print "# %s (%s)" % (name, `keyword`), compiledata(what)
def findenumsinevent(event, enumsneeded):
"""Find all enums for a single event"""
[name, desc, code, subcode, returns, accepts, arguments] = event
for a in arguments:
if is_enum(a[2]):
ename = a[2][0]
if ename <> '****':
enumsneeded[ename] = 1
#
# This class stores the code<->name translations for a single module. It is used
# to keep the information while we're compiling the module, but we also keep these objects
# around so if one suite refers to, say, an enum in another suite we know where to
# find it. Finally, if we really can't find a code, the user can add modules by
# hand.
#
class CodeNameMapper:
def __init__(self):
self.code2name = {
"property" : {},
"class" : {},
"enum" : {},
"comparison" : {},
}
self.name2code = {
"property" : {},
"class" : {},
"enum" : {},
"comparison" : {},
}
self.modulename = None
self.star_imported = 0
def addnamecode(self, type, name, code):
self.name2code[type][name] = code
if not self.code2name[type].has_key(code):
self.code2name[type][code] = name
def hasname(self, type, name):
return self.name2code[type].has_key(name)
def hascode(self, type, code):
return self.code2name[type].has_key(code)
def findcodename(self, type, code):
if not self.hascode(type, code):
return None, None, None
name = self.code2name[type][code]
if self.modulename and not self.star_imported:
qualname = '%s.%s'%(self.modulename, name)
else:
qualname = name
return name, qualname, self.modulename
def getall(self, type):
return self.code2name[type].items()
def addmodule(self, module, name, star_imported):
self.modulename = name
self.star_imported = star_imported
for code, name in module._propdeclarations.items():
self.addnamecode('property', name, code)
for code, name in module._classdeclarations.items():
self.addnamecode('class', name, code)
for code in module._enumdeclarations.keys():
self.addnamecode('enum', '_Enum_'+identify(code), code)
for code, name in module._compdeclarations.items():
self.addnamecode('comparison', name, code)
def prepareforexport(self, name=None):
if not self.modulename:
self.modulename = name
return self
class ObjectCompiler: class ObjectCompiler:
def __init__(self, fp, basesuite=None): def __init__(self, fp, basesuite=None, othernamemappers=None):
self.fp = fp self.fp = fp
self.propnames = {}
self.classnames = {}
self.propcodes = {}
self.classcodes = {}
self.compcodes = {}
self.enumcodes = {}
self.othersuites = []
self.basesuite = basesuite self.basesuite = basesuite
self.namemappers = [CodeNameMapper()]
if othernamemappers:
self.othernamemappers = othernamemappers[:]
else:
self.othernamemappers = []
if basesuite:
basemapper = CodeNameMapper()
basemapper.addmodule(basesuite, '', 1)
self.namemappers.append(basemapper)
def getprecompinfo(self, modname):
list = []
for mapper in self.namemappers:
emapper = mapper.prepareforexport(modname)
if emapper:
list.append(emapper)
return list
def findcodename(self, type, code): def findcodename(self, type, code):
while 1: while 1:
if type == 'property': # First try: check whether we already know about this code.
# First we check whether we ourselves have defined it for mapper in self.namemappers:
if self.propcodes.has_key(code): if mapper.hascode(type, code):
return self.propcodes[code], self.propcodes[code], None return mapper.findcodename(type, code)
# Next we check whether our base suite module has defined it # Second try: maybe one of the other modules knows about it.
if self.basesuite and self.basesuite._propdeclarations.has_key(code): for mapper in self.othernamemappers:
name = self.basesuite._propdeclarations[code].__name__ if mapper.hascode(type, code):
return name, name, None self.othernamemappers.remove(mapper)
# Finally we test whether one of the other suites we know about has defined self.namemappers.append(mapper)
# it. if self.fp:
for s in self.othersuites: self.fp.write("import %s\n"%mapper.modulename)
if s._propdeclarations.has_key(code): break
name = s._propdeclarations[code].__name__ else:
return name, '%s.%s' % (s.__name__, name), s.__name__
if type == 'class':
if self.classcodes.has_key(code):
return self.classcodes[code], self.classcodes[code], None
if self.basesuite and self.basesuite._classdeclarations.has_key(code):
name = self.basesuite._classdeclarations[code].__name__
return name, name, None
for s in self.othersuites:
if s._classdeclarations.has_key(code):
name = s._classdeclarations[code].__name__
return name, '%s.%s' % (s.__name__, name), s.__name__
if type == 'enum':
if self.enumcodes.has_key(code):
return self.enumcodes[code], self.enumcodes[code], None
if self.basesuite and self.basesuite._enumdeclarations.has_key(code):
name = '_Enum_' + identify(code)
return name, name, None
for s in self.othersuites:
if s._enumdeclarations.has_key(code):
name = '_Enum_' + identify(code)
return name, '%s.%s' % (s.__name__, name), s.__name__
if type == 'comparison':
if self.compcodes.has_key(code):
return self.compcodes[code], self.compcodes[code], None
if self.basesuite and self.basesuite._compdeclarations.has_key(code):
name = self.basesuite._compdeclarations[code].__name__
return name, name, None
for s in self.othersuites:
if s._compdeclarations.has_key(code):
name = s._compdeclarations[code].__name__
return name, '%s.%s' % (s.__name__, name), s.__name__
# If all this has failed we ask the user for a guess on where it could # If all this has failed we ask the user for a guess on where it could
# be and retry. # be and retry.
if self.fp:
m = self.askdefinitionmodule(type, code) m = self.askdefinitionmodule(type, code)
else:
m = None
if not m: return None, None, None if not m: return None, None, None
self.othersuites.append(m) mapper = CodeNameMapper()
mapper.addmodule(m, m.__name__, 0)
self.namemappers.append(mapper)
def askdefinitionmodule(self, type, code): def askdefinitionmodule(self, type, code):
fss, ok = macfs.PromptGetFile('Where is %s %s declared?'%(type, code)) fss, ok = macfs.PromptGetFile('Where is %s %s declared?'%(type, code))
@ -535,16 +644,17 @@ class ObjectCompiler:
def compileclass(self, cls): def compileclass(self, cls):
[name, code, desc, properties, elements] = cls [name, code, desc, properties, elements] = cls
pname = identify(name) pname = identify(name)
if self.classcodes.has_key(code): if self.namemappers[0].hascode('class', code):
# plural forms and such # plural forms and such
self.fp.write("\n%s = %s\n"%(pname, self.classcodes[code])) othername, dummy, dummy = self.namemappers[0].findcodename('class', code)
self.classnames[pname] = code if self.fp:
self.fp.write("\n%s = %s\n"%(pname, othername))
else: else:
if self.fp:
self.fp.write('\nclass %s(aetools.ComponentItem):\n' % pname) self.fp.write('\nclass %s(aetools.ComponentItem):\n' % pname)
self.fp.write('\t"""%s - %s """\n' % (name, desc)) self.fp.write('\t"""%s - %s """\n' % (name, desc))
self.fp.write('\twant = %s\n' % `code`) self.fp.write('\twant = %s\n' % `code`)
self.classnames[pname] = code self.namemappers[0].addnamecode('class', pname, code)
self.classcodes[code] = pname
for prop in properties: for prop in properties:
self.compileproperty(prop) self.compileproperty(prop)
for elem in elements: for elem in elements:
@ -556,24 +666,28 @@ class ObjectCompiler:
# Something silly with plurals. Skip it. # Something silly with plurals. Skip it.
return return
pname = identify(name) pname = identify(name)
if self.propcodes.has_key(code): if self.namemappers[0].hascode('property', code):
# XXXX Why don't we handle these the same as classes??
if self.fp:
self.fp.write("# repeated property %s %s\n"%(pname, what[1])) self.fp.write("# repeated property %s %s\n"%(pname, what[1]))
else: else:
if self.fp:
self.fp.write("class %s(aetools.NProperty):\n" % pname) self.fp.write("class %s(aetools.NProperty):\n" % pname)
self.fp.write('\t"""%s - %s """\n' % (name, what[1])) self.fp.write('\t"""%s - %s """\n' % (name, what[1]))
self.fp.write("\twhich = %s\n" % `code`) self.fp.write("\twhich = %s\n" % `code`)
self.fp.write("\twant = %s\n" % `what[0]`) self.fp.write("\twant = %s\n" % `what[0]`)
self.propnames[pname] = code self.namemappers[0].addnamecode('property', pname, code)
self.propcodes[code] = pname
def compileelement(self, elem): def compileelement(self, elem):
[code, keyform] = elem [code, keyform] = elem
if self.fp:
self.fp.write("# element %s as %s\n" % (`code`, keyform)) self.fp.write("# element %s as %s\n" % (`code`, keyform))
def fillclasspropsandelems(self, cls): def fillclasspropsandelems(self, cls):
[name, code, desc, properties, elements] = cls [name, code, desc, properties, elements] = cls
cname = identify(name) cname = identify(name)
if self.classcodes[code] != cname: if self.namemappers[0].hascode('class', code) and \
self.namemappers[0].findcodename('class', code)[0] != cname:
# This is an other name (plural or so) for something else. Skip. # This is an other name (plural or so) for something else. Skip.
return return
plist = [] plist = []
@ -590,10 +704,12 @@ class ObjectCompiler:
continue continue
name, ename, module = self.findcodename('class', ecode) name, ename, module = self.findcodename('class', ecode)
if not name: if not name:
if self.fp:
self.fp.write("# XXXX %s element %s not found!!\n"%(cname, `ecode`)) self.fp.write("# XXXX %s element %s not found!!\n"%(cname, `ecode`))
else: else:
elist.append((name, ename)) elist.append((name, ename))
if self.fp:
self.fp.write("%s._propdict = {\n"%cname) self.fp.write("%s._propdict = {\n"%cname)
for n in plist: for n in plist:
self.fp.write("\t'%s' : %s,\n"%(n, n)) self.fp.write("\t'%s' : %s,\n"%(n, n))
@ -606,18 +722,20 @@ class ObjectCompiler:
def compilecomparison(self, comp): def compilecomparison(self, comp):
[name, code, comment] = comp [name, code, comment] = comp
iname = identify(name) iname = identify(name)
self.compcodes[code] = iname self.namemappers[0].addnamecode('comparison', iname, code)
if self.fp:
self.fp.write("class %s(aetools.NComparison):\n" % iname) self.fp.write("class %s(aetools.NComparison):\n" % iname)
self.fp.write('\t"""%s - %s """\n' % (name, comment)) self.fp.write('\t"""%s - %s """\n' % (name, comment))
def compileenumeration(self, enum): def compileenumeration(self, enum):
[code, items] = enum [code, items] = enum
name = "_Enum_%s" % identify(code) name = "_Enum_%s" % identify(code)
if self.fp:
self.fp.write("%s = {\n" % name) self.fp.write("%s = {\n" % name)
for item in items: for item in items:
self.compileenumerator(item) self.compileenumerator(item)
self.fp.write("}\n\n") self.fp.write("}\n\n")
self.enumcodes[code] = name self.namemappers[0].addnamecode('enum', name, code)
return code return code
def compileenumerator(self, item): def compileenumerator(self, item):
@ -628,28 +746,32 @@ class ObjectCompiler:
"""This enum code is used by an event. Make sure it's available""" """This enum code is used by an event. Make sure it's available"""
name, fullname, module = self.findcodename('enum', enum) name, fullname, module = self.findcodename('enum', enum)
if not name: if not name:
if self.fp:
self.fp.write("# XXXX enum %s not found!!\n"%(enum)) self.fp.write("# XXXX enum %s not found!!\n"%(enum))
return return
if module: if module:
if self.fp:
self.fp.write("from %s import %s\n"%(module, name)) self.fp.write("from %s import %s\n"%(module, name))
def dumpindex(self): def dumpindex(self):
if not self.fp:
return
self.fp.write("\n#\n# Indices of types declared in this module\n#\n") self.fp.write("\n#\n# Indices of types declared in this module\n#\n")
self.fp.write("_classdeclarations = {\n") self.fp.write("_classdeclarations = {\n")
for k in self.classcodes.keys(): for k, v in self.namemappers[0].getall('class'):
self.fp.write("\t%s : %s,\n" % (`k`, self.classcodes[k])) self.fp.write("\t%s : %s,\n" % (`k`, v))
self.fp.write("}\n") self.fp.write("}\n")
self.fp.write("\n_propdeclarations = {\n") self.fp.write("\n_propdeclarations = {\n")
for k in self.propcodes.keys(): for k, v in self.namemappers[0].getall('property'):
self.fp.write("\t%s : %s,\n" % (`k`, self.propcodes[k])) self.fp.write("\t%s : %s,\n" % (`k`, v))
self.fp.write("}\n") self.fp.write("}\n")
self.fp.write("\n_compdeclarations = {\n") self.fp.write("\n_compdeclarations = {\n")
for k in self.compcodes.keys(): for k, v in self.namemappers[0].getall('comparison'):
self.fp.write("\t%s : %s,\n" % (`k`, self.compcodes[k])) self.fp.write("\t%s : %s,\n" % (`k`, v))
self.fp.write("}\n") self.fp.write("}\n")
self.fp.write("\n_enumdeclarations = {\n") self.fp.write("\n_enumdeclarations = {\n")
for k in self.enumcodes.keys(): for k, v in self.namemappers[0].getall('enum'):
self.fp.write("\t%s : %s,\n" % (`k`, self.enumcodes[k])) self.fp.write("\t%s : %s,\n" % (`k`, v))
self.fp.write("}\n") self.fp.write("}\n")
def compiledata(data): def compiledata(data):