• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Gallopsled / pwntools / aacbe9c2ab44d984e2b5554bc64f5e06f24ee293

pending completion
aacbe9c2ab44d984e2b5554bc64f5e06f24ee293

push

github-actions

gogo
Fix testing.

3931 of 6497 branches covered (60.5%)

16 of 16 new or added lines in 1 file covered. (100.0%)

12337 of 16864 relevant lines covered (73.16%)

0.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

37.76
/pwnlib/term/keymap.py
1
from __future__ import absolute_import
1✔
2
from __future__ import division
1✔
3

4
from pwnlib.term import key
1✔
5

6
__all__ = ['Keymap']
1✔
7

8

9

10
class Keymap:
1✔
11
    def __init__(self, bindings, on_match = None, on_nomatch = None,
1✔
12
                  on_key = None):
13
        self._on_match = on_match
1✔
14
        self._on_nomatch = on_nomatch
1✔
15
        self._on_key = on_key
1✔
16
        self._top = {}
1✔
17
        self._cur = self._top
1✔
18
        self.trace = []
1✔
19
        self.register(bindings)
1✔
20

21
    def handle_input(self):
1✔
22
        self._doread = True
×
23
        while self._doread:
×
24
            self.send(key.get())
×
25

26
    def stop(self):
1✔
27
        self._doread = False
×
28

29
    @property
1✔
30
    def currently_entered(self):
31
        return ' '.join(map(str, self.trace))
×
32

33
    def reset(self):
1✔
34
        self._cur = self._top
×
35
        self.trace = []
×
36

37
    def send(self, k):
1✔
38
        if k is None:
×
39
            raise EOFError
×
40
        self.trace.append(k)
×
41
        if self._on_key:
×
42
            self._on_key(self.trace)
×
43
        match = False
×
44
        for m, (t, cbs) in self._cur.items():
×
45
            if m(k):
×
46
                self._cur = t
×
47
                if cbs:
×
48
                    match = True
×
49
                    if self._on_match:
×
50
                        self._on_match(self.trace)
×
51
                    for cb in cbs:
×
52
                        cb(self.trace)
×
53
        if not match and self._on_nomatch:
×
54
            self._on_nomatch(self.trace)
×
55
        tr = self.trace
×
56
        if len(self._cur) == 0 or not match:
×
57
            self.reset()
×
58
        if len(tr) > 1 and not match:
×
59
            self.send(k)
×
60

61
    def register(self, desc, cb = None):
1✔
62
        if isinstance(desc, dict):
1✔
63
            for k, v in desc.items():
1✔
64
                self.register(k, v)
1✔
65
        else:
66
            if   desc == '<match>':
1!
67
                self.on_match(cb)
×
68
            elif desc == '<nomatch>':
1✔
69
                self.on_nomatch(cb)
1✔
70
            elif desc == '<any>':
1✔
71
                self.on_key(cb)
1✔
72
            else:
73
                ms = map(key.Matcher, desc.split(' '))
1✔
74
                if not ms:
1!
75
                    return
×
76
                t = self._top
1✔
77
                for m in ms:
1✔
78
                    if m not in t:
1!
79
                        t[m] = ({}, [])
1✔
80
                    t, cbs = t[m]
1✔
81
                cbs.append(cb)
1✔
82

83
    def unregister(self, desc, cb = None):
1✔
84
        ms = map(key.Matcher, desc.split(' '))
×
85
        if not ms:
×
86
            return
×
87
        t = self._top
×
88
        bt = []
×
89
        cbs = None
×
90
        for m in ms:
×
91
            if m not in t:
×
92
                return
×
93
            bt.append((t, cbs))
×
94
            t, cbs = t[m]
×
95
        if cb and cb in cbs:
×
96
            cbs.remove(cb)
×
97
        else:
98
            while True:
×
99
                try:
×
100
                    cbs.pop()
×
101
                except IndexError:
×
102
                    break
×
103
        # delete empty branch by backtracking
104
        while not t and not cbs:
×
105
            m = ms.pop()
×
106
            t, cbs = bt.pop()
×
107
            del t[m]
×
108

109
    def on_match(self, cb):
1✔
110
        self._on_match = cb
×
111

112
    def on_nomatch(self, cb):
1✔
113
        self._on_nomatch = cb
1✔
114

115
    def on_key(self, cb):
1✔
116
        self._on_key = cb
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc