本文整理汇总了Python中pygments.format函数的典型用法代码示例。如果您正苦于以下问题:Python format函数的具体用法?Python format怎么用?Python format使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了format函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: format
def format(tok, tik=None):
if tik:
tok = (tok, tik)
if isinstance(tok, tuple):
return pygments.format([tok], formatter)
else:
return pygments.format(tok, formatter)
示例2: render
def render(self, results):
target = None
self.trunc_top = self.args.reverse
t_res, m_res = results
if t_res and t_res.is_success and len(t_res.targets) > 0:
target = t_res.targets[0]
if self.args.deref or self.args.words:
self.args.bytes = target['addr_size']
f = pygments.formatters.get_formatter_by_name(self.config.format.pygments_formatter,
style=self.config.format.pygments_style)
if m_res and m_res.is_success:
lines = pygments.format(self.generate_tokens(results), f).split('\n')
self.body = '\n'.join(reversed(lines)).strip() if self.args.reverse else '\n'.join(lines)
self.info = '[0x{0:0=4x}:'.format(len(m_res.memory)) + self.config.format.addr_format.format(m_res.address) + ']'
else:
log.error("Error reading memory: {}".format(m_res.message))
self.body = pygments.format([(Error, m_res.message)], f)
self.info = ''
# Store the memory
if self.args.track:
self.last_address = m_res.address
self.last_memory = m_res.memory
else:
self.body = self.colour("Failed to get targets", 'red')
if not self.title:
self.title = "[memory]"
super(MemoryView, self).render(results)
示例3: test_unicode_handling
def test_unicode_handling(self):
# test that the formatter supports encoding and Unicode
tokens = list(lexers.PythonLexer(encoding='utf-8').
get_tokens("def f(): 'ä'"))
for formatter, info in formatters.FORMATTERS.iteritems():
try:
inst = formatter(encoding=None)
except (ImportError, FontNotFound):
# some dependency or font not installed
continue
if formatter.name != 'Raw tokens':
out = format(tokens, inst)
if formatter.unicodeoutput:
self.assert_(type(out) is unicode)
inst = formatter(encoding='utf-8')
out = format(tokens, inst)
self.assert_(type(out) is bytes, '%s: %r' % (formatter, out))
# Cannot test for encoding, since formatters may have to escape
# non-ASCII characters.
else:
inst = formatter()
out = format(tokens, inst)
self.assert_(type(out) is bytes, '%s: %r' % (formatter, out))
示例4: test_bare_class_handler
def test_bare_class_handler():
from pygments.formatters import HtmlFormatter
from pygments.lexers import PythonLexer
try:
lex('test\n', PythonLexer)
except TypeError as e:
assert 'lex() argument must be a lexer instance' in str(e)
else:
assert False, 'nothing raised'
try:
format([], HtmlFormatter)
except TypeError as e:
assert 'format() argument must be a formatter instance' in str(e)
else:
assert False, 'nothing raised'
示例5: test_unicode_handling
def test_unicode_handling(self):
# test that the formatter supports encoding and Unicode
tokens = list(lexers.PythonLexer(encoding='utf-8').get_tokens("def f(): 'ä'"))
for formatter, info in formatters.FORMATTERS.iteritems():
try:
inst = formatter(encoding=None)
except (ImportError, FontNotFound):
# some dependency or font not installed
continue
out = format(tokens, inst)
if formatter.unicodeoutput:
self.assert_(type(out) is unicode)
inst = formatter(encoding='utf-8')
out = format(tokens, inst)
self.assert_(type(out) is str)
示例6: push
def push(self, line):
"""Push a line of code onto the buffer, run the buffer
If the interpreter successfully runs the code, clear the buffer
Return ("for stdout", "for_stderr", finished?)
"""
self.buffer.append(line)
indent = len(re.match(r'[ ]*', line).group())
if line.endswith(':'):
indent = max(0, indent + INDENT_AMOUNT)
elif line and line.count(' ') == len(self._current_line):
indent = max(0, indent - INDENT_AMOUNT)
elif line and ':' not in line and line.strip().startswith(('return', 'pass', 'raise', 'yield')):
indent = max(0, indent - INDENT_AMOUNT)
out_spot = sys.stdout.tell()
err_spot = sys.stderr.tell()
#logging.debug('running %r in interpreter', self.buffer)
unfinished = self.interp.runsource('\n'.join(self.buffer))
self.display_buffer.append(bpythonparse(format(self.tokenize(line), self.formatter))) #current line not added to display buffer if quitting
sys.stdout.seek(out_spot)
sys.stderr.seek(err_spot)
out = sys.stdout.read()
err = sys.stderr.read()
if unfinished and not err:
logging.debug('unfinished - line added to buffer')
return (None, None, False, indent)
else:
logging.debug('finished - buffer cleared')
self.display_lines.extend(self.display_buffer_lines)
self.display_buffer = []
self.buffer = []
if err:
indent = 0
return (out[:-1], err[:-1], True, indent)
示例7: string_to_fmtstr
def string_to_fmtstr(x):
from pygments import format
from bpython.formatter import BPythonFormatter
from bpython._py3compat import PythonLexer
from bpython.config import Struct, loadini, default_config_path
config = Struct()
loadini(config, default_config_path())
return parse(format(PythonLexer().get_tokens(x), BPythonFormatter(config.color_scheme)))
示例8: highlightBlock
def highlightBlock(self, text):
"""Takes a block, applies format to the document.
according to what's in it.
"""
# I need to know where in the document we are,
# because our formatting info is global to
# the document
cb = self.currentBlock()
p = cb.position()
'''print cb
print p
print cb.text()'''
blockText = unicode(cb.text())+'\n'
# The \n is not really needed, but sometimes
# you are in an empty last block, so your position is
# **after** the end of the document.
text=unicode(self.document().toPlainText())+'\n'
# Yes, re-highlight the whole document.
# There **must** be some optimization possibilities
# but it seems fast enough.
#highlight(blockText,self.lexer,self.formatter)
tokens = pygments.lex(blockText, self.lexer)
self.docTokens[cb.blockNumber()] = tokens
pygments.format(tokens, self.formatter)
data = self.formatter.getData()
# Just apply the formatting to this block.
# For titles, it may be necessary to backtrack
# and format a couple of blocks **earlier**.
for i in range(len(unicode(blockText))):
try:
self.setFormat(i,1,data[i])
except IndexError:
pass
# I may need to do something about this being called
# too quickly.
self.tstamp=time.time()
示例9: raw_format
def raw_format(cls, tokens):
"""Format the given list of tokens as a simple string (no color)
:param tokens: the input list of token to format
:type tokens: tuple[Token, str]
:rtype: str
"""
formatter = get_formatter_by_name(Formatter.NO_COLOR, encoding=Formatter.ENCODING)
return pygments.format(tokens, formatter)
示例10: test_formatter_encodings
def test_formatter_encodings():
from pygments.formatters import HtmlFormatter
# unicode output
fmt = HtmlFormatter()
tokens = [(Text, u"ä")]
out = format(tokens, fmt)
assert type(out) is text_type
assert u"ä" in out
# encoding option
fmt = HtmlFormatter(encoding="latin1")
tokens = [(Text, u"ä")]
assert u"ä".encode("latin1") in format(tokens, fmt)
# encoding and outencoding option
fmt = HtmlFormatter(encoding="latin1", outencoding="utf8")
tokens = [(Text, u"ä")]
assert u"ä".encode("utf8") in format(tokens, fmt)
示例11: test_encodings
def test_encodings(self):
from pygments.formatters import HtmlFormatter
# unicode output
fmt = HtmlFormatter()
tokens = [(Text, u"ä")]
out = format(tokens, fmt)
self.assert_(type(out) is unicode)
self.assert_(u"ä" in out)
# encoding option
fmt = HtmlFormatter(encoding="latin1")
tokens = [(Text, u"ä")]
self.assert_(u"ä".encode("latin1") in format(tokens, fmt))
# encoding and outencoding option
fmt = HtmlFormatter(encoding="latin1", outencoding="utf8")
tokens = [(Text, u"ä")]
self.assert_(u"ä".encode("utf8") in format(tokens, fmt))
示例12: format
def format(cls, tokens):
"""Format the given list of tokens
:param tokens: the input list of token to format
:type tokens: tuple[Token, str]
:rtype: str
"""
cls.__initialize()
return pygments.format(tokens, cls.formatter)
示例13: print_color
def print_color(self, string, hide=False, **kwargs):
if isinstance(string, str):
s = self.format_color(string, hide=hide)
else:
# assume this is a list of (Token, str) tuples and format it
env = builtins.__xonsh_env__
self.styler.style_name = env.get('XONSH_COLOR_STYLE')
style_proxy = pyghooks.xonsh_style_proxy(self.styler)
formatter = Terminal256Formatter(style=style_proxy)
s = pygments.format(string, formatter).rstrip()
print(s, **kwargs)
示例14: verify
def verify(formatter):
try:
inst = formatter(encoding=None)
except (ImportError, FontNotFound):
# some dependency or font not installed
return
if formatter.name != "Raw tokens":
out = format(tokens, inst)
if formatter.unicodeoutput:
assert type(out) is unicode
inst = formatter(encoding="utf-8")
out = format(tokens, inst)
assert type(out) is bytes, "%s: %r" % (formatter, out)
# Cannot test for encoding, since formatters may have to escape
# non-ASCII characters.
else:
inst = formatter()
out = format(tokens, inst)
assert type(out) is bytes, "%s: %r" % (formatter, out)
示例15: current_line_formatted
def current_line_formatted(self):
"""The colored current line (no prompt, not wrapped)"""
if self.config.syntax:
fs = bpythonparse(format(self.tokenize(self._current_line), self.formatter))
logging.debug('Display line %r -> %r', self._current_line, fs)
else:
fs = fmtstr(self._current_line)
if hasattr(self, 'old_fs') and str(fs) != str(self.old_fs):
pass
#logging.debug('calculating current formatted line: %r', repr(fs))
self.old_fs = fs
return fs