# Path: linux_story/commands_fake.py
# def cd(real_path, line, has_access=True):
#     if not has_access:
#         # Could simplify this to "Permission denied"
#         print "-bash: cd: {}: Permission denied".format(line)
#         return
# 
#     if not line:
#         new_path = fake_home_dir
#     else:
#         if line.startswith('~'):
#             new_line = line.replace('~', fake_home_dir)
#             new_path = os.path.expanduser(new_line)
#         else:
#             new_path = os.path.join(real_path, line)
#             new_path = os.path.abspath(new_path)
# 
#         if not os.path.exists(new_path) or not os.path.isdir(new_path):
#             new_path = real_path
# 
#     if new_path[-1] == '/':
#         new_path = new_path[:-1]
#     return new_path
# 
# Path: linux_story/step_helper_functions.py
# def route_between_paths(start_path, end_path):
#     """
#     Args:
#         start_path (str)
#         end_path (str)
# 
#         Assume they are in the same form,
#         as absolute fake paths, e.g.
#         ~/town/.hidden-shelter and
#         ~/farm/barn/.shelter
# 
#     Return:
#         list of strings: listing every path you could hit on
#         a direct route from start_path to end_path.
#     """
#     common_path = find_common_parent(start_path, end_path)
# 
#     start_split = start_path.split(common_path)
#     if len(start_split) != 2:
#         raise Exception("common_path is not correct")
# 
#     end_split = end_path.split(common_path)
#     if len(end_split) != 2:
#         raise Exception("common_path is not correct")
# 
#     start_diff = start_split[1]
#     end_diff = end_split[1]
# 
#     start_dirs = filter(None, start_diff.split("/"))
#     end_dirs = filter(None, end_diff.split("/"))
# 
#     a_dest_path = start_path
#     dest_paths = []
# 
#     for d in start_dirs[::-1]:
#         a_dest_path = a_dest_path.replace(d, "")
# 
#         if a_dest_path.endswith("//"):
#             a_dest_path = a_dest_path[:-2]
#         if a_dest_path.endswith("/"):
#             a_dest_path = a_dest_path[:-1]
# 
#         a_dest_path = os.path.expanduser(a_dest_path)
#         dest_paths.append(a_dest_path)
# 
#     for d in end_dirs:
#         a_dest_path = os.path.join(a_dest_path, d)
#         a_dest_path = os.path.expanduser(a_dest_path)
#         dest_paths.append(a_dest_path)
# 
#     return dest_paths
# 
# Path: linux_story/story/terminals/terminal_cat.py
# class TerminalCat(TerminalLs):
#     terminal_commands = ["ls", "cat"]
# 
#     def do_cat(self, line):
#         shell_command(self._get_real_path(), line, "cat")
# 
#     def complete_cat(self, text, line, begidx, endidx):
#         return self._autocomplete_files(text, line, begidx, endidx)


import os
import stat
from linux_story.commands_fake import cd
from linux_story.step_helper_functions import route_between_paths
from linux_story.story.terminals.terminal_cat import TerminalCat

    def do_cd(self, line):
        if self.__check_cd(line):
            self._set_command_blocked(False)
            new_path = cd(self._location.get_real_path(), line)
            if not not_locked(new_path):
                self._set_command_blocked(True)
                print (
                    _("bash: cd: " + line + ": Permission denied")
                )
            elif new_path:
                self._location.set_real_path(new_path)
                self._set_prompt()
        else:
            self._set_command_blocked(True)
            print (_("Nice try! But you entered an unexpected destination path."))

    def complete_cd(self, text, line, begidx, endidx):
        try:
            return self._autocomplete_files(text, line, begidx, endidx, only_dirs=True)
        except Exception as e:
            print str(e)

    def __check_cd(self, line):
        """If returns True, that means that cd will bring the user closer
        to their destination, so cd should be allowed to run with the
        user's choice of path.
        """

        # Get the current list of the paths that we're allowed to go on

