# -*- coding: utf-8 -*-

from nimp.commands._command     import *
from nimp.utilities.ue3         import *
from nimp.utilities.file_mapper import *

#-------------------------------------------------------------------------------
class MapCommand(Command):
    def __init__(self):
        Command.__init__(self, 'fileset', 'Do stuff on a list of files')

    #---------------------------------------------------------------------------
    def configure_arguments(self, env, parser):
        parser.add_argument('--arg',
                            help    = 'Add a key/value pair for use in string interpolation',
                            metavar = '<KEY>=<VALUE>',
                            nargs   = 1,
                            action  = 'append',
                            default = [])

        parser.add_argument('set_name',
                            help    = 'Set name to load (e.g. binaries, version…)',
                            metavar = '<SET_FILE>')

        parser.add_argument('action',
                            help    = 'Action to execute on listed files (one of: robocopy, checkout, toc, reconcile, reconcile_and_submit, list)',
                            metavar = '<ACTION>',
                            choices = ['robocopy', 'checkout', 'toc', 'reconcile', 'reconcile_and_submit', 'list'])

        parser.add_argument('--src',
                            help    = 'Source directory',
                            metavar = '<DIR>')

        parser.add_argument('--to',
                            help    = 'Destination, if relevant',
                            metavar = '<DIR>',
                            default = None)

        return True

    #---------------------------------------------------------------------------
    def run(self, env):
        if env.arg is not None:
            for key, value in [x[0].split('=') for x in env.arg]:
                setattr(env, key, value)
        env.standardize_names()

        files = FileMapper(format_args = vars(env))
        files_chain = files
        if env.src is not None:
            files_chain = files_chain.src(env.src)

        if env.to is not None:
            files_chain = files_chain.to(env.to)

        files_chain.load_set(env.set_name)

        if env.action == 'robocopy':
            for source, destination in files():
                robocopy(source, destination)
        elif env.action == 'checkout':
            with p4_transaction('Checkout') as trans:
                map(trans.add, files)
        elif env.action == 'list':
            for source, destination in files():
                log_notification("{0} => {1}", source, destination)
        elif env.action == 'toc':
            log_error("Not implemented yet")

        return True

@@ -1101,16 +1101,24 @@
 obocopy,
+ delete,
  checkou
@@ -1123,13 +1123,8 @@
 out,
- toc,
  rec
@@ -1261,16 +1261,26 @@
 bocopy',
+ 'delete',
  'checko
@@ -1287,15 +1287,8 @@
 ut',
- 'toc',
  're
@@ -2378,32 +2378,33 @@
 e, destination)%0A
+%0A
         elif env
@@ -2518,16 +2518,187 @@
  files)%0A
+%0A        elif env.action == 'delete':%0A            for source, destination in files():%0A                if os.path.isfile(source):%0A                    force_delete(source)%0A%0A
         
@@ -2843,87 +2843,8 @@
 ion)
-%0A        elif env.action == 'toc':%0A            log_error(%22Not implemented yet%22)
 %0A%0A  
@@ -2841,28 +2841,29 @@
 ation)%0A%0A        return True%0A
+%0A

Add “delete” action for the fileset command.