1 from __future__
import division, absolute_import
7 from RO.AddCallback
import safeCall2
8 from twistedActor
import CommandError
11 from .showBlock
import blockAttrDict
12 from tcc.msg import showAxeLim, showTune
14 __all__ = [
"setBlock"]
16 exitChars = (
"^z",
"exit")
19 PurePythonBlocks = set([
"Tune"])
21 blockNameShowFuncDict = dict(
27 """Call a function, if one exists, to show the contents of the specified block using standard keywords
29 @param[in] blockName block name (case ignored)
30 @param[in] tccActor tcc actor
31 @param[in] userCmd user command
33 showFunc = blockNameShowFuncDict.get(blockName.lower())
35 safeCall2(
"show keywords for %s block" % (blockName,), showFunc, tccActor, userCmd)
39 @param[in,out] tccActor tcc actor
40 @param[in,out] userCmd a twistedActor BaseCommand with parseCmd attribute
42 from a userCmd set the appropriate block, an attribute on the tccActor.
43 single fields may be changed, a complete block file may be read, or if no params
44 are passed the block is reloaded
46 blockName = userCmd.parsedCmd.paramDict[
"blockname"].valueList[0].keyword
48 if userCmd.parsedCmd.qualDict[
'input'].boolValue:
49 dataDir = tcc.base.getDataDir()
50 inputPath = userCmd.parsedCmd.qualDict[
'input'].valueList[0]
51 if not os.path.splitext(inputPath)[1]:
53 inputPath = os.path.join(dataDir, inputPath)
54 if not os.path.isfile(inputPath):
55 raise CommandError(
"No such file %r" % (inputPath,))
56 noRead = userCmd.parsedCmd.qualDict[
'noread'].boolValue
58 baseObj = getattr(
tcc.base, blockName)
62 setattr(tccActor, blockAttrDict[blockName], blockCopy)
63 userCmd.setState(userCmd.Done)
66 currBlock = getattr(tccActor, blockAttrDict[blockName])
67 if blockName
in PurePythonBlocks:
68 blockCopy = copy(currBlock)
70 blockCopy = baseObj(currBlock)
73 blockCopy.loadPath(inputPath)
74 setattr(tccActor, blockAttrDict[blockName], blockCopy)
76 userCmd.setState(userCmd.Done)
83 """update the block being modified directly from socket input
85 def __init__(self, tccActor, blockCopy, blockName, userCmd):
87 self.
sock = self.tccActor.userDict[userCmd.userID]
90 userCmd.addCallback(callFunc=self.
_revert)
91 userCmd.setTimeLimit(0)
94 self.
origCB = self.sock._readCallback
99 line = sock.readLine()
101 num1, num2, line = line.strip().lower().split(
None, 2)
105 if line
in exitChars:
109 self.userCmd.setState(self.userCmd.Done)
111 self.blockCopy.load([line])
113 self.userCmd.setState(self.userCmd.Failed, textMsg=
"Failed to load %s into block: %s" % (line, self.
blockName))
115 def _revert(self, cmd):
116 """callback for current userCmd to ensure that the correct parse method
117 is replaced on the actor once the command is done
120 self.sock.setReadCallback(self.
origCB)