def prettify_nsdecls(xml):

    def parse_attrs(rootline):
        """
        Return 3-tuple (head, attributes, tail) looking like
        ('<p:sld', ['xmlns:p="html://..."', 'name="Office Theme>"'], '>').
        """
        attr_re = re.compile(r'([-a-zA-Z0-9_:.]+="[^"]*" *)')
        substrs = [substr.strip() for substr in attr_re.split(rootline)
                   if substr]
        head = substrs[0]
        attrs, tail = ((substrs[1:-1], substrs[-1]) if len(substrs) > 1
                       else ([], ''))
        return (head, attrs, tail)

    def sequence_attrs(attributes):
        """
        Sort attributes alphabetically within the subgroups: default
        namespace declaration, other namespace declarations, other
        attributes.
        """
        def_nsdecls, nsdecls, attrs = [], [], []
        for attr in attributes:
            if attr.startswith('xmlns='):
                def_nsdecls.append(attr)
            elif attr.startswith('xmlns:'):
                nsdecls.append(attr)
            else:
                attrs.append(attr)
        return sorted(def_nsdecls) + sorted(nsdecls) + sorted(attrs)

    def pretty_rootline(head, attrs, tail):
        """
        Return string containing prettified XML root line with *head* on the
        first line, *attrs* indented on following lines, and *tail* indented
        on the last line.
        """
        indent = 4 * ' '
        newrootline = head
        for attr in attrs:
            newrootline += '\n%s%s' % (indent, attr)
        newrootline += '\n%s%s' % (indent, tail) if tail else ''
        return newrootline

    lines = xml.splitlines()
    rootline = lines[1]
    head, attributes, tail = parse_attrs(rootline)
    attributes = sequence_attrs(attributes)
    lines[1] = pretty_rootline(head, attributes, tail)
    return '\n'.join(lines)
Loads a CloudApi into the loaded_apis map for this class.
dataset/ETHPy150Open python-openxml/opc-diag/opcdiag/presenter.py/prettify_nsdecls--crossover with--dataset/ETHPy150Open GoogleCloudPlatform/gsutil/gslib/cloud_api_delegator.py/CloudApiDelegator._LoadApi