Example #1
0
def parse_plots(plot_types, table_overrides):
    def get_module_base(n):
        return n
    def get_super_base(n):
        return "plot"

    module_specs = []
    for plot in plot_types:
        port_specs = {}
        print "========================================"
        print plot
        print "========================================"
        
        (plot, module_name, super_name) = \
            get_names(plot, get_module_base, get_super_base, "Mpl", "")

        try:
            plot_obj = getattr(matplotlib.pyplot, plot)
        except AttributeError:
            print '*** CANNOT ADD PLOT "%s";' \
                'IT DOES NOT EXIST IN THIS MPL VERSION ***' % plot
            continue
        
        port_specs_list = parse_argspec(plot_obj)
        for port_spec in port_specs_list:
            port_specs[port_spec.arg] = port_spec

        docstring = plot_obj.__doc__
        if plot == 'contour':
            # want to change the double newline to single newline...
            print "&*&* FINDING:", \
                docstring.find("*extent*: [ *None* | (x0,x1,y0,y1) ]\n\n")
            docstring = docstring.replace("*extent*: [ *None* | (x0,x1,y0,y1) ]\n\n", 
                              "*extent*: [ *None* | (x0,x1,y0,y1) ]\n")
        if plot == 'annotate':
            docstring = docstring % dict((k,v) for k, v in matplotlib.docstring.interpd.params.iteritems() if k == 'Annotation')
        elif plot == 'barbs':
            docstring = docstring % dict((k,v) for k,v in matplotlib.docstring.interpd.params.iteritems() if k == 'barbs_doc')

        cleaned_docstring, output_port_specs = \
            process_docstring(docstring, port_specs, ('pyplot', plot),
                              table_overrides)

        # for port_spec in port_specs.itervalues():
        #     if port_spec.defaults is not None:
        #         port_spec.defaults = [str(v) for v in port_spec.defaults]
        #     if port_spec.values is not None:
        #         port_spec.values = [[str(v) for v in port_spec.values[0]]]
        #     for alt_ps in port_spec.alternate_specs:
        #         if alt_ps.defaults is not None:
        #             alt_ps.defaults = [str(v) for v in alt_ps.defaults]
        #         if alt_ps.values is not None:
        #             alt_ps.values = [[str(v) for v in alt_ps.values[0]]]
                
        module_specs.append(ModuleSpec(module_name, super_name,
                                       "matplotlib.pyplot.%s" % plot, 
                                       cleaned_docstring, port_specs.values(),
                                       output_port_specs))
    my_specs = SpecList(module_specs)
    return my_specs
Example #2
0
def apply_diff(in_fname, diff_fname, out_fname):
    in_specs = SpecList.read_from_xml(in_fname)
    
    in_refs = dict((spec.code_ref, (i, spec))
                   for i, spec in enumerate(in_specs.module_specs))
    in_ips_refs = dict(((spec.code_ref, ps.arg), (i, ps))
                       for spec in in_specs.module_specs 
                       for i, ps in enumerate(spec.port_specs))
    in_ops_refs = dict(((spec.code_ref, ps.arg), (i, ps))
                       for spec in in_specs.module_specs
                       for i, ps in enumerate(spec.output_port_specs))
    in_alt_refs = dict(((spec.code_ref, ps.arg, alt_ps.name), (i, ps))
                       for spec in in_specs.module_specs
                       for ps in spec.port_specs
                       for i, alt_ps in enumerate(ps.alternate_specs))

    tree = ET.parse(diff_fname)
    for elt in tree.getroot():
        if elt.tag == "changeCustomCode":
            val = elt.getchildren()[0].text
            in_specs.custom_code = val
            continue
        code_ref = elt.get("code_ref")
        m_spec = in_refs[code_ref][1]
        port = elt.get("port", None)
        if port:
            port_type = elt.get("type")
            if port_type == "alternate":
                alt_name = elt.get("altName")
        if elt.tag.startswith('delete'):
            if port:
                if port_type == "input":
                    idx = in_ips_refs[(code_ref, port)][0]
                    del m_spec.port_specs[idx]
                elif port_type == 'output':
                    idx = in_ops_refs[(code_ref, port)][0]
                    del m_spec.output_port_specs[idx]
                elif port_type == 'alternate':
                    ps = in_ips_refs[(code_ref, port)][1]
                    idx = in_alt_refs[(code_ref, port, alt_name)][0]
                    del ps.alternate_specs[idx]
                else:
                    raise ValueError('Cannot access list of type "%s"' %
                                     port_type)
            else:
                idx = in_refs[code_ref][0]
                del in_specs.module_specs[idx]
        elif elt.tag.startswith('add'):
            for child in elt.getchildren():
                if child.tag == 'value':
                    for subchild in child.getchildren():
                        value = subchild
            if port:
                if port_type == "input":
                    m_spec.port_specs.append(InputPortSpec.from_xml(value))
                elif port_type == "output":
                    m_spec.output_port_specs.append(
                        OutputPortSpec.from_xml(value))
                elif port_type == "alternate":
                    ps = in_ips_refs[(code_ref, port)][1]
                    ps.alternate_specs.append(AlternatePortSpec.from_xml(value))
                else:
                    raise ValueError('Cannot access list of type "%s"' %
                                     port_type)
            else:
                in_specs.module_specs.append(ModuleSpec.from_xml(value))
        elif elt.tag.startswith('change'):
            attr = elt.get("attr")
            for child in elt.getchildren():
                if child.tag == 'value':
                    value = child.text
            if port:
                # KLUDGE to fix change from output_type to port_type
                if attr == "output_type":
                    attr = "port_type"
                if port_type == "input":
                    port_spec = in_ips_refs[(code_ref, port)][1]
                    setattr(port_spec, attr, value)
                elif port_type == "output":
                    port_spec = in_ops_refs[(code_ref, port)][1]
                    setattr(port_spec, attr, value)
                elif port_type == "alternate":
                    port_spec = in_alt_refs[(code_ref, port, alt_name)][1]
                    setattr(port_spec, attr, value)
            else:
                setattr(m_spec, attr, value)

    in_specs.write_to_xml(out_fname)
