# Path: nexus/reader.py
# class NexusReader(object):
#     """A nexus reader"""
#     def __init__(self, filename=None, debug=False):
#         self.debug = debug
#         self.blocks = {}
#         self.rawblocks = {}
#         self.handlers = {
#             'data': DataHandler,
#             'characters': DataHandler,
#             'trees': TreeHandler,
#             'taxa': TaxaHandler,
#         }
#         if filename:
#             return self.read_file(filename)
#     
#     def _do_blocks(self):
#         """Iterates over all nexus blocks and parses them appropriately"""
#         for block, data in self.raw_blocks.items():
#             if block == 'characters':
#                 block = 'data' # override
#             self.blocks[block] = self.handlers.get(block, GenericHandler)()
#             self.blocks[block].parse(data)
#             setattr(self, block, self.blocks[block])
#     
#     def read_file(self, filename):
#         """
#         Loads and Parses a Nexus File
# 
#         :param filename: filename of a nexus file
#         :type filename: string
# 
#         :raises IOError: If file reading fails.
# 
#         :return: None
#         """
#         self.filename = filename
#         self.short_filename = os.path.split(filename)[1]
#         
#         if os.path.isfile(filename) == False:
#             raise IOError("Unable To Read File %s" % filename)
#         
#         if filename.endswith('.gz'):
#             import gzip
#             handle = gzip.open(filename, 'rb')
#         else:
#             handle = open(filename, 'rU')
#         self._read(handle)
#         handle.close()
#     
#     def read_string(self, contents):
#         """
#         Loads and Parses a Nexus from a string
# 
#         :param contents: string or string-like object containing a nexus to parse
#         :type contents: string
# 
#         :return: None
#         """
#         self.filename = "<String>"
#         self._read(io.StringIO(unicode(contents)))
#     
#     def _read(self, handle):
#         """Reads from a iterable object"""
#         store = {}
#         block = None
#         for line in handle.readlines():
#             line = line.strip()
#             if len(line) == 0:
#                 continue
#             elif line.startswith('[') and line.endswith(']'):
#                 continue
#             
#             # check if we're in a block and initialise
#             found = BEGIN_PATTERN.findall(line)
#             if found:
#                 block = found[0].lower()
#                 if block in store:
#                     raise Exception("Duplicate Block %s" % block)
#                 store[block] = []
#             
#             # check if we're ending a block
#             if END_PATTERN.search(line):
#                 block = None
#             
#             if block is not None:
#                 store[block].append(line)
#         self.raw_blocks = store
#         self._do_blocks()
#     
#     def write(self):
#         """
#         Generates a string containing a complete nexus from
#         all the data.
#         
#         :return: String
#         """
#         out = ["#NEXUS\n"]
#         for block in self.blocks:
#             out.append(self.blocks[block].write())
#         return "\n".join(out)
#     
#     def write_to_file(self, filename):
#         """
#         Writes the nexus to a file.
#         
#         :return: None
#         
#         :raises IOError: If file writing fails.
#         """
#         handle = open(filename, 'w')
#         handle.writelines(self.write())
#         handle.close()


from .reader import NexusReader
"""
Tools for writing a nexus file
"""

TEMPLATE = """
#NEXUS 
%(comments)s

# Path: nexus/reader.py
# class NexusReader(object):
#     """A nexus reader"""
#     def __init__(self, filename=None, debug=False):
#         self.debug = debug
#         self.blocks = {}
#         self.rawblocks = {}
#         self.handlers = {
#             'data': DataHandler,
#             'characters': DataHandler,
#             'trees': TreeHandler,
#             'taxa': TaxaHandler,
#         }
#         if filename:
#             return self.read_file(filename)
#     
#     def _do_blocks(self):
#         """Iterates over all nexus blocks and parses them appropriately"""
#         for block, data in self.raw_blocks.items():
#             if block == 'characters':
#                 block = 'data' # override
#             self.blocks[block] = self.handlers.get(block, GenericHandler)()
#             self.blocks[block].parse(data)
#             setattr(self, block, self.blocks[block])
#     
#     def read_file(self, filename):
#         """
#         Loads and Parses a Nexus File
# 
#         :param filename: filename of a nexus file
#         :type filename: string
# 
#         :raises IOError: If file reading fails.
# 
#         :return: None
#         """
#         self.filename = filename
#         self.short_filename = os.path.split(filename)[1]
#         
#         if os.path.isfile(filename) == False:
#             raise IOError("Unable To Read File %s" % filename)
#         
#         if filename.endswith('.gz'):
#             import gzip
#             handle = gzip.open(filename, 'rb')
#         else:
#             handle = open(filename, 'rU')
#         self._read(handle)
#         handle.close()
#     
#     def read_string(self, contents):
#         """
#         Loads and Parses a Nexus from a string
# 
#         :param contents: string or string-like object containing a nexus to parse
#         :type contents: string
# 
#         :return: None
#         """
#         self.filename = "<String>"
#         self._read(io.StringIO(unicode(contents)))
#     
#     def _read(self, handle):
#         """Reads from a iterable object"""
#         store = {}
#         block = None
#         for line in handle.readlines():
#             line = line.strip()
#             if len(line) == 0:
#                 continue
#             elif line.startswith('[') and line.endswith(']'):
#                 continue
#             
#             # check if we're in a block and initialise
#             found = BEGIN_PATTERN.findall(line)
#             if found:
#                 block = found[0].lower()
#                 if block in store:
#                     raise Exception("Duplicate Block %s" % block)
#                 store[block] = []
#             
#             # check if we're ending a block
#             if END_PATTERN.search(line):
#                 block = None
#             
#             if block is not None:
#                 store[block].append(line)
#         self.raw_blocks = store
#         self._do_blocks()
#     
#     def write(self):
#         """
#         Generates a string containing a complete nexus from
#         all the data.
#         
#         :return: String
#         """
#         out = ["#NEXUS\n"]
#         for block in self.blocks:
#             out.append(self.blocks[block].write())
#         return "\n".join(out)
#     
#     def write_to_file(self, filename):
#         """
#         Writes the nexus to a file.
#         
#         :return: None
#         
#         :raises IOError: If file writing fails.
#         """
#         handle = open(filename, 'w')
#         handle.writelines(self.write())
#         handle.close()


# Path: nexus/writer.py
from .reader import NexusReader

"""
Tools for writing a nexus file
"""

TEMPLATE = """
#NEXUS 
%(comments)s

BEGIN DATA;