LSSTApplications  11.0-24-g0a022a1,15.0+13,15.0+9,15.0-1-g19261fa+5,15.0-1-g1eca518+15,15.0-1-g60afb23+12,15.0-1-g615e0bb+4,15.0-1-g6668b0b+5,15.0-1-g788a293+12,15.0-1-ga91101e+12,15.0-1-gae1598d+8,15.0-1-gc45031d+15,15.0-1-gd076f1f+12,15.0-1-gdf18595+2,15.0-1-gf4f1c34+8,15.0-2-g100d730+5,15.0-2-g18f3f21+5,15.0-2-g35685a8+6,15.0-2-gf38729e+5,15.0-21-g91b8abf62,15.0-3-g150fc43+14,15.0-3-g6f085af+5,15.0-3-g707930d,15.0-3-g9103c06+8,15.0-3-ga03b4ca+16,15.0-3-gaec6799+5,15.0-3-gb7a597c+12,15.0-3-ge6a6747+5,15.0-4-g45f767a+8,15.0-4-g654b129+10,15.0-4-gf5d1e39,15.0-4-gff20472+15,15.0-5-ga70c291+5,15.0-6-g9a9df217+5,15.0-7-gab4c137+6,15.0-7-gab79a70c+4
LSSTDataManagementBasePackage
printers_oldgdb.py
Go to the documentation of this file.
1 """
2 Code that works with gdb 7.1's python pretty printing. When gdb >= 7.2 is widely available this
3 file should be deleted (it's only used after importing gdb.printing fails)
4 """
5 from __future__ import absolute_import, division, print_function
6 from builtins import str
7 from builtins import range
8 from builtins import object
9 import gdb
10 import re
11 
12 
14  "Print a Citizen"
15 
16  def __init__(self, typename, val):
17  self.val = val
18 
19  def to_string(self):
20  return "{0x%x %d %s 0x%x}" % (self.val, self.val["_CitizenId"],
21  self.val["_typeName"], self.val["_sentinel"])
22 
23 
24 # afw
26  "Print a BaseSourceAttributes"
27 
28  def __init__(self, typename, val):
29  self.val = val
30 
31  def to_string(self):
32  return "Base: {id=%d astrom=(%.3f, %.3f)}" % (self.val["_id"],
33  self.val["_xAstrom"],
34  self.val["_yAstrom"])
35 
36 
38  "Print a Source"
39 
40  def __init__(self, typename, val):
41  self.val = val
42 
43  def to_string(self):
44  return "{id=%d astrom=(%.3f, %.3f)}" % (self.val["_id"], self.val["_xAstrom"], self.val["_yAstrom"])
45 
46 
48  "Print a Footprint"
49 
50  def __init__(self, typename, val):
51  self.val = val
52 
53  def to_string(self):
54  return "RHL Footprint (fixme when gdb 7.3 arrives)"
55 
56 
58  "Print a CoordinateBase"
59 
60  def __init__(self, typename, val):
61  self.val = val
62 
63  def to_string(self):
64  # Make sure &foo works, too.
65  type = self.val.type
66  if type.code == gdb.TYPE_CODE_REF:
67  type = type.target()
68 
69  return self.val["_vector"]["m_storage"]["m_data"]["array"]
70 
71  def display_hint(self):
72  return "array"
73 
74 
76  "Print an ImageBase or derived class"
77 
78  def dimenStr(self, val=None):
79  if val is None:
80  val = self.val
81 
82  # Make sure &foo works, too.
83  type = val.type
84  if type.code == gdb.TYPE_CODE_REF:
85  type = type.target()
86 
87  gilView = val["_gilView"]
88  arr = val["_origin"]["_vector"]["m_storage"]["m_data"]["array"]
89 
90  return "%dx%d+%d+%d" % (
91  # val["getWidth"](), val["getHeight"](),
92  gilView["_dimensions"]["x"], gilView["_dimensions"]["y"],
93  arr[0], arr[1])
94 
95  def typeName(self):
96  return self.typename.split(":")[-1]
97 
98  def __init__(self, typename, val):
99  self.typename = typename
100  self.val = val
101 
102  def to_string(self):
103  return "%s(%s)" % (self.typeName(), self.dimenStr())
104 
105 
107  "Print a MaskedImage"
108 
109  def to_string(self):
110  return "%s(%s)" % (self.typeName(), self.dimenStr(self.val["_image"]["px"].dereference()))
111 
112 
114  "Print an Exposure"
115 
116  def to_string(self):
117  return "%s(%s)" % (self.typeName(),
118  self.dimenStr(self.val["_maskedImage"]["_image"]["px"].dereference()))
119 
120 
121 class PrintImageCommand(gdb.Command):
122  """Print an Image
123 Usage: image x0 y0 [nx [ny] [centerPatch] [obeyXY0]]
124 """
125 
126  def __init__(self):
127  super(PrintImageCommand, self).__init__("show image",
128  gdb.COMMAND_DATA,
129  gdb.COMPLETE_SYMBOL)
130 
131  def get(self, var, x, y):
132  if False:
133  return var["operator()(int, int, bool)"](x, y, True)
134  else:
135  dimensions = var["_gilView"]["_dimensions"]
136  if x < 0 or x >= dimensions["x"] or y < 0 or y >= dimensions["y"]:
137  raise gdb.GdbError("Pixel (%d, %d) is out of range 0:%d, 0:%d" %
138  (x, y, dimensions["x"] - 1, dimensions["y"] - 1))
139 
140  pixels = var["_gilView"]["_pixels"]["_p"]
141  step = pixels["_step_fn"]["_step"] / \
142  var.type.template_argument(0).sizeof
143 
144  return pixels["m_iterator"][x + y*step]["_v0"]
145 
146  def invoke(self, args, fromTty):
147  self.dont_repeat()
148 
149  args = gdb.string_to_argv(args)
150  if len(args) < 1:
151  raise gdb.GdbError("Please specify an image")
152  imgName = args.pop(0)
153  var = gdb.parse_and_eval(imgName)
154 
155  if re.search(r"MaskedImage", str(var.type)):
156  print("N.b. %s is a MaskedImage; showing image" % (imgName))
157  var = var["_image"]
158 
159  if re.search(r"shared_ptr<", str(var.type)):
160  var = var["px"].dereference()
161 
162  if var.type.code == gdb.TYPE_CODE_PTR:
163  var = var.dereference() # be nice
164 
165  pixelTypeName = str(var.type.template_argument(0))
166 
167  if len(args) < 2:
168  raise gdb.GdbError("Please specify a pixel's x and y indexes")
169 
170  x0 = gdb.parse_and_eval(args.pop(0))
171  y0 = gdb.parse_and_eval(args.pop(0))
172 
173  if len(args) == 0:
174  print("%g" % self.get(var, x0, y0))
175  return
176 
177  nx = int(args.pop(0))
178  if args:
179  ny = int(args.pop(0))
180  else:
181  ny = 1
182 
183  if args:
184  centerPatch = gdb.parse_and_eval(args.pop(0))
185  if centerPatch:
186  x0 -= nx//2
187  y0 -= ny//2
188 
189  if args:
190  obeyXY0 = gdb.parse_and_eval(args.pop(0))
191 
192  if obeyXY0:
193  arr = var["_origin"]["_vector"]["m_storage"]["m_data"]["array"]
194 
195  x0 -= arr[0]
196  y0 -= arr[1]
197 
198  if args:
199  raise gdb.GdbError(
200  'Unexpected trailing arguments: "%s"' % '", "'.join(args))
201  #
202  # OK, finally time to print
203  #
204  if pixelTypeName in ["short", "unsigned short"]:
205  dataFmt = "0x%x"
206  elif pixelTypeName in ["int", "unsigned int"]:
207  dataFmt = "%d"
208  else:
209  dataFmt = "%.2f"
210 
211  print("%-4s" % "", end=' ')
212  for x in range(x0, x0 + nx):
213  print("%8d" % x, end=' ')
214  print("")
215 
216  for y in reversed(list(range(y0, y0 + ny))):
217  print("%-4d" % y, end=' ')
218  for x in range(x0, x0 + nx):
219  print("%8s" % (dataFmt % self.get(var, x, y)), end=' ')
220  print("")
221 
222 
224 
225 
226 #
227 # These two classes (RxPrinter and Printer) come directly from
228 # python/libstdcxx/v6/printers.py and the GPL license therein applies
229 #
230 # A "regular expression" printer which conforms to the
231 # "SubPrettyPrinter" protocol from gdb.printing.
233  def __init__(self, name, function):
234  super(RxPrinter, self).__init__()
235  self.name = name
236  self.function = function
237  self.enabled = True
238 
239  def invoke(self, value):
240  if not self.enabled:
241  return None
242  return self.function(self.name, value)
243 
244 
245 # A pretty-printer that conforms to the "PrettyPrinter" protocol from
246 # gdb.printing. It can also be used directly as an old-style printer.
247 #
249  def __init__(self, name):
250  super(Printer, self).__init__()
251  self.name = name
252  self.subprinters = []
253  self.lookup = {}
254  self.enabled = True
255  self.compiled_rx = re.compile('^([a-zA-Z0-9_:]+)<.*>$')
256 
257  def add(self, name, function):
258  # A small sanity check.
259  # FIXME
260  if not self.compiled_rx.match(name + '<>'):
261  raise ValueError(
262  'libstdc++ programming error: "%s" does not match' % name)
263  printer = RxPrinter(name, function)
264  self.subprinters.append(printer)
265  self.lookup[name] = printer
266 
267  @staticmethod
268  def get_basic_type(type):
269  # If it points to a reference, get the reference.
270  if type.code == gdb.TYPE_CODE_REF:
271  type = type.target()
272 
273  # Get the unqualified type, stripped of typedefs.
274  type = type.unqualified().strip_typedefs()
275 
276  return type.tag
277 
278  def __call__(self, val):
279  typename = self.get_basic_type(val.type)
280  if not typename:
281  return None
282 
283  # All the types we match are template types, so we can use a
284  # dictionary.
285  match = self.compiled_rx.match(typename)
286  if not match:
287  return None
288 
289  basename = match.group(1)
290  if basename in self.lookup:
291  return self.lookup[basename].invoke(val)
292 
293  # Cannot find a pretty printer. Return None.
294  return None
295 
296 
297 printers = []
298 
299 
300 def register(obj):
301  "Register my pretty-printers with objfile Obj."
302 
303  if obj is None:
304  obj = gdb
305 
306  for p in printers:
307  obj.pretty_printers.insert(0, p)
308 
309 
311  printer = Printer("afw")
312 
313  printer.add('lsst::afw::detection::Footprint', FootprintPrinter)
314  printer.add('lsst::afw::detection::Source', SourcePrinter)
315  printer.add('lsst::afw::detection::BaseSourceAttributes',
316  BaseSourceAttributesPrinter)
317 
318  printer.add('lsst::afw::geom::Point', CoordinateBasePrinter)
319  printer.add('lsst::afw::geom::Extent', CoordinateBasePrinter)
320 
321  printer.add('lsst::afw::image::ImageBase', ImagePrinter)
322  printer.add('lsst::afw::image::Image', ImagePrinter)
323  printer.add('lsst::afw::image::Mask', ImagePrinter)
324  printer.add('lsst::afw::image::MaskedImage', MaskedImagePrinter)
325  printer.add('lsst::afw::image::Exposure', ExposurePrinter)
326 
327  return printer
328 
329 
330 printers.append(build_afw_dictionary())
331 
332 
334  printer = Printer("daf::base")
335 
336  printer.add('lsst::daf::base::Citizen', CitizenPrinter)
337 
338  return printer
339 
340 
341 printers.append(build_daf_base_dictionary())
def __init__(self, name, function)
std::shared_ptr< FrameSet > append(FrameSet const &first, FrameSet const &second)
Construct a FrameSet that performs two transformations in series.
Definition: functional.cc:33
daf::base::PropertyList * list
Definition: fits.cc:819