def run_interactive_shell(command_database): web_shell = WebShell(command_database) print '-' * 80 print 'Welcome to the AWebShell Console.' print print "Type 'webbrowser=disabled' if you wish to disable browser control." print '-' * 80 print print webbrowser_enabled = True try: while (True): #loop forever (well, until an exception...) raw_query = raw_input('> ') if (raw_query.strip() == 'webbrowser=enabled'): webbrowser_enabled = True elif (raw_query.strip() == 'webbrowser=disabled'): webbrowser_enabled = False elif (raw_query.strip() == 'burn-new-test-db-csv-to-gql'): burn_new_test_db_csv_to_gql() elif (raw_query.strip() == 'burn-live-db-gql-to-csv'): burn_live_db_gql_to_csv() else: try: evaluated_url, can_inline = web_shell.evaluate(raw_query) if (evaluated_url): print if (webbrowser_enabled): print 'GOTO', evaluated_url webbrowser.open(evaluated_url) else: print 'RESULT:', evaluated_url print 'CAN_INLINE:', can_inline print except WebShellError, wse: print print 'ERROR: ' + wse.__class__.__name__ + ': ' + str(wse) print except EOFError: pass except KeyboardInterrupt: pass
def run_test_cases(command_database_filename, test_cases_filename): test_command_database = CSVCommandDatabase("test_web_shell_command_database.csv") web_shell = WebShell(test_command_database) csv_file = open(test_cases_filename, "r") csv_reader = csv.reader(csv_file) row_number = 1 successes = 0 failures = 0 first_line_passed = False for row in csv_reader: if not first_line_passed: first_line_passed = True continue if row[0].strip(): command = row[0].strip() if ( command.find("\xc2") != -1 or command.find("\xa0") != -1 ): # detects some garbage that LibreOffice Calc or MS Excel left in the .csv file. raise Exception("bad command string in input file (bad characters): " + repr(command)) expected_result = row[1].strip() try: actual_result, can_inline = web_shell.evaluate(command) if expected_result != actual_result: print "--------------MISMATCH! ROW %i--------------------------------" % row_number print "COMMAND:", repr(command) print "EXPECTED:", expected_result print " ACTUAL:", actual_result print failures += 1 else: successes += 1 except WebShellError, wse: print "--------------MISMATCH! ROW %i--------------------------------" % row_number print "COMMAND:", repr(command) print "ERROR: " + wse.__class__.__name__ + ": " + str(wse) print failures += 1 row_number += 1
class ParseHandler(webapp2.RequestHandler): def __init__(self, *args, **kwargs): webapp2.RequestHandler.__init__(self, *args, **kwargs) self.__command_database = GqlCommandDatabase() self.__web_shell = WebShell(self.__command_database) def get(self, enteredCommand): if self.request.get('q'): enteredCommand = self.request.get('q') # Use q= gttp get var if supplied. #enteredCommand = enteredCommand.replace('+', ' ') #enteredCommand = enteredCommand.replace('+', ' ') #enteredCommand = enteredCommand.replace('%20', ' ') enteredCommandString = urllib.quote(enteredCommand.encode('utf-8'), ' /') # Store string of entered command. enteredCommand = enteredCommand.split() # Split entered command into separate words. if len(enteredCommand) > 0: if enteredCommand[0].lower() == 'create': # User wants to create a command, let's try. self.response.write(create_new_command(enteredCommand)) elif enteredCommand[0].lower() == 'ls': # User is performing a command search. search = enteredCommandString[3:] self.redirect('/ls/' + search) else: # User is performing a command, convert it into a URI and redirect to it. # Send hit to Google Analytics, since we're not displaying a page... thisUUID = uuid.uuid4() httplib.HTTPConnection('www.google-analytics.com/collect/?v=1&tid=UA-34105964-1&cid=thisUUID&an=awebshell&t=event&ec=parse&ea=redirected', 80, timeout=10) try: logging.info('enteredCommandString = ' + repr(enteredCommandString)) final_url, can_inline = self.__web_shell.evaluate(enteredCommandString) self.redirect(final_url.encode('utf-8')) except WebShellError, web_shell_error: exception_name = web_shell_error.__class__.__name__ error_message = str(web_shell_error) logging.info('ERROR: ' + exception_name + ': ' + error_message) #TODO: do something about error, like display the error message and offer a new command entry self.redirect('http://google.com/search?q=' + urllib.quote(enteredCommandString)) #ZZZ default to google search on error. # logging.info("Redirecting to: " + redirectURI) # self.redirect(str(redirectURI)) else:
def __init__(self, *args, **kwargs): webapp2.RequestHandler.__init__(self, *args, **kwargs) self.__command_database = GqlCommandDatabase() self.__web_shell = WebShell(self.__command_database)
def run_one_command(command_database, command_text): web_shell = WebShell(command_database) final_url, can_inline = web_shell.evaluate(command) webbrowser.open(final_url)