def resolve_port_type(port_types, port_spec): port_types_set = set(p for p in port_types if p is not None) was_set = False if port_spec.port_type is not None: port_types_set.add(port_spec.port_type) if len(port_types_set) == 1: port_spec.port_type = next(iter(port_types_set)) was_set = True elif len(port_types_set) == 2: if 'basic:Float' in port_types_set and \ 'basic:Integer' in port_types_set: port_spec.port_type = 'basic:Float' was_set = True elif 'basic:List' in port_types_set: port_spec.port_type = 'basic:List' base_name = port_spec.name port_spec.name = base_name + "Sequence" port_types_set.discard('basic:List') alternate_spec = \ AlternatePortSpec(name=base_name + "Scalar", port_type=next(iter(port_types_set))) port_spec.alternate_specs.append(alternate_spec) was_set = True if not was_set: if "color" in port_spec.name: port_spec.port_type = "basic:Color" port_spec.translations = "translate_color" elif port_spec.name == "x": port_spec.port_type = "basic:List" elif port_spec.name == "y": port_spec.port_type = "basic:List" else: port_spec.port_type = None
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)
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)
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)