Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.apo.nmsu.edu/Telescopes/TCC/html/set_block_8py_source.html
Дата изменения: Tue Sep 15 02:25:37 2015
Дата индексирования: Sun Apr 10 01:35:38 2016
Кодировка:

Поисковые слова: п п п п п п п п п п п п п п п
lsst.tcc: python/tcc/cmd/setBlock.py Source File
lsst.tcc  1.2.2-3-g89ecb63
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
setBlock.py
Go to the documentation of this file.
1 from __future__ import division, absolute_import
2 """SET Block command
3 """
4 from copy import copy
5 import os.path
6 
7 from RO.AddCallback import safeCall2
8 from twistedActor import CommandError
9 
10 import tcc.base
11 from .showBlock import blockAttrDict
12 from tcc.msg import showAxeLim, showTune
13 
14 __all__ = ["setBlock"]
15 
16 exitChars = ("^z", "exit") # should be control z # add ^Y or ^C?
17 
18 # pure python blocks must be copied differently than SWIGged C++ blocks
19 PurePythonBlocks = set(["Tune"])
20 
21 blockNameShowFuncDict = dict(
22  axelim = showAxeLim,
23  tune = showTune,
24 )
25 
26 def callShowFunc(blockName, tccActor, userCmd):
27  """Call a function, if one exists, to show the contents of the specified block using standard keywords
28 
29  @param[in] blockName block name (case ignored)
30  @param[in] tccActor tcc actor
31  @param[in] userCmd user command
32  """
33  showFunc = blockNameShowFuncDict.get(blockName.lower())
34  if showFunc:
35  safeCall2("show keywords for %s block" % (blockName,), showFunc, tccActor, userCmd)
36 
37 def setBlock(tccActor, userCmd):
38  """
39  @param[in,out] tccActor tcc actor
40  @param[in,out] userCmd a twistedActor BaseCommand with parseCmd attribute
41 
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
45  """
46  blockName = userCmd.parsedCmd.paramDict["blockname"].valueList[0].keyword
47  inputPath = None
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]:
52  inputPath += ".dat"
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
57 
58  baseObj = getattr(tcc.base, blockName)
59  if noRead:
60  # get a copy of the block with null values, not the copy in the global database
61  blockCopy = baseObj()
62  setattr(tccActor, blockAttrDict[blockName], blockCopy)
63  userCmd.setState(userCmd.Done)
64  return
65  else:
66  currBlock = getattr(tccActor, blockAttrDict[blockName])
67  if blockName in PurePythonBlocks:
68  blockCopy = copy(currBlock)
69  else:
70  blockCopy = baseObj(currBlock)
71 
72  if inputPath:
73  blockCopy.loadPath(inputPath)
74  setattr(tccActor, blockAttrDict[blockName], blockCopy)
75  callShowFunc(blockName, tccActor, userCmd)
76  userCmd.setState(userCmd.Done)
77  else:
78  # raw input is expected, enter an input state
79  # this intercepts incoming data at the socket's readLine level
80  InteractiveSetBlock(tccActor, blockCopy, blockName, userCmd)
81 
82 class InteractiveSetBlock(object):
83  """update the block being modified directly from socket input
84  """
85  def __init__(self, tccActor, blockCopy, blockName, userCmd):
86  self.tccActor = tccActor
87  self.sock = self.tccActor.userDict[userCmd.userID]
88  self.blockCopy = blockCopy
89  self.blockName = blockName
90  userCmd.addCallback(callFunc=self._revert)
91  userCmd.setTimeLimit(0) # command won't terminate until exit lines are received
92  self.userCmd = userCmd
93  # short circuit the socket's read callback
94  self.origCB = self.sock._readCallback
95  self.sock.setReadCallback(self.readCallback)
96 
97  def readCallback(self, sock):
98  try:
99  line = sock.readLine()
100  # dispatcher sends commands prepended with a number, strip it
101  num1, num2, line = line.strip().lower().split(None, 2)
102  # note some assumption of incoming line's structure here, may need to change
103  # once implemented in the real system
104  #int(num) # assertion
105  if line in exitChars:
106  setattr(self.tccActor, blockAttrDict[self.blockName], self.blockCopy)
107  callShowFunc(self.blockName, self.tccActor, self.userCmd)
108  # finish the cmd, will cause the tcc to revert to correct normal state
109  self.userCmd.setState(self.userCmd.Done)
110  else:
111  self.blockCopy.load([line]) # load loops through lines, hence the list
112  except:
113  self.userCmd.setState(self.userCmd.Failed, textMsg="Failed to load %s into block: %s" % (line, self.blockName))
114 
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
118  """
119  if cmd.isDone:
120  self.sock.setReadCallback(self.origCB)