def draw_banner(): # Get terminal size and store in globals global NROWS, NCOLS NROWS, NCOLS, hp, wp = struct.unpack( 'HHHH', fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, struct.pack('HHHH', 0, 0, 0, 0))) banner_height = 15 # Make sure we have enough lines to draw banner. Inform user if not if NROWS < banner_height + 5 or NCOLS < 60: mess = 'Please make terminal ' if NROWS < banner_height + 5: mess += 'taller' if NCOLS < 60: mess += ' and wider' if NCOLS < 60: mess += 'wider' MOVETO(1, 1) CLEARLINE() sys.stdout.write(REVERESED) PRINTCENTERED(1, mess) sys.stdout.write(RESET) MOVETO(1, NROWS) sys.stdout.flush() return # Draw border/background sys.stdout.write(BACKGROUND_MAGENTA + BRIGHT_WHITE + BOLD) PRINTAT(1, 1, '-' * NCOLS) for i in range(2, banner_height): PRINTAT(1, i, '|' + ' ' * (NCOLS - 2) + '|') PRINTAT(1, banner_height, '-' * NCOLS) # Draw status PRINTAT(3, 2, 'Num. Events Processed: %d' % jana.GetNeventsProcessed()) #PRINTAT( 3, 3, ' Num. Tasks Completed: %d' % jana.GetNtasksCompleted() ) # This no longer exists PRINTAT(3, 4, ' Num. Cores: %d' % jana.GetNcores()) PRINTAT(3, 5, ' Num. JThreads: %d' % jana.GetNJThreads()) PRINTAT( NCOLS / 2, 2, 'Rate: %5.0fHz (%5.0fHz avg.)' % (jana.GetInstantaneousRate(), jana.GetIntegratedRate())) # Cursor is repositioned and stdout flushed in command_line after calling this # so no need to do it here. Just reset to default drawing options. sys.stdout.write(RESET)
def command_line(): global CLI_ACTIVE, history, mode, BANNER_ON # Record tty settings so we can restore them when finished # processing the CLI mode = termios.tcgetattr(sys.stdin) # Wait for first event to process so all messages are printed # before switching to "raw" mode which screws up newlines while jana.GetNeventsProcessed()<1: time.sleep(1) # Set to raw mode so single characters get passed to us as they are typed tty.setraw(sys.stdin) prompt = 'jana> ' CLI_ACTIVE = True BANNER_ON = True history = [] last_draw_time = 0 while CLI_ACTIVE and not jana.IsDrainingQueues(): # loop for each line # Define data-model for an input-string with a cursor input = "" index = 0 input_save = "" history_index = len(history) sys.stdout.write(prompt) sys.stdout.flush() while CLI_ACTIVE and not jana.IsDrainingQueues(): # loop for each character # Redraw the screen periodically if BANNER_ON and (time.time() - last_draw_time > 1): draw_banner() MOVETO(1,NROWS) if index >= 0: RIGHT( index+len(prompt) ) ; sys.stdout.flush() last_draw_time = time.time() # Wait for a character to be typed. 0.100 is timeout in seconds # Doing this first allows us to check if the app is quitting # even if the user is not typing so *maybe* we can restore the # termios settings before the final report is printed. It also # allows us to update the screen periodically when nothing is # being typed. i, o, e = select.select( [sys.stdin], [], [], 0.100 ) if not i: continue # nothing was typed char = ord(sys.stdin.read(1)) # read one char and get char code # Manage internal data-model if char == 3: return # CTRL-C elif char in {10, 13}: break # LF or CR elif char == 9: # TAB rindex = len(input) - index input = tab_complete(input) index = len(input) - rindex elif 32 <= char <= 126: # Non-special character input = input[:index] + chr(char) + input[index:] index += 1 elif char == 127: # Delete/Backspace input = input[:index-1] + input[index:] index -= 1 elif char == 27: # Left or right arrow start next1, next2 = ord(sys.stdin.read(1)), ord(sys.stdin.read(1)) if next1==91 and next2==68: index = max(0, index - 1) # Left arrow if next1==91 and next2==67: index = min(len(input), index + 1) # Right arrow if next1==91 and next2==66: # Down arrow if history_index < (len(history)-1): history_index += 1 input = history[ history_index ] index = len(input) elif history_index == (len(history)-1): history_index += 1 input = input_save index = len(input) if next1==91 and next2==65: # Up arrow if history_index > 0: if history_index == len(history): input_save = input history_index -= 1 input = history[ history_index ] index = len(input) # else: # print '\nunknown char: %d\n' % int(char) # sys.stdout.flush() # Print current input-string MOVETO(1,NROWS) CLEARLINE() sys.stdout.write(prompt + syntax_highlight(input)) MOVETO(1,NROWS) if index >= 0: RIGHT( index+len(prompt) ) sys.stdout.flush() # Process one input line print '' MOVETO(1,NROWS) termios.tcsetattr(sys.stdin, termios.TCSAFLUSH, mode) process_command( input ) tty.setraw(sys.stdin) MOVETO(1,NROWS) sys.stdout.flush() input = "" index = 0 last_draw_time = 0 # force banner to redraw on next iteration # Restore terminal settings termios.tcsetattr(sys.stdin, termios.TCSAFLUSH, mode) print '\nCLI exiting ...\n'
def process_command( input ): global CLI_ACTIVE, BANNER_ON, mode stripped = input.rstrip() tokens = input.rstrip().split() if len(tokens) < 1: return cmd = tokens[0] args = tokens[1:] print 'processing command: ' + ' '.join(tokens) LEFT(1000) #--- banner if cmd=='banner': if len(args) == 0: print 'command banner requires you to specify "on" or "off". (see help for details)' elif args[0] == 'on' : BANNER_ON = True elif args[0] == 'off': BANNER_ON = False else: print 'command banner requires you to specify "on" or "off". (see help for details)' #--- exit, quit elif cmd=='exit' or cmd=='quit': CLI_ACTIVE = False jana.Quit() #--- help elif cmd=='help': print_help() #--- history elif cmd=='history': idx = 0 for h in history: print '%3d %s\r' % (idx, h) idx += 1 print '%3d %s\r' % (idx, ' '.join(tokens) ) # include this history command which will be added below #--- nthreads elif cmd=='nthreads': if len(args) == 0: print 'Number of JThreads: %d' % jana.GetNJThreads() elif len(args)==1: print 'Number of JThreads now at: %d' % jana.SetNJThreads( int(args[0]) ) else: print 'njthreads takes either 0 or 1 argument (see help for details)' #--- parameter elif cmd=='parameter': if len(args) == 0: print 'command parameter requires arguments. (see help for details)' elif args[0] == 'get': if len(args)==2: print '%s: %s' % (args[1], jana.GetParameterValue(args[1])) else: print 'command parameter get requires exactly 1 argument! (see help for details)' elif args[0] == 'set': if len(args)==3: jana.SetParameter(args[1], args[2]) else: print 'command parameter set requires exactly 2 arguments! (see help for details)' elif args[0] == 'list': if len(args)==1: jana.PrintParameters(True) else: print 'command parameter list requires no arguments! (see help for details)' #--- resume elif cmd=='resume': jana.Resume() print 'event processing resumed' #--- status elif cmd=='status': jana.PrintStatus() print '' #--- stop elif cmd=='stop': jana.Stop(True) print 'event processing stopped after %d events (use "resume" to start again)' % jana.GetNeventsProcessed() #--- unknown command else: print 'Unknown command: ' + cmd return history.append( ' '.join(tokens) ) # add to history
import time import jana jana.SetTicker( False ) jana.WaitUntilAllThreadsRunning() for i in range(0,20): time.sleep(1) print '\nCalled jana.GetNeventsProcessed: %d' % jana.GetNeventsProcessed()
# Copyright 2020, Jefferson Science Associates, LLC. # Subject to the terms in the LICENSE file found in the top-level directory. # # This example JANA python script import time import jana print('Hello from jana.py!!!') # Turn off JANA's standard ticker so we can print our own updates jana.SetTicker(False) # Wait for 4 seconds before allowing processing to start for i in range(1, 5): time.sleep(1) print(" waiting ... %d" % (4 - i)) # Start event processing jana.Start() # Wait for 5 seconds while processing events for i in range(1, 6): time.sleep(1) print(" running ... %d (Nevents: %d)" % (i, jana.GetNeventsProcessed())) # Tell program to quit gracefully jana.Quit()