<commit_before># -*- coding:  UTF-8 -*-
__author__ = '<NAME>'
__mail__ = '<EMAIL>'
"""
created 2015/5/8 
WinSession is the basic class for telnet session, it provide the basic functions, as SendLine, Expect...
"""


import  os
from telnetlib import Telnet as spawn
import time
import re as sre
import traceback
reSessionClosed =sre.compile ('Connection closed by foreign host',sre.M)
class WinSession(spawn, object):
    match = None
    attrs={}
    sutname='SUT'
    logger= None
    loginstep=[]
    argvs=[]
    kwargvs={}
    seslog=None
    output=''
    Connect2SUTDone=False
    lastActionTime=None
    fInteractionMode=False
    InteractionBuffer=''
    InteractionMatch=''
    fExpecting=False
    fSending = False
    stdout=None
    def __del__(self):
        if self.seslog:
            try:
                output =self.expect(['.+'], 1)
                self.seslog.flush()
            except:
                pass
            if not self.seslog.closed:
                self.seslog.flush()
                self.close()

    def __init__(self,name,attrs={},logger=None, logpath=None):
        if logpath ==None:
            import os
            logpath = '.%s'%(os.path.sep)
        self.sutname=name
        self.attrs=attrs
        command = attrs.get('CMD')
        if not attrs.get('TIMEOUT'):
            self.attrs.update({'TIMEOUT':int(30)})
        else:
            self.attrs.update({'TIMEOUT':int(attrs.get('TIMEOUT'))})
        import os
        log = os.path.abspath(logpath)
        log= '%s%s%s'%(log,os.path.sep,'%s.log'%name)
        if self.attrs.get('LOGIN'):
            from common import csvstring2array
            self.loginstep= csvstring2array(self.attrs['LOGIN'])

        self.seslog = open(log, "wb")
        host=""
        port=23
        reHostOnly=  sre.compile('\s*telnet\s+([\d.\w\-_]+)\s*',sre.I)
        reHostPort = sre.compile('\s*telnet\s+([\d.\w]+)\s+(\d+)', sre.I )
        m1=sre.match(reHostOnly, command)
        m2=sre.match(reHostPort, command)
        if m1:
            host= m1.groups(1)[0]
        elif m2:
            host= m2.groups(1)[0]
            port= m2.groups(2)[0]

        spawn.__init__(self, str(host), port)
        self.set_debuglevel(0)
        self.Login2SUT()
        self.Connect2SUTDone=True

    def info(self,msg):
        if not self.logger:
            print(self.sutname,':INFO:%s'%time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),msg)
            return
        for line in str(msg).split("\n"):
            ln = "%s::%s"%(self.sutname,line)
            print(ln)
            self.logger.info(ln)
    def debug(self,msg):
        if not self.logger:<commit_msg>while in interaction mode, off all info printed out<commit_after># -*- coding:  UTF-8 -*-
__author__ = '<NAME>'
__mail__ = '<EMAIL>'
"""
created 2015/5/8 
WinSession is the basic class for telnet session, it provide the basic functions, as SendLine, Expect...
"""


import  os
from telnetlib import Telnet as spawn
import time
import re as sre
import traceback
reSessionClosed =sre.compile ('Connection closed by foreign host',sre.M)
class WinSession(spawn, object):
    match = None
    attrs={}
    sutname='SUT'
    logger= None
    loginstep=[]
    argvs=[]
    kwargvs={}
    seslog=None
    output=''
    Connect2SUTDone=False
    lastActionTime=None
    fInteractionMode=False
    InteractionBuffer=''
    InteractionMatch=''
    fExpecting=False
    fSending = False
    stdout=None
    def __del__(self):
        if self.seslog:
            try:
                output =self.expect(['.+'], 1)
                self.seslog.flush()
            except:
                pass
            if not self.seslog.closed:
                self.seslog.flush()
                self.close()

    def __init__(self,name,attrs={},logger=None, logpath=None):
        if logpath ==None:
            import os
            logpath = '.%s'%(os.path.sep)
        self.sutname=name
        self.attrs=attrs
        command = attrs.get('CMD')
        if not attrs.get('TIMEOUT'):
            self.attrs.update({'TIMEOUT':int(30)})
        else:
            self.attrs.update({'TIMEOUT':int(attrs.get('TIMEOUT'))})
        import os
        log = os.path.abspath(logpath)
        log= '%s%s%s'%(log,os.path.sep,'%s.log'%name)
        if self.attrs.get('LOGIN'):
            from common import csvstring2array
            self.loginstep= csvstring2array(self.attrs['LOGIN'])

        self.seslog = open(log, "wb")
        host=""
        port=23
        reHostOnly=  sre.compile('\s*telnet\s+([\d.\w\-_]+)\s*',sre.I)
        reHostPort = sre.compile('\s*telnet\s+([\d.\w]+)\s+(\d+)', sre.I )
        m1=sre.match(reHostOnly, command)
        m2=sre.match(reHostPort, command)
        if m1:
            host= m1.groups(1)[0]
        elif m2:
            host= m2.groups(1)[0]
            port= m2.groups(2)[0]

        spawn.__init__(self, str(host), port)
        self.set_debuglevel(0)
        self.Login2SUT()
        self.Connect2SUTDone=True

    def info(self,msg):
        if not self.logger:
            if self.fInteractionMode:#while in interaction mode, off all info
                pass
            else:
                print(self.sutname,':INFO:%s'%time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),msg)
            return
        for line in str(msg).split("\n"):
            ln = "%s::%s"%(self.sutname,line)
            print(ln)
            self.logger.info(ln)
    def debug(self,msg):
        if not self.logger:
{'fraction_non_alphanumeric': 0.14585998903308353, 'fraction_numerical': 0.011332480350941327, 'mean_word_length': 2.0501672240802677, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5785748', 'n_tokens_mistral': 1850, 'n_tokens_neox': 1802, 'n_words': 363}