# Path: linux_story/commands_fake.py
# def cd(real_path, line, has_access=True):
#     if not has_access:
#         # Could simplify this to "Permission denied"
#         print "-bash: cd: {}: Permission denied".format(line)
#         return
# 
#     if not line:
#         new_path = fake_home_dir
#     else:
#         if line.startswith('~'):
#             new_line = line.replace('~', fake_home_dir)
#             new_path = os.path.expanduser(new_line)
#         else:
#             new_path = os.path.join(real_path, line)
#             new_path = os.path.abspath(new_path)
# 
#         if not os.path.exists(new_path) or not os.path.isdir(new_path):
#             new_path = real_path
# 
#     if new_path[-1] == '/':
#         new_path = new_path[:-1]
#     return new_path
# 
# Path: linux_story/step_helper_functions.py
# def route_between_paths(start_path, end_path):
#     """
#     Args:
#         start_path (str)
#         end_path (str)
# 
#         Assume they are in the same form,
#         as absolute fake paths, e.g.
#         ~/town/.hidden-shelter and
#         ~/farm/barn/.shelter
# 
#     Return:
#         list of strings: listing every path you could hit on
#         a direct route from start_path to end_path.
#     """
#     common_path = find_common_parent(start_path, end_path)
# 
#     start_split = start_path.split(common_path)
#     if len(start_split) != 2:
#         raise Exception("common_path is not correct")
# 
#     end_split = end_path.split(common_path)
#     if len(end_split) != 2:
#         raise Exception("common_path is not correct")
# 
#     start_diff = start_split[1]
#     end_diff = end_split[1]
# 
#     start_dirs = filter(None, start_diff.split("/"))
#     end_dirs = filter(None, end_diff.split("/"))
# 
#     a_dest_path = start_path
#     dest_paths = []
# 
#     for d in start_dirs[::-1]:
#         a_dest_path = a_dest_path.replace(d, "")
# 
#         if a_dest_path.endswith("//"):
#             a_dest_path = a_dest_path[:-2]
#         if a_dest_path.endswith("/"):
#             a_dest_path = a_dest_path[:-1]
# 
#         a_dest_path = os.path.expanduser(a_dest_path)
#         dest_paths.append(a_dest_path)
# 
#     for d in end_dirs:
#         a_dest_path = os.path.join(a_dest_path, d)
#         a_dest_path = os.path.expanduser(a_dest_path)
#         dest_paths.append(a_dest_path)
# 
#     return dest_paths
# 
# Path: linux_story/story/terminals/terminal_cat.py
# class TerminalCat(TerminalLs):
#     terminal_commands = ["ls", "cat"]
# 
#     def do_cat(self, line):
#         shell_command(self._get_real_path(), line, "cat")
# 
#     def complete_cat(self, text, line, begidx, endidx):
#         return self._autocomplete_files(text, line, begidx, endidx)


# Path: linux_story/story/terminals/terminal_cd.py
import os
import stat
from linux_story.commands_fake import cd
from linux_story.step_helper_functions import route_between_paths
from linux_story.story.terminals.terminal_cat import TerminalCat


    def do_cd(self, line):
        if self.__check_cd(line):
            self._set_command_blocked(False)
            new_path = cd(self._location.get_real_path(), line)
            if not not_locked(new_path):
                self._set_command_blocked(True)
                print (
                    _("bash: cd: " + line + ": Permission denied")
                )
            elif new_path:
                self._location.set_real_path(new_path)
                self._set_prompt()
        else:
            self._set_command_blocked(True)
            print (_("Nice try! But you entered an unexpected destination path."))

    def complete_cd(self, text, line, begidx, endidx):
        try:
            return self._autocomplete_files(text, line, begidx, endidx, only_dirs=True)
        except Exception as e:
            print str(e)

    def __check_cd(self, line):
        """If returns True, that means that cd will bring the user closer
        to their destination, so cd should be allowed to run with the
        user's choice of path.
        """

        # Get the current list of the paths that we're allowed to go on

        route = route_between_paths(self._location.get_fake_path(), self._location.get_end_dir())