def find_matching_compartment(self, compartment, interact=True): """ :param compartment: should be an iterable, as-stored in an archive :param interact: if true, interactively prompt user to merge and save missing compartments :return: the Compartment. Also adds it to self._c_dict """ cs = compartment_string(compartment) if cs in self._c_dict.keys(): return self._c_dict[cs] match = traverse_compartments(self.compartments, compartment) if match is None: if interact: c = merge_compartment(self.compartments, compartment) match = traverse_compartments(self.compartments, compartment) if c is match and c is not None: print('match: %s' % match.to_list()) print('Updating compartments...') save_compartments(self.compartments, file=self._compartments_file) return c else: raise MissingCompartment('%s' % cs) else: self._c_dict[cs] = match return match
def merge_compartment(compartment, missing): my_missing = [] my_missing.extend(missing) while len(my_missing) > 0: sub = traverse_compartments(compartment, my_missing[:1]) if sub is not None: my_missing.pop(0) compartment = sub else: print('Missing compartment: %s' % my_missing[0]) subs = sorted(s.name for s in compartment.subcompartments()) print('subcompartments of %s:' % compartment) c = _pick_list(subs, 'Merge "%s" into %s' % (my_missing[0], compartment), 'Create new Subcompartment of %s' % compartment) if c == (None, 0): compartment.add_syn(my_missing.pop(0)) elif c == (None, 1): # now we add all remaining compartments while len(my_missing) > 0: new_sub = my_missing.pop(0) compartment.add_sub(new_sub) compartment = compartment[new_sub] elif c == (None, None): raise ValueError('User break') else: compartment = compartment[subs[c[0]]] return compartment