Example #3
0
def apply_diff(in_fname, diff_fname, out_fname):
    in_specs = SpecList.read_from_xml(in_fname)
    
    in_refs = dict((spec.code_ref, (i, spec))
                   for i, spec in enumerate(in_specs.module_specs))
    in_ips_refs = dict(((spec.code_ref, ps.arg), (i, ps))
                       for spec in in_specs.module_specs 
                       for i, ps in enumerate(spec.port_specs))
    in_ops_refs = dict(((spec.code_ref, ps.arg), (i, ps))
                       for spec in in_specs.module_specs
                       for i, ps in enumerate(spec.output_port_specs))
    in_alt_refs = dict(((spec.code_ref, ps.arg, alt_ps.name), (i, ps))
                       for spec in in_specs.module_specs
                       for ps in spec.port_specs
                       for i, alt_ps in enumerate(ps.alternate_specs))

    tree = ET.parse(diff_fname)
    for elt in tree.getroot():
        if elt.tag == "changeCustomCode":
            val = elt.getchildren()[0].text
            in_specs.custom_code = val
            continue
        code_ref = elt.get("code_ref")
        m_spec = in_refs[code_ref][1]
        port = elt.get("port", None)
        if port:
            port_type = elt.get("type")
            if port_type == "alternate":
                alt_name = elt.get("altName")
        if elt.tag.startswith('delete'):
            if port:
                if port_type == "input":
                    idx = in_ips_refs[(code_ref, port)][0]
                    del m_spec.port_specs[idx]
                elif port_type == 'output':
                    idx = in_ops_refs[(code_ref, port)][0]
                    del m_spec.output_port_specs[idx]
                elif port_type == 'alternate':
                    ps = in_ips_refs[(code_ref, port)][1]
                    idx = in_alt_refs[(code_ref, port, alt_name)][0]
                    del ps.alternate_specs[idx]
                else:
                    raise ValueError('Cannot access list of type "%s"' %
                                     port_type)
            else:
                idx = in_refs[code_ref][0]
                del in_specs.module_specs[idx]
        elif elt.tag.startswith('add'):
            for child in elt.getchildren():
                if child.tag == 'value':
                    for subchild in child.getchildren():
                        value = subchild
            if port:
                if port_type == "input":
                    m_spec.port_specs.append(InputPortSpec.from_xml(value))
                elif port_type == "output":
                    # print "VALUE:", ET.tostring(value)
                    m_spec.output_port_specs.append(
                        OutputPortSpec.from_xml(value))
                elif port_type == "alternate":
                    ps = in_ips_refs[(code_ref, port)][1]
                    ps.alternate_specs.append(AlternatePortSpec.from_xml(value))
                else:
                    raise ValueError('Cannot access list of type "%s"' %
                                     port_type)
            else:
                in_specs.module_specs.append(ModuleSpec.from_xml(value))
        elif elt.tag.startswith('change'):
            attr = elt.get("attr")
            for child in elt.getchildren():
                if child.tag == 'value':
                    value = child.text
            if port:
                # KLUDGE to fix change from output_type to port_type
                if attr == "output_type":
                    attr = "port_type"
                if port_type == "input":
                    port_spec = in_ips_refs[(code_ref, port)][1]
                    setattr(port_spec, attr, value)
                elif port_type == "output":
                    port_spec = in_ops_refs[(code_ref, port)][1]
                    setattr(port_spec, attr, value)
                elif port_type == "alternate":
                    port_spec = in_alt_refs[(code_ref, port, alt_name)][1]
                    setattr(port_spec, attr, value)
            else:
                setattr(m_spec, attr, value)

    # with f = open(diff_fname):
    #     f_iter = f.__iter__()
    #     for line in f_iter:
    #         line = line.strip()
    #         if not re.match("[+-C]", line):
    #             raise RuntimeError("Problem parsing line\n%s" % line)
    #         if line.startswith('-'):
    #             arr = line.split(' ', 1)
    #             prop = arr[1].split('.')
    #             if len(prop) < 2:
    #                 idx = in_refs[prop[0]][0]
    #                 del in_specs.module_specs[idx]
    #             else:
    #                 m_specs = in_refs[prop[0]][1]
    #                 if prop[1] == 'input':
    #                     idx = in_ips_refs[prop[0], prop[2]][0]
    #                     del m_specs.port_specs[idx]
    #                 elif prop[1] == 'output':
    #                     idx = in_ops_refs[prop[0], prop[2]][0]
    #                     del m_specs.output_port_specs[idx]
    #                 else:
    #                     raise ValueError('Cannot access list of type "%s"' %
    #                                      prop[1])
    #         elif line.startswith('+'):
    #             arr = line.split(' ', 2)
    #             prop = arr[1].split('.')
    #             if len(prop) < 2:
    #                 in_specs.module_specs.append(ModuleSpec.from_xml(arr[2]))
                
    #         line.split(' ', 2)
            
    in_specs.write_to_xml(out_fname)
