'Return an output command based on the URL filename component.
As a side-effect, the filename component will be removed from the
path_parts list.
>>> path_parts = \'/a/b/as.json\'.split(\'/\')
>>> command = urlmap._choose_output(path_parts)
>>> (path_parts, command)
([\'\', \'a\', \'b\'], <mailpile.plugins.core.Output...>)
If there is no filename part, the path_parts list is unchanged
aside from stripping off the trailing empty string if present.
>>> path_parts = \'/a/b/\'.split(\'/\')
>>> command = urlmap._choose_output(path_parts)
>>> (path_parts, command)
([\'\', \'a\', \'b\'], <mailpile.plugins.core.Output...>)
>>> path_parts = \'/a/b\'.split(\'/\')
>>> command = urlmap._choose_output(path_parts)
Traceback (most recent call last):
UsageError: Invalid output format: b
>>> path_parts = \'/a/b/%%%%%bogon.json\'.split(\'/\')
>>> command = urlmap._choose_output(path_parts)
Traceback (most recent call last):
UsageError: Invalid output format: %%%%%bogon.json'

def _choose_output(self, path_parts, fmt='html'):

    if ((len(path_parts) > 1) and (not path_parts[(-1)])):
        path_parts.pop((-1))
    else:
        om = path_parts.pop((-1))
        if re.match('^[a-zA-Z0-9\\.!_-]+$', om):
            fn = om.split('!')[0]
            for suffix in self.OUTPUT_SUFFIXES:
                if (fn.endswith(suffix) or (suffix == ('.' + fn))):
                    return self._command('output', [om], method=False)
        raise UsageError(('Invalid    output    format:    %s' % om))
    return self._command('output', [fmt], method=False)
