def do_translation_override(port_specs, names, rows, opts): if 'name' in opts: names = opts['name'] if names is None: raise ValueError("Must specify name of port to use translation for") if isinstance(names, basestring) or not matplotlib.cbook.iterable(names): names = [names] should_reverse = opts.get('reverse', True) values_only = opts.get('values_only', False) (t, port_type, values) = \ parse_translation(rows, should_reverse) for name in names: print "TRANSLATING", name if name not in port_specs: port_specs[name] = InputPortSpec(name) port_specs[name].entry_types = ['enum'] port_specs[name].values = [values] if not values_only: port_specs[name].translations = t
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
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 "" # if obj.__bases__[0].__name__ != 'object': # return obj.__bases__[0].__name__ # else: # 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) # port_spec.port_type = port_type 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
def process_docstring(docstring, port_specs, parent, table_overrides): (cleaned_docstring, args, tables, call_sigs) = \ parse_docutils_str(docstring) if len(call_sigs) > 0: for call_sig in call_sigs: port_specs_list = parse_argspec(call_sig) for port_spec in port_specs_list: if port_spec.arg in port_specs: # have to reconcile the two old_port_spec = port_specs[port_spec.arg] resolve_port_type([port_spec.port_type], old_port_spec) if old_port_spec.defaults is None: old_port_spec.defaults = port_spec.defaults elif old_port_spec.defaults != port_spec.defaults: # keep it as the old spec is print "*** Different defaults!" + \ str(old_port_spec.defaults) + \ " : " + str(port_spec.defaults) # raise RuntimeError("Different defaults! %s: %s" % ( # old_port_spec.defaults, # port_spec.defaults)) else: port_specs[port_spec.arg] = port_spec output_port_specs = [] for (deflist_intro, deflist) in args: print "PROCESSING DEFLIST", deflist_intro if re.search("return value", deflist_intro, re.IGNORECASE): print " -> RETURN VALUE" for (name, accepts, port_doc) in deflist: (port_types, option_strs, default_val, allows_none) = \ parse_description(accepts) (pt2, _, dv2, _) = parse_description(port_doc) port_types.extend(pt2) if default_val is None: default_val = dv2 oport = OutputPortSpec(name, docstring=port_doc) resolve_port_type(port_types, oport) output_port_specs.append(oport) elif (re.search("argument", deflist_intro, re.IGNORECASE) or re.search("kwarg", deflist_intro, re.IGNORECASE)): print " -> ARGUMENTS" for (name, accepts, port_doc) in deflist: if name not in port_specs: port_specs[name] = InputPortSpec(name, docstring=port_doc) else: port_specs[name].docstring = port_doc (port_types, option_strs, default_val, allows_none) = \ parse_description(accepts) (pt2, _, dv2, _) = parse_description(port_doc) port_types.extend(pt2) if default_val is None: default_val = dv2 resolve_port_type(port_types, port_specs[name]) if len(option_strs) > 0: port_specs[name].entry_types = ['enum'] port_specs[name].values = [option_strs] if default_val is not None: port_specs[name].defaults = [str(default_val)] for (table_intro, header, rows) in tables: print "GOT TABLE", table_intro, rows[0] table_key = parent + (table_intro,) if table_key in table_overrides: (override_type, opts) = table_overrides[table_key] if override_type == "translation": do_translation_override(port_specs, None, rows, opts) continue elif override_type == "ports": table_intro = "kwarg" elif override_type == "skip": continue if re.search("return value", table_intro, re.IGNORECASE): print " -> RETURN" if len(rows[0]) != 2: raise ValueError("row that has more/less than 2 columns!") for (name, port_doc) in rows: (port_types, option_strs, default_val, allows_none) = \ parse_description(port_doc) # (port_types, option_strs) = parse_desc(port_doc) # port_type_set = set(port_types) # # print port_name, "PORT_TYPES:", port_type_set # port_type = "UNKNOWN" # if len(port_type_set) == 1: # port_type = port_types[0] oport = OutputPortSpec(name, docstring=port_doc) resolve_port_type(oport, port_types) output_port_specs.append(oport) elif (re.search("argument", table_intro, re.IGNORECASE) or re.search("kwarg", table_intro, re.IGNORECASE)): print " -> ARGUMENT" if len(rows[0]) != 2: raise ValueError("row that has more/less than 2 columns!") for (name, port_doc) in rows: if name not in port_specs: port_specs[name] = InputPortSpec(name, docstring=port_doc) else: port_specs[name].docstring = port_doc (port_types, option_strs, default_val, allows_none) = \ parse_description(port_doc) # (port_types, option_strs) = parse_desc(port_doc) # port_type_set = set(port_types) # # print port_name, "PORT_TYPES:", port_type_set # port_type = "UNKNOWN" # if len(port_type_set) == 1: # port_specs[name].port_type = port_types[0] resolve_port_type(port_types, port_specs[name]) if len(option_strs) > 0: port_specs[name].entry_types = ['enum'] port_specs[name].values = [option_strs] if default_val is not None: port_specs[name].defaults = [str(default_val)] else: raise ValueError("Unknown table: %s\n %s %s" % ( parent, table_intro, header)) # print "HIT SPEC:", name # if name not in port_specs: # port_specs[name] = PortSpec(name, name, "UNKNOWN", "") # port_specs[name].translations = dict(reversed(r) for r in rows) return cleaned_docstring, output_port_specs