Example #4
0
def parse_artists(artist_types, table_overrides={}):
    def get_module_name(obj):
        return obj.__name__

    def get_super_name(obj):
        for base in obj.__bases__:
            if issubclass(base, Artist):
                return base.__name__
        return ""

    module_specs = []
    for klass in artist_types:
        (klass, module_name, super_name) = \
            get_names(klass, get_module_name, get_super_name, "Mpl",
                      "Properties")

        port_specs = {}
        insp = ArtistInspector(klass)
        klass_name = klass.__name__
        klass_qualname = klass.__module__ + "." + klass_name
        for (s, t) in insp._get_setters_and_targets():
            print "** %s **" % s
            if t.rsplit('.', 1)[0] != klass_qualname:
                # let inheritance work
                continue

            if s in port_specs:
                raise ValueError('duplicate port "%s"' % s)
            port_spec = InputPortSpec(s)
            port_specs[s] = port_spec

            accepts_raw = insp.get_valid_values(s)
            (accepts, deflists, tables, call_sigs) = \
                parse_docutils_str(accepts_raw)
            if len(deflists) + len(tables) > 0:
                raise ValueError("accepts has deflists and/or tables")
            (port_types, option_strs, default_val, allows_none) = \
                parse_description(accepts)
            if default_val is not None:
                port_spec.default_val = default_val
            if len(option_strs) > 0:
                port_spec.entry_types = ['enum']
                port_spec.values = [option_strs]
            port_spec.hide = False

            docstring = getattr(insp.o, 'set_' + s).__doc__
            if docstring is None:
                docstring = ""
            else:
                docstring = docstring % matplotlib.docstring.interpd.params
            match = _get_accepts_regex.search(docstring)
            if match is not None:
                print "STARTING DOCSTRING:", docstring
                groups = match.groups()
                if len(groups) > 2 and groups[2]:
                    docstring = groups[0] + groups[2]
                else:
                    docstring = groups[0]
                print "FIXED DOCSTRING:", docstring

            (cleaned_docstring, args, tables, call_sigs) = \
                parse_docutils_str(docstring)
            port_spec.docstring = cleaned_docstring

            translations = None
            for (table_intro, header, rows) in tables:
                print "TABLE:", table_intro
                if (klass.__name__, s, table_intro) in table_overrides:
                    (override_type, opts) = \
                        table_overrides[(klass.__name__, s, table_intro)]
                    if override_type == "translation":
                        do_translation_override(port_specs, s, rows, opts)
                        continue
                    elif override_type == "ports":
                        table_intro = "kwarg"
                    elif override_type == "skip":
                        continue
                if len(header) != 2:
                    raise ValueError("Table not two columns!")
                if translations is not None:
                    raise ValueError("Two translations in one attr")
                (translations, pt2, values) = parse_translation(rows)
                port_spec.translations = translations
                port_spec.values = [values]
                port_types.extend(pt2)
            resolve_port_type(port_types, port_spec)

        constructor_port_specs = {}
        port_specs_list = parse_argspec(klass.__init__)
        for port_spec in port_specs_list:
            constructor_port_specs[port_spec.arg] = port_spec
        constructor_docstring = klass.__init__.__doc__
        if constructor_docstring is not None:
            _, output_port_specs = process_docstring(
                constructor_docstring, constructor_port_specs,
                (klass.__name__, '__init__'), table_overrides)
        for arg, ps in constructor_port_specs.iteritems():
            if arg not in port_specs:
                ps.constructor_arg = True
                ps.required = False
                port_specs[arg] = ps

        module_spec = ModuleSpec(module_name, super_name, klass_qualname,
                                 klass.__doc__, port_specs.values())
        module_specs.append(module_spec)

    my_specs = SpecList(module_specs)
    return my_specs
