<commit_before>#  Copyright 2008 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.


import telnetlib
import time
import re
from types import MethodType, StringTypes

from robot import utils


class Telnet:

    """A test library providing Telnet connections and communicating with them.

    This library takes the optional arguments `timeout`, `newline`,
    `prompt` and `prompt_is_regexp`. These are default values used
    when a new connection is opened with the keyword `Open Connection`,
    and they can be overridden. The purpose of these parameters can be
    found from the documentation of `Open Connection`.
    
    Examples (use only one of these):

    | *Setting* | *Value* | *Value* | *Value* | *Value* | *Value* | *Comment* |
    | Library | Telnet |     |    |     |    | # default values                |
    | Library | Telnet | 0.5 |    |     |    | # set only timeout              |
    | Library | Telnet |     | LF |     |    | # set only newline              |
    | Library | Telnet | 2.0 | LF |     |    | # set timeout and newline       |
    | Library | Telnet | 2.0 | CRLF | $ |    | # set also prompt               |
    | Library | Telnet | 2.0 | LF | ($|~) | True | # set prompt with simple regexp |
    """

    ROBOT_LIBRARY_SCOPE = 'TEST_SUITE'
    ROBOT_LIBRARY_VERSION = utils.get_version()

    def __init__(self, timeout=3.0, newline='CRLF', 
                 prompt=None, prompt_is_regexp=False):
        self._timeout = timeout == '' and 3.0 or timeout
        self._newline = newline == '' and 'CRLF' or newline
        self._prompt = (prompt, prompt_is_regexp)
        self._cache = utils.ConnectionCache()
        self._conn = None
        self._conn_kws = self._lib_kws = None
        
    def get_keyword_names(self):
        return self._get_library_keywords() + self._get_connection_keywords()

    def _get_library_keywords(self):
        if self._lib_kws is None:
            self._lib_kws = [ name for name in dir(self) 
                              if not name.startswith('_') and name != 'get_keyword_names' 
                              and type(getattr(self,name)) is MethodType ]
        return self._lib_kws
    
    def _get_connection_keywords(self):
        if self._conn_kws is None:
            conn = self._get_connection()
            excluded = [ name for name in dir(telnetlib.Telnet()) 
                         if name not in ['write', 'read', 'read_until'] ]
            self._conn_kws = [ name for name in dir(conn)
                               if not name.startswith('_') and name not in excluded
                               and type(getattr(conn,name)) is MethodType ]
        return self._conn_kws

    def __getattr__(self, name):
        if name not in self._get_connection_keywords():
            raise AttributeError(name)
        # If no connection is initialized, get attributes from a non-active 
        # connection. This makes it possible for Robot to create keyword 
        # handlers when it imports the library.
        conn = self._conn is None and self._get_connection() or self._conn
        return getattr(conn, name)

    def open_connection(self, host, alias=None, port=23, timeout=None, 
                        newline=None, prompt=None, prompt_is_regexp=False):
        """Opens a new Telnet connection to the given host and port.

        Possible already opened connections are cached.

        Returns the index of this connection, which can be used later
        to switch back to the connection. The index starts from 1 and
        is reset back to it when the `Close All Connections` keyword
        is used.

        The optional `alias` is a name for the connection, and it can
        be used for switching between connections, similarly as the
        index. See `Switch Connection` for more details about that.

        The `timeout`, `newline`, `prompt` and `prompt_is_regexp`
        arguments got default values when the library is taken into
        use, but setting them here overrides those values for the
        opened connection. They can also be set after opening the
        connection using the `Set Timeout`, `Set Newline` and `Set
        Prompt` keywords. See these keywords for more information
        about possible values for these arguments.
        """
        if timeout is None or timeout == '':<commit_msg>modified to use new libdoc<commit_after>#  Copyright 2008 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.


import telnetlib
import time
import re
from types import MethodType, StringTypes

from robot import utils


class Telnet:

    """A test library providing Telnet connections and communicating with them.
    """

    ROBOT_LIBRARY_SCOPE = 'TEST_SUITE'
    ROBOT_LIBRARY_VERSION = utils.get_version()

    def __init__(self, timeout=3.0, newline='CRLF', 
                 prompt=None, prompt_is_regexp=False):
        """This library takes the optional arguments `timeout`, `newline`,
        `prompt` and `prompt_is_regexp`. These are default values used when a
        new connection is opened with the keyword `Open Connection`, and they
        can be overridden. They can also be set after opening the connection
        using the `Set Timeout`, `Set Newline` and `Set Prompt` keywords. See
        these keywords for more information.

        Examples (use only one of these):

        | *Setting* | *Value* | *Value* | *Value* | *Value* | *Value* | *Comment* |
        | Library | Telnet |     |    |     |    | # default values                |
        | Library | Telnet | 0.5 |    |     |    | # set only timeout              |
        | Library | Telnet |     | LF |     |    | # set only newline              |
        | Library | Telnet | 2.0 | LF |     |    | # set timeout and newline       |
        | Library | Telnet | 2.0 | CRLF | $ |    | # set also prompt               |
        | Library | Telnet | 2.0 | LF | ($|~) | True | # set prompt with simple regexp |
        """
        self._timeout = timeout == '' and 3.0 or timeout
        self._newline = newline == '' and 'CRLF' or newline
        self._prompt = (prompt, prompt_is_regexp)
        self._cache = utils.ConnectionCache()
        self._conn = None
        self._conn_kws = self._lib_kws = None
        
    def get_keyword_names(self):
        return self._get_library_keywords() + self._get_connection_keywords()

    def _get_library_keywords(self):
        if self._lib_kws is None:
            self._lib_kws = [ name for name in dir(self) 
                              if not name.startswith('_') and name != 'get_keyword_names' 
                              and type(getattr(self,name)) is MethodType ]
        return self._lib_kws
    
    def _get_connection_keywords(self):
        if self._conn_kws is None:
            conn = self._get_connection()
            excluded = [ name for name in dir(telnetlib.Telnet()) 
                         if name not in ['write', 'read', 'read_until'] ]
            self._conn_kws = [ name for name in dir(conn)
                               if not name.startswith('_') and name not in excluded
                               and type(getattr(conn,name)) is MethodType ]
        return self._conn_kws

    def __getattr__(self, name):
        if name not in self._get_connection_keywords():
            raise AttributeError(name)
        # If no connection is initialized, get attributes from a non-active 
        # connection. This makes it possible for Robot to create keyword 
        # handlers when it imports the library.
        conn = self._conn is None and self._get_connection() or self._conn
        return getattr(conn, name)

    def open_connection(self, host, alias=None, port=23, timeout=None, 
                        newline=None, prompt=None, prompt_is_regexp=False):
        """Opens a new Telnet connection to the given host and port.

        Possible already opened connections are cached.

        Returns the index of this connection, which can be used later
        to switch back to the connection. The index starts from 1 and
        is reset back to it when the `Close All Connections` keyword
        is used.

        The optional `alias` is a name for the connection, and it can
        be used for switching between connections, similarly as the
        index. See `Switch Connection` for more details about that.

        The `timeout`, `newline`, `prompt` and `prompt_is_regexp` arguments get
        default values when the library is taken into use, but setting them
        here overrides those values for this connection. See `library
        initialization` for more information.
        """
        if timeout is None or timeout == '':
{'fraction_non_alphanumeric': 0.07809288943690916, 'fraction_numerical': 0.004726674886970818, 'mean_word_length': 2.281523937963587, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '218526', 'n_tokens_mistral': 2664, 'n_tokens_neox': 2546, 'n_words': 1025}