from plugin import plugin
import os
import shutil

@plugin("file manage")
class file_manage:
    def __call__(self,jarvis,s):
        self.get_file_directory(jarvis)
        self.get_cmd(jarvis)

        if self.cmd == "delete":
            self.delete(jarvis,self.file)
        elif self.cmd == "move":
            self.move(jarvis,self.file)

    def get_file_directory(self,jarvis):
        self.file = jarvis.input("Enter the directory of the file you would like to edit: ")

    def get_cmd(self,jarvis):
        # function to find command to be performed to file

        self.possibleCmds = ["delete","move","rename"]

        cmdValid = False
        while not cmdValid:
            # iterate through possible commands and say each
            jarvis.say("Commands Avaliable")

            i = 1
            for cmd in self.possibleCmds:
                jarvis.say(str(i) + ". " + cmd)
                i = i + 1

            self.cmd = jarvis.input("Enter command to be performed: ")

            # check if command is valid. If not, end cycle
            if self.cmd not in self.possibleCmds:
                jarvis.say("Invalid command")
            else:
                cmdValid = True

    def delete(self,jarvis,file):
        # function to delete files

        # first, check if file exists
        if os.path.exists(file):

            yes = True
            while yes:
                # confirm that file should be deleted
                confirmation = jarvis.input("Are you sure you want to delete this file? This cannot be undone. (y/n)").lower()

                if confirmation == "y":
                    # delete file
                    os.remove(file)

                    # break loop after removing file
                    yes = False

                elif confirmation == "n":

                    # break loop if no confirmation
                    yes = False
                else:
                    jarvis.say("Invalid input")

        else:
            jarvis.say("file does not exist")

    def move(self,jarvis,file):
        # function to move files

        path_invalid = True
        while path_invalid:
            # get destination
            dest = jarvis.input("Where would you like to move this file to? :")

            if os.path.exists(dest):
                # move from old location
                shutil.move(file,dest)
                path_invalid = False
            else:
                jarvis.say("Invalid path")
@@ -332,24 +332,101 @@
 s,self.file)
+%0A        elif self.cmd == %22rename%22:%0A            self.rename(jarvis,self.file)
 %0A%0A    def ge
@@ -2542,8 +2542,532 @@
 d path%22)
+%0A%0A    def rename(self,jarvis,file):%0A        # function to rename files%0A%0A        path_invalid = True%0A        while path_invalid:%0A            # get new name%0A            new_name = jarvis.input(%22What would you like to rename this file to? :%22)%0A%0A            # get root directory%0A            root = os.path.split(file)%5B0%5D%0A%0A            new_dir = os.path.join(root,new_name)%0A%0A            try:%0A                os.rename(file,new_dir)%0A                path_invalid = False%0A            except:%0A                jarvis.say(%22Invalid Path%22)

rename func