def sub(self, other, keep=True): # print "sub:", type(other) other_type = str(type(other)) if 'instance' in other_type or 'Money' in other_type: if keep == True: self._cents -= other._cents return self else: return self._cents - other._cents elif 'str' in other_type: if keep == True: self._cents -= Money(other)._cents return self else: return self._cents - Money(other)._cents elif 'long' in other_type: if keep == True: self._cents -= other return self else: return self._cents - other else: reportError("sub: Unrecognized input param (" + str(other) + ") variable type: " + other_type) (frame, filename, line_number, function_name, lines, index) = inspect.getouterframes(inspect.currentframe())[1] print((frame, filename, line_number, function_name, lines, index)) sys.exit(1)
def add(self, other, keep=True): # print "add:", type(other) other_type = str(type(other)) if 'instance' in other_type or 'Money' in other_type: if keep == True: self._cents += other._cents return self else: return self._cents + other._cents elif 'str' in other_type: if keep == True: self._cents += Money(other)._cents return self else: return self._cents + Money(other)._cents elif 'long' in other_type or 'int' in other_type: if keep == True: self._cents += other return self else: return self._cents + other else: (frame, filename, line_number, function_name, lines, index) = inspect.getouterframes(inspect.currentframe())[1] reportError("add: Unrecognized input param (%s) variable type: %s. Stack info:\nframe: %s\nfilename: %s\nline_number: %s\nfunction_name: %s\nlines: %s\nindex: %s\n" % (str(other), other_type, frame, filename, line_number, function_name, lines, index)) sys.exit(1)
def get_command_from_list(id_num, command_list_global): for command in command_list_global: if command['count'] == id_num: return 0, command['command'] msg = "id_num " + str(id_num) + " not found." reportError(msg) return 1, msg
def get_share_price(symbols_csv_string): # Can also use-- http://www.jarloo.com/yahoo_finance/ url = "https://www.google.com/finance/info?q=" + symbols_csv_string rc, results = get_web_data(url, json_data=True) if rc != 0: return rc, reportError("Calling get_web_data(). Symbol: " + symbols_csv_string + ". Results: " + str(results), mode="return_msg_only") # print results # print results.content # for entry in results: # print entry['t'] + ":" + entry['l'] # return 0, results[0]['l'] return_dict = {} for entry in results: # if hasattr(test_flags, '_product_called_from_test'): # if test_flags._product_called_from_test == True: # # called from within a test run # return_dict[entry['t']] = '20.00' # continue # called normally: return_dict[entry['t']] = entry['l'] return 0, return_dict
def set_by_string(self, value='0.00'): self._errors = [] value = value.replace('$', '') # if present if value == '' or re.search('[^0-9\.\$\-]', value): msg = "ERROR: Non-numeric value passed to Money.__set_by_string method. Value = " + value reportError(msg) self._errors.append(msg) (frame, filename, line_number, function_name, lines, index) = inspect.getouterframes(inspect.currentframe())[1] print((frame, filename, line_number, function_name, lines, index)) return self if not '.' in value: value += '.0' dollar_str, cents_str = value.split('.') self._cents = int(dollar_str) * 100 + int(cents_str) # Above statements will set neg sign # if value[0] == '-': # self._cents = -self._cents return self
def run_command(cmd, realtime_output=False, silent=False): if realtime_output == True: output = '' for output_line in read_command_output(cmd): output_line_string = output_line.decode('UTF-8') output_line_string_stripped = output_line_string.rstrip() output += output_line_string # output += output_line_string # print(output_line_string_stripped) sys.stdout.write(output_line_string_stripped + '\n\r') # sys.stdout.write(str(output_line)) error = '' return 0, output, error # import shlex # process = subprocess.Popen(shlex.split(cmd), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) # process = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) if re.search('^I:', cmd): import string rest_of_cmd = string.split(cmd, ':', 1)[1] # just 1 split process = subprocess.Popen(rest_of_cmd, shell=True) else: process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) output, error = process.communicate() # print(56, output) # print(57, error) output = output.decode() # convert from bytes to strings error = error.decode() # convert from bytes to strings # for line in execute_generator(cmd): # print(line) # # Always show any current cmd output before errors. # output = bytes(output).decode('UTF-8') # if str(output) != 'None' and str(output) != '': # print(str(output)) # This decode gets rid of 'u' when outputting strings: # if error != None: # error = bytes(error).decode('UTF-8') # # but it also causes the following error when executing direct commands, e.g., ls: # ERROR: Command cannot be executed: ls. Exception e = 'ascii' codec can't decode byte 0xc2 in position 2733: ordinal not in range(128) if error != None and error != '' and error != b'': return 1, output, reportError('subprocess() error running command: ' + str(cmd) + " . Error: " + str(error), mode='return_msg_only') else: return 0, output, error
def percent(self, other): other_type = str(type(other)) if 'instance' in other_type or 'Money.Money' in other_type: divisor = other._cents elif 'str' in other_type: divisor = Money().set_by_string(other)._cents elif 'long' in other_type or 'int' in other_type: divisor = other elif 'float' in other_type: divisor = other else: reportError("percent: Unrecognized input param (" + str(other) + ") variable type: " + other_type) (frame, filename, line_number, function_name, lines, index) = inspect.getouterframes(inspect.currentframe())[1] print((frame, filename, line_number, function_name, lines, index)) sys.exit(1) if divisor == 0: return 0.00 # percent = long(round((float(self._cents) / float(divisor._cents)) * 100)) percent = (round((float(self._cents) / float(divisor)) * 100, 2)) return percent
def thread_task(threadname, q, func, func_args=None): logging.debug("Entered") logging.debug("thread_task running. os.getpid() = " + str(os.getpid())) logging.debug("thread_task: Before func()") if func_args == None: rc, results = func(threadname) else: rc, results = func(threadname, func_args) logging.debug("thread_task: Returned from func()") if rc != 0: results_formatted = reportError( "In thread_task() for func. Results: " + results, mode="return_msg_only") else: results_formatted = results q.put([threadname, rc, results_formatted]) logging.debug("Adding result to queue. From function " + str(func) + ". rc = " + str(rc) + ". New q.qsize = " + str(q.qsize()) + ". results = " + results_formatted) logging.debug("Exited") return
def get_stock_asset_class(symbols_csv_string): global threadname func_dict = {} all_results = '' # func_call = globals()['get_and_filter_stock_asset_class']() for symbol in symbols_csv_string.split(','): if symbol == 'NOSYMB': all_results += 'NOSYMB:NOSYMB' continue threadname = symbol # func_dict[symbol] = {'func':get_and_filter_stock_asset_class(symbol), 'timeout':5} # func_dict[symbol] = {'func':globals()['get_and_filter_stock_asset_class'](symbol), 'timeout':5} func_dict[symbol] = { 'func': get_and_filter_stock_asset_class, 'args': None, 'timeout': 5 } # func_dict[symbol] = {'func':globals()['get_and_filter_stock_asset_class'](), 'timeout':5} # func_dict[symbol] = {'func':func_call, 'param': symbol, 'timeout':5} # print 72, threadname rc, all_results_temp = launch_threads(func_dict=func_dict) all_results += all_results_temp ''' # url = "http://www.morningstar.com/funds/XNAS/" + symbol + "/quote.html" # url = "http://quotes.morningstar.com/fund/f?t=" + symbol url = "https://www.bloomberg.com/markets/symbolsearch?query=" + symbol + "&commit=Find+Symbols" rc, results = get_web_data(url, json_data=False) if rc != 0: return rc, reportError("Calling get_web_data(). Symbol: " + symbols_csv_string + ". Results: " + str(results), mode="return_msg_only") ''' if rc != 0: return rc, reportError("launch_threads() error. results: " + all_results, mode='return_msg_only') else: return rc, all_results
def get_and_filter_stock_asset_class(threadname, url=''): # global threadname symbol = threadname # url = "https://finance.yahoo.com/quote/" + symbol + "/profile?p=" + symbol ''' # url = "https://www.bloomberg.com/markets/symbolsearch?query=" + symbol + "&commit=Find+Symbols" rc, results = get_web_data(url, json_data=False) # print 60, threadname if rc != 0: return rc, reportError("Calling get_web_data(). Symbol: " + symbols_csv_string + ". Results: " + str(results), mode="return_msg_only") results_list = results.content.split('\n') for index in xrange(len(results_list)): if 'Industry/Objective' in results_list[index]: # print("%s:%s" % (symbol, results_list[index+7].replace('<td>','').replace('</td>', ''))) return 0, symbol + ':' + results_list[index+7].replace('<td>','').replace('</td>', '') return 1, symbol + ':None' ''' ''' url = "https://www.google.com/finance?q=" + symbol rc, results = get_web_data(url, json_data=False) # print 77, threadname if rc != 0: return rc, reportError("Calling get_web_data(). Symbol: " + symbols_csv_string + ". Results: " + str(results), mode="return_msg_only") results_list = results.content.split('\n') for index in xrange(len(results_list)): if 'Morningstar category:' in results_list[index]: # print("%s:%s" % (symbol, results_list[index+7].replace('<td>','').replace('</td>', ''))) return 0, symbol + ':' + re.sub('</*div[^>]*>', '', results_list[index]).replace('Morningstar category: ', '') return 1, symbol + ':None' ''' if symbol == 'NOSYMB': return 0, 'NOSYMB:NOSYMB' url_list = [ "http://www.morningstar.com/funds/XNAS/" + symbol + "/quote.html", "http://www.morningstar.com/funds/XNYS/" + symbol + "/quote.html" ] if url != '': url_list.insert(0, url) return_msg = reportError("Calling get_web_data(). Symbol: " + symbol + ". url_problem .", mode="return_msg_only") rc = 1 for url_entry in url_list: rc, results = get_web_data(url_entry, json_data=False) # print 77, threadname if rc == 0: break return_msg += reportError("Calling get_web_data(). Symbol: " + symbol + ". url_entry = " + url_entry + " . Results: " + str(results), mode="return_msg_only") if rc != 0: return rc, return_msg results_list = str(results.content).split('\n') # print(106, results_list) for index in range(len(results_list)): if '"fundCategoryName":' in results_list[index]: # print("%s:%s" % (symbol, results_list[index+7].replace('<td>','').replace('</td>', ''))) return 0, symbol + ':' + re.sub( '^.*?"fundCategoryName":"*([^"]*)"*,.*$', r'\1', results_list[index]) if not re.search('error', str(results.content), re.IGNORECASE): return 0, symbol + ':Individual_Stocks' return 1, reportError("get ac for symbol " + symbol + " = not_found. url_entry = " + url_entry + " . Results: " + str(results), mode="return_msg_only")
fd.close() return 0, 'success' #==================================================== if __name__ == '__main__': if len(sys.argv) == 1: usage() try: opts, args = getopt.getopt(sys.argv[1:], "", ["debug", "user_created", "one_line", "do_not_report_missing"]) except getopt.GetoptError as err: reportError("Unrecognized runstring " + str(err)) usage() logging_setup(logMsgPrefix=scriptName, logfilename=scriptName + '.log', loglevel=logging.ERROR) options = {} for opt, arg in opts: if opt == "--user_created": options[opt] = True elif opt == "--one_line": options[opt] = True elif opt == "--do_not_report_missing": options[opt] = True elif opt == "--debug": options[opt] = True
def columnize_output(input_data=[], justify_cols='L,R', save_filename=''): global debug # print debug_run_status("columnize_output") debug = debug_option() rc = 0 list_of_lists_of_strings = [] columnized_output_list = [] if debug: print("Entering columnize_output") if debug: print(("input_data = " , input_data)) # print(152, input_data) # print(153, type(input_data)) if 'list' in str(type(input_data)): if 'str' in str(type(input_data[0])): for row in input_data: for entry in csv.reader([row], skipinitialspace=True): list_of_lists_of_strings.append(entry) if 'list' in str(type(input_data[0])): list_of_lists_of_strings = input_data else: if input_data == sys.stdin: reader = csv.reader(input_data) for entry in reader: if debug: print(('49', entry)) list_of_lists_of_strings.append(entry) elif os.path.exists(input_data): csvfile = open( input_data, 'rb') reader = csv.reader( csvfile ) for entry in reader: if debug: print(('49', entry)) list_of_lists_of_strings.append(entry) csvfile.close() else: usage() if test_mode != '' or debug == True: print("All input should be in list_of_lists format at this point:") print(("input_data = ", input_data)) # print list_of_lists_of_strings for row in list_of_lists_of_strings: print(row) if len(list_of_lists_of_strings) == 0: msg = "len(list_of_lists_of_strings) == 0" reportError(msg, mode='return_msg_only') return 1, "line 195" if save_filename != '': save_input_data_to_csv_file(input_data=list_of_lists_of_strings, csv_filename=save_filename) justify_cols_list = justify_cols.split(',') columnized_output_list_of_strings = [] curr_max_num_cols = 0 row_num = 0 # Loop through all rows and if they don't all have the same list length, find out the max number of columns needed. for index in range(len(list_of_lists_of_strings)): row = list_of_lists_of_strings[index] # print(201, row) if 'list' in str(type(row)): if len(row) == 1: continue elif 'str' in str(type(row)): continue for column_index in range(len(list_of_lists_of_strings[index])): if list_of_lists_of_strings[index][column_index] == None: list_of_lists_of_strings[index][column_index] = '' if debug: print(("68", row)) if 'list' not in str(type(row)): reportError("Outer list row is not a list-- Row type: " + str(type(row)) + ". Row num: " + str(row_num) + ". Row: " + str(row)) return 1, "line 221" row_num += 1 if curr_max_num_cols == 0: curr_max_num_cols = len(row) continue if len(row) == curr_max_num_cols: continue # print "debug: " , len(row) , curr_max_num_cols if len(row) == curr_max_num_cols: continue if len(row) > curr_max_num_cols: rc = 2 reportWarning("Detected in columnize_output(), row " + str(row_num) + " contains an extra column: " + str(len(row)) + " instead of " + str(curr_max_num_cols) + ". Row: " + str(row) + ". Pad all previous rows and continue.") curr_max_num_cols = len(row) else: rc = 2 reportWarning("Detected in columnize_output(), row " + str(row_num) + " is missing a column: " + str(len(row)) + " instead of " + str(curr_max_num_cols) + ". Row: " + str(row) + ". Will pad the row and continue.") for index2 in range(len(list_of_lists_of_strings)): # print "len: " + str(len(list_of_lists_of_strings[index2])) # print "len: " + str(list_of_lists_of_strings[index2]) while len(list_of_lists_of_strings[index2]) < curr_max_num_cols: list_of_lists_of_strings[index2].append('') # Pad our justify list if padding was done. while len(justify_cols_list) < curr_max_num_cols: justify_cols_list.append(justify_cols_list[-1]) # Determine the max num of characters for each column. max_col_widths = [] for row in list_of_lists_of_strings: if len(row) == 1: continue for index in range(curr_max_num_cols): # print index # print len(row) # print row # print curr_max_num_cols if type(row[index]) is not string: row[index] = str(row[index]) if len(max_col_widths) < len(row): max_col_widths.append(len(row[index])) else: if max_col_widths[index] < len(row[index]): max_col_widths[index] = len(row[index]) # for col in max_col_widths: print col # print 'end of list' # Create/format the table. columnized_output_list_of_strings = [] for row in list_of_lists_of_strings: # If a row only has one string in it or the row contains one list with one string in it, output the string as is and don't align it. Treat it like a comment or title row. if 'list' in str(type(row)): if len(row) == 1: columnized_output_list_of_strings.append(row[0]) continue elif 'str' in str(type(row)): columnized_output_list_of_strings.append(row) continue line = '' justify_col_curr = -1 curr_col = -1 while True: curr_col += 1 if curr_col >= len(max_col_widths): break if line != '': line += ' ' if justify_col_curr < (len(justify_cols_list)-1): justify_col_curr += 1 if justify_cols_list[justify_col_curr] == 'L': line += "".join(row[curr_col]).ljust(max_col_widths[curr_col]+1) elif justify_cols_list[justify_col_curr] == 'R': line += "".join(row[curr_col]).rjust(max_col_widths[curr_col]+1) else: found = re.search('^( +)$', justify_cols_list[justify_col_curr]) if found: line += found.group(1) curr_col -= 1 else: print("ERROR: Unrecognized justify param = ", justify_cols_list[justify_col_curr]) sys.exit(1) # print "139", line columnized_output_list_of_strings.append(line) if debug: print(("142", len(columnized_output_list_of_strings))) if save_filename != '': if not re.search('\.txt$', save_filename): save_filename += '.txt' with open(save_filename, 'w') as fd: for row in columnized_output_list_of_strings: fd.write(row + '\n') return rc,columnized_output_list_of_strings
for suboption in arg.split(','): if 'wa:' in suboption: trace_options['watch_var_name'] = suboption.split(':')[1] trace_options['watch_var_action'] = 'report_all' elif 'wc:' in suboption: trace_options['watch_var_name'] = suboption.split(':')[1] trace_options['watch_var_action'] = 'report_changed' elif 'f:' in suboption: trace_options[ 'tracePythonLogfileBasename'] = suboption.split(':')[1] else: print("ERROR: Unrecognized suboption = " + suboption) sys.exit(1) trace_mi.enableTrace(trace_options) else: reportError("Unrecognized runstring option: " + opt) usage() stats_dict = {} orphan_list = [] test_result_file_list = [] trf_list_expected = [] trf_list_actual = [] ID_num = -1 search_dir = sys.argv[1] for entry in os.listdir(search_dir): entry_full_path = search_dir + '/' + entry # print(150, entry_full_path) # if len(test_result_file_list) > 0: # pprint(test_result_file_list[-1]) # debug
def launch_threads(func_dict=None): logging.debug("46, here") # global debug debug = debug_option(__file__ + "," + srcLineNum()) if debug: # logging.basicConfig(level=logging.DEBUG, format='[%(levelname)s] (%(threadName)-10s) %(message)s',) logging.basicConfig( level=logging.DEBUG, format= ('%(asctime)s %(levelname)s (%(threadName)-10s) %(funcName)s %(message)s' )) logging.debug("Entered") ''' if commands_string != '': func_dict = {} for entry in commands_string.split('%'): threadname, timeout, command = entry.split(',',2) func_dict[threadname] = {'func':run_command(command), 'timeout':timeout} elif func_dict != None and len(func_dict) > 0: pass else: ''' if func_dict == None: logging.error("ERROR: No commands found to run with launched threads.") return 1, "" logging.debug("main task running. os.getpid() = " + str(os.getpid())) # results_list = [] # results_list_index = -1 for key, value in dict.items(func_dict): logging.debug("threads entry: " + key) threadname = key # results_list.append("placeholder") # Placeholder # results_list_index += 1 # resp_all += "threadname:" + threadname + "\n" # Queses don't work with multiprocessing module. # tid = Process(name=threadname, target=thread_task, args=(results_list_index, str(command), timeout)) # tid = Process(name=threadname, target=thread_task, args=(results_list, str(command), timeout)) # print 83, threadname # global threadname logging.debug("86 " + threadname) func_args = func_dict[threadname].get('args', None) tid = Process(name=threadname, target=thread_task, args=(threadname, q, func_dict[threadname]['func'], func_args)) # tid = threading.Thread(name=threadname, target=thread_task, args=(q, threadname, str(command), timeout)) logging.debug(scriptName + ":" + threadname + ": tid = " + str(tid)) thread_dict[threadname] = tid logging.debug(scriptName + ":" + threadname + ": Creating new thread_dict index " + str(len(thread_dict) - 1) + " for new thread " + str(tid)) tid.start() logging.debug(scriptName + ":" + threadname + ": Thread " + str(tid) + " started") # logging.debug(scriptName + ":" + threadname + ": Number of results " + str(len(results_list))) # for row in results_list: # logging.debug(scriptName + ":" + threadname + ": row = " + row) # if retcode != 0: # resp_all += "ERROR: ssh.py error." # resp_all += ("%d, %s" % (retcode, resp)) # continue # if resp == '': # continue # print resp ''' for line in resp.splitlines(): # print "line: " + line # print resp if re.search('retval:', line) != None: continue # print '[' + user + '@' + threadname if re.search('\\[' + user + '@' + threadname, line) != None: continue # print line ''' # resp_all += resp + "\n" overall_rc = 0 threads_killed = 0 max_retries = 10 attempts = {} return_output = '' finished = False while finished == False: if len(attempts) > 0: finished = True for key, value in dict.items(attempts): if attempts[key] <= max_retries: finished = False break else: logging.info("Thread " + key + ": Max retries reached.") if finished == True: logging.info("Max retries for all threads reached. Aborting.") break logging.debug("Check the status of each thread") finished = True for key, value in dict.items(thread_dict): if not key in attempts: attempts[key] = 0 attempts[key] += 1 logging.debug(key + ": Attempt " + str(attempts[key]) + " of " + str(max_retries)) logging.debug("Is thread alive? Join it: " + str(value)) value.join(5) logging.debug("After join") if value.is_alive(): logging.debug("Thread " + key + " still alive. Status = " + str(value)) finished = False continue else: logging.debug("Thread stopped = " + key) # logging.debug(scriptName + ":" + threadname + ": Number of results " + str(len(results_list))) logging.debug("Terminate/kill thread = " + key) value.terminate() if q.empty(): logging.debug("Thread " + key + " stopped but no result yet : " + str(value)) time.sleep(3) finished = False continue logging.debug("q.qsize = " + str(q.qsize())) logging.debug( "Check the queue for user command output from the thread") while q.qsize() > 0: try: (threadname, rc, q_obj_content) = q.get_nowait() except q.empty(): logging.debug( "Thread " + threadname + " stopped, not q.empty(), but q.get_nowait() returns Empty, try a few more times" ) time.sleep(3) finished = False continue logging.debug("Obtained threadname from queued results: " + threadname) if rc != 0: overall_rc = rc logging.debug("thread_result_from_queue: threadname:" + threadname + '. rc = ' + str(rc) + '. results = ' + q_obj_content) return_output += "\nthreadname:" + threadname + '\n' + q_obj_content logging.debug( "Successfully retrieved queued messsage from thread so reset retries counter to 0 ." ) attempts[threadname] = 0 if re.search('^sleep', q_obj_content): continue logging.debug("Remove " + threadname + " from thread_dict") thread_dict.pop(threadname, None) threads_killed += 1 # logging.debug("resp_all = " + resp_all) # logging.debug(scriptName + ":" + threadname + ": Before collecting results: Number of results " + str(len(results_list))) # return_output = '' # for row in results_list: # return_output += row logging.debug("Final report:") for key, value in dict.items(thread_dict): logging.error("Thread " + key + " still in thread_dict") if value.is_alive(): logging.debug("Thread " + key + " still alive. Status = " + str(value)) logging.debug("Terminate/kill thread = " + key) value.terminate() if value.is_alive(): logging.debug("ERROR: Thread not killed = " + key) logging.debug("Threads killed = " + str(threads_killed)) logging.debug("return_output = vvvvvvvvvv\n" + return_output + "\nend of return_output ^^^^^^^^^^^^^\n") logging.debug("Exited") if overall_rc != 0: return overall_rc, reportError("In launch_threads(). Results: " + return_output, mode="return_msg_only") else: return 0, return_output
def command_list_main_loop(which_command='', extra_params=[], last_command=''): if not os.path.exists(command_list_file_global): fd = open(command_list_file_global, "w") fd.close() try: readline.read_history_file(command_list_readline_history_file) except: pass if invocation_mode == embedded: last_command = '' # else: # last_command = get_last_command_from_history() command_list_global = assemble_command_lists_from_files( last_command=last_command) if which_command != '': which_command_source = 'runstring' else: which_command_source = 'interactive' while True: # print 482, which_command, which_command_source if which_command_source == 'interactive': which_command = user_input( 'Enter number, command to run, history arrow keys, or h for help: ', defaultText=defaultText) # last_command = readline.get_history_item(readline.get_current_history_length()) # if which_command_source == 'runstring': # which_command_source = 'interactive' # After processing runstring command, immediately go into interactive mode. defaultText = '' if which_command == 'l': show_command_list(command_list_global) which_command_source = 'interactive' # if which_command_source == 'runstring': # sys.exit(1) continue if which_command == '': continue if which_command == 'q': sys.exit(1) if which_command == 'h' or which_command == '?': print( "# = Run entry number #. For easier reading for longer entries, entry numbers show up at the end of entries also, e.g., 3 long_entry :3" ) print("#e = Edit and run entry #.") print("l = Show the command list.") print( "key = Use arrow keys to access this script's bash-type command stack history." ) print("al = Add the last command executed to the command list.") print("d N = Delete command N.") print("m N1,N2 = Move N1 command to N2 position.") print( "e = Edit your command list file using $EDITOR. Manually add/delete entries as well." ) print("h = Show this help.") print("r = Show runstring help.") if your_help_function != None: print("s = Show your script " + scriptName_parent + " help screen.") print("q = Quit.") print( "cmd = Type in any os command (e.g., ls) or program runstring with no enclosing quotes." ) if invocation_mode == embedded: print("Embedded global command list file = " + command_list_file_global) else: print("Standalone global command list file = " + command_list_file_global_cl) continue if which_command == 's': if your_help_function != None: if your_help_function_params != None: your_help_function(your_help_function_params) else: your_help_function() continue if which_command == 'r': cl_usage() continue if which_command == 'e': EDITOR = os.environ.get('EDITOR', 'vim') if EDITOR == '': EDITOR = 'vi' if os.path.exists(command_list_file_global): call([EDITOR, command_list_file_global]) # Show refreshed list. command_list_global = assemble_command_lists_from_files() show_command_list(command_list_global) continue if re.search('^m ', which_command): source_position, dest_position = which_command.replace( ' ', ' ').split(' ')[1].replace(' ', '').split(',') source_position_int = int(source_position) - 1 dest_position_int = int(dest_position) - 1 command_list_global = move_command(source_position_int, dest_position_int, command_list_global) show_command_list(command_list_global) continue if re.search('^d [0-9]+$', str(which_command)): position = int(which_command.replace(' ', ' ').split(' ')[1]) command_list_global = delete_command(position, command_list_global) show_command_list(command_list_global) continue if re.search('^al', which_command): if last_command == '': print("No last_command available.") continue command_list_global = assemble_command_lists_from_files() # new_command = ' '.join(which_command.replace(' ', ' ').split(' ')[1:]) command_list_global.append({ 'count': -1, 'type': 'global', 'filename': command_list_file_global, 'command': last_command }) save_command_list(command_list_global) show_command_list(command_list_global) continue if re.search('^[0-9]+e$', str(which_command)): rc, command_from_list = get_command_from_list( int(which_command.split('e')[0]), command_list_global) if rc != 0: reportError("Error getting command from list.") continue defaultText = command_from_list continue if re.search('^[0-9]+$', str(which_command)): which_command_int = int(which_command) if which_command_int > len(command_list_global): reportError("Number too big = " + which_command) continue elif which_command_int <= 0: reportError("Number is too small = " + which_command) continue rc, command_from_list = get_command_from_list( which_command_int, command_list_global) if rc != 0: reportError("Error getting command from list.") continue command_to_run = re.sub('^Last: ', '', command_from_list) # print scriptName_parent, re.sub(' ', ' ', command_to_run).split(' ') # os.execv(scriptName_parent, re.sub(' ', ' ', command_to_run).split(' ')) else: command_to_run = which_command if re.search( "^ *#", command_to_run): # Ignore comment lines in the command_list. continue command_before_env_var_expansion = '' # Works for interactive commands such as vi command_edited = re.sub(' ', ' ', command_to_run).split(' ') for index in xrange(len(extra_params)): command_edited.insert(index + 1, extra_params[index]) command_before_env_var_expansion = ' '.join(command_edited) error_flag = False for index in xrange( len(command_edited )): # look for environment variables and get their values if re.search("^\$", command_edited[index]): env_var = re.sub('^\$', '', command_edited[index]) env_var_value = os.environ.get(env_var) if env_var_value == None: reportError("Environment variable " + command_edited[index] + " is not set for command: " + command_to_run) error_flag = True break command_edited[index] = env_var_value # Command does not specify a directory path? get_first_command_list = command_edited if get_first_command_list[0] != 'cd': scriptDir_get_first_command = os.path.dirname( get_first_command_list[0]) if scriptDir_get_first_command == '': # No directory path, so... if not os.path.isfile(scriptDir_get_first_command ): # can we access it? No, so... get_first_command_list[ 0] = scriptDir + '/' + get_first_command_list[ 0] # Use the scriptDir ussed to call this command_list script. if os.path.isfile(get_first_command_list[0] ): # can we access it? Yes, so... command_edited = get_first_command_list # else: Let the command go through to try it as is. if error_flag == True: continue # print(589, command_edited) ''' rc = call(command_edited) reportError("call() rc = " + str(rc) + ". command_edited = " + str(command_edited)) ''' try: command_edited_string = ' '.join(command_edited) rc, output, error = run_command(command_edited_string) results = str(output) + str(error) if rc != 0: # results2 = reportError("run_command() error: Problem launching or running command or program.", mode='return_msg_only') for line in results.split('\n'): print(line) if 'run_command' in line: print(" run_command() rc = " + str(rc) + ". command_edited_string = " + str(command_edited_string)) # print(results) if which_command_source == 'runstring': # break return 1, results else: if output != None and error != 'None': print(results) except KeyboardInterrupt: print except Exception as e: # reportError("Command cannot be executed: " + str(command_edited_string) + ". Exception e.message = " + str(e.message) + ". e.args = " + str(e.args)) reportError("Command cannot be executed: " + str(command_edited_string) + ". Exception e = " + str(e)) continue # command_list_global, command_list_local = update_last_command_in_command_list(command_to_run, command_list_global, command_list_local) # save_command_list(command_list_global) if command_before_env_var_expansion != '': if command_before_env_var_expansion != last_command: # avoid saving duplicate last commands readline.add_history(command_before_env_var_expansion) readline.write_history_file(command_list_readline_history_file) last_command = command_before_env_var_expansion if which_command_source == 'runstring': break
def command_list(argv=[], your_scripts_help_function=None): global invocation_mode global your_help_function global your_help_function_params if your_scripts_help_function == None: your_help_function = None else: if len(your_scripts_help_function) > 1: your_help_function = your_scripts_help_function[0] your_help_function_params = your_scripts_help_function[1:] else: your_help_function = your_scripts_help_function[0] your_help_function_params = None # How can we tell whether we are getting called in embedded mode vs. standalone mode? # print "realpath: ", os.path.realpath(sys.argv[0]) # print "argv: ", sys.argv[0] import inspect # callerframerecord = inspect.list()[1] # 0 represents this line # frame = callerframerecord[0] # info = inspect.getframeinfo(frame) # print "inspect:", info.filename # print "inspect getfile:", os.path.basename(inspect.getfile(command_list)) # print "__main__", os.path.basename(sys.modules['__main__'].__file__) if os.path.basename(inspect.getfile(command_list)) == os.path.basename( sys.modules['__main__'].__file__): invocation_mode = standalone else: invocation_mode = embedded # print(743, invocation_mode) last_command = '' extra_params = [] which_command = 'l' if invocation_mode == embedded: if len(argv) <= 1: # User specified no params print() print("=======================================") print("For command list help, enter:") print() print(scriptName_parent_help + " --cl h") print("=======================================") print() return else: # User specified some params if argv[1] != '--cl': # But if the first one is not --cl, we'll ignore them and pass them on to the script we're embedded in. return if len(argv) == 2: which_command = 'l' # --cl with no params else: which_command = argv[2] if which_command == 'h': print("=======================================") print() print(scriptName_parent_help + " uses the " + scriptName_cl + " feature.") print() print("=======================================") cl_usage() sys.exit(1) if len(argv) > 3: extra_params = sys.argv[3:] if which_command == 'ag': command_list_global = assemble_command_lists_from_files( last_command='') add_to_command_list(' '.join(extra_params), command_list_global) which_command = 'l' else: # standalone mode try: opts, args = getopt.getopt(sys.argv[1:], "h", ["ag", "h", "help", "cl=", "cl_file="]) except getopt.GetoptError as err: reportError("Unrecognized runstring " + str(err)) cl_usage() return for opt, arg in opts: if opt == '--h' or opt == '--help' or opt == '-h': cl_usage() return if opt == '--cl_file': global command_list_file_global_cl command_list_file_global_cl = arg global command_list_file_global command_list_file_global = command_list_file_global_cl elif opt == '--cl': # if arg == 'last_command': # last_command = argv[2] # del argv[1:2] # elif arg == 'ag': # which_command = ' '.join(argv[1:]) if arg == 'l': which_command = 'l' elif arg == 'h': cl_usage() return elif re.search('[0-9]+', arg): which_command = arg else: reportError("Unrecognized command = " + arg) sys.exit(1) if len(args) > 1: if args[0] == 'h': cl_usage() return elif args[0] == 'ag': extra_params = [] if len(argv) > 3: extra_params = sys.argv[2:] command_list_global = assemble_command_lists_from_files( last_command='') add_to_command_list(' '.join(extra_params), command_list_global) which_command = 'l' else: reportError("Unrecognized command = " + args[0]) command_list_main_loop(which_command, extra_params, last_command=last_command) sys.exit(1)
# global debug debug = debug_option(__file__ + "," + srcLineNum()) if debug: # logging.basicConfig(level=logging.DEBUG, format='[%(levelname)s] (%(threadName)-10s) %(message)s',) logging.basicConfig( level=logging.DEBUG, format= ('%(asctime)s %(levelname)s (%(threadName)-10s) %(funcName)s %(message)s' )) try: opts, args = getopt.getopt(sys.argv[1:], "", ["strings=", "func_example"]) except getopt.GetoptError as err: reportError("ERROR: Unrecognized runstring " + str(err)) usage() commands_string = '' functions_string = '' func_dict = {} for opt, arg in opts: if opt == "--strings": commands_string = arg elif opt == "--func_example": rc, all_results = func_example() else: print("ERROR: Unrecognized runstring option: " + opt) usage()