2016-07-07 13:43:31 -04:00
|
|
|
#!/bin/python
|
2016-07-08 19:01:00 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-07-07 13:43:31 -04:00
|
|
|
|
|
|
|
import difflib
|
|
|
|
import textwrap
|
|
|
|
import time
|
2016-07-20 07:16:32 -04:00
|
|
|
import re
|
2016-07-07 13:43:31 -04:00
|
|
|
|
|
|
|
#import fenrir.utils.debug
|
2016-07-12 17:09:11 -04:00
|
|
|
class screen():
|
2016-07-07 13:43:31 -04:00
|
|
|
def __init__(self, device='/dev/vcsa'):
|
2016-07-08 05:34:56 -04:00
|
|
|
self.vcsaDevicePath = device
|
2016-07-10 17:02:17 -04:00
|
|
|
self.textWrapper = textwrap.TextWrapper()
|
|
|
|
self.textWrapper.drop_whitespace = False
|
2016-07-15 10:31:19 -04:00
|
|
|
def analyzeScreen(self, environment, trigger='updateScreen'):
|
2016-07-18 13:42:49 -04:00
|
|
|
newTTY = ''
|
|
|
|
newContentBytes = b''
|
2016-07-07 13:43:31 -04:00
|
|
|
try:
|
2016-07-18 13:42:49 -04:00
|
|
|
# read screen
|
|
|
|
currTTY = open('/sys/devices/virtual/tty/tty0/active','r')
|
|
|
|
newTTY = currTTY.read()[3:-1]
|
|
|
|
currTTY.close()
|
2016-07-15 10:31:19 -04:00
|
|
|
vcsa = open(self.vcsaDevicePath + newTTY,'rb',0)
|
|
|
|
newContentBytes = vcsa.read()
|
2016-07-07 13:43:31 -04:00
|
|
|
vcsa.close()
|
2016-07-15 10:31:19 -04:00
|
|
|
if len(newContentBytes) < 5:
|
2016-07-10 12:34:44 -04:00
|
|
|
return environment
|
2016-07-07 13:43:31 -04:00
|
|
|
except:
|
2016-07-08 12:33:32 -04:00
|
|
|
return environment
|
2016-07-18 13:42:49 -04:00
|
|
|
|
2016-07-16 18:56:18 -04:00
|
|
|
# set new "old" values
|
|
|
|
environment['screenData']['oldContentBytes'] = environment['screenData']['newContentBytes']
|
|
|
|
environment['screenData']['oldContentText'] = environment['screenData']['newContentText']
|
|
|
|
environment['screenData']['oldContentTextAttrib'] = environment['screenData']['newContentAttrib']
|
|
|
|
environment['screenData']['oldCursor']['x'] = environment['screenData']['newCursor']['x']
|
|
|
|
environment['screenData']['oldCursor']['y'] = environment['screenData']['newCursor']['y']
|
|
|
|
environment['screenData']['oldTTY'] = environment['screenData']['newTTY']
|
|
|
|
environment['screenData']['oldDelta'] = environment['screenData']['newDelta']
|
2016-08-02 20:26:44 -04:00
|
|
|
environment['screenData']['oldNegativeDelta'] = environment['screenData']['newNegativeDelta']
|
2016-07-15 10:31:19 -04:00
|
|
|
environment['screenData']['newTTY'] = newTTY
|
|
|
|
environment['screenData']['newContentBytes'] = newContentBytes
|
2016-07-07 13:43:31 -04:00
|
|
|
# get metadata like cursor or screensize
|
2016-07-08 05:34:56 -04:00
|
|
|
environment['screenData']['lines'] = int( environment['screenData']['newContentBytes'][0])
|
|
|
|
environment['screenData']['columns'] = int( environment['screenData']['newContentBytes'][1])
|
|
|
|
environment['screenData']['newCursor']['x'] = int( environment['screenData']['newContentBytes'][2])
|
|
|
|
environment['screenData']['newCursor']['y'] = int( environment['screenData']['newContentBytes'][3])
|
2016-07-07 13:43:31 -04:00
|
|
|
# analyze content
|
2016-07-17 10:12:06 -04:00
|
|
|
environment['screenData']['newContentText'] = str(environment['screenData']['newContentBytes'][4:][::2].decode("ascii", "replace"))
|
2016-07-08 05:34:56 -04:00
|
|
|
environment['screenData']['newContentAttrib'] = environment['screenData']['newContentBytes'][5:][::2]
|
2016-07-15 10:43:19 -04:00
|
|
|
environment['screenData']['newContentText'] = '\n'.join(self.textWrapper.wrap(environment['screenData']['newContentText'], ))[:-2]
|
2016-07-17 08:01:55 -04:00
|
|
|
|
2016-07-08 05:34:56 -04:00
|
|
|
if environment['screenData']['newTTY'] != environment['screenData']['oldTTY']:
|
2016-07-10 17:02:17 -04:00
|
|
|
self.textWrapper.width = environment['screenData']['columns']
|
2016-07-08 05:34:56 -04:00
|
|
|
environment['screenData']['oldContentBytes'] = b''
|
|
|
|
environment['screenData']['oldContentAttrib'] = b''
|
|
|
|
environment['screenData']['oldContentText'] = ''
|
|
|
|
environment['screenData']['oldCursor']['x'] = 0
|
|
|
|
environment['screenData']['oldCursor']['y'] = 0
|
2016-07-12 17:09:11 -04:00
|
|
|
environment['screenData']['oldDelta'] = ''
|
|
|
|
environment['screenData']['newDelta'] = ''
|
2016-08-02 20:26:44 -04:00
|
|
|
environment['screenData']['oldNegativeDelta'] = ''
|
|
|
|
environment['screenData']['newNegativeDelta'] = ''
|
2016-07-19 04:29:28 -04:00
|
|
|
|
2016-07-07 13:43:31 -04:00
|
|
|
# changes on the screen
|
2016-07-10 12:34:44 -04:00
|
|
|
if (environment['screenData']['oldContentText'] != environment['screenData']['newContentText']) and \
|
2016-07-19 12:06:05 -04:00
|
|
|
(environment['screenData']['newContentText'] != '' ):
|
2016-07-19 04:29:28 -04:00
|
|
|
if environment['screenData']['oldContentText'] == '' and\
|
|
|
|
environment['screenData']['newContentText'] != '':
|
|
|
|
environment['screenData']['newDelta'] = environment['screenData']['newContentText']
|
|
|
|
else:
|
2016-07-19 07:37:51 -04:00
|
|
|
diffStart = 0
|
2016-08-05 22:34:37 -04:00
|
|
|
if environment['screenData']['oldCursor']['x'] != environment['screenData']['newCursor']['x'] and \
|
|
|
|
environment['screenData']['oldCursor']['y'] == environment['screenData']['newCursor']['y'] and \
|
|
|
|
environment['screenData']['newContentText'][:environment['screenData']['newCursor']['y']] == environment['screenData']['oldContentText'][:environment['screenData']['newCursor']['y']]:
|
|
|
|
diffStart = environment['screenData']['newCursor']['y'] * environment['screenData']['newCursor']['x'] + environment['screenData']['newCursor']['y']
|
|
|
|
diff = difflib.ndiff(environment['screenData']['oldContentText'][diffStart:],\
|
|
|
|
environment['screenData']['newContentText'][diffStart:])
|
|
|
|
else:
|
|
|
|
diff = difflib.ndiff( environment['screenData']['oldContentText'][diffStart:].splitlines(),\
|
|
|
|
environment['screenData']['newContentText'][diffStart:].splitlines())
|
|
|
|
|
|
|
|
diffList = list(diff)
|
|
|
|
environment['screenData']['newDelta'] = ''.join(x[2:] for x in diffList if x.startswith('+ '))
|
|
|
|
environment['screenData']['newNegativeDelta'] = ''.join(x[2:] for x in diffList if x.startswith('- '))
|
2016-07-12 17:09:11 -04:00
|
|
|
|
2016-07-08 05:34:56 -04:00
|
|
|
return environment
|