def load_relation(filename: str, defname:Optional[str]=None) -> Optional[str]: ''' Loads a relation into the set. Defname is the given name to the relation. Returns the name to the relation, or None if it was not loaded. ''' if not os.path.isfile(filename): print(colorize( "%s is not a file" % filename, ERROR_COLOR), file=sys.stderr) return None if defname is None: f = filename.split('/') defname = f[-1].lower() if defname.endswith(".csv"): # removes the extension defname = defname[:-4] if not rtypes.is_valid_relation_name(defname): print(colorize( "%s is not a valid relation name" % defname, ERROR_COLOR), file=sys.stderr) return None try: relations[defname] = relation.relation(filename) completer.add_completion(defname) printtty(colorize("Loaded relation %s" % defname, COLOR_GREEN)) return defname except Exception as e: print(colorize(str(e), ERROR_COLOR), file=sys.stderr) return None
def run(self, cmd_string, docker, cwd): if docker: cmd_string = 'docker run -v {}:/root -w /root {} bash -c "{}"'.format( cwd, docker, cmd_string) # TODO: previously these files were having trailing whitespace stripped when written. Need to move that to when they're read stdout_path = os.path.join(cwd, 'stdout') stderr_path = os.path.join(cwd, 'stderr') stdout_file = open(stdout_path, 'w') stderr_file = open(stderr_path, 'w') print(colorize(cmd_string, ansi=9)) proc = subprocess.Popen(cmd_string, shell=True, universal_newlines=True, stdout=stdout_file, stderr=stderr_file, close_fds=True, cwd=cwd) pid = proc.pid print(colorize("Started as pid {}".format(pid), ansi=9)) self.pid_to_state[pid] = LocalProcState(pid, cwd, (stdout_path, stderr_path), proc) return pid
def run_fail_test(testname): '''Runs a test, which executes a query that is supposed to fail''' print ("Running fail test: " + colorize(testname, COLOR_MAGENTA)) query = readfile('%s%s.fail' % (tests_path, testname)).strip() test_succeed = True try: expr = parser.parse(query) expr(rels) test_succeed = False except: pass try: o_query = optimizer.optimize_all(query, rels) o_expr = parser.parse(o_query) o_expr(rels) test_succeed = False except: pass try: c_expr = parser.tree(query).toCode() eval(c_expr, rels) test_succeed = False except: pass if test_succeed: print (colorize('Test passed', COLOR_GREEN)) else: print (colorize('Test failed (by not raising any exception)', COLOR_RED)) return test_succeed
def check_for_sdk_updates(auto_update_prompt=False): try: theirs = get_latest_version() yours = config.VERSION except LatestVersionCheckError: return if theirs <= yours: return url = 'https://github.com/grow/grow/releases/tag/{}'.format(theirs) logging.info('') logging.info(' Please update to the newest version of the Grow SDK.') logging.info(' See release notes: {}'.format(url)) logging.info(' Your version: {}, latest version: {}'.format( colorize(yours, ansi=226), colorize(theirs, ansi=82))) if utils.is_packaged_app() and auto_update_prompt: if raw_input('Auto update now? [y/N]: ').lower() != 'y': return if subprocess.call(INSTALLER_COMMAND, shell=True) == 0: logging.info('Restarting...') os.execl(sys.argv[0], *sys.argv) # Restart on successful install. else: text = ('In-place update failed. Update manually or use:\n' ' curl https://install.grow.io | bash') logging.error(text) sys.exit(-1) else: logging.info(' Update using: ' + colorize('pip install --upgrade grow', ansi=200)) print ''
def exec_query(command): '''This function executes a query and prints the result on the screen if the command terminates with ";" the result will not be printed ''' # If it terminates with ; doesn't print the result if command.endswith(';'): command = command[:-1] printrel = False else: printrel = True # Performs replacements for weird operators command = replacements(command) # Finds the name in where to save the query parts = command.split('=', 1) relname,query = maintenance.UserInterface.split_query(command) # Execute query try: pyquery = parser.parse(query) result = pyquery(relations) printtty(colorize("-> query: %s" % pyquery, 0x00ff00)) if printrel: print () print (result) relations[relname] = result completer.add_completion(relname) except Exception as e: print (colorize(str(e), ERROR_COLOR))
def load_relation(filename, defname=None): if not os.path.isfile(filename): print (colorize( "%s is not a file" % filename, ERROR_COLOR), file=sys.stderr) return None f = filename.split('/') if defname == None: defname = f[len(f) - 1].lower() if defname.endswith(".csv"): # removes the extension defname = defname[:-4] if not rtypes.is_valid_relation_name(defname): print (colorize( "%s is not a valid relation name" % defname, ERROR_COLOR), file=sys.stderr) return try: relations[defname] = relation.relation(filename) completer.add_completion(defname) printtty(colorize("Loaded relation %s" % defname, 0x00ff00)) return defname except Exception as e: print (colorize(e, ERROR_COLOR), file=sys.stderr) return None
def main(files=[]): printtty(colorize('> ', PROMPT_COLOR) + "; Type HELP to get the HELP") printtty(colorize('> ', PROMPT_COLOR) + "; Completion is activated using the tab (if supported by the terminal)") for i in files: load_relation(i) readline.set_completer(completer.complete) readline.parse_and_bind('tab: complete') readline.parse_and_bind('set editing-mode emacs') readline.set_completer_delims(" ") while True: try: line = input(colorize('> ' if TTY else '', PROMPT_COLOR)) if isinstance(line, str) and len(line) > 0: exec_line(line) except KeyboardInterrupt: if TTY: print ('^C\n') continue else: break except EOFError: printtty() sys.exit(0)
def cli(): parser = argparse.ArgumentParser(description=description, epilog='Created by Scott Frazer (https://github.com/scottfrazer)', formatter_class=RawTextHelpFormatter) parser.add_argument('--version', action='version', version=str(pkg_resources.get_distribution('mdtoc'))) parser.add_argument('markdown_file') parser.add_argument('--check-links', action='store_true', help="Find all hyperlinks and ensure that \nthey point to something valid") cli = parser.parse_args() cli.markdown_file = os.path.expanduser(cli.markdown_file) if not os.path.isfile(cli.markdown_file): sys.exit('File not found: {}'.format(cli.markdown_file)) modify_and_write(cli.markdown_file) if cli.check_links: with open(cli.markdown_file) as fp: contents = fp.read() valid_http_fragments = ['#'+as_link(h) for (l, h) in headers(contents)] for (text, link, line, col) in get_links(contents): valid = 'unrecognized link type' if link.startswith('#'): if link not in valid_http_fragments: valid = colorize('INVALID', ansi=1) else: valid = colorize('VALID', ansi=2) elif link.startswith('http://') or link.startswith('https://'): r = requests.get(link) valid = 'Response: {}'.format(r.status_code) print('Checking {line}:{col} [{text}]({link}) --> {valid} '.format( text=colorize(text, ansi=3), link=colorize(link, ansi=4), line=line, col=col, valid=valid ))
def display_game_agent_state(self, context): self.game_state['current_run_duration'] = (datetime.utcnow() - self.game_state['current_run_started_at']).seconds print("\033c") print("======================================================") print(f"GAME: Cloney PLATFORM: Steam VERSION: v0.0.1") print("======================================================") print("") print(xtermcolor.colorize("OBJECT DETECTION", ansi=9)) print(f"Detected: {len(self.object_predictions)} objects") if self.warning == "HIGH": print(xtermcolor.colorize(f"Danger Level: {self.warning}", ansi=1)) elif self.warning == "SAFE": print(xtermcolor.colorize(f"Danger Level: {self.warning}", ansi=2)) # print(f"DRAGON POS: {self.dragon_object['bb_o'][0]}, {self.dragon_object['bb_o'][1]}, {self.dragon_object['bb_o'][2]}, {self.dragon_object['bb_o'][3]}") # print(f"LAST LEAF POS: {self.leaf_object['bb_o'][0]}, {self.leaf_object['bb_o'][1]}. {self.leaf_object['bb_o'][2]}. {self.leaf_object['bb_o'][3]}") print("") print(xtermcolor.colorize("GAME STATISTICS", ansi=9)) print(f"Current Context: {context}\n") print(f"Current Run: #{self.game_state['current_run']}") print(f"Current Run Duration: {self.game_state['current_run_duration']}s") print("") print(f"Last Run: #{self.game_state['last_run']}") print(f"Last Run Duration: {self.game_state['last_run_duration']}s") print(f"Last Run Duration Actual: {self.game_state['last_run_duration_actual']}") print(f"Last Run Distance: {self.game_state['last_run_distance']}m") print(f"Last Run Coins Collected: {self.game_state['last_run_coins_collected']}") print(f"Record Duration: {self.game_state['record_duration']}s (Run #{self.game_state['record_run']})")
def colored_change(old, new, unit='', inverse=False): condition = old > new if inverse: condition = not condition if condition: return colorize(str(old) + unit, ansi=9) + ' -> ' + colorize(str(new) + unit, ansi=2) else: return colorize(str(old) + unit, ansi=2) + ' -> ' + colorize(str(new) + unit, ansi=9)
def main( files=[ 'samples/people.csv', 'samples/skills.csv', 'samples/rooms.csv' ]): printtty(colorize('> ', PROMPT_COLOR) + "; Type HELP to get the HELP") printtty( colorize('> ', PROMPT_COLOR) + "; Completion is activated using the tab (if supported by the terminal)" ) for i in files: load_relation(i) readline.set_completer(completer.complete) readline.parse_and_bind('tab: complete') readline.parse_and_bind('set editing-mode emacs') readline.set_completer_delims(" ") while True: try: line = input(colorize('> ' if TTY else '', PROMPT_COLOR)) if isinstance(line, str) and len(line) > 0: exec_line(line) except KeyboardInterrupt: if TTY: print('^C\n') continue else: break except EOFError: printtty() sys.exit(0)
def load_relation(filename: str, defname: Optional[str] = None) -> Optional[str]: ''' Loads a relation into the set. Defname is the given name to the relation. Returns the name to the relation, or None if it was not loaded. ''' if not os.path.isfile(filename): print(colorize("%s is not a file" % filename, ERROR_COLOR), file=sys.stderr) return None if defname is None: f = filename.split('/') defname = f[-1].lower() if defname.endswith(".csv"): # removes the extension defname = defname[:-4] if not rtypes.is_valid_relation_name(defname): print(colorize("%s is not a valid relation name" % defname, ERROR_COLOR), file=sys.stderr) return None try: relations[defname] = relation.relation(filename) completer.add_completion(defname) printtty(colorize("Loaded relation %s" % defname, COLOR_GREEN)) return defname except Exception as e: print(colorize(str(e), ERROR_COLOR), file=sys.stderr) return None
def run_fail_test(testname): '''Runs a test, which executes a query that is supposed to fail''' print("Running fail test: " + colorize(testname, COLOR_MAGENTA)) query = readfile('%s%s.fail' % (tests_path, testname)).strip() test_succeed = True try: expr = parser.parse(query) expr(rels) test_succeed = False except: pass try: o_query = optimizer.optimize_all(query, rels) o_expr = parser.parse(o_query) o_expr(rels) test_succeed = False except: pass try: c_expr = parser.tree(query).toCode() eval(c_expr, rels) test_succeed = False except: pass if test_succeed: print(colorize('Test passed', COLOR_GREEN)) else: print(colorize('Test failed (by not raising any exception)', COLOR_RED)) return test_succeed
def run_py_test(testname): '''Runs a python test, which evaluates expressions directly rather than queries''' print("Running expression python test: " + colorize(testname, COLOR_MAGENTA)) try: expr = readfile('%s%s.python' % (tests_path, testname)) result = eval(expr, rels) expr = readfile('%s%s.result' % (tests_path, testname)) exp_result = eval(expr, rels) if result == exp_result: print(colorize('Test passed', COLOR_GREEN)) return True except: pass print(colorize('ERROR', COLOR_RED)) print(colorize('=====================================', COLOR_RED)) print("Expected %s" % exp_result) print("Got %s" % result) print(colorize('=====================================', COLOR_RED)) return False
def check_for_sdk_updates(auto_update_prompt=False): try: theirs = get_latest_version() yours = config.VERSION except LatestVersionCheckError: return if theirs <= yours: return url = 'https://github.com/grow/grow/releases/tag/{}'.format(theirs) logging.info('') logging.info(' Please update to the newest version of the Grow SDK.') logging.info(' See release notes: {}'.format(url)) logging.info(' Your version: {}, latest version: {}'.format( colorize(yours, ansi=226), colorize(theirs, ansi=82))) if utils.is_packaged_app() and auto_update_prompt: # If the installation was successful, restart the process. try: if (raw_input('Auto update now? [y/N]: ').lower() == 'y' and subprocess.call(INSTALLER_COMMAND, shell=True) == 0): logging.info('Restarting...') os.execl(sys.argv[0], *sys.argv) except Exception as e: text = ( 'In-place update failed. Update manually or use:\n' ' curl https://install.growsdk.org | bash') logging.error(text) sys.exit(-1) else: logging.info(' Update using: ' + colorize('pip install --upgrade grow', ansi=200)) print ''
def run_exec_test(testname): '''Runs a python test, which executes code directly rather than queries''' print ("Running python test: " + colorize(testname, COLOR_MAGENTA)) glob = rels.copy() exp_result = {} try: expr = readfile('%s%s.exec' % (tests_path, testname)) try: exec(expr, glob) # Evaluating the expression except Exception as e: print (e) raise Exception("") expr = readfile('%s%s.result' % (tests_path, testname)) exp_result = eval(expr, rels) # Evaluating the expression if isinstance(exp_result, dict): fields_ok = True for i in exp_result: fields_ok = fields_ok and glob[i] == exp_result[i] if fields_ok: print (colorize('Test passed', COLOR_GREEN)) return True except: pass print (colorize('ERROR', COLOR_RED)) print (colorize('=====================================', COLOR_RED)) print ("Expected %s" % exp_result) # print ("Got %s" % glob) print (colorize('=====================================', COLOR_RED)) return False
def setup_lisense(): """ Configure global lisense settings by asking user about the defaults. """ print colorize( "This utility will configure Lisense globally on your system.", ansi=4) print colorize( "Lisense will use this configuration to generate licenses, in case you don't provide any arguments.\n", ansi=4) print colorize( "1. Enter default license that you want to use in your projects?", ansi=253) default_license = raw_input().strip() if check_license(default_license.lower()): pass else: print colorize( "No such license named %s.\nUse command 'lisense list' to see all available licenses." % (default_license), ansi=196) exit() print colorize( "\n2. Enter default owner name that you want to use in your projects?", ansi=253) default_owner = raw_input().strip() set_config(default_license, default_owner)
def generate_license_file(license, owner): """ Generates a license file and populate it accordingly. Asks user for extra context variables if any in the license. """ template_file_path = join(dirname(__file__), "../data/template/%s.tmpl" % (license)) template_file = abspath(template_file_path) with open(template_file) as f: content = f.read() parsed_context = parse_template(content) default_context_keys = ['owner', 'year'] context = {'year': datetime.now().year, 'owner': owner} extra_context = {} if (len(parsed_context) > len(default_context_keys)): for key in parsed_context: if key not in default_context_keys: print colorize( "\n%s license also asks for %s. What do you want to fill there?" % (license, key), ansi=4) extra_context[key] = raw_input().strip() arguments = context.copy() arguments.update(extra_context) template = Template(content) content = template.render(arguments) with open('LICENSE', 'w') as f: f.write(content)
def run_py_test(testname): '''Runs a python test, which evaluates expressions directly rather than queries''' print ("Running expression python test: " + colorize(testname, COLOR_MAGENTA)) try: expr = readfile('%s%s.python' % (tests_path, testname)) result = eval(expr, rels) # Evaluating the expression expr = readfile('%s%s.result' % (tests_path, testname)) exp_result = eval(expr, rels) # Evaluating the expression if result == exp_result: print (colorize('Test passed', COLOR_GREEN)) return True except: pass print (colorize('ERROR', COLOR_RED)) print (colorize('=====================================', COLOR_RED)) print ("Expected %s" % exp_result) print ("Got %s" % result) print (colorize('=====================================', COLOR_RED)) return False
def run_test(testname): '''Runs a specific test executing the file testname.query and comparing the result with testname.result The query will be executed both unoptimized and optimized''' print("Running test: " + colorize(testname, COLOR_MAGENTA)) query = None expr = None o_query = None o_expr = None result_rel = None result = None o_result = None try: result_rel = relation.Relation.load('%s%s.result' % (tests_path, testname)) query = readfile('%s%s.query' % (tests_path, testname)).strip() o_query = optimizer.optimize_all(query, rels) expr = parser.parse(query) result = expr(rels) o_expr = parser.parse(o_query) o_result = o_expr(rels) c_expr = parser.tree(query).toCode() c_result = eval(c_expr, rels) if (o_result == result_rel) and (result == result_rel) and (c_result == result_rel): print(colorize('Test passed', COLOR_GREEN)) return True except Exception as inst: traceback.print_exc(file=sys.stdout) print(inst) pass print(colorize('ERROR', COLOR_RED)) print("Query: %s -> %s" % (query, expr)) print("Optimized query: %s -> %s" % (o_query, o_expr)) print(colorize('=====================================', COLOR_RED)) print(colorize("Expected result", COLOR_GREEN)) print(result_rel.pretty_string(tty=True)) print(colorize("Result", COLOR_RED)) print(result.pretty_string(tty=True)) print(colorize("Optimized result", COLOR_RED)) print(o_result.pretty_string(tty=True)) print( colorize("optimized result match %s" % str(result_rel == o_result), COLOR_MAGENTA)) print( colorize("result match %s" % str(result == result_rel), COLOR_MAGENTA)) print(colorize('=====================================', COLOR_RED)) return False
def execute_tests(): py_bad = 0 py_good = 0 py_tot = 0 q_bad = 0 q_good = 0 q_tot = 0 ex_bad = 0 ex_good = 0 ex_tot = 0 for i in os.listdir(tests_path): if i.endswith('.query'): q_tot += 1 if run_test(i[:-6]): q_good += 1 else: q_bad += 1 elif i.endswith('.python'): py_tot += 1 if run_py_test(i[:-7]): py_good += 1 else: py_bad += 1 elif i.endswith('.exec'): ex_tot += 1 if run_exec_test(i[:-5]): ex_good += 1 else: ex_bad += 1 print (colorize("Resume of the results", COLOR_CYAN)) print (colorize("Query tests", COLOR_MAGENTA)) print ("Total test count: %d" % q_tot) print ("Passed tests: %d" % q_good) if q_bad > 0: print (colorize("Failed tests count: %d" % q_bad, COLOR_RED)) print (colorize("Python tests", COLOR_MAGENTA)) print ("Total test count: %d" % py_tot) print ("Passed tests: %d" % py_good) if py_bad > 0: print (colorize("Failed tests count: %d" % py_bad, COLOR_RED)) print (colorize("Execute Python tests", COLOR_MAGENTA)) print ("Total test count: %d" % ex_tot) print ("Passed tests: %d" % ex_good) if ex_bad > 0: print (colorize("Failed tests count: %d" % ex_bad, COLOR_RED)) print (colorize("Total results", COLOR_CYAN)) if q_bad + py_bad + ex_bad == 0: print (colorize("No failed tests", COLOR_GREEN)) return 0 else: print (colorize("There are %d failed tests" % (py_bad + q_bad + ex_bad), COLOR_RED)) return 1
def compute_graph_matrix(self): """ Compute and return contribution graph matrix """ logger.debug("Printing graph") sorted_normalized_daily_contribution = sorted(self.daily_contribution_map) matrix = [] first_day = sorted_normalized_daily_contribution[0] if first_day.strftime("%A") != "Sunday": c = self._Column(self.width) d = first_day - datetime.timedelta(days=1) while d.strftime("%A") != "Saturday": d = d - datetime.timedelta(days=1) c.append([None, self.width]) matrix.append(c) else: new_column = self._Column(self.width) matrix.append(new_column) for current_day in sorted_normalized_daily_contribution: last_week_col = matrix[-1] day_contribution_color_index = int(self.daily_contribution_map[current_day]) color = self.colors[day_contribution_color_index] try: last_week_col.append([current_day, colorize(self.width, ansi=0, ansi_bg=color)]) except ValueError: # column (e.g. week) has ended new_column = self._Column(self.width) matrix.append(new_column) last_week_col = matrix[-1] last_week_col.append([current_day, colorize(self.width, ansi=0, ansi_bg=color)]) next_day = current_day + datetime.timedelta(days=1) if next_day.month != current_day.month: # if the column we're at isn't 7 days yet, fill it with empty blocks last_week_col.fill() # make new empty col to separate months matrix.append(self._Column(self.width, full_empty_col=True)) matrix.append(self._Column(self.width)) last_week_col = matrix[-1] # update to the latest column # if next_day (which is first day of new month) starts in middle of the # week, prepend empty blocks in the column before inserting 'next day' next_day_num = DAYS.index(next_day.strftime("%A")) last_week_col.fill_by(next_day_num) # make sure that the most current week (last col of matrix) col is of size 7, # so fill it if it's not matrix[-1].fill() return matrix
def start(pod, host=None, port=None, open_browser=False, debug=False, preprocess=True): observer, podspec_observer = file_watchers.create_dev_server_observers(pod) if preprocess: # Run preprocessors for the first time in a thread. thread = threading.Thread(target=pod.preprocess) thread.start() try: # Create the development server. app = main_lib.CreateWSGIApplication(pod) port = 8080 if port is None else int(port) host = 'localhost' if host is None else host num_tries = 0 while num_tries < 10: try: httpd = simple_server.make_server(host, port, app, handler_class=DevServerWSGIRequestHandler) num_tries = 99 except socket.error as e: if e.errno == 48: num_tries += 1 port += 1 else: raise e except Exception as e: logging.error('Failed to start server: {}'.format(e)) observer.stop() observer.join() sys.exit() try: root_path = pod.get_root_path() url = 'http://{}:{}{}'.format(host, port, root_path) print 'Pod: '.rjust(20) + pod.root print 'Address: '.rjust(20) + url print colorize('Server ready. '.rjust(20), ansi=47) + 'Press ctrl-c to quit.' def start_browser(server_ready_event): server_ready_event.wait() if open_browser: webbrowser.open(url) server_ready_event = threading.Event() browser_thread = threading.Thread(target=start_browser, args=(server_ready_event,)) browser_thread.start() server_ready_event.set() httpd.serve_forever() browser_thread.join() except KeyboardInterrupt: logging.info('Goodbye. Shutting down.') httpd.server_close() observer.stop() observer.join() # Clean up once server exits. sys.exit()
def start(pod, host=None, port=None, open_browser=False, debug=False): observer, podspec_observer = file_watchers.create_dev_server_observers(pod) # Run preprocessors for the first time in a thread. thread = threading.Thread(target=pod.preprocess) thread.start() try: # Create the development server. app = main_lib.CreateWSGIApplication(pod) port = 8080 if port is None else int(port) host = 'localhost' if host is None else host num_tries = 0 while num_tries < 10: try: httpd = simple_server.make_server(host, port, app, handler_class=DevServerWSGIRequestHandler) num_tries = 99 except socket.error as e: if e.errno == 48: num_tries += 1 port += 1 else: raise e except Exception as e: logging.error('Failed to start server: {}'.format(e)) observer.stop() observer.join() sys.exit() try: root_path = pod.get_root_path() url = 'http://{}:{}{}'.format(host, port, root_path) print 'Grow SDK (growsdk.org) is in alpha. Thanks for testing and contributing.' print 'Pod: '.rjust(20) + pod.root print 'Address: '.rjust(20) + url print colorize('Server ready.'.rjust(19), ansi=47) + ' Press ctrl-c to quit.' def start_browser(server_ready_event): server_ready_event.wait() if open_browser: webbrowser.open(url) server_ready_event = threading.Event() browser_thread = threading.Thread(target=start_browser, args=(server_ready_event,)) browser_thread.start() server_ready_event.set() httpd.serve_forever() browser_thread.join() except KeyboardInterrupt: logging.info('Goodbye. Shutting down.') httpd.server_close() observer.stop() observer.join() # Clean up once server exits. sys.exit()
def current_thread(sol): for elem in zip(sol.list_boat, sol.list_quays): print( colorize(str(elem[0].type_boat), ansi=30) + " arrive à " + colorize(str(elem[0].arrival_time), ansi=2) + " servi à : " + colorize(str(elem[0].starting_time), ansi=3) + " fini à : " + colorize(str(elem[0].ending_time), ansi=5) + " au quai N° : " + colorize(str(elem[1].lib), ansi=2) + " matricule : " + str(elem[0].name))
def generate_list(): """ Prints listing of all the available licenses. """ print colorize("Lisense currently supports %d licenses.\n" % (len(catalogue)), ansi=4) for label, license in catalogue.iteritems(): print colorize("- %s" % (license), ansi=253)
def run_exec_test(testname): '''Runs a python test, which executes code directly rather than queries''' print "Running python test: " + colorize(testname, COLOR_MAGENTA) glob = rels.copy() exp_result = {} try: expr = readfile('%s%s.exec' % (tests_path, testname)) exec(expr, glob) # Evaluating the expression expr = readfile('%s%s.exec' % (tests_path, testname)) exec(expr, glob) # Evaluating the expression expr = readfile('%s%s.result' % (tests_path, testname)) exp_result = eval(expr, rels) # Evaluating the expression if isinstance(exp_result, dict): fields_ok = True for i in exp_result: fields_ok = fields_ok and glob[i] == exp_result[i] if fields_ok: print colorize('Test passed', COLOR_GREEN) return True except: pass print colorize('ERROR', COLOR_RED) print colorize('=====================================', COLOR_RED) print "Expected %s" % exp_result # print "Got %s" % result print colorize('=====================================', COLOR_RED) return False
def run_test(testname): '''Runs a specific test executing the file testname.query and comparing the result with testname.result The query will be executed both unoptimized and optimized''' print ("Running test: " + colorize(testname, COLOR_MAGENTA)) query = None expr = None o_query = None o_expr = None result_rel = None result = None o_result = None try: result_rel = relation.relation('%s%s.result' % (tests_path, testname)) query = readfile('%s%s.query' % (tests_path, testname)).strip() o_query = optimizer.optimize_all(query, rels) expr = parser.parse(query) # Converting expression to python string result = eval(expr, rels) # Evaluating the expression o_expr = parser.parse( o_query) # Converting expression to python string o_result = eval(o_expr, rels) # Evaluating the expression c_expr = parser.tree(query).toCode() # Converting to python code c_result = eval(c_expr, rels) if (o_result == result_rel) and (result == result_rel) and (c_result == result_rel): print (colorize('Test passed', COLOR_GREEN)) return True except Exception as inst: print (inst) pass print (colorize('ERROR', COLOR_RED)) print ("Query: %s -> %s" % (query, expr)) print ("Optimized query: %s -> %s" % (o_query, o_expr)) print (colorize('=====================================', COLOR_RED)) print (colorize("Expected result", COLOR_GREEN)) print (result_rel) print (colorize("Result", COLOR_RED)) print (result) print (colorize("Optimized result", COLOR_RED)) print (o_result) print (colorize("optimized result match %s" % str(result_rel == o_result), COLOR_MAGENTA)) print (colorize("result match %s" % str(result == result_rel), COLOR_MAGENTA)) print (colorize('=====================================', COLOR_RED)) return False
def machine_translate(pod_path, locale): """Translates the pod message catalog using machine translation.""" root = os.path.abspath(os.path.join(os.getcwd(), pod_path)) pod = pods.Pod(root, storage=storage.FileStorage) pod.catalogs.extract() for locale in locale: catalog = pod.catalogs.get(locale) catalog.update() catalog.machine_translate() print colorize('WARNING! Use machine translations with caution.', ansi=197) print colorize('Machine translations are not intended for use in production.', ansi=197)
def print_server_ready_message(pod, host, port): home_doc = pod.get_home_doc() if home_doc: root_path = home_doc.url.path else: root_path = pod.get_root_path() url = 'http://{}:{}{}'.format(host, port, root_path) print 'Pod: '.rjust(20) + pod.root print 'Address: '.rjust(20) + url print colorize('Server ready. '.rjust(20), ansi=47) + 'Press ctrl-c to quit.' return url
def log_request(self, code=0, size='-'): if not self._logging_enabled: return line = self.requestline[:-9] method, line = line.split(' ', 1) color = 19 if int(code) >= 500: color = 161 code = colorize(code, ansi=color) size = colorize(DevServerWSGIRequestHandler.sizeof(size), ansi=19) self.log_message('{} {} {}'.format(code, line, size))
def quiz(dictionary, number, ranked=False): global words_api_enabled quiz_words = get_quiz_words(dictionary, number) quiz_words_accuracy = get_word_accuracy(quiz_words) quiz_words_iteration = [] if len(quiz_words) == 0: sys.exit('No words to quiz') print(quiz_menu) print(colorize('Set contains {} words:'.format(len(quiz_words)), ansi=4)) print(', '.join([ '{} ({}%)'.format(word, quiz_words_accuracy[word]) for word in quiz_words ])) while True: if len(quiz_words_iteration) == 0: quiz_words_iteration = list(quiz_words) # Copy print(colorize("Reloading words", ansi=4)) word = random.choice(quiz_words_iteration) quiz_words_iteration.remove(word) while True: number = len(quiz_words) - len(quiz_words_iteration) question = "{}. {} [enter/q/m/h/s/w] ".format( number, colorize(word, ansi=1)) ch = prompt(question, ['q', 'm', 'h', 's', 'w', 13]) if ch == 'q': sys.exit(0) elif ch == 'h': print(quiz_menu) continue elif ch == 'm': return elif ch == 's': (rc, out, err) = eggshell.run('say {}'.format(word)) continue elif ch == 'w': words_api_enabled = not words_api_enabled print("WordsAPI Enabled: {}".format(words_api_enabled)) continue elif ord(ch) == 13: print(colorize(dictionary[word], ansi=2)) if words_api_enabled: words_api_definitions(word) break if ranked: correct = prompt("Correct? [y/n/s] ", ['y', 'n', 's']) if correct != 's': now = arrow.now('US/Eastern') with open('results.txt', 'a') as fp: fp.write('{} word={},correct={}\n'.format( now.format('YYYY-MM-DD HH:mm:ss'), word, correct)) print(json.dumps(words, indent=4)) print(ranked)
def log(message, type=None, ansi=ansi_color.blue, tag=''): if type == 'info': print colorize(message, ansi=ansi_color.blue) elif type == 'success': print colorize(message, ansi=ansi_color.green) elif type == 'warning': print colorize(message, ansi=ansi_color.orange) elif type == 'error': print colorize(message, ansi=ansi_color.red) elif type == 'custom': print colorize(message, ansi=ansi) else: print message
def words_api_definitions(word): r = requests.get( 'https://www.wordsapi.com/words/{word}?accessToken={token}'.format( word=word, token=words_api_token)) j = json.loads(r.text) definitions = j['results'] print('\n{}'.format(colorize('WordsAPI Definitions:', ansi=9))) for i, definition in enumerate(definitions): print(' {}. ({}) {}'.format( i + 1, colorize(definition['partOfSpeech'] if 'partOfSpeech' in definition else 'N/A', ansi=6), definition['definition'])) print('')
def log(message, type=None, ansi=ansi_color.blue, tag=''): message = message.encode("ascii", errors="ignore").decode() if type == 'info': print(colorize(message, ansi=ansi_color.blue)) elif type == 'success': print(colorize(message, ansi=ansi_color.green)) elif type == 'warning': print(colorize(message, ansi=ansi_color.orange)) elif type == 'error': print(colorize(message, ansi=ansi_color.red)) elif type == 'custom': print(colorize(message, ansi=ansi)) else: print(message)
def run(self, cmd_string, docker, cwd): if docker: cmd_string = 'docker run -v {}:/root -w /root {} bash -c "{}"'.format( cwd, docker, cmd_string) # TODO: previously these files were having trailing whitespace stripped when written. Need to move that to when they're read stdout_path = os.path.join(cwd, 'stdout') stderr_path = os.path.join(cwd, 'stderr') status_file = os.path.join(cwd, 'status') print(colorize(cmd_string, ansi=9)) with tempfile.NamedTemporaryFile(prefix="command", suffix=".sh", dir=cwd, delete=False, mode='w+t') as fd: script_name = fd.name fd.write("#!/bin/sh\n") fd.write(cmd_string) fd.write("\necho $? > {}\n".format(status_file)) job_name = "job" cmd = [ "qsub", "-N", job_name, "-V", "-b", "n", "-cwd", "-o", stdout_path, "-e", stderr_path, script_name ] print( colorize("executing qsub command: {}".format(" ".join(cmd)), ansi=9)) handle = subprocess.Popen(cmd, stdout=subprocess.PIPE, cwd=cwd) stdout, stderr = handle.communicate() stdout = stdout.decode("utf-8") bjob_id_pattern = re.compile( "Your job (\\d+) \\(.* has been submitted.*") sge_job_id = None for line in stdout.split("\n"): m = bjob_id_pattern.match(line) if m != None: sge_job_id = m.group(1) if sge_job_id == None: raise Exception("Could not parse output from qsub: %s" % stdout) print(colorize("Started as SGE job {}".format(sge_job_id), ansi=9)) self.pid_to_state[sge_job_id] = SGEProcState( sge_job_id, cwd, (stdout_path, stderr_path), None, status_file) return sge_job_id
def map(h2: 'Histogram2D', **kwargs): """Heat map. Note: Available only if xtermcolor present. """ # Value format val_format = kwargs.pop("value_format", ".2f") if isinstance(val_format, str): value_format = lambda val: (("{0:" + val_format + "}").format(val)) data = (h2.frequencies / h2.frequencies.max() * 255).astype(int) # Colour map cmap = kwargs.pop("cmap", DEFAULT_CMAP) if cmap == "Greys": data = 255 - data colorbar_range = range(h2.shape[1] + 1, -1, -1) elif cmap == "Greys_r": colorbar_range = range(h2.shape[1] + 2) else: raise ValueError("Unsupported colormap: {0}, select from: {1}".format(cmap, SUPPORTED_CMAPS)) colors = (65536 + 256 + 1) * data print((value_format(h2.get_bin_right_edges(0)[-1]) + " →").rjust(h2.shape[1] + 2, " ")) print("+" + "-" * h2.shape[1] + "+") for i in range(h2.shape[0]-1, -1, -1): line = [ xtermcolor.colorize("█", bg=0, rgb=colors[i,j]) for j in range(h2.shape[1]) ] line = "|" + "".join(line) + "|" if i == h2.shape[0]-1: line += value_format(h2.get_bin_right_edges(1)[-1]) + " ↑" if i == 0: line += value_format(h2.get_bin_left_edges(1)[0]) + " ↓" print(line) print("+" + "-" * h2.shape[1] + "+") print("←", value_format(h2.get_bin_left_edges(0)[0])) colorbar = [ xtermcolor.colorize("█", bg=0, rgb=(65536 + 256 + 1) * int(j * 255 / (h2.shape[1] + 2))) for j in colorbar_range ] colorbar = "".join(colorbar) print() print("↓", 0) print(colorbar) print(str(h2.frequencies.max()).rjust(h2.shape[1], " "), "↑")
def list(auth, headers, url): if args.ssh: clonetag = 'ssh_url' else: clonetag = args.clonetag repos = requests.get(url=url, auth=auth, headers=headers) if args.verbose: json.dump(repos.json(), sys.stderr, indent=4) rows = repos.json() if type(rows) is dict: #fix for stash rows = rows['values'] for row in rows: if type(row) == dict and clonetag in row.keys(): if args.ignore and os.path.isdir(row['name']): sys.stderr.write(colorize('%s\n' % (row[clonetag]), ansi=118)) sys.stderr.flush() else: sys.stdout.write('%s\n' % row[clonetag]) sys.stdout.flush() if args.verbose: json.dump(row, sys.stderr, indent=4) return
def print_total(self): colors = {} for i, person in enumerate(sorted(list(self.people))): colors[person] = i+1 for (p1, p2), value in self.graph.iteritems(): if value > 0: creditor, debitor = p1, p2 elif value < 0: value *= -1 creditor, debitor = p2, p1 else: continue debitor = colorize(debitor, ansi=colors[debitor]) creditor = colorize(creditor, ansi=colors[creditor]) print("{} owes {} {}".format(debitor, creditor, value)) print("")
def c(v, v_max, pallete=" \\#", color=0): if color: bg = int((v/float(v_max))*((2**24)-1)) fg = (bg + int(color,16)) % ((2**24)-1) return colorize(pallete[v], rgb=fg, bg=bg) else: return pallete[v]
def dbg_format_exception_msg(self, profile, extype, msg, tb): # override message format if configured if 'format' in profile: msg_format = profile['format'] else: msg_format = "[{timestamp}] EXCEPTION:[{thrid}): {msg}\n{trace}" thrid = '0' if multiprocessing.current_process().name == 'MainProcess' else multiprocessing.current_process().name now = datetime.now() now_ms = "{:.3f}".format(round(int(now.strftime("%f")).__float__() / 1000000, 3)) now_ms = now_ms.split('.')[1] tb_lines = traceback.format_list(traceback.extract_tb(tb)) trace = '' for tb_line in tb_lines: trace += "\t{0}".format(tb_line) format_options = { 'timestamp' : now.strftime("%d/%m/%Y %H:%M:%S,{0}".format(now_ms)), 'shorttime' : now.strftime("%H:%M:%S.{0}".format(now_ms)), 'thrid' : thrid, 'msg' : msg, 'extype' : extype.__name__, 'trace' : trace } format_options.update(self._dbg_msg_format_vars) msg_text = msg_format.format(**format_options) # override message color if configured if 'term_color' in profile and profile['term_color'] is not None and '#' == profile['term_color'][0]: rgb_color = profile['term_color'].replace('#', '') msg_text = colorize(msg_text, int(rgb_color, 16)) return msg_text
def dbg_format_msg(self, profile, msg, location={'file': None, 'line': None, 'module': None, 'func': None, 'call_path': None, 'class' : None }, level=1, channel=[]): """Method implements message formating Args: profile (dict) : logger dictionary profile msg (string) : message location (dict) : message trace parameters level (int) : debug level channel (list) : channel filter list Returns: bool: result """ msg_text = False process_msg = False if profile['log_type'] == 'debug': debug_level = self._debug_level if self._debug_level is not None else profile['level'] debug_channel = self._debug_channel if len(self._debug_channel) > 0 else profile['channel'] if (debug_level >= level and self.match_channel(channel, debug_channel) == True): process_msg = True else: process_msg = True if process_msg == True: thrid = '0' if multiprocessing.current_process().name == 'MainProcess' else multiprocessing.current_process().name # override message format if configured if 'format' in profile: msg_format = profile['format'] else: msg_format = '[{timestamp}] DEBUG({level}): {callpath}:{func}:{thrid}: {msg}{lf}' now = datetime.now() now_ms = "{:.3f}".format(round(int(now.strftime("%f")).__float__() / 1000000, 3)) now_ms = now_ms.split('.')[1] format_options = { 'timestamp' : now.strftime("%d/%m/%Y %H:%M:%S,{0}".format(now_ms)), 'shorttime' : now.strftime("%H:%M:%S.{0}".format(now_ms)), 'level' : level, 'file' : location['file'], 'line' : location['line'], 'module' : location['module'], 'callpath' : location['call_path'], 'func' : "{0}.{1}".format(location['class'],location['func']) if location['class'] != None else location['func'], 'thrid' : thrid, 'msg' : msg, 'channel' : channel, } format_options.update(self._dbg_msg_format_vars) msg_text = msg_format.format(**format_options) # override message color if configured if 'term_color' in profile and profile['term_color'] is not None and '#' == profile['term_color'][0]: rgb_color = profile['term_color'].replace('#', '') msg_text = colorize(msg_text, int(rgb_color, 16)) return msg_text
def __format_value(v): if (v.get('@id', None)): return colorize('<{0}>'.format(underline(v['@id'])), rgb=0x66D9EF) elif (v.get('@value', None) is not None): type = v.get('@type', None) if type == 'http://www.w3.org/2001/XMLSchema#integer': return colorize(v['@value'], rgb=0xF92672) elif type == 'http://www.w3.org/2001/XMLSchema#float': return colorize(v['@value'], rgb=0xF92672) elif type == 'http://www.w3.org/2001/XMLSchema#dateTime': return colorize(v['@value'], rgb=0xFD971F) else: return colorize(v['@value'], rgb=0xA6E22E) else: return v
def getName(self, color=False): '''Returns a nice version of the name If color is true, then 256-color escapes will be added to give the name the color of the line''' if self.sname: name = self.sname + ' ' else: name = self.name + ' ' if self.direction is not None: name += self.direction + ' ' if len(name) > 29: name = name[:29] + ' ' if self.wheelchair: name += '♿ ' if self.night: name += '☾ ' name += self.type.symbol while len(name) < 33: name = ' ' + name if not color: return name bgcolor = int('0x' + self.fgColor[1:], 16) fgcolor = int('0x' + self.bgColor[1:], 16) return colorize(name, fgcolor, bg=bgcolor)
def getName(self,color=False): '''Returns a nice version of the name If color is true, then 256-color escapes will be added to give the name the color of the line''' name = self.name name = name.replace(u'Spårvagn','') name = name.replace(u'Buss','') name += " " if self.direction !=None: name+=self.direction+ " " if self.wheelchair: name += u"♿ " if self.night: name += u"☾ " while len(name)<20: name=" "+name if not color: return name bgcolor = int('0x' + self.fgcolor[1:],16) fgcolor = int('0x' + self.bgcolor[1:],16) return colorize(name.encode('utf8'),fgcolor,bg=bgcolor).decode('utf8')
def print_graph_legend(starting_x, y, width, block_seperation_width, colors, screen, term): for color in colors: c = Cursor(y, starting_x, term) value = colorize(width, ansi=color, ansi_bg=color) echo_yx(c, value) screen[y, starting_x] = value starting_x += block_seperation_width
def hash_color(hash): global _hashes_fg, _hashes_bg if hash not in _hashes_fg: _hashes_fg[hash] = rand_fg() _hashes_bg[hash] = rand_bg() return colorize(hash, rgb=_hashes_fg[hash], bg=_hashes_bg[hash])
def get_latest_version(): try: releases = json.loads(urllib.urlopen(RELEASES_API).read()) return releases[0]['tag_name'] except Exception as e: text = 'Cannot check for updates to the SDK while offline.' logging.error(colorize(text, ansi=198)) raise LatestVersionCheckError(str(e))
def test_top_authors_to_string_colorized(): ansii_colors = [1, 2, 3, 4, 5] authors = [("John", 4), ("Jason", 3), ("James", 2), ("Jordon", 2), ("J", 0)] colored = [] for tup in authors: colored.append(colorize(tup[0], ansi=ansii_colors[tup[1]])) colored = ", ".join(colored) assert interactive.top_authors_to_string(authors, colors=ansii_colors) == colored
def refresh(self): """ Print new lantern icon with current parameters """ if self._is_on: print(colorize('\u2B24', rgb=self._color_rgb)) else: print('\u2B55')
def print_server_ready_message(pod, host, port): home_doc = pod.get_home_doc() root_path = home_doc.url.path if home_doc else '/' url = 'http://{}:{}{}'.format(host, port, root_path) logging.info('Pod: '.rjust(20) + pod.root) logging.info('Address: '.rjust(20) + url) ready_message = colorize('Server ready. '.rjust(20), ansi=47) logging.info(ready_message + 'Press ctrl-c to quit.') return url