File: utils.py

package info (click to toggle)
pyqonsole 0.2.0-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 448 kB
  • ctags: 647
  • sloc: python: 4,383; ansic: 111; makefile: 52; sh: 2
file content (118 lines) | stat: -rw-r--r-- 3,512 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Copyright (c) 2005-2006 LOGILAB S.A. (Paris, FRANCE).
# Copyright (c) 2005-2006 CEA Grenoble 
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the CECILL license, available at
# http://www.inria.fr/valorisation/logiciels/Licence.CeCILL-V2.pdf
#
import weakref
import unittest

import qt
from pyqonsole import emuVt102, emulation

LOGGERS = []

def register_logger(l):
    LOGGERS.append(weakref.ref(l))
    
def reset_logs():
    for wr in LOGGERS[:]:
        logger = wr()
        if logger is None: # deleted
            LOGGERS.remove(wr)
            continue
        logger._logs = []
        
def logged(func, name):
    def wrapper(self, *args, **kwargs):
        record = (name,)
        if args:
            record += (args,)
        if kwargs:
            record += (kwargs,)
        try:
            self._logs.append(record)
        except AttributeError:
            self._logs = [record]
        return func(self, *args, **kwargs)
    return wrapper


class NullObject(object):
    def __init__(self, *args, **kwargs):
        self._logs = []
        LOGGERS.append(weakref.ref(self))
        #super(NullObject,self).__init__(*args, **kwargs)
        
    def __getattribute__(self, name):
        if name != '_logs':
            self._logs.append( ('getattr', name) )
        try:
            return super(NullObject, self).__getattribute__(name)
        except AttributeError:
            return self

    __call__ = logged(lambda *args, **kwargs: None, 'call')

        
class NullGui(NullObject, qt.QObject):
    columns = 72
    lines = 60
    def __init__(self, lines=lines, columns=columns):
        NullObject.__init__(self)
        qt.QObject.__init__(self)

    
class NullScreen(NullObject, qt.QObject):
    columns = 72
    lines = 60
    def getCursorX(self):
        return 1
    def getCursorY(self):
        return 1
    def __init__(self, lines=lines, columns=columns):
        NullObject.__init__(self)
        qt.QObject.__init__(self)

    
class MyEmuVt102(emuVt102.EmuVt102):
    def emit(self, signal, args):
        try:
            self._logs.append( (signal, args) )
        except AttributeError:
            self._logs = [ (signal, args) ]
        emuVt102.EmuVt102.emit(self, signal, args)
        
    def myemit(self, signal, args=()):
        try:
            self._logs.append( (signal, args) )
        except AttributeError:
            self._logs = [ (signal, args) ]
        emuVt102.EmuVt102.myemit(self, signal, args)
        
    def reportErrorToken(self, token, p, q):
        try:
            self._logs.append( ('token error', token, p, q) )
        except AttributeError:
            self._logs = [ ('token error', token, p, q) ]
            
    _setCharset = logged(emuVt102.EmuVt102._setCharset, '_setCharset')
    _useCharset = logged(emuVt102.EmuVt102._useCharset, '_useCharset')
    _setAndUseCharset = logged(emuVt102.EmuVt102._setAndUseCharset, '_setAndUseCharset')
    setMode = logged(emuVt102.EmuVt102.setMode, 'setMode')
    resetMode = logged(emuVt102.EmuVt102.resetMode, 'resetMode')
    saveMode = logged(emuVt102.EmuVt102.saveMode, 'saveMode')
    setPrinterMode = logged(emuVt102.EmuVt102.setPrinterMode, 'setPrinterMode')
            

_baseScreen = emulation.Screen

class NoScreenTC(unittest.TestCase):
    
    def setUp(self):
        emulation.Screen = NullScreen

    def tearDown(self):
        emulation.Screen = _baseScreen