def handle(self, *args, **options): try: from pygments import lexers except ImportError: self.stdout.write("This command requires Pygments package.") self.stdout.write("Please install it with:\n\n") self.stdout.write(" pip install Pygments\n\n") return # Invocation examples _process("bash_curl", lexers.BashLexer()) _process("bash_wget", lexers.BashLexer()) _process("browser", lexers.JavascriptLexer()) _process("crontab", lexers.BashLexer()) _process("node", lexers.JavascriptLexer()) _process("python_urllib2", lexers.PythonLexer()) _process("python_requests", lexers.PythonLexer()) _process("php", lexers.PhpLexer()) _process("powershell", lexers.shell.PowerShellLexer()) _process("powershell_inline", lexers.shell.BashLexer()) _process("ruby", lexers.RubyLexer()) # API examples _process("list_checks_request", lexers.BashLexer()) _process("list_checks_response", lexers.JsonLexer()) _process("create_check_request_a", lexers.BashLexer()) _process("create_check_request_b", lexers.BashLexer()) _process("update_check_request_a", lexers.BashLexer()) _process("update_check_request_b", lexers.BashLexer()) _process("create_check_response", lexers.JsonLexer()) _process("pause_check_request", lexers.BashLexer()) _process("pause_check_response", lexers.JsonLexer()) _process("delete_check_request", lexers.BashLexer())
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 output_format(obj): ''' python2/3 中文没有被解析 obj is dict http://stackoverflow.com/questions/35950573/python-unicode-string-to-javascript ''' formatted_json = json.dumps(obj, sort_keys=True, indent=4,ensure_ascii=False).encode('utf8') if (sys.version_info > (3, 0)): # Python 3 code in this block colorful_json = highlight(formatted_json, lexers.JsonLexer(), formatters.TerminalFormatter()) return colorful_json else: colorful_json = highlight(unicode(formatted_json, 'UTF-8'), lexers.JsonLexer(), formatters.TerminalFormatter()) return colorful_json #中文没有解决
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 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 colored_json(j): j = Logger.pretty_json(j) l = lexers.JsonLexer() l.add_filter('whitespace') colorful_json = highlight(unicode(j, 'UTF-8'), l, formatters.TerminalFormatter()) return colorful_json
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 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 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 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 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 sendtoaddress(self, priv, to_addr, amnt, message, multisig=None): remote = Remote("ws://%s:%s" % (self.__host2, str(self.__port2)), priv ) if isinstance(amnt, Decimal): amnt = int(amnt*PREC) try : if len(message)>0: message = int(message) except: raise Exception("Invalid message") result_node = remote.send_payment( to_addr, amnt, flags=0, destination_tag=message) print('TxHash: %s' % result_node.hash) try: result = result_node.wait() except ResponseError as e: result = e.response if True: formatted_json = json.dumps(result, sort_keys=True, indent=4) colorful_json = highlight(unicode(formatted_json, 'UTF-8'), lexers.JsonLexer(), formatters.TerminalFormatter()) print(colorful_json) print(result['engine_result_message']) return (result_node, result)
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)
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))
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 get_settings(tr): formatted_json = json.dumps(tr.settings(), indent=2) if sys.stdout.isatty(): colorful_json = highlight(formatted_json, lexers.JsonLexer(), formatters.TerminalFormatter()) return colorful_json else: return formatted_json
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 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 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 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 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 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(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 _beautify(data, *, colors: bool, table: bool) -> str: """ 1. Returns table if `table=True` 1. Returns colored JSON if `json=True` 1. Returns plain JSON otherwise. """ if table: # one dict if isinstance(data, dict): data = FlatDict(data, delimiter='.').items() return tabulate(data, headers=('key', 'value'), tablefmt='fancy_grid') # list of dicts if isinstance(data, list) and data and isinstance(data[0], dict): table = [] for row in data: row = FlatDict(row, delimiter='.') keys = tuple(row) row = [v for _, v in sorted(row.items())] table.append(row) return tabulate(table, headers=keys, tablefmt='fancy_grid') json_params = dict(indent=2, sort_keys=True, ensure_ascii=False) dumped = json.dumps(data, **json_params) if not colors: return dumped return highlight(dumped, lexers.JsonLexer(), formatters.TerminalFormatter())
def colorizer(content): # dump to hcl is not supported, so fallback to json return highlight( content, lexers.JsonLexer(), # lexers.TerraformLexer() formatters.TerminalFormatter(), )
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 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 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 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']]
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)