<commit_before>#
# Reads in a spec document and generates pretty data structures from it
#

import sys
import xml.dom.minidom

import xincludator

XMLNS_TP = 'http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0'

def getText (dom):
    if dom.childNodes[0].nodeType == dom.TEXT_NODE:
        return dom.childNodes[0].data
    else:
        return ''

def getChildrenByName (dom, namespace, name):
    return filter (lambda n: n.nodeType == n.ELEMENT_NODE and \
                             n.namespaceURI == namespace and \
                             n.localName == name,
                   dom.childNodes)

def build_name (namespace, name):
    """Returns a name by appending `name' to the namespace of this object.
    """
    return '.'.join (
        filter (lambda n: n is not None, [namespace, name.replace (' ', '')])
        )

class base (object):
    """The base class for any type of XML node in the spec that implements the
       'name' attribute.

       Don't instantiate this class directly.
    """
    def __init__ (self, parent, namespace, dom):
        self.short_name = name = dom.getAttribute ('name')
        self.namespace = namespace
        self.name = build_name (namespace, name)
        self.parent = parent

        try:
            self.docstring = getChildrenByName (dom, XMLNS_TP, 'docstring')[0]
        except IndexError:
            self.docstring = None

    def get_spec (self):
        return self.parent.get_spec ()

    def get_root_namespace (self):
        return self.get_interface ().name

    def get_interface (self):
        return self.parent.get_interface ()

    def get_url (self):
        return "%s#%s" % (self.get_interface ().get_url (), self.name)

    def get_docstring (self):
        """Get the docstring for this node, but do node substitution to
           rewrite types, interfaces, etc. as links.
        """
        if self.docstring is None:
            return ''
        else:
            # make a copy of this node, turn it into a HTML <div> tag
            node = self.docstring.cloneNode (True)
            node.tagName = 'div'
            node.baseURI = None
            node.setAttribute ('class', 'docstring')

            # rewrite <tp:rationale>
            for n in node.getElementsByTagNameNS (XMLNS_TP, 'rationale'):
                n.tagName = 'div'
                n.namespaceURI = None
                n.setAttribute ('class', 'rationale')

            # rewrite <tp:member-ref>
            spec = self.get_spec ()
            namespace = self.get_root_namespace ()
            for n in node.getElementsByTagNameNS (XMLNS_TP, 'member-ref'):
                key = getText (n)
                try:
                    o = spec.lookup (key, namespace = namespace)
                except KeyError:
                    print >> sys.stderr, \
                        "Key `%s' not known in namespace `%s'" % (
                            key, namespace)
                    continue

                n.tagName = 'a'
                n.namespaceURI = None
                n.setAttribute ('href', o.get_url ())
                n.setAttribute ('title', o.get_title ())

            # rewrite <tp:dbus-ref>
            for n in node.getElementsByTagNameNS (XMLNS_TP, 'dbus-ref'):
                namespace = n.getAttribute ('namespace')
                key = getText (n)
                try:
                    o = spec.lookup (key, namespace = namespace)
                except KeyError:
                    print >> sys.stderr, \
                        "Key `%s' not known in namespace `%s'" % (
                            key, namespace)
                    continue

                n.tagName = 'a'
                n.namespaceURI = None
                n.setAttribute ('href', o.get_url ())
                n.setAttribute ('title', o.get_title ())

            return node.toxml ().encode ('ascii', 'xmlcharrefreplace')

    def get_title (self):
        return '%s %s' % (self.__class__.__name__, self.name)

    def __repr__ (self):
        return '%s(%s)' % (self.__class__.__name__, self.name)

class PossibleError (base):
    def __init__ (self, parent, namespace, dom):
        super (PossibleError, self).__init__ (parent, namespace, dom)

    def get_error (self):
        spec = self.get_spec ()
        return spec.errors[self.name]

    def get_url (self):
        self.get_error ().get_url ()

    def get_title (self):
        self.get_error ().get_title ()

    def get_docstring (self):
        d = super (PossibleError, self).get_docstring ()
        if d == '':
            return self.get_error ().get_docstring ()
        else:
            return d

