# Path: linux_story/commands_real.py
# def run_executable(real_path, line):
#     '''
#     Runs the executable.
# 
#     Args:
#         real_path (str): the current location of the user
#         in the pseudo-terminal.
#         line (str): the line the user entered.
#     '''
# 
#     line = line.strip()
# 
#     if line.startswith("./"):
#         line = line[2:]
# 
#     p = subprocess.Popen(["sh", line], cwd=real_path)
#     stdout, stderr = p.communicate()
# 
#     # notifying the SoundManager about the script that was just run
#     script_name = line.split()[0]
#     sounds_manager.on_command_run([script_name])
# 
#     if stdout:
#         print stdout.strip()
# 
#     if stderr:
#         print stderr.strip()
# 
# Path: linux_story/common.py
# def get_max_challenge_number():
# def get_username():
# def get_story_file(name):
# 
# Path: linux_story/helper_functions.py
# def get_script_cmd(string, real_path):
#     """
#     Checks whether the path (from the user's point of view)
#     is an executable.
# 
#     We convert the path entered into the real path (i.e. including the
#     .linux-story) and check it's a file and and executable.
# 
#     Args:
#         string (str): command that the user has typed.
#         real_path (str): the path that the user is currently based at.
# 
#     Returns:
#         tuple = (bool, str):
#             The first argument says if the script is a valid executable.
#             The second argument gives the full filepath of the executable.
#     """
# 
#     is_script = False
# 
#     if string.startswith("./"):
#         string = string[2:]
#         script = os.path.join(real_path, string)
# 
#     elif string.startswith("/"):
#         script = string
# 
#     else:
#         script = os.path.join(real_path, string)
# 
#     # directories are executable, so exclude directories
#     if os.path.exists(script) and not os.path.isdir(script):
#         is_script = is_exe(script)
# 
#     return is_script, script
# 
# def is_exe(fpath):
#     return (os.path.isfile(fpath) and os.access(fpath, os.X_OK))
# 
# def colour_string_with_preset(string, colour_name="white", input_fn=True):
#     """
#     Args:
#         string (str): the string we want to colourise.
#         colour_name (str): takes the values "yellow", "white", "blue", "green"
#         input_fn (bool): determines which colourise function we use.
# 
#     Returns:
#         str: string which will appear with specified colour in a terminal.
#     """
# 
#     colours = {
#         "yellow": 226,
#         "white": 231,
#         "blue": 69,
#         "green": 118
#     }
#     if input_fn:
#         colour_fn = colourize_input256
#     else:
#         colour_fn = colourize256
# 
#     colour_id = colours[colour_name]
#     bold = True
# 
#     # For now, if colour is white, don't make it bold.
#     if colour_name == "white":
#         bold = False
# 
#     return colour_fn(string, colour_id, None, bold)


import os
import readline
            import readline
from cmd import Cmd
from linux_story.commands_real import run_executable
from linux_story.common import fake_home_dir, get_username
from kano.logging import logger
from linux_story.helper_functions import get_script_cmd, is_exe, colour_string_with_preset
    # Public methods
    def emptyline(self):
        """To overwrite default behaviour in the cmd module.
        Do nothing if the user enters an empty line.
        """
        pass

    def do_shell(self, line):
        """This is run by default if the line starts with !.
        """
        # We use this over os.system so that the command is run in the
        # correct directory
        run_executable(self._location.get_real_path(), line)

    def precmd(self, line):
        """Hook before the command is run
        If the self.block_command returns True, the command is not run
        Otherwise, it is run
        """

        line = line.strip()
        self._step.set_last_user_input(line)
        if self._step.block_command(line):
            self._set_command_blocked(True)
            return Cmd.precmd(self, "")
        else:
            self._set_command_blocked(False)
            return Cmd.precmd(self, line)

    def onecmd(self, line):

