def _write(self, stream, taxon_namespaces=None, tree_lists=None, char_matrices=None, global_annotations_target=None): # reset book-keeping self._taxon_namespaces_to_write = [] self._taxon_namespace_id_map = {} self._taxon_id_map = {} self._node_id_map = {} self._state_alphabet_id_map = {} self._state_id_map = {} # Destination: # Writing to buffer instead of directly to output # stream so that all namespaces referenced in metadata # can be written body = StringIO() # comments and metadata self._write_annotations_and_comments(global_annotations_target, body, 1) # Taxon namespace discovery candidate_taxon_namespaces = collections.OrderedDict() if self.attached_taxon_namespace is not None: candidate_taxon_namespaces[self.attached_taxon_namespace] = True elif taxon_namespaces is not None: if self.suppress_unreferenced_taxon_namespaces: # preload to preserve order for tns in taxon_namespaces: candidate_taxon_namespaces[tns] = False else: for tns in taxon_namespaces: candidate_taxon_namespaces[tns] = True for data_collection in (tree_lists, char_matrices): if data_collection is not None: for i in data_collection: if self.attached_taxon_namespace is None or i.taxon_namespace is self.attached_taxon_namespace: candidate_taxon_namespaces[i.taxon_namespace] = True self._taxon_namespaces_to_write = [tns for tns in candidate_taxon_namespaces if candidate_taxon_namespaces[tns]] for tns in self._taxon_namespaces_to_write: self._write_taxon_namespace(tns, body) if char_matrices: for char_matrix in char_matrices: self._write_char_matrix(char_matrix=char_matrix, dest=body) if tree_lists: for tree_list in tree_lists: self._write_tree_list(tree_list=tree_list, dest=body) self._write_to_nexml_open(stream, indent_level=0) stream.write(body.getvalue()) self._write_to_nexml_close(stream, indent_level=0)
def write_out_validate_equal_and_return(self, writeable, schema, kwargs): self.maxDiff = None sio = StringIO() writeable.write(file=sio, schema=schema, **kwargs) s0 = sio.getvalue() s1 = writeable.as_string(schema=schema, **kwargs) self.assertEqual(s0, s1) with pathmap.SandboxedFile() as tempf: writeable.write(path=tempf.name, schema=schema, **kwargs) tempf.flush() tempf.close() with open(tempf.name, "r") as src: s2 = src.read() self.assertEqual(s0, s2) return s0
def description_text(): from dendropy.utility.textprocessing import StringIO s = StringIO() description(s) return s.getvalue()
def _write(self, stream, taxon_namespaces=None, tree_lists=None, char_matrices=None, global_annotations_target=None): # reset book-keeping self._taxon_namespaces_to_write = [] self._taxon_namespace_id_map = {} self._taxon_id_map = {} self._node_id_map = {} self._state_alphabet_id_map = {} self._state_id_map = {} # Destination: # Writing to buffer instead of directly to output # stream so that all namespaces referenced in metadata # can be written body = StringIO() # comments and metadata self._write_annotations_and_comments(global_annotations_target, body, 1) # Taxon namespace discovery candidate_taxon_namespaces = collections.OrderedDict() if self.attached_taxon_namespace is not None: candidate_taxon_namespaces[self.attached_taxon_namespace] = True elif taxon_namespaces is not None: if self.suppress_unreferenced_taxon_namespaces: # preload to preserve order for tns in taxon_namespaces: candidate_taxon_namespaces[tns] = False else: for tns in taxon_namespaces: candidate_taxon_namespaces[tns] = True for data_collection in (tree_lists, char_matrices): if data_collection is not None: for i in data_collection: if self.attached_taxon_namespace is None or i.taxon_namespace is self.attached_taxon_namespace: candidate_taxon_namespaces[i.taxon_namespace] = True self._taxon_namespaces_to_write = [ tns for tns in candidate_taxon_namespaces if candidate_taxon_namespaces[tns] ] for tns in self._taxon_namespaces_to_write: self._write_taxon_namespace(tns, body) if char_matrices: for char_matrix in char_matrices: self._write_char_matrix(char_matrix=char_matrix, dest=body) if tree_lists: for tree_list in tree_lists: self._write_tree_list(tree_list=tree_list, dest=body) self._write_to_nexml_open(stream, indent_level=0) stream.write(body.getvalue()) self._write_to_nexml_close(stream, indent_level=0)