fenrir/src/fenrir-package/screen/linux.py

80 lines
4.4 KiB
Python
Raw Normal View History

#!/bin/python
2016-07-08 19:01:00 -04:00
# -*- coding: utf-8 -*-
import difflib
import textwrap
import time
2016-07-20 07:16:32 -04:00
import re
#import fenrir.utils.debug
2016-07-12 17:09:11 -04:00
class screen():
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
def analyzeScreen(self, environment, trigger='updateScreen'):
2016-07-18 13:42:49 -04:00
newTTY = ''
newContentBytes = b''
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()
vcsa = open(self.vcsaDevicePath + newTTY,'rb',0)
newContentBytes = vcsa.read()
vcsa.close()
if len(newContentBytes) < 5:
2016-07-10 12:34:44 -04:00
return environment
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']
environment['screenData']['newTTY'] = newTTY
environment['screenData']['newContentBytes'] = newContentBytes
# 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])
# 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]
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-07-19 04:29:28 -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-18 15:05:01 -04:00
# diff = difflib.ndiff(environment['screenData']['oldContentText'], 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:
diffStart = 0
2016-07-19 17:35:29 -04:00
lastLine = len(environment['screenData']['newContentText']) - environment['screenData']['columns']
if environment['screenData']['newContentText'][:lastLine] == environment['screenData']['oldContentText'][:lastLine]:
diffStart = lastLine + 1
2016-07-19 17:35:29 -04:00
2016-07-20 07:16:32 -04:00
diff = difflib.ndiff(re.sub('[ \t]+', ' ', environment['screenData']['oldContentText'][diffStart:]),\
re.sub('[ \t]+', ' ', environment['screenData']['newContentText'][diffStart:]))
2016-07-19 04:29:28 -04:00
environment['screenData']['newDelta'] = ''.join(x[2:] for x in diff if x.startswith('+ '))
2016-07-12 17:09:11 -04:00
2016-07-08 05:34:56 -04:00
return environment