Python colors.color() Examples

The following are 28 code examples of colors.color(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module colors , or try the search function .
Example #1
Source File: drive_gdb.py    From dcc with GNU General Public License v3.0 6 votes vote down vote up
def clarify_expression_value(expression_value, expression_type, color):
	debug_print(3, 'clarify_value expression_value=', expression_value)
	
	if expression_type == 'char':
		m = re.match(r"^(-?\d+) '(.*)'$", expression_value)
		if m:
			ascii = int(m.group(1))
			if (0 < ascii < 9) or (13 < ascii < 32)	 or (ascii == 127):
				 expression_value = '%d (non-printable ASCII character)' % ascii
			elif ascii < 0 or ascii > 128:
				 expression_value = '%d (not valid ASCII)' % ascii
			elif ascii == 0:
				 expression_value = "0 = '\\0'"
			else:
				 expression_value = "%s = '%s'" % m.groups()
	return clarify_values(expression_value, color)
	
# transform value into something a novice programmer more likely to understand 
Example #2
Source File: compiler_explanations.py    From dcc with GNU General Public License v3.0 5 votes vote down vote up
def get_short_explanation(self, message, colorize_output):
		match = None
		if self.regex:
			match = re.search(self.regex, "\n".join(message.text_without_ansi_codes), flags=re.I|re.DOTALL)
			if not match:
				return None

		if hasattr(self.precondition, '__call__') :
			r = self.precondition(message, match)
			if not r:
				return None
			if isinstance(r, str) and not self.explanation:
				return r

		if hasattr(self.explanation, '__call__'):
			return self.explanation(message, match)
			
		if colorize_output:
			color = colors.color
		else:
			color = lambda text, *args, **kwargs: text

		parameters = dict((name, getattr(message, name)) for name in dir(message) if not name.startswith('__'))
		parameters['match'] = match
		parameters['color'] = color
		parameters['emphasize'] = lambda text: color(text, style='bold')
		parameters['danger'] = lambda text: color(text, color='red', style='bold')
		parameters['info'] = lambda text: color(text, 'cyan', style='bold')
		return eval('f"' + self.explanation.replace('\n', '\\n') +'"',  globals(), parameters) 
Example #3
Source File: formatters.py    From open-humans with MIT License 5 votes vote down vote up
def format(self, record):
        if record.levelname == "DEBUG":
            record.levelname = color(record.levelname, fg=240)
        elif record.levelname == "INFO":
            record.levelname = color(record.levelname, fg=248)
        elif record.levelname == "WARNING":
            record.levelname = color(record.levelname, fg=220)
        elif record.levelname == "ERROR":
            record.levelname = color(record.levelname, fg=9)

        record.context = color("{0.name}:{0.lineno}".format(record), fg=5)

        return super(LocalFormat, self).format(record) 
Example #4
Source File: drive_gdb.py    From dcc with GNU General Public License v3.0 5 votes vote down vote up
def explain_location(loc, color):
	if not isinstance(loc, Location):
		return "Execution stopped at '%s'" % (loc)
	else:
		return 'Execution stopped ' + loc.long_description(color) 
Example #5
Source File: drive_gdb.py    From dcc with GNU General Public License v3.0 5 votes vote down vote up
def clarify_values(values, color):
	# novices will understand 0x0 better as NULL if it is a pointer
	values = re.sub(r'\b0x0\b', 'NULL', values)
	
	# strip type cast from strings
	values = re.sub(r'^0x[0-9a-f]+\s*(<.str>)?\s*"', '"', values)
	
	# strip type cast from NULL pointers
	values = re.sub(r'^\([^()]+\s+\*\)\s*NULL\b', 'NULL', values)

	# strip type cast from uninitialized valuess
	values = re.sub(r'^\([^()]+\s+\*\)\s*0xbebebebe(\w+)', r'0xbebebebe\1', values)

	values = re.sub(r"'\000'", r"'\\0'", values)

	warning_text = color("<uninitialized value>", 'red')
	
	for value in ['-1094795586', '-1.8325506472120096e-06', '-0.372548997', '-66 (not valid ASCII)', '0xbebebebe', '0xbebebebebebebebe']:
		values = re.sub(r'(^|\D)' + re.escape(value) + r'($|\W)', r'\1' + warning_text + r'\2', values)

	values = re.sub(r"'\\276' <repeats (\d+) times>", color("<\\1 uninitialized values>", 'red'), values)
	
	# convert "\276\276\276" ->  <3 uninitialized values>
	values = re.sub(r'"((\\276)+)"',  lambda m: color("<{} uninitialized values>".format(len(m.group(1))//4), 'red'), values)
	
	# make display of arrays more concise
	if values and values[0] == '{' and len(values) > 128:
		values = re.sub(r'\{(.{100}.*?),.*\}', r'{\1, ...}', values)
		
	return values 
Example #6
Source File: drive_gdb.py    From dcc with GNU General Public License v3.0 5 votes vote down vote up
def relevant_variables(c_source, color, arrays=[]):
	expressions = extract_expressions(c_source)
#	 arrays=[r'[a-z][a-zA-Z0-9_]*']
#	 debug_print(2, 'relevant_variables', arrays, c_source)
#	 for array in arrays:
#		 indices = extract_indices(array, c_source)
#		 expressions += indices

	# avoid trying to evaluate types/keywords for efficiency
	done = set(['NULL', 'char', 'int', 'double', 'while', 'if', 'else', 'for', 'while', 'return', 'main'])

	explanation = ''
	debug_print(3, 'relevant_variables expressions=', c_source, expressions)
	for expression in sorted(expressions, key=lambda e: (len(re.findall(r'\w+', e)), e)):
		try:
			expression = expression.strip()
			if expression not in done:
				done.add(expression)
				expression_value = evaluate_expression(expression, color)
				if expression_value is not None:
					explanation +=	"%s = %s\n" % (expression, expression_value)
		except RuntimeError as e:
			debug_print(2, 'print_variables_expressions: RuntimeError', e)
	if explanation:
		prefix = color('\nValues when execution stopped:', 'cyan')
		explanation = prefix + '\n\n' + explanation
	return explanation 
Example #7
Source File: drive_gdb.py    From dcc with GNU General Public License v3.0 5 votes vote down vote up
def surrounding_source(self, color, radius=2, clean=False, markMiddle=False):
		lines = []
		marked_line = None
		for offset in range(-3*radius, 2*radius):
			line = fileline(self.filename, self.line_number+offset)

			if re.match(r'^\S', line) and offset < 0:
				lines = []

			if markMiddle and offset == 0 and line :
				marked_line = line
				line = color(re.sub(r'^ {0,3}', '-->', line), 'red')
				
			lines.append(clean_c_source(line) if clean else line)
			
			if re.match(r'^\S', line) and offset > 0:
				break

		while lines and re.match(r'^[\s}]*$', lines[0]):
			lines.pop(0)

		while lines and re.match(r'^[\s{]*$', lines[-1]):
			lines.pop()

		if len(lines) == 1 and not marked_line:
			return ''

		return ''.join(lines).rstrip('\n') + '\n' 
Example #8
Source File: drive_gdb.py    From dcc with GNU General Public License v3.0 5 votes vote down vote up
def long_description(self, color):
		where =  'in ' + self.short_description(color)
		source = self.surrounding_source(color, markMiddle=True)
		if source:
			where +=  ':\n\n' + source
		return where 
Example #9
Source File: drive_gdb.py    From dcc with GNU General Public License v3.0 5 votes vote down vote up
def short_description(self, color):
		return self.function_call(color) + ' in ' + self.location(color) 
Example #10
Source File: drive_gdb.py    From dcc with GNU General Public License v3.0 5 votes vote down vote up
def location(self, color):
		return  color(self.filename, 'red') + ' at ' + color('line ' + str(self.line_number), 'red') 
Example #11
Source File: drive_gdb.py    From dcc with GNU General Public License v3.0 5 votes vote down vote up
def explain_asan_error(loc, output_stream, color):	
	if loc:
		print("%s:%d" % (loc.filename, loc.line_number), end=' ', file=output_stream)
	report = os.environ.get('DCC_ASAN_ERROR')
	if report:
		report = report.replace('-', ' ')
		report = report.replace('heap', 'malloc')
		report = report.replace('null deref', 'NULL pointer dereferenced')
	else:
		report = "illegal array, pointer or other operation"
	print('runtime error -', color(report, 'red'), file=output_stream)

	prefix = '\n' + color('dcc explanation:', 'cyan')
	if "malloc buffer overflow" in report:
		print(prefix, """access past the end of malloc'ed memory.
  Make sure you have allocated enough memory for the size of your struct/array.
  A common error is to use the size of a pointer instead of the size of the struct or array.
""", file=output_stream)
		print('For more information see:', explanation_url('malloc_sizeof'), file=output_stream)
	if "stack buffer overflow" in report:
		print(prefix, """access past the end of a local variable.
  Make sure the size of your array is correct.
  Make sure your array indices are correct.
""", file=output_stream)
	elif "use after return" in report:
		print(prefix, """You have used a pointer to a local variable that no longer exists.
  When a function returns its local variables are destroyed.
""", file=output_stream)
		print('For more information see:', explanation_url('stack_use_after_return'), file=output_stream)
	elif "use after" in report:
		print(prefix, "access to memory that has already been freed.\n", file=output_stream)
	elif "double free" in report:
		print(prefix, "attempt to free memory that has already been freed.\n", file=output_stream)
	elif "null" in report.lower():
		print(prefix, "attempt to access value using a pointer which is NULL.\n", file=output_stream) 
Example #12
Source File: drive_gdb.py    From dcc with GNU General Public License v3.0 5 votes vote down vote up
def drive_gdb():
	global debug_level
	windows_subsystem_for_linux = "microsoft" in platform.uname()[3].lower()
	debug_level = int(os.environ.get('DCC_DEBUG', '0'))
	output_stream = os.fdopen(3, "w", encoding='utf-8', errors='replace')
	colorize_output = output_stream.isatty() or os.environ.get('DCC_COLORIZE_OUTPUT', False)
	if colorize_output:
		color = colors.color
	else:
		color = lambda text, *args, **kwargs: text
#	signal.signal(signal.SIGINT, interrupt_handler)

	try:
		gdb_attach()
		pid = os.environ.get('DCC_PID', '')
		sanitizer2_pid = os.environ.get('DCC_SANITIZER2_PID', '')
		sanitizer1_pid = os.environ.get('DCC_SANITIZER1_PID', '')
		if pid and sanitizer2_pid and sanitizer1_pid:
			if pid == sanitizer2_pid:
				os.kill(int(sanitizer1_pid), signal.SIGUSR1)
		explain_error(output_stream, color)
	except gdb.error as e:
		if 'ptrace' in str(e).lower() and os.path.exists('/.dockerenv'):
			print('\ndcc : can not provide information about variables because docker not run with --cap-add=SYS_PTRACE\n' , file=output_stream)
		elif debug_level:
			traceback.print_exc(file=output_stream)
		sys.exit(1)
	except:
		if debug_level:
			traceback.print_exc(file=output_stream)
		sys.exit(1)

	output_stream.flush()
	# __dcc_error_exit hangs for unknown reason on WSL
	if not windows_subsystem_for_linux:
		gdb_execute('call __dcc_error_exit()')
#	kill_all()
	gdb_execute('quit') 
Example #13
Source File: e07_py_monitor_erlang.py    From Pyrlang with Apache License 2.0 5 votes vote down vote up
def main():
    node = Node(node_name="py@127.0.0.1", cookie="COOKIE")
    event_loop = node.get_loop()

    #
    # 1. At the same time as P1 (they should not interfere) create a process P2
    #   Send a message to process example7 on the Erlang node with "test_monitor"
    #   command. This will spawn an Erlang process and tell us the pid.
    #   Reply from Erlang node will trigger next steps above in ExampleProcess6
    #
    proc = MonitorExample7()

    LOG.info("Sending {example7, test_monitor, %s} to remote 'example7'" % proc.pid_)
    remote_receiver_name = (Atom('erl@127.0.0.1'), Atom("example7"))
    send_task = node.send(sender=proc.pid_,
                          receiver=remote_receiver_name,
                          message=(Atom("example7"),
                                   Atom("test_monitor"),
                                   proc.pid_))

    sleep_sec = 5
    LOG.info("Sleep %d sec" % sleep_sec)

    #
    # 3. End, sending a stop message
    #
    def stop_task():
        LOG.info(color("Stopping remote loop", fg="red"))
        node.send_nowait(sender=proc.pid_, receiver=remote_receiver_name,
                         message=(Atom("example7"), Atom("stop")))

    def destroy_task():
        LOG.error("Destroying")
        node.destroy()
        LOG.error("Done")

    event_loop.create_task(send_task)
    event_loop.call_later(sleep_sec, stop_task)
    event_loop.call_later(2 * sleep_sec, destroy_task)
    node.run() 
Example #14
Source File: start_gdb.py    From dcc with GNU General Public License v3.0 5 votes vote down vote up
def watch_stdin_for_valgrind_errors():
	colorize_output = sys.stderr.isatty() or os.environ.get('DCC_COLORIZE_OUTPUT', False)
	if colorize_output:
		color = colors.color
	else:
		color = lambda text, color_name: text
	debug_level = int(os.environ.get('DCC_DEBUG', '0'))
	while True:
		line = sys.stdin.readline()
		if not line:
			break
		if debug_level > 1: print('valgrind: ', line, file=sys.stderr)
		if 'vgdb me' in line:
			error = 'Runtime error: ' + color('uninitialized variable accessed', 'red') + '.'
			os.environ['DCC_VALGRIND_ERROR'] = error
			print('\n'+error, file=sys.stderr)
			sys.stderr.flush()
			start_gdb()
			sys.exit(0)
		elif 'loss record' in line:
			line = sys.stdin.readline()
			if 'malloc' in line:
				line = sys.stdin.readline()
				m = re.search(r'(\S+)\s*\((.+):(\d+)', line)
				if m:
					print('Error: free not called for memory allocated with malloc in function {} in {} at line {}.'.format(m.group(1),m.group(2),m.group(3)), file=sys.stderr)
				else:
					print('Error: free not called for memory allocated with malloc.', file=sys.stderr)
			else:
				print('Error: memory allocated not de-allocated.', file=sys.stderr)
			sys.exit(0) 
Example #15
Source File: termcolors.py    From gitver with Apache License 2.0 5 votes vote down vote up
def prom(self, text):
        """
        Decorate the specified text with the PROMOTED color class
        """
        return self.__decorate(text, color_promoted) 
Example #16
Source File: termcolors.py    From gitver with Apache License 2.0 5 votes vote down vote up
def ver(self, text):
        """
        Decorate the specified text with the VERSION color class
        """
        return self.__decorate(text, color_version) 
Example #17
Source File: termcolors.py    From gitver with Apache License 2.0 5 votes vote down vote up
def next(self, text):
        """
        Decorate the specified text with the NEXT color class
        """
        return self.__decorate(text, color_next) 
Example #18
Source File: termcolors.py    From gitver with Apache License 2.0 5 votes vote down vote up
def tag(self, text):
        """
        Decorate the specified text with the TAG color class
        """
        return self.__decorate(text, color_tag) 
Example #19
Source File: termcolors.py    From gitver with Apache License 2.0 5 votes vote down vote up
def bold(text):
        return color(text, style='bold') 
Example #20
Source File: termcolors.py    From gitver with Apache License 2.0 5 votes vote down vote up
def color_warn(text):
        return color(text, fg=214, style='bold') 
Example #21
Source File: termcolors.py    From gitver with Apache License 2.0 5 votes vote down vote up
def color_promoted(text):
        return color(text, fg=255, bg=33, style='bold') 
Example #22
Source File: termcolors.py    From gitver with Apache License 2.0 5 votes vote down vote up
def color_version(text):
        return color(text, fg=255, bg=25, style='bold') 
Example #23
Source File: termcolors.py    From gitver with Apache License 2.0 5 votes vote down vote up
def color_next(text):
        return color(text, fg=231, bg=28, style='bold') 
Example #24
Source File: termcolors.py    From gitver with Apache License 2.0 5 votes vote down vote up
def color_tag(text):
        return color(text, fg=231, bg=239, style='bold') 
Example #25
Source File: e05_erl_link_monitor_python.py    From Pyrlang with Apache License 2.0 5 votes vote down vote up
def exit(self, reason=None):
        LOG.info("TestMonitorProcess: Received EXIT(%s)" % reason)
        node = self.get_node()
        #
        # 3. End, sending a stop message
        #
        LOG.info(color("Stopping erlang node", fg="red"))
        node.send_nowait(sender=self.pid_, receiver=remote_receiver_name(),
                         message=(Atom("example5"), Atom("stop")))
        LOG.error("Done")
        super().exit(reason) 
Example #26
Source File: e06_py_link_erlang.py    From Pyrlang with Apache License 2.0 5 votes vote down vote up
def main():
    node = Node(node_name="py@127.0.0.1", cookie="COOKIE")
    event_loop = node.get_loop()

    #
    # 1. Create a process P1
    #   Send a message to process example6 on the Erlang node with "test_link"
    #   command. This will spawn an Erlang process and tell us the pid.
    #   Reply from Erlang node will trigger next steps above in ExampleProcess6
    #
    p1 = LinkExample6()

    LOG.info("Sending {example6, test_link, %s} to remote 'example6'" % p1.pid_)
    remote_receiver_name = (Atom('erl@127.0.0.1'), Atom("example6"))

    def send_task():
        node.send_nowait(sender=p1.pid_,
                         receiver=remote_receiver_name,
                         message=(Atom("example6"), Atom("test_link"), p1.pid_))

    sleep_sec = 5
    LOG.info("Sleep %d sec" % sleep_sec)

    #
    # 3. End, sending a stop message
    #
    def task_sleep1():
        LOG.info(color("Stopping remote loop", fg="red"))
        node.send_nowait(sender=p1.pid_,
                         receiver=remote_receiver_name,
                         message=(Atom("example6"), Atom("stop")))

    def task_sleep2():
        node.destroy()
        LOG.error("Done")

    event_loop.call_soon(send_task)
    event_loop.call_later(sleep_sec, task_sleep1)
    event_loop.call_later(2 * sleep_sec, task_sleep2)
    node.run() 
Example #27
Source File: drive_gdb.py    From dcc with GNU General Public License v3.0 4 votes vote down vote up
def explain_error(output_stream, color):
	debug_print(2, 'explain_error() in drive_gdb.py starting')
	# file descriptor 3 is a dup of stderr (see below)
	# stdout & stderr have been diverted to /dev/null
	print(file=output_stream)
	stack = gdb_set_frame()
	loc = stack[0] if stack else None
	signal_number = int(os.environ.get('DCC_SIGNAL', signal.SIGABRT))

	if signal_number != signal.SIGABRT:
		 print(explain_signal(signal_number), file=output_stream)
	elif 'DCC_ASAN_ERROR' in os.environ:
		explain_asan_error(loc, output_stream, color)
	elif 'DCC_UBSAN_ERROR_KIND' in os.environ:
		explain_ubsan_error(loc, output_stream, color)
	elif 'DCC_OUTPUT_ERROR' in os.environ:
		explain_output_difference(loc, output_stream, color)
	elif os.environ.get('DCC_SANITIZER', '') == 'MEMORY':
		if loc:
			print("%s:%d" % (loc.filename, loc.line_number), end=' ', file=output_stream)
		print("runtime error",  color("uninitialized variable used", 'red'),  file=output_stream)

	if loc:
		print(explain_location(loc, color), end='', file=output_stream)
		print(relevant_variables(loc.surrounding_source(color, clean=True), color), end='', file=output_stream)

	if (len(stack) > 1):
		print(color('\nFunction Call Traceback', 'cyan'), file=output_stream)
		for (frame, caller) in zip(stack, stack[1:]):
			print(frame.function_call(color), 'called at line', color(caller.line_number, 'red'), 'of', color(caller.filename, 'red'), file=output_stream)
		print(stack[-1].function_call(color), file=output_stream)

	output_stream.flush()	
	gdb.flush(gdb.STDOUT)
	gdb.flush(gdb.STDERR)

# explain UndefinedBehaviorSanitizer error
# documentation: https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
# source: https://code.woboq.org/gcc/libsanitizer/ubsan/ubsan_handlers.cc.html
#
# There is plenty of room here to provide more specific explanation
# which would be more helpful to novice programmers 
Example #28
Source File: runner.py    From ann-benchmarks with MIT License 4 votes vote down vote up
def run_docker(definition, dataset, count, runs, timeout, batch, cpu_limit,
               mem_limit=None):
    cmd = ['--dataset', dataset,
           '--algorithm', definition.algorithm,
           '--module', definition.module,
           '--constructor', definition.constructor,
           '--runs', str(runs),
           '--count', str(count)]
    if batch:
        cmd += ['--batch']
    cmd.append(json.dumps(definition.arguments))
    cmd += [json.dumps(qag) for qag in definition.query_argument_groups]
    print('Running command', cmd)
    client = docker.from_env()
    if mem_limit is None:
        mem_limit = psutil.virtual_memory().available
    print('Memory limit:', mem_limit)
    if batch:
        cpu_limit = "0-%d" % (multiprocessing.cpu_count() - 1)
    print('Running on CPUs:', cpu_limit)

    container = client.containers.run(
        definition.docker_tag,
        cmd,
        volumes={
            os.path.abspath('ann_benchmarks'):
                {'bind': '/home/app/ann_benchmarks', 'mode': 'ro'},
            os.path.abspath('data'):
                {'bind': '/home/app/data', 'mode': 'ro'},
            os.path.abspath('results'):
                {'bind': '/home/app/results', 'mode': 'rw'},
        },
        cpuset_cpus=cpu_limit,
        mem_limit=mem_limit,
        detach=True)

    def stream_logs():
        for line in container.logs(stream=True):
            print(colors.color(line.decode().rstrip(), fg='blue'))

    if sys.version_info >= (3, 0):
        t = threading.Thread(target=stream_logs, daemon=True)
    else:
        t = threading.Thread(target=stream_logs)
        t.daemon = True
    t.start()
    try:
        exit_code = container.wait(timeout=timeout)

        # Exit if exit code
        if exit_code == 0:
            return
        elif exit_code is not None:
            print(colors.color(container.logs().decode(), fg='red'))
            raise Exception('Child process raised exception %d' % exit_code)

    finally:
        container.remove(force=True)