lsst.meas.base  13.0-16-gca4cc00
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Groups Pages
flagDecorator.py
Go to the documentation of this file.
1 from lsst.meas.base import FlagDefinition, FlagHandler
2 
3 
4 def addFlagHandler(*args):
5  '''
6  Class decorator to create a flag handler for a plugin. Adds the class variables FLAGDEFS and
7  ErrEnum. An instance variable flagHandler is added to the __init__ function to be created at
8  initialization. The arguments to this function are tuples which have the name of the failure
9  as the first element, and the documentation for the failure as the second element.
10 
11  Usage:
12  @addFlagHandler(("name_of_failure", "Doc for failure), ("name_of_second_faiure", "Doc of"
13  " second failure"), .....)
14  '''
15  def classFactory(cls):
16  cls.FLAGDEFS = [FlagDefinition(name, doc) for name, doc in args]
17  # Verify all flag names are unique
18  names = [entry[0] for entry in args]
19  if len(names) != len(set(names)):
20  raise ValueError("All flag names must be unique, given {}".format(names))
21  # Better to have a scoped enumeration rather than attach variables strait to the class to
22  # prevent shadowing
23  cls.ErrEnum = type('ErrEnum', (), {entry.name: i for i, entry in enumerate(cls.FLAGDEFS)})
24  oldInit = cls.__init__
25 
26  def newInit(self, *args, **kwargs):
27  if 'schema' in kwargs:
28  schema = kwargs['schema']
29  else:
30  schema = args[2]
31  if 'name' in kwargs:
32  name = kwargs['name']
33  else:
34  name = args[1]
35  self.flagHandler = FlagHandler.addFields(schema, name, self.FLAGDEFS)
36  oldInit(self, *args, **kwargs)
37  cls.__init__ = newInit
38  return cls
39  return classFactory
Simple class used to define and document flags The name and doc constitute the identity of the FlagDe...
Definition: FlagHandler.h:38