예제 #1
0
def execute_command(command):
    """Execute a known command (cd, dir, exit or cls)

    Args:
        command (string): command-argument pair. E.g "cd path/to/file" or "dir"
    """
    if command.lower() == "exit":
        exit_shell()

    elif command.lower() == "cls":
        clear_screen()

    # The cd command can be written as one word when using cd.., cd\ or only cd
    elif command.lower().startswith(
        ("cd ", "cd.", "cd\\")) or command.lower() == "cd":
        # Path starts from index 2 (after "cd")
        path = command[2:]
        cd(path.strip())

    # The dir command can be written as one word when using dir.., dir\ or only dir
    elif command.lower().startswith(
        ("dir ", "dir.", "dir\\")) or command.lower() == "dir":
        # Path starts from index 3 (after "dir")
        path = command[3:]
        dir_cmd(path.strip())

    else:
        raise UnknownCommand()
def test_command_cd_with_slash():
    home = Folder('home')
    working_dir = Folder('working_dir')
    working_dir = home
    Folder('folder1', home)
    Folder('folder2', home)
    answer = "cd folder1".split()
    working_dir = cd(answer, working_dir, home)
    assert working_dir.name == 'folder1'
    answer = "cd home/folder2".split()
    working_dir = cd(answer, working_dir, home)
    assert working_dir.name == 'folder2'
def test_command_cd_go_folder_up():
    home = Folder('home')
    working_dir = Folder('working_dir')
    working_dir = home
    folder1 = Folder('folder1', home)
    folder2 = Folder('folder2', home)
    folder3 = Folder('folder3', folder1)
    answer = "cd folder1".split()
    working_dir = cd(answer, working_dir, home)
    assert working_dir.name == 'folder1'
    answer = "cd folder3".split()
    working_dir = cd(answer, working_dir, home)
    assert working_dir.name == 'folder3'
    answer = "cd ..".split()
    working_dir = cd(answer, working_dir, home)
    assert working_dir.name == 'folder1'
예제 #4
0
파일: repl.py 프로젝트: igebus/pypm
 def do_cd(self, args):
     'Usage: cd GROUP\n' \
     'Changes current/working group to GROUP\n'
     args = parse(args)
     if len(args) != 1:
         print("[-] Command cd require exactly one argument\n")
     else:
         new_path = args[0]
         if new_path[0] != '/':
             new_path = self.path + new_path
         result = commands.cd(new_path)
         if result is None:
             print("[-] Invalid path, at least one node is entry or does not exist\n")
         else:
             self.path = result
             self.gen_prompt()
def test_command_cd_incorrect_folder():
    home = Folder('home')
    working_dir = Folder('working_dir')
    working_dir = home
    assert cd("cd a", working_dir, home) is None
예제 #6
0
def main():
    home = Folder('home')
    working_dir = Folder('working_dir')
    working_dir = home
    answer = True
    starting_msg = "File system is working. Type help if needed or exit to end program."
    print(starting_msg)
    while answer:
        try:
            path = get_path(working_dir, home)

            answer = input(path).split()

            if answer[0] == "cd":
                new_working_dir = cd(answer, working_dir, home)
                if new_working_dir:
                    working_dir = new_working_dir
                else:
                    print("Incorrect usage of: cd. Try again")
            elif answer[0] == 'mkdir':
                # make folder
                try:
                    Folder(answer[1], working_dir)
                except Exception:
                    print("Incorrect usage of: mkdir. Try again")
            elif answer[0] == 'mk':
                # make file
                try:
                    File(working_dir, answer[1], answer[2], answer[3])
                except Exception:
                    print("Incorrect usage of: mk. Try again")
            elif answer[0] == 'rm':
                # delete file or folder
                if len(answer) == 2:
                    to_be_deleted = working_dir.find(answer[1])
                    if to_be_deleted:
                        print(rm(to_be_deleted, working_dir, home))
                    else:
                        print(f"Error. {answer[1]} - Element does not exist")
                else:
                    print("Incorrect usage of: rm. Try again")
            elif answer[0] == 'ls':
                # print structure
                print(ls(working_dir, answer))
            elif answer[0] == 'cat':
                # print file/folder info
                print(cat(working_dir, answer[1]))
            elif answer[0] == 'size':
                # print folder size
                try:
                    size = working_dir.find(answer[1]).count_size_recursive()
                    print(f'Size: {size} kB')
                except AttributeError:
                    print("Incorrect usage of: size. Try again")
            elif answer[0] == 'wc':
                # count elements in folder
                try:
                    print(wc(working_dir, answer))
                except Exception:
                    print("Incorrect usage of: wc. Try again")
            elif answer[0] == 'pwd':
                print(working_dir.name)
            elif answer[0] == 'cp':
                try:
                    cp(working_dir, answer, home)
                except Exception:
                    print("Incorrect usage of: cp. Try again")
            elif answer[0] == 'mv':
                try:
                    mv(working_dir, answer, home)
                except Exception:
                    print("Incorrect usage of: mv. Try again")
            elif answer[0] == 'help':
                print(help())
            elif answer[0] == 'exit':
                # end program
                answer = False
            else:
                print("Command not found")
        except IndexError:
            print("No command was given. Try again.")
            main()
예제 #7
0
def on_cd(bot, update):
    path = update.message.text[3:].strip()
    user = update.message.from_user['username']
    bot.sendMessage(update.message.chat_id,
                    text='<pre>%s</pre>' % cd(user, path),
                    parse_mode='HTML')