def testPreTests(self): # make sure that NatSpeak is loaded into memory testForException = self.doTestForException # these function should all fail before natConnect is called testForException( natlink.NatError, "natlink.playString('')" ) testForException( natlink.NatError, "natlink.getCurrentModule()" ) testForException( natlink.NatError, "natlink.getCurrentUser()" ) testForException( natlink.NatError, "natlink.getMicState()" ) testForException( natlink.NatError, "natlink.setMicState('off')" ) testForException( natlink.NatError, "natlink.execScript('')" ) testForException( natlink.NatError, "natlink.recognitionMimic('')" ) testForException( natlink.NatError, "natlink.playEvents([])" ) testForException( natlink.NatError, "natlink.inputFromFile('test.wav')" ) testForException( natlink.NatError, "natlink.setTimerCallback(None)" ) testForException( natlink.NatError, "natlink.getTrainingMode()" ) testForException( natlink.NatError, "natlink.startTraining('calibrate')" ) testForException( natlink.NatError, "natlink.finishTraining()" ) testForException( natlink.NatError, "natlink.getAllUsers()" ) testForException( natlink.NatError, "natlink.openUser('testUser')" ) testForException( natlink.NatError, "natlink.saveUser()" ) testForException( natlink.NatError, "natlink.getUserTraining()" ) testForException( natlink.NatError, "natlink.waitForSpeech(0)" ) testForException( natlink.NatError, "natlink.GramObj().load('')" ) testForException( natlink.NatError, "natlink.DictObj()" ) # these functions should all work before natConnect is called natlink.displayText('',0) natlink.getClipboard() natlink.getCallbackDepth() natlink.getCursorPos() natlink.getScreenSize() natlink.setBeginCallback(None) natlink.setChangeCallback(None) natlink.isNatSpeakRunning()
def OnInitDialog (self): rc = dialog.Dialog.OnInitDialog (self) self.HookMessage (self.on_size, win32con.WM_SIZE) self.HookNotify(self.OnListItemChange, commctrl.LVN_ITEMCHANGED) self.HookCommand(self.onStart, IDC_START) self.HookCommand(self.onStop, IDC_STOP) self.itemsControl = self.GetDlgItem(win32ui.IDC_LIST1) self.butOK = self.GetDlgItem(win32con.IDOK) self.butCancel = self.GetDlgItem(win32con.IDCANCEL) self.butStart = self.GetDlgItem(IDC_START) self.butStop = self.GetDlgItem(IDC_STOP) self.Output= self.GetDlgItem(IDC_EDIT) self.Phrase= self.GetDlgItem(IDC_PHRASE) self.SelItems=[] self.FillList() size = self.GetWindowRect() self.LayoutControls(size[2]-size[0], size[3]-size[1]) self.butOK.EnableWindow(0) # wait for first selection self.butStop.EnableWindow(0) # wait for first selection self.SetFocus() #Select First Item self.itemsControl.SetItemState(0,commctrl.LVNI_SELECTED+commctrl.LVNI_FOCUSED,255) self.oldStdout = sys.stdout sys.stdout = self self.message = '' self.grammar = TrainDialogGrammar() self.grammar.initialize(self) self.trainedPhrases=[] natlink.setChangeCallback(self.changeCallback) SetMic(self.MicStart) return rc
def OnInitDialog(self): rc = dialog.Dialog.OnInitDialog(self) self.HookMessage(self.on_size, win32con.WM_SIZE) self.HookNotify(self.OnListItemChange, commctrl.LVN_ITEMCHANGED) self.HookCommand(self.onStart, IDC_START) self.HookCommand(self.onStop, IDC_STOP) self.itemsControl = self.GetDlgItem(win32ui.IDC_LIST1) self.butOK = self.GetDlgItem(win32con.IDOK) self.butCancel = self.GetDlgItem(win32con.IDCANCEL) self.butStart = self.GetDlgItem(IDC_START) self.butStop = self.GetDlgItem(IDC_STOP) self.Output = self.GetDlgItem(IDC_EDIT) self.Phrase = self.GetDlgItem(IDC_PHRASE) self.SelItems = [] self.FillList() size = self.GetWindowRect() self.LayoutControls(size[2] - size[0], size[3] - size[1]) self.butOK.EnableWindow(0) # wait for first selection self.butStop.EnableWindow(0) # wait for first selection self.SetFocus() #Select First Item self.itemsControl.SetItemState( 0, commctrl.LVNI_SELECTED + commctrl.LVNI_FOCUSED, 255) self.oldStdout = sys.stdout sys.stdout = self self.message = '' self.grammar = TrainDialogGrammar() self.grammar.initialize(self) self.trainedPhrases = [] natlink.setChangeCallback(self.changeCallback) SetMic(self.MicStart) return rc
def testPreTests(self): # make sure that NatSpeak is loaded into memory testForException = self.doTestForException # these function should all fail before natConnect is called testForException(natlink.NatError, "natlink.playString('')") testForException(natlink.NatError, "natlink.getCurrentModule()") testForException(natlink.NatError, "natlink.getCurrentUser()") testForException(natlink.NatError, "natlink.getMicState()") testForException(natlink.NatError, "natlink.setMicState('off')") testForException(natlink.NatError, "natlink.execScript('')") testForException(natlink.NatError, "natlink.recognitionMimic('')") testForException(natlink.NatError, "natlink.playEvents([])") testForException(natlink.NatError, "natlink.inputFromFile('test.wav')") testForException(natlink.NatError, "natlink.setTimerCallback(None)") testForException(natlink.NatError, "natlink.getTrainingMode()") testForException(natlink.NatError, "natlink.startTraining('calibrate')") testForException(natlink.NatError, "natlink.finishTraining()") testForException(natlink.NatError, "natlink.getAllUsers()") testForException(natlink.NatError, "natlink.openUser('testUser')") testForException(natlink.NatError, "natlink.saveUser()") testForException(natlink.NatError, "natlink.getUserTraining()") testForException(natlink.NatError, "natlink.waitForSpeech(0)") testForException(natlink.NatError, "natlink.GramObj().load('')") testForException(natlink.NatError, "natlink.DictObj()") # these functions should all work before natConnect is called natlink.displayText('', 0) natlink.getClipboard() natlink.getCallbackDepth() natlink.getCursorPos() natlink.getScreenSize() natlink.setBeginCallback(None) natlink.setChangeCallback(None) natlink.isNatSpeakRunning()
def start(self) -> None: self.logger.info('starting natlink loader') natlink.active_loader = self self._add_dirs_to_path(self.config.directories) if self.config.load_on_startup: self.trigger_load() natlink.setBeginCallback(self.on_begin_callback) natlink.setChangeCallback(self.on_change_callback)
def initialize(): # Redirect standard output and standard error to the # natlink-controlled no save file log window. redirect_std_streams() import dragonfly.log as log global log_; log_ = log director = Director() director.load_modules() natlink.setBeginCallback(director.begin_callback) natlink.setChangeCallback(director.change_callback)
def OnInitDialog(self): rc = dialog.Dialog.OnInitDialog(self) self.HookCommand(self.onButton,IDC_PRESS) self.HookCommand(self.onButton,IDC_CLICK) self.HookCommand(self.onButton,IDC_SPEECH) self.HookCommand(self.onButton,IDC_NOTHING) self.HookCommand(self.onMic,IDC_MIC) self.oldStdout = sys.stdout sys.stdout = self self.message = '' self.grammar = TestGrammar() self.grammar.initialize(self) natlink.setChangeCallback(self.changeCallback)
def start(self) -> None: self.logger.info( f'starting natlink loader from config file:\n\t"{self.config.config_path}"' ) natlink.active_loader = self if not self.config.directories: self.logger.warning( f'Starting Natlink, but no directories to load are specified.\n\tPlease add one or more directories\n\tin config file: "{self.config.config_path}".' ) return # self.logger.debug(f'directories: {self.config.directories}') self._add_dirs_to_path(self.config.directories) if self.config.load_on_startup: # set language property: self.set_user_language() self.trigger_load() natlink.setBeginCallback(self.on_begin_callback) natlink.setChangeCallback(self.on_change_callback)
def disconnect(): """Dicsconnects from SR system. **INPUTS** *none* -- **OUTPUTS** *none* -- """ global sr_is_connected, sr_mic_change_callback if sr_mic_change_callback: natlink.setChangeCallback(None) sr_mic_change_callback = None natlink.natDisconnect() sr_is_connected = 0
def run(): global s global g natlink.natConnect(1) #if natlink.isNatSpeakRunning() else wait try: print 'MicState:', natlink.getMicState() natlink.setBeginCallback(beginCallback) natlink.setChangeCallback(changeCallback) updateGrammarFromClient() natlink.setMicState('on') natlink.waitForSpeech(300) #data = client_socket.recv(512) #data = raw_input ( "SEND( TYPE q or Q to Quit):" ) except: #print sys.exc_info() print traceback.format_exc() finally: g.unload() natlink.natDisconnect() print 'iDisconnected.' s.close() print 'all closed'
def unload(): print "unloading callback" natlink.setChangeCallback(natlinkmain.changeCallback)
import natlink import natlinkmain import pprint import redis def myCallback(command, args): if command == "mic": try: print "setting natlink.mic to %s" % args r = redis.Redis() r.set('natlink.mic', args) except Exception as e: print e print "falied to connect to redis" apply(natlinkmain.changeCallback, [command, args]) natlink.setChangeCallback(myCallback) def unload(): print "unloading callback" natlink.setChangeCallback(natlinkmain.changeCallback)
def start_natlink(doNatConnect=None): """do the startup of the python macros system Better not use doNatConnect, but ensure this is done before calling, and with a finally: natlink.natDisconnect() call """ global loadedFiles print('--') nGrammarsLoaded = len(loadedFiles) if nGrammarsLoaded: if debugLoad: print("unload everything, %s grammars loaded"% nGrammarsLoaded) unloadEverything() else: if debugLoad: print("no grammars loaded yet") if natlinkmainPrintsAtStart: print('-- natlinkmain starting...') if not natlink.isNatSpeakRunning(): print('start Dragon first, then rerun the script natlinkmain...') time.sleep(10) return if not doNatConnect is None: if doNatConnect: print('start_natlink, do natConnect with option 1, threading') natlink.natConnect(1) # 0 or 1, should not be needed when automatic startup else: print('start_natlink, do natConnect with option 0, no threading') natlink.natConnect(0) # 0 or 1, should not be needed when automatic startup print("----natlink.natConnect succeeded") # for modname in ['natlink', 'natlinkmain']: if not baseDirectory in sys.path: sys.path.insert(0,baseDirectory) if debugLoad: print('insert baseDirectory: %s to sys.path!'% baseDirectory) if debugLoad: print(("Natlink base dir" + baseDirectory)) # get the current user information from the Natlink module if userDirectory and os.path.isdir(userDirectory): if not userDirectory in sys.path: sys.path.insert(0,userDirectory) if debugLoad: print('insert userDirectory: %s to sys.path!'% userDirectory) else: if debugLoad: print('userDirectory: %s'% userDirectory) else: if debugLoad: print('no userDirectory') # for unimacro, in order to reach unimacro files to be imported: if unimacroDirectory and os.path.isdir(unimacroDirectory): if status.UnimacroIsEnabled(): if not unimacroDirectory in sys.path: sys.path.insert(0,unimacroDirectory) if debugLoad: print('insert unimacroDirectory: %s to sys.path!'% unimacroDirectory) else: if debugLoad: print('unimacroDirectory: %s'% unimacroDirectory) else: if debugLoad: print('Unimacro not enabled') else: if debugLoad: print('no unimacroDirectory') # setting searchImportDirs, also insert at front of sys.path if not in the list yet. setSearchImportDirs() ## BaseModel, BaseTopic = status.getBaseModelBaseTopic() # load all global files in user directory and current directory # findAndLoadFiles() # initialize our callbacks natlink.setBeginCallback(beginCallback) natlink.setChangeCallback(changeCallback) # init things identical to when user changes: changeCallback('user', natlink.getCurrentUser()) print(('natlinkmain started from %s:\n Natlink version: %s\n DNS version: %s\n Python version: %s\n Windows Version: %s'% \ (status.getCoreDirectory(), status.getInstallVersion(), DNSVersion, status.getPythonVersion(), windowsVersion, ))) if debugLoad: print("userDirectory: %s\nbaseDirectory: %s\nunimacroDirectory: %s\n"% (userDirectory, baseDirectory, unimacroDirectory)) print("loadedFiles: %s"% loadedFiles) print("natlinkmain imported-----------------------------------") elif natlinkmainPrintsAtEnd: if status.UnimacroIsEnabled(): print('Unimacro enabled, UnimacroUserDirectory:\n %s'% status.getUnimacroUserDirectory()) if status.VocolaIsEnabled(): print('Vocola enabled, VocolaUserDirectory:\n %s'% status.getVocolaUserDirectory()) if userDirectory: print("User defined macro's enabled, UserDirectory:\n %s"% userDirectory) print('-'*40) #else: # natlinkLogMessage('natlinkmain started (imported)\n') if status.hadWarning: print('='*30) print(status.getWarningText()) print('='*30) status.emptyWarning()
def set_change_callback(mic_change_callback): global sr_mic_change_callback sr_mic_change_callback = mic_change_callback natlink.setChangeCallback(mic_change_callback)
def start_natlink(doNatConnect=None): """do the startup of the python macros system """ global userDirectory, DNSVersion, baseDirectory, WindowsVersion try: # compute the directory where this module came from if not natlink.isNatSpeakRunning(): print 'start Dragon first, the rerun the script natlinkmain...' time.sleep(10) return if doNatConnect: natlink.natConnect(1) # 0 or 1, should not be needed when automatic startup #print "\n".join(["%s=%s" % (k,v) for k, v in sys.modules ]) #print "\n".join(sys.modules.keys()) for modname in ['natlink', 'natlinkmain']: try: coreDirectory = os.path.split( sys.modules[modname].__dict__['__file__'])[0] except KeyError: pass else: break if debugLoad: print "NatLink pyd dir " + coreDirectory baseDirectory = os.path.normpath(os.path.abspath(os.path.join(coreDirectory,".."))) if debugLoad: print "NatLink base dir" + baseDirectory # get the current user information from the NatLink module userDirectory = status.getUserDirectory() # for unimacro, in order to reach unimacro files to be imported: if userDirectory and os.path.isdir(userDirectory) and not userDirectory in sys.path: if debugLoad: print 'insert userDirectory: %s to sys.path!'% userDirectory sys.path.insert(0,userDirectory) # setting searchImportDirs: setSearchImportDirs() # get invariant variables: DNSVersion = status.getDNSVersion() WindowsVersion = status.getWindowsVersion() # init things identical to when user changes: changeCallback('user', natlink.getCurrentUser()) ## BaseModel, BaseTopic = status.getBaseModelBaseTopic() print 'Starting natlinkmain from %s:\n NatLink version: %s\n DNS version: %s\n Python version: %s\n Windows Version: %s\n'% \ (status.getCoreDirectory(), status.getInstallVersion(), DNSVersion, status.getPythonVersion(), WindowsVersion) # load all global files in user directory and current directory findAndLoadFiles() # initialize our callbacks natlink.setBeginCallback(beginCallback) natlink.setChangeCallback(changeCallback) except: sys.stderr.write( 'Error initializing natlinkmain\n' ) traceback.print_exc() if debugLoad: print "userDirectory: %s\nbaseDirectory: %s"% (userDirectory, baseDirectory) print "natlinkmain imported-----------------------------------" elif natlinkmainPrintsAtEnd: print 'natlinkmain started (imported)\n' else: natlinkLogMessage('natlinkmain started (imported)\n') if status.hadWarning: print '='*30 print status.getWarningText() print '='*30 status.emptyWarning()
def OnDestroy(self,msg): natlink.setChangeCallback(None) self.grammar.terminate() self.grammar = None sys.stdout = self.oldStdout
def start_natlink(doNatConnect=None): """do the startup of the python macros system """ global userDirectory, DNSVersion, baseDirectory, WindowsVersion, unimacroDirectory try: # compute the directory where this module came from if not natlink.isNatSpeakRunning(): print 'start Dragon first, the rerun the script natlinkmain...' time.sleep(10) return if doNatConnect: natlink.natConnect( 1) # 0 or 1, should not be needed when automatic startup #print "\n".join(["%s=%s" % (k,v) for k, v in sys.modules ]) #print "\n".join(sys.modules.keys()) for modname in ['natlink', 'natlinkmain']: try: coreDirectory = os.path.split( sys.modules[modname].__dict__['__file__'])[0] except KeyError: pass else: break if debugLoad: print "NatLink pyd dir " + coreDirectory baseDirectory = os.path.normpath( os.path.abspath(os.path.join(coreDirectory, ".."))) if not baseDirectory in sys.path: sys.path.insert(0, baseDirectory) if debugLoad: print 'insert baseDirectory: %s to sys.path!' % baseDirectory if debugLoad: print "NatLink base dir" + baseDirectory # get the current user information from the NatLink module userDirectory = status.getUserDirectory() if userDirectory: if not userDirectory in sys.path: sys.path.insert(0, userDirectory) if debugLoad: print 'insert userDirectory: %s to sys.path!' % userDirectory else: if debugLoad: print 'userDirectory: %s' % userDirectory else: if debugLoad: print 'no userDirectory' # for unimacro, in order to reach unimacro files to be imported: unimacroDirectory = status.getUnimacroDirectory() if unimacroDirectory: if status.UnimacroIsEnabled(): if not unimacroDirectory in sys.path: sys.path.insert(0, unimacroDirectory) if debugLoad: print 'insert unimacroDirectory: %s to sys.path!' % unimacroDirectory else: if debugLoad: print 'unimacroDirectory: %s' % unimacroDirectory else: if debugLoad: print 'Unimacro not enabled' else: if debugLoad: print 'no unimacroDirectory' # setting searchImportDirs: setSearchImportDirs() # get invariant variables: DNSVersion = status.getDNSVersion() WindowsVersion = status.getWindowsVersion() # init things identical to when user changes: # [MDL: this calls findAndLoadFiles()!] changeCallback('user', natlink.getCurrentUser()) ## BaseModel, BaseTopic = status.getBaseModelBaseTopic() # load all global files in user directory and current directory findAndLoadFiles() # initialize our callbacks natlink.setBeginCallback(beginCallback) natlink.setChangeCallback(changeCallback) print 'natlinkmain started from %s:\n NatLink version: %s\n DNS version: %s\n Python version: %s\n Windows Version: %s\n'% \ (status.getCoreDirectory(), status.getInstallVersion(), DNSVersion, status.getPythonVersion(), WindowsVersion) except: sys.stderr.write('Error initializing natlinkmain\n') traceback.print_exc() if debugLoad: print "userDirectory: %s\nbaseDirectory: %s\nunimacroDirectory: %s\n" % ( userDirectory, baseDirectory, unimacroDirectory) print "natlinkmain imported-----------------------------------" elif natlinkmainPrintsAtEnd: if status.UnimacroIsEnabled(): print 'Unimacro enabled, user directory: %s' % status.getUnimacroUserDirectory( ) if status.VocolaIsEnabled(): print 'Vocola enabled, user directory: %s' % status.getVocolaUserDirectory( ) if userDirectory: print "User defined macro's (UserDirectory) enabled: %s" % userDirectory print '-' * 40 #else: # natlinkLogMessage('natlinkmain started (imported)\n') if status.hadWarning: print '=' * 30 print status.getWarningText() print '=' * 30 status.emptyWarning()
def unload(): natlink.setChangeCallback(original_changeCallback)