Ejemplo n.º 1
0
    def __init__(self,
                 connection,
                 prompt,
                 expected_prompt,
                 newline_chars=None,
                 runner=None,
                 set_timeout=None,
                 set_prompt=None,
                 target_newline="\n",
                 allowed_newline_after_prompt=False,
                 prompt_after_login=None):
        """
        Moler base class for commands that change prompt.

        :param connection: moler connection to device, terminal when command is executed.
        :param prompt: prompt on start system (where command starts).
        :param expected_prompt: prompt on server (where command connects).
        :param newline_chars: characters to split lines.
        :param runner: Runner to run command.
        :param set_timeout: Command to set timeout after telnet connects.
        :param set_prompt: Command to set prompt after telnet connects.
        :param target_newline: newline chars on remote system where ssh connects.
        :param allowed_newline_after_prompt: If True then newline chars may occur after expected (target) prompt.
        :param prompt_after_login: prompt after login before send export PS1. If you do not change prompt exporting PS1
         then leave it None.
        """
        super(CommandChangingPrompt,
              self).__init__(connection=connection,
                             prompt=prompt,
                             newline_chars=newline_chars,
                             runner=runner)

        # Parameters defined by calling the command
        self._re_expected_prompt = CommandTextualGeneric._calculate_prompt(
            expected_prompt)  # Expected prompt on device
        self._re_prompt_after_login = self._re_expected_prompt
        if prompt_after_login:
            self._re_prompt_after_login = CommandTextualGeneric._calculate_prompt(
                prompt_after_login)
        self.set_timeout = set_timeout
        self.set_prompt = set_prompt
        self.target_newline = target_newline
        self.allowed_newline_after_prompt = allowed_newline_after_prompt
        self.enter_on_prompt_without_anchors = True  # Set True to try to match prompt in line without ^ and $.

        # Internal variables
        self._re_failure_exceptions_indication = None
        self._sent_timeout = False
        self._sent_prompt = False
        self._sent = False
        self._finish_on_final_prompt = True  # Set True to finish Moler command by this generic after prompt after
        # command output. False if you want to finish command in your class.

        self._re_expected_prompt_without_anchors = regexp_without_anchors(
            self._re_expected_prompt)
        self._re_prompt_after_login_without_anchors = regexp_without_anchors(
            self._re_prompt_after_login)
Ejemplo n.º 2
0
    def __init__(self, connection, prompt=None, newline_chars=None, runner=None):
        """
        Base class for textual commands.

        :param connection: connection to device.
        :param prompt: expected prompt sending by device after command execution. Maybe String or compiled re.
        :param newline_chars:  new line chars on device (a list).
        :param runner: runner to run command.
        """
        self.command_path = None  # path to command executable - allow non standard locations like /usr/local/bin/
        self._max_index_from_beginning = 20  # Right (from 0 to this) index of substring of command_string passed
        # as _cmd_escaped. Set 0 to disable functionality of substring.
        self._max_index_from_end = 20  # Left (from this to the end) index of substring of command_string passed
        # as _cmd_escaped. Set 0 to disable functionality of substring.
        self._multiline_cmd = False
        self.__command_string = None  # String representing command on device
        self._cmd_escaped = None  # Escaped regular expression string with command
        super(CommandTextualGeneric, self).__init__(connection=connection, runner=runner)
        self.terminating_timeout = 3.0  # value for terminating command if it timeouts. Set positive value for command
        #                                 if they can do anything if timeout. Set 0 for command if it cannot do
        #                                 anything if timeout.
        self.current_ret = dict()  # Placeholder for result as-it-grows, before final write into self._result
        self._cmd_output_started = False  # If false parsing is not passed to command
        self._regex_helper = RegexHelper()  # Object to regular expression matching
        self.ret_required = True  # # Set False for commands not returning parsed result
        self.break_on_timeout = True  # If True then Ctrl+c on timeout
        self._last_not_full_line = None  # Part of line
        self._re_prompt = CommandTextualGeneric._calculate_prompt(prompt)  # Expected prompt on device
        self._newline_chars = newline_chars  # New line characters on device
        self.do_not_process_after_done = True  # Set True if you want to break processing data when command is done. If
        # False then on_new_line will be called after done if more lines are in the same data package.
        self.newline_after_command_string = True  # Set True if you want to send a new line char(s) after command
        # string (sendline from connection)- most cases. Set False if you want to sent command string without adding
        # new line char(s) - send from connection.
        self.wait_for_prompt_on_exception = True  # Set True to wait for command prompt on failure. Set False to cancel
        # command immediately on failure.
        self._concatenate_before_command_starts = True  # Set True to concatenate all strings from connection before
        # command starts, False to split lines on every new line char
        self._stored_exception = None  # Exception stored before it is passed to base class when command is done.
        self._lock_is_done = Lock()
        self._ignore_unicode_errors = True  # If True then UnicodeDecodeError will be logged not raised in data_received
        self._last_recv_time_data_read_from_connection = None  # Time moment when data was really received from
        # connection (not when was passed to command).  Time is given as datetime.datetime instance
        self._remove_ctrlc_chars_for_prompt = True  # after sending Ctrl-C response might be concatenated ^Cprompt
        # This flag removes "^C" from prompt before processing prompt against self._re_prompt
        self._break_exec_regex = None  # Regex if not None then command will call break_cmd when this regex is caught
        # in on_new_line. Do not set directly, use setter break_exec_regex.
        self.break_exec_only_full_line = True  # Set True to consider only full lines to match _break_exec_regex or
        # False to consider also chunks.
        self.enter_on_prompt_without_anchors = False  # Set True to try to match prompt in line without ^ and $.

        if not self._newline_chars:
            self._newline_chars = CommandTextualGeneric._default_newline_chars

        self._re_prompt_without_anchors = regexp_without_anchors(self._re_prompt)
Ejemplo n.º 3
0
def test_regexp_with_right_anchor():
    from moler.helpers import regexp_without_anchors
    expected = "abc"
    regex = re.compile(r"abc$")
    assert expected == regexp_without_anchors(regex).pattern
Ejemplo n.º 4
0
def test_regexp_without_anchors():
    from moler.helpers import regexp_without_anchors
    expected = "abc"
    assert expected == regexp_without_anchors(re.compile(expected)).pattern