def outputformat(obj, outputformat='txt'): df = pd.DataFrame(obj) if not df.empty: if outputformat == 'csv': click.echo(df.to_csv(index=False)) elif outputformat == 'html': click.echo(df.to_html(index=False)) elif outputformat == 'pretty-html': colorful_html = highlight(df.to_html(index=False), lexers.HtmlLexer(), formatters.TerminalFormatter()) click.echo(colorful_html) elif outputformat == 'json': click.echo(df.to_json(orient='records')) elif outputformat == 'pretty-json': colorful_json = highlight(df.to_json(orient='records'), lexers.JsonLexer(), formatters.TerminalFormatter()) click.echo(colorful_json) elif outputformat == 'latex': click.echo(df.to_latex(index=False)) elif outputformat == 'raw': click.echo(obj) else: # default to txt click.echo(df.to_string(index=False, )) else: click.secho('Warning: no data found', fg='yellow', err=True)
def emit_old(self, record): try: if record.msg[0] == "servers": if "server" in record.msg[1]: temp = record.msg[1] server = temp['server'] del temp['server'] stdout.write( highlight( dumps({server: temp}, sort_keys=True, indent=4, cls=ComplexEncoder), lexers.JsonLexer(), formatters.TerminalFormatter())) else: stdout.write( highlight( dumps(record.msg, sort_keys=True, indent=4, cls=ComplexEncoder), lexers.JsonLexer(), formatters.TerminalFormatter())) except Exception as e: stdout.write( highlight( dumps({"logger": repr(record)}, sort_keys=True, indent=4, cls=ComplexEncoder), lexers.JsonLexer(), formatters.TerminalFormatter())) stdout.flush()
def colorized(response, response_type): """Colorized responses output \nArguments:\n `response` (str) -- Response string\n `response_type` (str) -- Content-type\n \nReturns:\n `None` """ response_formatted = response # Content-types from response headers # todo: # - Add more types to support see supported lexers by pygment if response_type and "application/json" in response_type: jsonified = json.loads(response) formatted_json = json.dumps(jsonified, sort_keys=True, indent=4) response_formatted = highlight(formatted_json, lexers.JsonLexer(), formatters.TerminalFormatter()) if response_type and "text/html" in response_type: response_formatted = highlight(response, lexers.HtmlLexer(), formatters.TerminalFormatter()) return response_formatted
def get(self, extension_name): """ run the extension """ if config['force_json']: self.set_header("Content-Type", "application/json; charset=UTF-8") extension = self.get_extension(extension_name, 'get') if extension.output == 'combined': retcode, stdout = yield gen.Task(extension.execute, self.params) # Get return values from SDK return_vals = self.find_return_values(stdout) # Get subprocess reutrn code and make that the default # return response. (Can be overriden by the user-supplied vals) return_stat = {"status": retcode} # Merge the above two dicts job_results = return_stat.copy() job_results.update(return_vals) body_struct = {} if config['disable_debug_console'] == False: body_struct['debug'] = { 'out': self.filter_return_values(stdout) } body_struct['request'] = job_results if config['output_highlighter']: body_struct = highlight( unicode(json.dumps(body_struct, indent=5), 'UTF-8'), lexers.guess_lexer(json.dumps(body_struct)), formatters.TerminalFormatter()) self.finish(body_struct) else: retcode, stdout, stderr = yield gen.Task(extension.execute, self.params) return_vals = self.find_return_values(stdout) return_stat = {"status": retcode} job_results = return_stat.copy() job_results.update(return_vals) body_struct = {} if config['disable_debug_console'] == False: body_struct['debug'] = { 'out': self.filter_return_values(stdout), 'out': self.filter_return_values(stderr) } body_struct['request'] = job_results if config['output_highlighter']: body_struct = highlight( unicode(json.dumps(body_struct, indent=5), 'UTF-8'), lexers.guess_lexer(json.dumps(body_struct)), formatters.TerminalFormatter()) self.finish(body_struct)
def post(self, extension_name): """ run the extension """ if config['force_json']: self.set_header("Content-Type", "application/json; charset=UTF-8") extension = self.get_extension(extension_name, 'post') if extension.output == 'combined': retcode, stdout = yield gen.Task(extension.execute, self.params) return_vals = self.find_return_values(stdout) return_stat = {"status": retcode} job_results = return_stat.copy() job_results.update(return_vals) body_struct = {} if config['disable_debug_console'] == False: body_struct['debug'] = { 'out': self.filter_return_values(stdout) } body_struct['request'] = job_results if config['output_highlighter']: body_struct = highlight( unicode(json.dumps(body_struct, indent=5), 'UTF-8'), lexers.guess_lexer(json.dumps(body_struct)), formatters.TerminalFormatter()) self.finish(body_struct) else: retcode, stdout, stderr = yield gen.Task(extension.execute, self.params) return_vals = self.find_return_values(stdout) return_stat = {"status": retcode} job_results = return_stat.copy() job_results.update(return_vals) body_struct = {} if config['disable_debug_console'] == False: body_struct['debug'] = { 'out': self.filter_return_values(stdout), 'err': self.filter_return_values(stderr) } body_struct['request'] = job_results if config['output_highlighter']: body_struct = highlight( unicode(json.dumps(body_struct, indent=5), 'UTF-8'), lexers.guess_lexer(json.dumps(body_struct)), formatters.TerminalFormatter()) self.finish(body_struct)
def formatb(obj, title=None, lvl=1, color=False): if not isinstance(obj, dict): if hasattr(obj, "__dict__"): obj = obj.__dict__ orig = json.dumps(obj, indent=4, sort_keys=True, skipkeys=False, cls=MyEnc) text = eval("u'''%s'''" % orig).encode('utf-8') if color: fmt = formatters.TerminalFormatter() res = highlight(unicode(text, 'UTF-8'), lexers.JsonLexer(), fmt) else: res = text if title is not None: f = sys._getframe(lvl) ln = f.f_lineno fn = f.f_code.co_filename title = "%s |%s:%d" % (title, fn, ln) pre = cp.r("\r\n>>> %s\r\n" % title) pst = cp.r("\r\n<<< %s\r\n" % title) res = pre + res + pst return res
def run_auth(host, verbose): user = click.prompt('Please enter a username', type=str) if len(user) > 32: click.echo(click.UsageError("Username too long.")) click.Context(exit(3)) if click.confirm('Please press the Hue button in order to get the token'): json_data_user = "******"devicetype\":\"my_hue_app#%s\"}" % user r = requests.post("http://%s/api" % host, data=json_data_user) if verbose: click.echo( highlight( json.dumps(json.loads(r.text), sort_keys=True, indent=4), lexers.JsonLexer(), formatters.TerminalFormatter())) else: if 'error' in json.loads(r.text)[0]: click.secho("Error: " + json.loads(r.text)[0]['error']['description'], fg='red') click.Context(exit(100)) if 'success' in json.loads(r.text)[0]: click.secho("Your token: " + json.loads(r.text)[0]['success']['username'], fg='green') click.Context(exit(0)) else: click.secho("unexpected output.", fg='yellow') click.Context(exit(4)) sys.exit(0)
def prettify_json(json_string): formatted_json = json.dumps(json_string, sort_keys=True, indent=4) colorful_json = highlight(unicode(formatted_json, 'UTF-8'), lexers.JsonLexer(), formatters.TerminalFormatter()) print(colorful_json)
def do_updateMetaData(self, args): """Update MetaData of a given path""" if self.checkConnection(): resp = self.commThread.updateMetaData(args.Path, args.Json) print( highlight(resp, lexers.JsonLexer(), formatters.TerminalFormatter()))
def do_authorize(self, args): """Authorize the client to interact with the server""" if self.checkConnection(): resp = self.commThread.authorize(args.Token) print( highlight(resp, lexers.JsonLexer(), formatters.TerminalFormatter()))
def print_content(content: str, filename: Optional[str] = None, lexer: Optional[Any] = None, start_line_number: Optional[int] = None) -> None: from pygments import highlight, lexers, formatters from pygments.lexers import get_lexer_for_filename, guess_lexer if rich_output(): if lexer is None: if filename is None: lexer = guess_lexer(content) else: lexer = get_lexer_for_filename(filename) colorful_content = highlight(content, lexer, formatters.TerminalFormatter()) content = colorful_content.rstrip() if start_line_number is None: print(content, end="") else: content_list = content.split("\n") no_of_lines = len(content_list) size_of_lines_nums = len(str(start_line_number + no_of_lines)) for i, line in enumerate(content_list): content_list[i] = ('{0:' + str(size_of_lines_nums) + '} ').format(i + start_line_number) + " " + line content_with_line_no = '\n'.join(content_list) print(content_with_line_no, end="")
def write(self, obj): formatted_json = json.dumps(obj, default=serialize, sort_keys=True) formatted_yaml = yaml.dump(json.loads(formatted_json)) colorful_yaml = highlight(formatted_yaml, lexers.YamlLexer(), formatters.TerminalFormatter()) return colorful_yaml
def print_channel_list(client, max_limit=2000): """ (Clubhouse) -> NoneType Print list of channels """ # Get channels and print out console = Console() table = Table(show_header=True, header_style="bold magenta") table.add_column("") table.add_column("channel_name", style="cyan", justify="right") table.add_column("topic") table.add_column("club_name") table.add_column("speaker_count") table.add_column("speakers") # SarveshRanjan user_id=1665037778 # channels = client.follow_club(club_id="1091619179") clubID = input("[.] Enter clubID to follow: ") formatted_json = json.dumps(client.follow_club(clubID), sort_keys=True, indent=4) colorful_json = highlight(formatted_json, lexers.JsonLexer(), formatters.TerminalFormatter()) print(colorful_json)
def write_error(self, status_code, **kwargs): """ return an exception as an error json dict """ if kwargs['exc_info'] and hasattr(kwargs['exc_info'][1], 'log_message'): message = kwargs['exc_info'][1].log_message else: # TODO: What should go here? message = [-1] body_struct = {} if config['disable_debug_console'] == False: body_struct['debug'] = {'err': message, 'out': [-1]} body_struct['request'] = { 'status': status_code, 'troubleshoot': [httplib.responses[status_code].upper().replace(" ", "_")], 'status': "500 Internal Server Error" } if config['output_highlighter']: body_struct = highlight( unicode(json.dumps(body_struct, indent=5), 'UTF-8'), lexers.guess_lexer(json.dumps(body_struct)), formatters.TerminalFormatter()) self.write(body_struct)
def nmap_scan(data, host): #scan tcp ports if 'tcp' in data: nmap = nmap3.NmapScanTechniques() result = nmap.nmap_tcp_scan(host) #scan udp ports elif 'udp' in data: nmap = nmap3.NmapScanTechniques() result = nmap.nmap_udp_scan(host) #scan top ports elif 'top port' in data: nmap = nmap3.Nmap() result = nmap.scan_top_ports(host) #scan for subdomains elif 'subdomains' in data: nmap = nmap3.Nmap() result = nmap.nmap_dns_brute_script("domain") #scon for detect os elif 'os' in data: nmap = nmap3.Nmap() result = nmap.nmap_os_detection("your-host") #print in terminal with json format colored_json = highlight(json.dumps(result, indent=4, sort_keys=True), lexers.JsonLexer(), formatters.TerminalFormatter()) print("\n\n", colored_json)
def stderr(exception, ctx=None): try: LOGGER.error(traceback.format_exc()) except Exception: LOGGER.error(exception) if type(exception) == VcdErrorResponseException: message = str(exception) elif hasattr(exception, 'message'): message = exception.message else: message = str(exception) if ctx is not None and ctx.find_root().params['json_output']: message = {'error': str(message)} text = json.dumps(message, sort_keys=True, indent=4, separators=(',', ': ')) if sys.version_info[0] < 3: text = str(text, 'utf-8') message = highlight(text, lexers.JsonLexer(), formatters.TerminalFormatter()) click.echo(message) sys.exit(1) else: message = Fore.RED + str(message) + Fore.BLACK click.echo('\x1b[2K\r', nl=False) if ctx is not None: ctx.fail(message) else: click.echo(message) sys.exit(1)
def main(argv=None): # We REALLY don't want to be using the cache for this stuff. os.environ.pop('SGCACHE', None) parser = argparse.ArgumentParser() parser.add_argument('-v', '--verbose', action='store_true') parser.add_argument('-p', '--pretty', action='store_true') parser.add_argument('-C', '--colour', action='store_true') parser.add_argument('-o', '--output') parser.add_argument('--last-id', type=int) args = parser.parse_args(argv) if args.colour: from pygments import highlight, lexers, formatters setup_logs(debug=args.verbose) out_fh = open(args.output, 'w') if args.output else None event_log = EventLog(last_id=args.last_id) for event in event_log.iter_events_forever(): encoded = json.dumps(dict(event), default=json_default, indent=4 if args.pretty else None, sort_keys=True) if out_fh: out_fh.write(encoded + '\n\n') if args.colour: encoded = highlight(unicode(encoded, 'UTF-8'), lexers.JsonLexer(), formatters.TerminalFormatter()) print encoded print sys.stdout.flush()
def main(xsd_file, pretty): """ CLI interface for parsing XSD file """ json_schema = xsd_to_json_schema(xsd_file) if pretty: json_schema = highlight(json_schema, lexers.JsonLexer(), formatters.TerminalFormatter()) print(json_schema)
def print_content(content, filename=None, lexer=None, start_line_number=None): from pygments import highlight, lexers, formatters from pygments.lexers import get_lexer_for_filename, guess_lexer if rich_output(): if lexer is None: if filename is None: lexer = guess_lexer(content) else: lexer = get_lexer_for_filename(filename) colorful_content = highlight(content, lexer, formatters.TerminalFormatter()) if start_line_number is None: print(colorful_content, end="") return colorful_content_list = colorful_content.split("\n") no_of_lines = len(colorful_content_list) size_of_lines_nums = len(str(no_of_lines)) for i in range(start_line_number, start_line_number + no_of_lines): colorful_content_list[i] = ( '{0:' + str(size_of_lines_nums) + '}').format(i) + " " + colorful_content_list[i] colorful_content_with_line_no = '\n'.join(colorful_content_list) print(colorful_content_with_line_no, end="") else: print(content, end="")
def colorizer(content): # dump to hcl is not supported, so fallback to json return highlight( content, lexers.JsonLexer(), # lexers.TerraformLexer() formatters.TerminalFormatter(), )
def print_colorful_json(data: dict): """Colored dump JSON object to stdout.""" # https://stackoverflow.com/a/32166163/315168 formatted_json = json.dumps(data, sort_keys=True, indent=4) colorful_json = highlight(formatted_json, lexers.JsonLexer(), formatters.TerminalFormatter()) print(colorful_json)
def pretty_json(obj: dict, no_color: bool): obj = json.dumps(obj, indent=4, ensure_ascii=False, sort_keys=True) if no_color: return obj else: return highlight(obj, lexers.JsonLexer(), formatters.TerminalFormatter())
def print_json(res): res_str = json.dumps(res, indent=2) if system() != "Windows": res_str = highlight(res_str.encode('UTF-8'), lexers.JsonLexer(), formatters.TerminalFormatter()).strip() click.echo(res_str)
def prettyjson(req): formatted_json = json.dumps(req, sort_keys=True, indent=4) return highlight( str(formatted_json).encode("utf-8"), lexers.JsonLexer(), formatters.TerminalFormatter(), )
def color_json(json_str): """ Given an already formatted JSON string, return a colored variant which will produce colored output on terminals. """ assert(type(json_str) == six.text_type) return highlight(json_str, lexers.JsonLexer(), formatters.TerminalFormatter())
def main(config): with open(config) as file: config_data = json.load(file) tunnel = SSHTunnel(config_data["local_port"], config_data["remote_port"], config_data["remote_user"], config_data["remote_host"]) tunnel.start() time.sleep(1) headers = {'content-type': 'text/plain;'} data = '{"jsonrpc":"1.0","id":"curltext","method":"listunspent","params":[]}' response = requests.post( "http://127.0.0.1:{}/".format(config_data["local_port"]), headers=headers, data=data, auth=(config_data["rpc_user"], config_data["rpc_password"])) response_dict = response.json() my_vals = [{ 'txid': x['txid'], 'amount': x['amount'], 'address': x['address'], 'vout': x['vout'] } for x in response_dict['result']] print( highlight(json.dumps(my_vals, indent=2), lexers.JsonLexer(), formatters.TerminalFormatter()))
def do_getMetaData(self, args): """Get MetaData of the parameter""" resp = self.getMetaData(args.Parameter) print( highlight(resp, lexers.JsonLexer(), formatters.TerminalFormatter())) self.pathCompletionItems = []
def do_updateVSSTree(self, args): """Update VSS Tree Entry""" if self.checkConnection(): resp = self.commThread.updateVSSTree(args.Json) print( highlight(resp, lexers.JsonLexer(), formatters.TerminalFormatter()))
def call_nl_api(text): service = get_service('language', 'v1') service_request = service.documents().annotateText( body={ 'document': { 'type': 'PLAIN_TEXT', 'content': text, }, 'features': { "extractSyntax": True, "extractEntities": True, "extractDocumentSentiment": True, } }) response = service_request.execute() print( colored("\nHere's the JSON repsonse" + "for one token of your text:\n", "cyan")) formatted_json = json.dumps(response['tokens'][0], indent=2) colorful_json = highlight(formatted_json, lexers.JsonLexer(), formatters.TerminalFormatter()) print(colorful_json) score = response['documentSentiment']['score'] output_text = colored(analyze_sentiment(score), "cyan") if response['entities']: entities = str(analyze_entities(response['entities'])) output_text += colored("\nEntities found: " + entities, "white") return [output_text, response['language']]
async def _run(self): """ Процесс прослушивания LongPoll и обработки событий реакциями """ await self.lp.get_info() async for events in self.lp: for event in events: if self.debug and self.reactions.has_event(event.type): click.clear() data = json.dumps( event._mapping, ensure_ascii=False, indent=4 ) data = highlight( data, lexers.JsonLexer(), formatters.TerminalFormatter(bg="light"), ) self.debug_out( f"{'=' * 35}\nBelow is the current handled event\n{'=' * 35}\n" ) # print("=" * 35, "Below is the current handled event\n", sep="\n", end="=" * 35 + "\n") self.debug_out(data.strip()) self.debug_out( f"{'=' * 35}\nAbove is the current handled event\n{'=' * 35}\n" ) asyncio.create_task(self.reactions.resolve(event))