change structure

This commit is contained in:
chrys87 2016-07-08 11:34:56 +02:00 committed by GitHub
parent 71f4342367
commit 13d9252c23

View File

@ -7,52 +7,52 @@ import time
#import fenrir.utils.debug #import fenrir.utils.debug
class screenManager(): class screenManager():
def __init__(self, device='/dev/vcsa'): def __init__(self, device='/dev/vcsa'):
self.vcsaDevice = device self.vcsaDevicePath = device
def analyzeScreen(self, runtime): def analyzeScreen(self, environment):
# read screen # read screen
currTTY = open('/sys/devices/virtual/tty/tty0/active','r') currTTY = open('/sys/devices/virtual/tty/tty0/active','r')
runtime['newTTY'] = currTTY.read()[3:-1] runtime['newTTY'] = currTTY.read()[3:-1]
currTTY.close() currTTY.close()
try: try:
vcsa = open(self.vcsaDevice + runtime['newTTY'] ,'rb',0) vcsa = open(self.vcsaDevicePath + environment['screenData']['newTTY'] ,'rb',0)
runtime['newContentBytes'] = vcsa.read() environment['screenData']['newContentBytes'] = vcsa.read()
vcsa.close() vcsa.close()
except: except:
return runtime return runtime
# get metadata like cursor or screensize # get metadata like cursor or screensize
runtime['lines'] = int( runtime['newContentBytes'][0]) environment['screenData']['lines'] = int( environment['screenData']['newContentBytes'][0])
runtime['columns'] = int( runtime['newContentBytes'][1]) environment['screenData']['columns'] = int( environment['screenData']['newContentBytes'][1])
runtime['newCursor']['x'] = int( runtime['newContentBytes'][2]) environment['screenData']['newCursor']['x'] = int( environment['screenData']['newContentBytes'][2])
runtime['newCursor']['y'] = int( runtime['newContentBytes'][3]) environment['screenData']['newCursor']['y'] = int( environment['screenData']['newContentBytes'][3])
# analyze content # analyze content
runtime['newContentText'] = str(runtime['newContentBytes'][4:][::2].decode('cp1252').encode('utf-8'))[2:] environment['screenData']['newContentText'] = str(environment['screenData']['newContentBytes'][4:][::2].decode('cp1252').encode('utf-8'))[2:]
runtime['newContentAttrib'] = runtime['newContentBytes'][5:][::2] environment['screenData']['newContentAttrib'] = environment['screenData']['newContentBytes'][5:][::2]
runtime['newContentText'] = '\n'.join(textwrap.wrap(runtime['newContentText'], runtime['columns']))[:-2] environment['screenData']['newContentText'] = '\n'.join(textwrap.wrap(environment['screenData']['newContentText'], environment['screenData']['columns']))[:-2]
if runtime['newTTY'] != runtime['oldTTY']: if environment['screenData']['newTTY'] != environment['screenData']['oldTTY']:
runtime['oldContentBytes'] = b'' environment['screenData']['oldContentBytes'] = b''
runtime['oldContentAttrib'] = b'' environment['screenData']['oldContentAttrib'] = b''
runtime['oldContentText'] = '' environment['screenData']['oldContentText'] = ''
runtime['oldCursor']['x'] = 0 environment['screenData']['oldCursor']['x'] = 0
runtime['oldCursor']['y'] = 0 environment['screenData']['oldCursor']['y'] = 0
# changes on the screen # changes on the screen
if runtime['oldContentBytes'] != runtime['newContentBytes']: if environment['screenData']['oldContentBytes'] != environment['screenData']['newContentBytes']:
if ((len(runtime['delta']) < 4) or runtime['oldTTY'] != runtime['newTTY']): if ((len(environment['screenData']['delta']) < 4) or environment['screenData']['oldTTY'] != environment['screenData']['newTTY']):
runtime['speechDriver'].cancel() environment['runtime']['speechDriver'].cancel()
diff = difflib.ndiff(runtime['oldContentText'], runtime['newContentText']) diff = difflib.ndiff(runtime['oldContentText'], environment['screenData']['newContentText'])
runtime['delta'] = ''.join(x[2:] for x in diff if x.startswith('+ ')) environment['screenData']['delta'] = ''.join(x[2:] for x in diff if x.startswith('+ '))
runtime['speechDriver'].speak(runtime['delta']) environment['runtime']['speechDriver'].speak(environment['screenData']['delta'])
# set new "old" values # set new "old" values
runtime['oldContentBytes'] = runtime['newContentBytes'] environment['screenData']['oldContentBytes'] = environment['screenData']['newContentBytes']
runtime['oldContentText'] = runtime['newContentText'] environment['screenData']['oldContentText'] = environment['screenData']['newContentText']
runtime['oldContentTextAttrib'] = runtime['newContentAttrib'] environment['screenData']['oldContentTextAttrib'] = environment['screenData']['newContentAttrib']
runtime['oldCursor']['x'] = runtime['newCursor']['x'] environment['screenData']['oldCursor']['x'] = environment['screenData']['newCursor']['x']
runtime['oldCursor']['y'] = runtime['newCursor']['y'] environment['screenData']['oldCursor']['y'] = environment['screenData']['newCursor']['y']
runtime['oldTTY'] = runtime['newTTY'] environment['screenData']['oldTTY'] = environment['screenData']['newTTY']
return runtime return environment