I struggled to grasp some of this, and was very suspicious of all the 'ports[map_set][0]' bits.
I see now it's because of the "class variables" in the Mapping subtypes ('in_file', 'container' et al, being class rather than instance properties).  But also because we need to access information which belongs to the type, and we are relying on the "contract" that all the mappings in the list have the same type, so we dispatch through the element[0].
I reckon this could be that much clearer with some subsidiary variables, and especially to make that class-choice explicit,
like ```map_type = ports[map_set][0].__class__; if map_type.to_sort: map_str = dict_line_sort(map_str)```
But then I also realised that we **already had the type**, associated with each group of mappings (from itertools.group_by), and we lost it.
So actually, I'd suggest that the lowest level division of format_maps could be indexed by that type, rather than its __name__...
I think this could make all this rather neater, something like ...
```
    for map_type in pkeys:
        print direction
        print map_type
        if map_type == None:
           ...
        else:
            if map_type.in_file not in BUILT_FILES:
               ...
            mappings = ports[map_type]
            map_str = [port_mappings.encode()
                       for port_mappings in mappings]
            if map_type.to_sort:
                map_str = dict_line_sort(map_str)
            if map_type.container:
                map_str = map_type.container + map_str
            if map_type.closure:
                map_str = map_srt + map_type.closure
```