Example #5
0
def apply_diff(in_fname, diff_fname, out_fname):
    in_specs = SpecList.read_from_xml(in_fname)

    in_refs = dict((spec.code_ref, (i, spec))
                   for i, spec in enumerate(in_specs.module_specs))
    in_ips_refs = dict(((spec.code_ref, ps.arg), (i, ps))
                       for spec in in_specs.module_specs
                       for i, ps in enumerate(spec.port_specs))
    in_ops_refs = dict(((spec.code_ref, ps.arg), (i, ps))
                       for spec in in_specs.module_specs
                       for i, ps in enumerate(spec.output_port_specs))
    in_alt_refs = dict(((spec.code_ref, ps.arg, alt_ps.name), (i, ps))
                       for spec in in_specs.module_specs
                       for ps in spec.port_specs
                       for i, alt_ps in enumerate(ps.alternate_specs))

    tree = ET.parse(diff_fname)
    for elt in tree.getroot():
        if elt.tag == "changeCustomCode":
            val = elt.getchildren()[0].text
            in_specs.custom_code = val
            continue
        code_ref = elt.get("code_ref")
        m_spec = in_refs[code_ref][1]
        port = elt.get("port", None)
        if port:
            port_type = elt.get("type")
            if port_type == "alternate":
                alt_name = elt.get("altName")
        if elt.tag.startswith('delete'):
            if port:
                if port_type == "input":
                    idx = in_ips_refs[(code_ref, port)][0]
                    del m_spec.port_specs[idx]
                elif port_type == 'output':
                    idx = in_ops_refs[(code_ref, port)][0]
                    del m_spec.output_port_specs[idx]
                elif port_type == 'alternate':
                    ps = in_ips_refs[(code_ref, port)][1]
                    idx = in_alt_refs[(code_ref, port, alt_name)][0]
                    del ps.alternate_specs[idx]
                else:
                    raise ValueError('Cannot access list of type "%s"' %
                                     port_type)
            else:
                idx = in_refs[code_ref][0]
                del in_specs.module_specs[idx]
        elif elt.tag.startswith('add'):
            for child in elt.getchildren():
                if child.tag == 'value':
                    for subchild in child.getchildren():
                        value = subchild
            if port:
                if port_type == "input":
                    m_spec.port_specs.append(InputPortSpec.from_xml(value))
                elif port_type == "output":
                    m_spec.output_port_specs.append(
                        OutputPortSpec.from_xml(value))
                elif port_type == "alternate":
                    ps = in_ips_refs[(code_ref, port)][1]
                    ps.alternate_specs.append(
                        AlternatePortSpec.from_xml(value))
                else:
                    raise ValueError('Cannot access list of type "%s"' %
                                     port_type)
            else:
                in_specs.module_specs.append(ModuleSpec.from_xml(value))
        elif elt.tag.startswith('change'):
            attr = elt.get("attr")
            for child in elt.getchildren():
                if child.tag == 'value':
                    value = child.text
            if port:
                # KLUDGE to fix change from output_type to port_type
                if attr == "output_type":
                    attr = "port_type"
                if port_type == "input":
                    port_spec = in_ips_refs[(code_ref, port)][1]
                    setattr(port_spec, attr, value)
                elif port_type == "output":
                    port_spec = in_ops_refs[(code_ref, port)][1]
                    setattr(port_spec, attr, value)
                elif port_type == "alternate":
                    port_spec = in_alt_refs[(code_ref, port, alt_name)][1]
                    setattr(port_spec, attr, value)
            else:
                setattr(m_spec, attr, value)

    in_specs.write_to_xml(out_fname)