1 from __future__
import division, absolute_import
2 """Some tools for managing TCC keyword output from a block
7 from coordConv
import PVT
8 from RO.SeqUtil
import isSequence
10 __all__ = [
"WriterFromBlock",
"FieldKW"]
13 """!A class containing mapping information between fields on a block and a TCC keyword
16 """!Construct a WriterFromBlock
18 @param[in] objClass class of block
19 @param[in] fieldKWDict an ordered dictionary of FieldKW objects keyed by TCC keyword.
20 Keywords are output in the order specified by this dict.
32 def writeKWs(self, writeToUsers, cmd=None, keywords=None):
33 """!Write a list of keyword=value messages to users
35 @param[in] writeToUsers provided from the tccActor.writeToUsers method
36 @param[in] cmd command whose ID is used for output (a twistedActor.BaseCmd)
37 @param[in] keywords a list of keywords or None;
38 if None then use self.fieldKWDict.keys()
41 if not isSequence(keywords):
42 raise RuntimeError(
"keywords=%r; not a sequence" % (keywords,))
43 keywords = set(keywords)
45 for key, logic
in self.additionRules.iteritems():
47 keywords.update([key])
51 if len(group & keywords):
52 keywords.update(group)
55 keywords = self.fieldKWDict.keys()
56 for kw
in self.fieldKWDict.iterkeys():
57 if kw
not in keywords:
63 """!Add a suppression rule
65 @param[in] keyword the keyword to be suppressed (must be a keyword in self.fieldKWDict)
66 @param[in] logic a callable that evaluates to True or False. If True keyword will
67 not be output, else keyword will be output
70 raise RuntimeError(
"Keyword :%s, not found in dict"%keyword)
71 if not callable(logic):
72 raise RuntimeError(
"logic")
81 """!Add an addition rule
83 @param[in] keyword the keyword to be added (must be a keyword in self.fieldKWDict)
84 @param[in] logic a callable that evaluates to True or False. If True keyword will
88 raise RuntimeError(
"Keyword :%s, not found in dict"%keyword)
89 if not callable(logic):
90 raise RuntimeError(
"logic")
94 """!Define a keyword group
96 @param[in] keywords a list of keywords.
97 If one is specified, then all are output
99 self.groups.append(set(keywords))
103 """!Write a single keyword to users; if a suppression rule for this keyword exists, test it
105 @param[in] keyword the keyword to be suppressed (must be a keyword in self.fieldKWDict)
106 @param[in] writeToUsers output function (a twistedActor.BaseActor.writeToUsers)
107 @param[in] cmd command whose ID is used for output (a twistedActor.BaseCmd)
115 writeToUsers(
'i', outStr, cmd)
118 """!Compare attributes on the newBlock, vs. the cached block
120 @param[in] newBlock the new block with possibly (likely) new values
121 @return a list of keywords which have been updated
124 if "userSys" in dir(newBlock):
125 newBlock.userSys = newBlock.userSys.clone()
126 updatedKWs = [kw
for kw, val
in self.fieldKWDict.iteritems()
if val.wasUpdated(self.
block, newBlock)]
128 self.
block = newBlock
133 """Return True if a==b or False otherwise, with special handling for float and coordConv.PVT
135 Handle these types specially (and assumes a and b are the same type)
136 - float: return True if both are nan
137 - coordConv.PVT: return True if neither is finite
141 if isinstance(a, float):
142 if math.isnan(a)
and math.isnan(b):
144 elif isinstance(a, PVT):
145 if not a.isfinite()
and not b.isfinite():
150 """!Object for holding an attribute of a block, and a formatting function for that attribute
152 A block is a struct-like object, such as tcc.base.Obj
155 """!Construct a FieldKW
157 @param[in] tccKW keyword used to output the value
158 @param[in] blockAttr name of attribute on the block
159 @param[in] stringCast function that formats the specified attribute;
160 called with the Block.blockAttr as the only argument.
167 """!Return True if the attribute has changed
169 @param[in] oldBlock old value of the block
170 @param[in] newBlock new value of the block
172 attr1 = getattr(oldBlock, self.
blockAttr)
173 attr2 = getattr(newBlock, self.
blockAttr)
175 if isSequence(attr1):
176 for x, y
in itertools.izip(attr1, attr2):
188 """!Format the attribute as a key=value string
190 @param[in] block block whose attribute is to be formatted
def writeKWs
Write a list of keyword=value messages to users.
def __init__
Construct a FieldKW.
def wasUpdated
Return True if the attribute has changed.
def addAddition
Add an addition rule.
def getKeyValStr
Format the attribute as a key=value string.
def __init__
Construct a WriterFromBlock.
A class containing mapping information between fields on a block and a TCC keyword.
def writeSingleKW
Write a single keyword to users; if a suppression rule for this keyword exists, test it...
def addGroup
Define a keyword group.
Object for holding an attribute of a block, and a formatting function for that attribute.
def addSuppression
Add a suppression rule.
def updateBlock
Compare attributes on the newBlock, vs.