# Path: linux_story/commands_real.py
# def run_executable(real_path, line):
#     '''
#     Runs the executable.
# 
#     Args:
#         real_path (str): the current location of the user
#         in the pseudo-terminal.
#         line (str): the line the user entered.
#     '''
# 
#     line = line.strip()
# 
#     if line.startswith("./"):
#         line = line[2:]
# 
#     p = subprocess.Popen(["sh", line], cwd=real_path)
#     stdout, stderr = p.communicate()
# 
#     # notifying the SoundManager about the script that was just run
#     script_name = line.split()[0]
#     sounds_manager.on_command_run([script_name])
# 
#     if stdout:
#         print stdout.strip()
# 
#     if stderr:
#         print stderr.strip()
# 
# Path: linux_story/common.py
# def get_max_challenge_number():
# def get_username():
# def get_story_file(name):
# 
# Path: linux_story/helper_functions.py
# def get_script_cmd(string, real_path):
#     """
#     Checks whether the path (from the user's point of view)
#     is an executable.
# 
#     We convert the path entered into the real path (i.e. including the
#     .linux-story) and check it's a file and and executable.
# 
#     Args:
#         string (str): command that the user has typed.
#         real_path (str): the path that the user is currently based at.
# 
#     Returns:
#         tuple = (bool, str):
#             The first argument says if the script is a valid executable.
#             The second argument gives the full filepath of the executable.
#     """
# 
#     is_script = False
# 
#     if string.startswith("./"):
#         string = string[2:]
#         script = os.path.join(real_path, string)
# 
#     elif string.startswith("/"):
#         script = string
# 
#     else:
#         script = os.path.join(real_path, string)
# 
#     # directories are executable, so exclude directories
#     if os.path.exists(script) and not os.path.isdir(script):
#         is_script = is_exe(script)
# 
#     return is_script, script
# 
# def is_exe(fpath):
#     return (os.path.isfile(fpath) and os.access(fpath, os.X_OK))
# 
# def colour_string_with_preset(string, colour_name="white", input_fn=True):
#     """
#     Args:
#         string (str): the string we want to colourise.
#         colour_name (str): takes the values "yellow", "white", "blue", "green"
#         input_fn (bool): determines which colourise function we use.
# 
#     Returns:
#         str: string which will appear with specified colour in a terminal.
#     """
# 
#     colours = {
#         "yellow": 226,
#         "white": 231,
#         "blue": 69,
#         "green": 118
#     }
#     if input_fn:
#         colour_fn = colourize_input256
#     else:
#         colour_fn = colourize256
# 
#     colour_id = colours[colour_name]
#     bold = True
# 
#     # For now, if colour is white, don't make it bold.
#     if colour_name == "white":
#         bold = False
# 
#     return colour_fn(string, colour_id, None, bold)


# Path: linux_story/KanoCmd.py
import os
import readline
            import readline
from cmd import Cmd
from linux_story.commands_real import run_executable
from linux_story.common import fake_home_dir, get_username
from kano.logging import logger
from linux_story.helper_functions import get_script_cmd, is_exe, colour_string_with_preset

    # Public methods
    def emptyline(self):
        """To overwrite default behaviour in the cmd module.
        Do nothing if the user enters an empty line.
        """
        pass

    def do_shell(self, line):
        """This is run by default if the line starts with !.
        """
        # We use this over os.system so that the command is run in the
        # correct directory
        run_executable(self._location.get_real_path(), line)

    def precmd(self, line):
        """Hook before the command is run
        If the self.block_command returns True, the command is not run
        Otherwise, it is run
        """

        line = line.strip()
        self._step.set_last_user_input(line)
        if self._step.block_command(line):
            self._set_command_blocked(True)
            return Cmd.precmd(self, "")
        else:
            self._set_command_blocked(False)
            return Cmd.precmd(self, line)

    def onecmd(self, line):

        is_script, script = get_script_cmd(line,self._location.get_real_path())