本文整理汇总了Python中pygments.lexers.JsonLexer方法的典型用法代码示例。如果您正苦于以下问题:Python lexers.JsonLexer方法的具体用法?Python lexers.JsonLexer怎么用?Python lexers.JsonLexer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygments.lexers
的用法示例。
在下文中一共展示了lexers.JsonLexer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: json_out
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import JsonLexer [as 别名]
def json_out(data, pretty=False, mono=False, piped_out=False):
if not mono and not piped_out:
# set colors
class JcStyle(Style):
styles = set_env_colors()
if pretty:
print(highlight(json.dumps(data, indent=2), JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1])
else:
print(highlight(json.dumps(data), JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1])
else:
if pretty:
print(json.dumps(data, indent=2))
else:
print(json.dumps(data))
示例2: format_data
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import JsonLexer [as 别名]
def format_data(data):
formatter = HtmlFormatter(cssclass="codehilite")
if data is None:
return data
if isinstance(data, bool) or isinstance(data, int) or isinstance(data, float):
return highlight(str(data), TextLexer(), formatter)
elif isinstance(data, str):
try:
data = json.dumps(json.loads(data), indent=4, sort_keys=True)
lexer = JsonLexer()
except (ValueError, TypeError):
lexer = TextLexer()
elif isinstance(data, dict) or isinstance(data, list):
data = json.dumps(data, indent=4, sort_keys=True)
lexer = JsonLexer()
else:
lexer = TextLexer()
lexer.stripall = True
return highlight(data, lexer, formatter)
示例3: print_tc
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import JsonLexer [as 别名]
def print_tc(text, is_colorize):
try:
from pygments import highlight
from pygments.lexers import JsonLexer
from pygments.formatters import TerminalTrueColorFormatter
pygments_installed = True
except ImportError:
pygments_installed = False
if is_colorize and pygments_installed:
print(
highlight(
code=text, lexer=JsonLexer(), formatter=TerminalTrueColorFormatter(style="monokai")
)
)
else:
print(text)
示例4: print_result
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import JsonLexer [as 别名]
def print_result(text: str, colorize: bool) -> None:
if not sys.stdout.isatty() or not colorize:
# avoid to colorized when piped or redirected
print(text)
return
try:
from pygments import highlight
from pygments.lexers import JsonLexer
from pygments.formatters import TerminalTrueColorFormatter
print(
highlight(
code=text, lexer=JsonLexer(), formatter=TerminalTrueColorFormatter(style="monokai")
).strip()
)
except ImportError:
print(text)
示例5: format_dict
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import JsonLexer [as 别名]
def format_dict(tx):
from pygments import highlight, lexers, formatters
json_raw = json.dumps(tx, sort_keys=True, indent=4)
return highlight(
bytes(json_raw, "UTF-8"), lexers.JsonLexer(), formatters.TerminalFormatter()
)
示例6: format_json_color
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import JsonLexer [as 别名]
def format_json_color(obj):
from pygments import highlight, lexers, formatters
return highlight(format_json(obj), lexers.JsonLexer(), formatters.TerminalFormatter()) # pylint: disable=no-member
示例7: site_result_json
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import JsonLexer [as 别名]
def site_result_json(request: HttpRequest, site_id: int) -> HttpResponse:
if 'at' in request.GET:
# Check that the site even exists
site = get_object_or_404(Site, pk=site_id)
# TODO sanity check timestamp
try:
timestamp = datetime.strptime(request.GET['at'], "%Y-%m-%d")
except:
return render(request, 'frontend/site_result_json.html', {'site': site, 'highlighted_code': 'Incorrect timestamp format'})
try:
scan = Scan.objects.filter(site=site).filter(end__lte=timestamp).order_by('-end').first()
scan_result = ScanResult.objects.get(scan=scan).result
except Exception as e:
scan_result = None
else:
site = get_object_or_404(Site.objects.annotate_most_recent_scan_result(), pk=site_id)
scan_result = site.last_scan__result if site.last_scan__result else {}
if 'raw' in request.GET:
return JsonResponse(scan_result)
code = json.dumps(scan_result, indent=2)
if scan_result is not None:
highlighted_code = mark_safe(highlight(code, JsonLexer(), HtmlFormatter()))
else:
highlighted_code = 'No scan data found for these parameters'
return render(request, 'frontend/site_result_json.html', {
'site': site,
'highlighted_code': highlighted_code
})
示例8: colorize_json
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import JsonLexer [as 别名]
def colorize_json(data):
try:
from pygments import highlight, lexers, formatters
if PY3:
if isinstance(data, bytes):
data = data.decode('UTF-8')
else:
if not isinstance(data, unicode):
data = unicode(data, 'UTF-8')
colorful_json = highlight(data,
lexers.JsonLexer(),
formatters.TerminalFormatter())
return colorful_json
except ModuleNotFoundError:
return data
示例9: print_json
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import JsonLexer [as 别名]
def print_json(data):
#colorful_json = highlight(unicode(format_json(data), 'UTF-8'),
# lexers.JsonLexer(),
# formatters.TerminalFormatter())
pprint(colorize_json(format_json(data)))
示例10: log_json
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import JsonLexer [as 别名]
def log_json(json, header="", also_console=True, sort_keys=False):
json = dumps(
json,
ensure_ascii=False,
indent=4,
separators=(",", ": "),
sort_keys=sort_keys,
)
logger.info("{}\n{}".format(header, json)) # no coloring for log.html
if also_console:
json_data = highlight(
json, lexers.JsonLexer(), formatters.TerminalFormatter()
)
logger.console("{}\n{}".format(header, json_data), newline=False)
return json
示例11: prettify_json
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import JsonLexer [as 别名]
def prettify_json(some_text):
try:
return highlight(
json.dumps(json.loads(some_text), indent=4),
lexers.JsonLexer(),
formatters.TerminalFormatter())
except:
return some_text
示例12: get_attr_renderer
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import JsonLexer [as 别名]
def get_attr_renderer():
"""Return Dictionary containing different Pygements Lexers for Rendering & Highlighting"""
return {
'bash_command': lambda x: render(x, lexers.BashLexer),
'hql': lambda x: render(x, lexers.SqlLexer),
'sql': lambda x: render(x, lexers.SqlLexer),
'doc': lambda x: render(x, lexers.TextLexer),
'doc_json': lambda x: render(x, lexers.JsonLexer),
'doc_rst': lambda x: render(x, lexers.RstLexer),
'doc_yaml': lambda x: render(x, lexers.YamlLexer),
'doc_md': wrapped_markdown,
'python_callable': lambda x: render(get_python_source(x), lexers.PythonLexer),
}
示例13: json_pretty
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import JsonLexer [as 别名]
def json_pretty(data, colorize=False):
pretty = json.dumps(data,
sort_keys=True,
indent=4,
separators=(',', ': '))
if colorize:
pretty = highlight(pretty, lexers.JsonLexer(), formatters.TerminalFormatter())
return pretty.strip()
示例14: pp_json
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import JsonLexer [as 别名]
def pp_json(data):
if data is None:
warning('No data returned from module.')
else:
print(highlight(unicode(json.dumps(data, indent=4, default=jsondate), 'UTF-8'),
lexers.JsonLexer(), formatters.TerminalFormatter()))
示例15: _format_json
# 需要导入模块: from pygments import lexers [as 别名]
# 或者: from pygments.lexers import JsonLexer [as 别名]
def _format_json(data, theme):
"""Pretty print a dict as a JSON, with colors if pygments is present."""
output = json.dumps(data, indent=2, sort_keys=True)
if pygments and sys.stdout.isatty():
style = get_style_by_name(theme)
formatter = Terminal256Formatter(style=style)
return pygments.highlight(output, JsonLexer(), formatter)
return output