This function goes through all the programlisting nodes from the input file, sorting them by output file name. In the process it builds and returns a dictionary whose keys are the output file names, and the corresponding values are lists of the programlisting nodes directed at that output file.
# - - - b u i l d F r a g M a p - - -
def buildFragMap ( fragList ):
"""Assemble all fragments for a given output file name.
[ fragList is a node-set of programlisting DOM Element nodes
with role attributes ->
return a dictionary mapping unique output file names
from role attributes in fragList |-> lists of
corresponding programlisting nodes from fragList ]
"""
|
First we create an empty dictionary to hold the result:
#-- 1 --
result = {}
|
Then we go through the list of fragments looking for role attributes that start with "outFile:". Each time we find one, we add it to the list for that file name, or create a new list if it is the first one.
#-- 2 --
# [ result +:= entries mapping unique file names from the
# role attributes of nodes in fragList |-> lists of
# corresponding programlisting nodes from fragList ]
for frag in fragList:
|
If there are role attributes that don't start with "outFile:", the findFileName() function returns None, otherwise it returns the file name.
#-- 2 body --
# [ frag is a programlisting node with a role attribute ->
# if result has an value for key file-referenced(frag) ->
# corresponding value +:= frag
# else ->
# corresponding value := [frag]
#-- 2.1 --
# [ fileName := file-referenced(frag) ]
fileName = findFileName ( frag )
|
When a real file name is returned, we then either append it to the existing list of nodes for that file name, or create a new one if it's the first one for that name.
#-- 2.2 --
# [ if fileName is None -> I
# else if result has a key fileName ->
# corresponding value +:= frag
# else ->
# corresponding value := [frag]
if fileName:
try:
result[fileName].append ( frag )
except KeyError:
result[fileName] = [frag]
|
Finally we return the dictionary we've constructed.
#-- 3 --
return result
|