class Method (base):
    def __init__ (self, parent, namespace, dom):
        super (Method, self).__init__ (parent, namespace, dom)

        args = build_list (self, Arg, self.name,
                         dom.getElementsByTagName ('arg'))

        # separate arguments as input and output arguments
        self.in_args = filter (lambda a: a.direction == Arg.DIRECTION_IN, args)
        self.out_args = filter (lambda a: a.direction == Arg.DIRECTION_OUT, args)

        self.possible_errors = build_list (self, PossibleError, None,<commit_msg>Fix up case of the stoopids<commit_after>#
# Reads in a spec document and generates pretty data structures from it
#

import sys
import xml.dom.minidom

import xincludator

XMLNS_TP = 'http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0'

def getText (dom):
    if dom.childNodes[0].nodeType == dom.TEXT_NODE:
        return dom.childNodes[0].data
    else:
        return ''

def getChildrenByName (dom, namespace, name):
    return filter (lambda n: n.nodeType == n.ELEMENT_NODE and \
                             n.namespaceURI == namespace and \
                             n.localName == name,
                   dom.childNodes)

def build_name (namespace, name):
    """Returns a name by appending `name' to the namespace of this object.
    """
    return '.'.join (
        filter (lambda n: n is not None, [namespace, name.replace (' ', '')])
        )

class base (object):
    """The base class for any type of XML node in the spec that implements the
       'name' attribute.

       Don't instantiate this class directly.
    """
    def __init__ (self, parent, namespace, dom):
        self.short_name = name = dom.getAttribute ('name')
        self.namespace = namespace
        self.name = build_name (namespace, name)
        self.parent = parent

        try:
            self.docstring = getChildrenByName (dom, XMLNS_TP, 'docstring')[0]
        except IndexError:
            self.docstring = None

    def get_spec (self):
        return self.parent.get_spec ()

    def get_root_namespace (self):
        return self.get_interface ().name

    def get_interface (self):
        return self.parent.get_interface ()

    def get_url (self):
        return "%s#%s" % (self.get_interface ().get_url (), self.name)

    def get_docstring (self):
        """Get the docstring for this node, but do node substitution to
           rewrite types, interfaces, etc. as links.
        """
        if self.docstring is None:
            return ''
        else:
            # make a copy of this node, turn it into a HTML <div> tag
            node = self.docstring.cloneNode (True)
            node.tagName = 'div'
            node.baseURI = None
            node.setAttribute ('class', 'docstring')

            # rewrite <tp:rationale>
            for n in node.getElementsByTagNameNS (XMLNS_TP, 'rationale'):
                n.tagName = 'div'
                n.namespaceURI = None
                n.setAttribute ('class', 'rationale')

            # rewrite <tp:member-ref>
            spec = self.get_spec ()
            namespace = self.get_root_namespace ()
            for n in node.getElementsByTagNameNS (XMLNS_TP, 'member-ref'):
                key = getText (n)
                try:
                    o = spec.lookup (key, namespace = namespace)
                except KeyError:
                    print >> sys.stderr, \
                        "Key `%s' not known in namespace `%s'" % (
                            key, namespace)
                    continue

                n.tagName = 'a'
                n.namespaceURI = None
                n.setAttribute ('href', o.get_url ())
                n.setAttribute ('title', o.get_title ())

            # rewrite <tp:dbus-ref>
            for n in node.getElementsByTagNameNS (XMLNS_TP, 'dbus-ref'):
                namespace = n.getAttribute ('namespace')
                key = getText (n)
                try:
                    o = spec.lookup (key, namespace = namespace)
                except KeyError:
                    print >> sys.stderr, \
                        "Key `%s' not known in namespace `%s'" % (
                            key, namespace)
                    continue

                n.tagName = 'a'
                n.namespaceURI = None
                n.setAttribute ('href', o.get_url ())
                n.setAttribute ('title', o.get_title ())

            return node.toxml ().encode ('ascii', 'xmlcharrefreplace')

    def get_title (self):
        return '%s %s' % (self.__class__.__name__, self.name)

    def __repr__ (self):
        return '%s(%s)' % (self.__class__.__name__, self.name)

class PossibleError (base):
    def __init__ (self, parent, namespace, dom):
        super (PossibleError, self).__init__ (parent, namespace, dom)

    def get_error (self):
        spec = self.get_spec ()
        return spec.errors[self.name]

    def get_url (self):
        return self.get_error ().get_url ()

    def get_title (self):
        return self.get_error ().get_title ()

    def get_docstring (self):
        d = super (PossibleError, self).get_docstring ()
        if d == '':
            return self.get_error ().get_docstring ()
        else:
            return d

class Method (base):
    def __init__ (self, parent, namespace, dom):
        super (Method, self).__init__ (parent, namespace, dom)

        args = build_list (self, Arg, self.name,
                         dom.getElementsByTagName ('arg'))

        # separate arguments as input and output arguments
        self.in_args = filter (lambda a: a.direction == Arg.DIRECTION_IN, args)
        self.out_args = filter (lambda a: a.direction == Arg.DIRECTION_OUT, args)

        self.possible_errors = build_list (self, PossibleError, None,
{'fraction_non_alphanumeric': 0.10753306497987349, 'fraction_numerical': 0.0007667241709794901, 'mean_word_length': 1.7797016515716568, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '876835', 'n_tokens_mistral': 2988, 'n_tokens_neox': 2867, 'n_words': 848}