#!python
"""
Add new values to files for a given attribute present in the database

Usage:
  fastsubtrees-attr-add [options] <tree> <attribute>
                                  <attrmod> [<attrmod_data>...]

Arguments:
  tree          fastsubtrees tree representation file,
                e.g. output by fastsubtrees-construct
  attribute     name of the attribute
  attrmod       Python module defining a function attribute_values()
                which may take arguments (<attrmod_data>) and returns pairs
                (element_id, attribute_value) for each node to which an
                attribute value shall be added.
  attrmod_data  [optional] arguments to be passed to the attribute_values()
                function of the module specified as <attrmod>; to pass keyword
                arguments, use the syntax "key=value" and the option --keyargs

Options:
  --replace      remove existing values of the attribute for the nodes
                 (default: values are added to the existing ones)
  --strict       error if a node does not exist
                 (default: ignore values for nodes which do not exist)
  --keyargs      split the arguments specified in <idsmod_data> into
                 keywords and values by splitting on the first instance of '=';
                 arguments which do not contain '=' are passed as positional,
                 before any keyword argument
  --quiet        disable log messages
  --debug        print debug information
  --help         show this help message and exit
  --version      show program's version number and exit
  --datatype DT  function to apply to the attribute values before writing
                 them to the output file; <DT> must be a valid Python function;
                 the function can optionally be defined in the module specified
                 by <attrmod> [default: str]
"""

from docopt import docopt
from fastsubtrees import _scripts_support, logger, Tree, attribute, VERSION
from collections import defaultdict
from pathlib import Path

def read_attribute_values(m, posargs, keyargs, cast):
  result = defaultdict(list)
  for k, v in m.attribute_values(*posargs, **keyargs):
    result[int(k)].append(cast(v))
  return result

def write_attribute_file(args, attrvalues):
  logger.debug("Loading tree from file '{}'".format(args['<tree>']))
  tree = Tree.from_file(args["<tree>"])
  outfname = attribute.attrfilename(args["<tree>"], args["<attribute>"])
  logger.debug("Reading existing attribute values from file '{}'".\
      format(outfname))
  existing = attribute.read_attribute_values(tree, outfname)
  for k in attrvalues:
    if k not in existing:
      if args["--strict"]:
        raise ValueError("Node {} does not exist".format(k))
      else:
        continue
    elif args["--replace"] or existing[k] is None:
      existing[k] = attrvalues[k]
    else:
      existing[k].extend(attrvalues[k])
  logger.debug("Writing attribute values to file '{}'...".format(outfname))
  with open(outfname, "w") as outfile:
    attribute.write_attribute_values(tree, existing, outfile)
  logger.success("Attribute values successfully written to file '{}'".\
      format(outfname))

def main(args):
  m = _scripts_support.get_module(args["<attrmod>"], "attribute_values")
  posargs, keyargs = _scripts_support.get_fn_args(\
                          args["--keyargs"], args["<attrmod_data>"])
  cast = _scripts_support.get_datatype_casting_fn(args["--datatype"], m, \
                                                  args["<attrmod>"])
  attrvalues = read_attribute_values(m, posargs, keyargs, cast)
  write_attribute_file(args, attrvalues)

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