tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / solenv / gdb / libreoffice / util / printing.py
blobf50a427cc09169ab33e5f45c631d5f92210d51a5
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 try:
11 from collections.abc import Mapping
12 except Exception:
13 from collections import Mapping
14 import gdb
15 import re
16 import six
18 from libreoffice.util.compatibility import use_gdb_printing
20 class SimplePrinter(object):
22 def __init__(self, name, function):
23 self.name = name
24 self.function = function
25 self.enabled = True
27 def invoke(self, val):
28 if not self.enabled:
29 return None
30 return self.function(self.name, val)
32 class NameLookup(Mapping):
34 def __init__(self):
35 self.map = {}
36 self.name_regex = re.compile(r'^([\w:]+)(<.*>)?')
38 def add(self, name, printer):
39 self.map[name] = printer
41 def __len__(self):
42 return len(self.map)
44 def __getitem__(self, type):
45 typename = self._basic_type(type)
46 if typename and typename in self.map:
47 return self.map[typename]
48 return None
50 def __iter__(self):
51 return self.map
53 def _basic_type(self, type):
54 basic_type = self.basic_type(type)
55 if basic_type:
56 match = self.name_regex.match(basic_type)
57 if match:
58 return match.group(1)
59 return None
61 @staticmethod
62 def basic_type(type):
63 if type.code == gdb.TYPE_CODE_REF:
64 type = type.target()
65 type = type.unqualified().strip_typedefs()
66 return type.tag
68 class FunctionLookup(Mapping):
70 def __init__(self):
71 self.map = {}
73 def add(self, test, printer):
74 self.map[test] = printer
76 def __len__(self):
77 return len(self.map)
79 def __getitem__(self, type):
80 for (test, printer) in six.iteritems(self.map):
81 if test(type):
82 return printer
83 return None
85 def __iter__(self):
86 return self.map
88 class Printer(object):
90 def __init__(self, name):
91 self.name = name
92 self.subprinters = []
93 self.name_lookup = NameLookup()
94 self.func_lookup = FunctionLookup()
95 self.enabled = True
97 def add(self, name, function, lookup = None):
98 printer = SimplePrinter(name, function)
99 self.subprinters.append(printer)
100 if not lookup:
101 self.name_lookup.add(name, printer)
102 else:
103 self.func_lookup.add(lookup, printer)
106 def __call__(self, val):
107 printer = self.name_lookup[val.type]
108 if not printer:
109 printer = self.func_lookup[val.type]
111 if printer:
112 return printer.invoke(val)
113 return None
115 def register_pretty_printer(printer, obj):
116 '''Registers printer with objfile'''
118 if use_gdb_printing:
119 gdb.printing.register_pretty_printer(obj, printer)
120 else:
121 if obj is None:
122 obj = gdb
123 obj.pretty_printers.append(printer)
125 # vim:set shiftwidth=4 softtabstop=4 expandtab: