def saveAgents(self, filename): try: print 'save agents' doc = '<?xml version="1.0" ?>\n' doc += '<agents>\n' piw.tsd_lock() self.getAgentChangeSet(True) agents = [(agentID, self.__getNameOrdinalPair(self.__database.find_desc(agentID))) for agentID in self.__last_agentIDs] # sort by name-ordinal pair to make sure numbers sorted in right order agents.sort(key=lambda x:x[1]) piw.tsd_unlock() for (id, (n,o)) in agents: doc += self.getAgent(id) doc += '</agents>\n' print doc tabsFile = open(filename, 'w') tabsFile.write(doc) tabsFile.close() print " saved %s agents"%self.getNumAgents() except: traceback.print_exc(limit=None) return False return True
def execBelcanto(self, argument): piw.tsd_lock() try: r = self.__languageAgent.rpc_script(argument) except: piw.tsd_unlock() return False if r.status() is not None: piw.tsd_unlock() return r.status() args = [None, None, None] e = threading.Event() e.clear() def finished(returnstatus, *returnargs, **returnkwds): args[0] = returnstatus args[1] = returnargs args[2] = returnkwds e.set() r.setCallback(finished, True).setErrback(finished, False) piw.tsd_unlock() e.wait() return args[0]
def getValue(self, argument): piw.tsd_lock() try: r = self.__languageAgent.basic_get_value(argument) except: piw.tsd_unlock() return False args = [None, None, None] if r.status() is not None: args[0] = r.status() args[1] = r.args() args[2] = r.kwds() piw.tsd_unlock() else: e = threading.Event() e.clear() def finished(returnstatus, *returnargs, **returnkwds): args[0] = returnstatus args[1] = returnargs args[2] = returnkwds print 'args', args e.set() r.setCallback(finished, True).setErrback(finished, False) piw.tsd_unlock() e.wait() if args[0]: return '\n'.join([str(i) for i in args[1][0]]) return False
def addWidget(self, tabIndex, widgetXML): print 'add widget to tab',tabIndex # add widget to tab try: piw.tsd_lock() widgets = self.__tabs.get_tab(tabIndex)[1] widgets.new_widget(widgetXML) piw.tsd_unlock() # parse widget xml to get node widgetDoc = xml.dom.minidom.parseString(widgetXML) widgetNode = widgetDoc.documentElement # create widget in widget manager name = widgetNode.getAttribute('name') OSCPath = widgetNode.getAttribute('path') if OSCPath!='': self.__widgetManager.create_widget(OSCPath) print "created widget",name,OSCPath else: return False except: traceback.print_exc(limit=None) return False return True
def removeWidget(self, tabIndex, widgetIndex): # remove widget with index print 'remove widget', widgetIndex, 'from tab', tabIndex try: piw.tsd_lock() try: widgetXML = self.__tabs.get_tab(tabIndex)[1].get_widget( widgetIndex).getXML() widgetDoc = xml.dom.minidom.parseString(widgetXML) widgetNode = widgetDoc.documentElement OSCPath = widgetNode.getAttribute('path') widgetNode.unlink() self.__tabs.get_tab(tabIndex)[1].remove_widget(widgetIndex) finally: piw.tsd_unlock() # remove widget from widget manager, reference count deletion in destroy_widget if OSCPath != '': self.__widgetManager.destroy_widget(OSCPath) else: return False except: traceback.print_exc(limit=None) return False return True
def process_locked(self, line): piw.tsd_lock() try: event = self.browser.process(line) finally: piw.tsd_unlock() event.wait()
def getNumTabs(self): try: piw.tsd_lock() numTabs = len(self.__tabs) piw.tsd_unlock() #print 'get num tabs =',numTabs except: traceback.print_exc(limit=None) return 0 return numTabs
def setTab(self, index, tabXML): try: print 'set tab',index piw.tsd_lock() self.__tabs.get_tab(index).setXML(tabXML) piw.tsd_unlock() except: traceback.print_exc(limit=None) return False return True
def call_locked_callable(snapshot, callable, *args): """ call a callable object in the context of a mainloop lock """ snapshot.install() piw.tsd_lock() try: return callable(*args) finally: piw.tsd_unlock()
def run_bg_sync(self,func,*args,**kwds): s = piw.tsd_snapshot() try: piw.setenv(self.__env_bg.getenv()) piw.tsd_lock() try: return func(*args,**kwds) finally: piw.tsd_unlock() finally: s.install()
def getNumWidgets(self, tabIndex): # get number of widgets in a tab try: piw.tsd_lock() numWidgets = len(self.__tabs.get_tab(tabIndex)[1]) piw.tsd_unlock() print 'get num widgets =',numWidgets except: traceback.print_exc(limit=None) return 0 return numWidgets
def addTab(self, tabXML): try: print 'add tab' # receive new tab xml from stage for an empty tab - no widgets to add to manager piw.tsd_lock() # tab added with index numTabs self.__tabs.new_tab(tabXML) piw.tsd_unlock() except: traceback.print_exc(limit=None) return False return True
def moveTab(self, currentTabIndex, newTabIndex): # moves a tab (swaps two tabs) try: print 'move tab', currentTabIndex, 'to', newTabIndex piw.tsd_lock() # current and new must be valid tab indices self.__tabs.move_tab(currentTabIndex, newTabIndex); piw.tsd_unlock() except: traceback.print_exc(limit=None) return False return True
def __run(self,ctx,func,*args,**kwds): current_context = piw.tsd_snapshot() try: ctx.install() piw.tsd_lock() try: return func(*args,**kwds) finally: piw.tsd_unlock() finally: current_context.install()
def getWidget(self, tabIndex, widgetIndex): print 'get widget',widgetIndex,'from tab',tabIndex # get widget xml try: piw.tsd_lock() widgets = self.__tabs.get_tab(tabIndex)[1] widgetXML = widgets.get_widget(widgetIndex).getXML() piw.tsd_unlock() except: traceback.print_exc(limit=None) return '' return widgetXML
def getTab(self, index): # return tab xml to stage try: print 'get tab',index piw.tsd_lock() tabXML = self.__tabs.get_tab(index).getXML() piw.tsd_unlock() except: traceback.print_exc(limit=None) return '' # return xml string return tabXML
def run_background(self,func,*args,**kwds): current_context = piw.tsd_snapshot() try: piw.setenv(self.backend_context.getenv()) piw.tsd_lock() try: return func(*args,**kwds) finally: piw.tsd_unlock() finally: current_context.install()
def getNumTabs(self): try: print 'get num tabs' piw.tsd_lock() try: numTabs = len(self.__tabs) finally: piw.tsd_unlock() except: traceback.print_exc(limit=None) return 0 print 'get num tabs', numTabs return numTabs
def setWidget(self, tabIndex, widgetIndex, widgetXML): print 'set widget', widgetIndex, 'from tab', tabIndex # set widget xml try: piw.tsd_lock() try: widgets = self.__tabs.get_tab(tabIndex)[1] widgets.get_widget(widgetIndex).setXML(widgetXML) finally: piw.tsd_unlock() except: traceback.print_exc(limit=None) return '' return widgetXML
def getTabSessionChanges(self, index): # return the number of tab session changes # each time a tab is changed, the session change count is incremented try: piw.tsd_lock() tab = self.__tabs.get_tab(index) if tab is not None: changes = tab.get_session_changes() else: changes = -1 piw.tsd_unlock() #print 'get tab session changes index =',index,'changes =',changes return changes except: traceback.print_exc(limit=None) return -1
def run_session(session, user=None, mt=1, name='ctx', logger=None, clock=True, rt=True): def logfunc(msg): if logger: logger(msg) else: print context = None def ctxdun(status): context.release() scaffold = piagent.scaffold_mt(mt, utils.stringify(logfunc), utils.stringify(None), clock, rt) context = scaffold.context('main', utils.statusify(ctxdun), utils.stringify(logfunc), name) stdio = (sys.stdout, sys.stderr) x = None try: if logger: sys.stdout = Logger() sys.stderr = sys.stdout piw.setenv(context.getenv()) piw.tsd_lock() try: x = session(scaffold) context.trigger() finally: piw.tsd_unlock() scaffold.wait() finally: sys.stdout, sys.stderr = stdio return x
def removeTab(self, tabIndex): # removes a tab try: print 'remove tab', tabIndex # remove all the widgets from this tab # ensures that they are removed from the widget manager also numWidgets = self.getNumWidgets(tabIndex) for widgetIndex in range(0,numWidgets): # always remove widget 0 as the list gets shorter self.removeWidget(tabIndex,0) piw.tsd_lock() self.__tabs.remove_tab(tabIndex) piw.tsd_unlock() except: traceback.print_exc(limit=None) return False return True
def widgetRpc(self, tabIndex, widgetIndex, rpcMethod, rpcArg): print 'rpc widget', widgetIndex, 'from tab', tabIndex, 'method', rpcMethod, 'arg', rpcArg piw.tsd_lock() try: widgets = self.__tabs.get_tab(tabIndex)[1] widgetXML = widgets.get_widget(widgetIndex).getXML() widgetDoc = xml.dom.minidom.parseString(widgetXML) widgetNode = widgetDoc.documentElement widgetAddress = widgetNode.getAttribute('address') print 'rpc', rpcMethod, 'to', widgetAddress, 'with', rpcArg r = rpc.invoke_rpc(widgetAddress, rpcMethod, rpcArg) except: piw.tsd_unlock() traceback.print_exc(limit=None) return '' args = [None, None, None] e = threading.Event() e.clear() def finished(returnstatus, *returnargs, **returnkwds): args[0] = returnstatus args[1] = returnargs args[2] = returnkwds e.set() r.setCallback(finished, True).setErrback(finished, False) piw.tsd_unlock() e.wait() try: return args[1][0] except: return ''
def getAgentChangeSet(self, clientTimeStr): #print 'get agent change set' # get the set of agent IDs for the agents that have been updated # since the last time the client requested changes try: piw.tsd_lock() #if fromStartup: # time = 0 #else: # time = self.__last_agent_update_time clientTime = long(clientTimeStr) #print 'client time str = ',clientTimeStr changed_agent_ids = self.__database.changed_agents(clientTime) if len(changed_agent_ids)>0: agentIDs = self.__database.get_propcache('props').get_idset('agent') # only interested in changed agents (roots) changed_agent_ids = agentIDs.intersection(frozenset(changed_agent_ids)) # remove any agents that have been removed from the database removed_agentIDs = self.__last_agentIDs - agentIDs for id in removed_agentIDs: del self.agents[id] # add removed agents to the change list so Stage will attempt to # get deleted agents, and can determine that they have been # deleted when nothing is returned changed_agent_ids = changed_agent_ids.union(removed_agentIDs) self.__last_agentIDs = agentIDs piw.tsd_unlock() # build xml list of changed agent list xml = '<agentChangeSet time="%s">\n' % self.__database.get_timestamp() for id in changed_agent_ids: xml += '<agent address="%s"/>\n' % id xml += '</agentChangeSet>' #if len(changed_agent_ids)>0: # print 'number changes=',len(changed_agent_ids),'time =',clientTime # print 'changed set',changed_agent_ids # print 'removed set',removed_agentIDs # print xml else: piw.tsd_unlock() xml = '<agentChangeSet time="%s"></agentChangeSet>' % self.__database.get_timestamp() #self.__last_agent_update_time = self.__database.get_timestamp() return xml except: piw.tsd_unlock() traceback.print_exc(limit=None) return ''
def getWidget(self, tabIndex, widgetIndex): print 'get widget', widgetIndex, 'from tab', tabIndex # get widget xml try: piw.tsd_lock() try: widgets = self.__tabs.get_tab(tabIndex)[1] widgetXML = widgets.get_widget(widgetIndex).getXML() widgetDoc = xml.dom.minidom.parseString(widgetXML) widgetNode = widgetDoc.documentElement widgetAddress = widgetNode.getAttribute('address') widgetDomain = widgetNode.getElementsByTagName('domain')[0] atom = self.__database.find_item(widgetAddress) if atom: atomDomain = atom.domain() different = False widgetDomainType = widgetDomain.getAttribute('type') if widgetDomainType != self.__domainType(atomDomain): different = True boundMin, boundMax, boundUserMin, boundUserMax, boundUserStep = self.__domainBounds( atomDomain) if widgetDomain.hasAttribute('min'): if boundMin is None or round(boundMin, 3) != round( float(widgetDomain.getAttribute('min')), 3): different = True elif boundMin is not None: different = True if widgetDomain.hasAttribute('max'): if boundMax is None or round(boundMax, 3) != round( float(widgetDomain.getAttribute('max')), 3): different = True elif boundMax is not None: different = True # don't check the userMin, userStep and userMax as this can be changed in Stage atomDistribution = atomDomain.hint('distribution') if widgetDomain.hasAttribute('distribution'): # linear is the default distribution = 'linear' if atomDistribution: distribution = str(atomDistribution[0]) if distribution != widgetDomain.getAttribute( 'distribution'): different = True elif atomDistribution is not None: different = True widgetNode.unlink() if different: regexp = re.compile(r"<domain[^<]+/>", re.DOTALL) widgetXML = regexp.sub(self.__domainXml(atomDomain), widgetXML, 1) widgets.get_widget(widgetIndex).set_value(widgetXML) finally: piw.tsd_unlock() except: traceback.print_exc(limit=None) return '' return widgetXML