#!/usr/bin/env python3
"""
Parse a given tabular file and delete a given node
from the the tree

Usage:
  fastsubtrees-delete-subtree [options] <nodeid> <tree>

Arguments:
  nodeid      ID of the node that has to be deleted
  tree        File containing the tree data

Options:
  --attrs FN   file containing a list of filenames of attribute files that
               exist for the tree (so that they are updated too)
  --quiet      disable log messages
  --debug      print debug information
  --help       show this help message and exit
  --version    show program's version number and exit
"""

from docopt import docopt
from fastsubtrees import Tree, logger, _scripts_support

def main(args):
  logger.debug("Loading tree from file '{}'".format(args['<tree>']))
  tree = Tree.from_file(args["<tree>"])
  logger.debug("Successfully loaded tree from file '{}'".format(args['<tree>']))
  attrfiles = []
  if args['--attrs']:
    with open(args['--attrs'], 'r') as file:
      attrfiles = file.read().splitlines()
  tree.delete_node(int(args["<nodeid>"]), attrfiles)
  logger.success("Node deleted successfully")
  tree.to_file(args["<tree>"])

if __name__ == "__main__":
  args = docopt(__doc__, version="0.1")
  _scripts_support.setup_verbosity(args)
  main(args)
