in lib/pygments/mentos.py [0:0]
def start(self):
"""
Main loop, waiting for inputs on stdin. When it gets some data,
it goes to work.
mentos exposes most of the "High-level API" of pygments. It always
expects and requires a JSON header of metadata. If there is data to be
pygmentized, this header will be followed by the text to be pygmentized.
The header is of form:
{ "method": "highlight", "args": [], "kwargs": {"arg1": "v"}, "bytes": 128, "fd": "8"}
"""
lock = Lock()
while True:
# The loop begins by reading off a simple 32-arity string
# representing an integer of 32 bits. This is the length of
# our JSON header.
size = sys.stdin.read(32)
lock.acquire()
try:
# Read from stdin the amount of bytes we were told to expect.
header_bytes = int(size, 2)
# Sanity check the size
size_regex = re.compile('[0-1]{32}')
if not size_regex.match(size):
_write_error("Size received is not valid.")
line = sys.stdin.read(header_bytes)
header = json.loads(line)
method, args, kwargs, lexer = self._parse_header(header)
_bytes = 0
if lexer:
lexer = str(lexer)
# Read more bytes if necessary
if kwargs:
_bytes = kwargs.get("bytes", 0)
# Read up to the given number bytes (possibly 0)
text = sys.stdin.read(_bytes)
# Sanity check the return.
if _bytes:
start_id, end_id = self._get_ids(text)
text = self._check_and_return_text(text, start_id, end_id)
# Get the actual data from pygments.
res = self.get_data(method, lexer, args, kwargs, text)
# Put back the sanity check values.
if method == "highlight":
res = start_id + " " + res + " " + end_id
self._send_data(res, method)
except:
tb = traceback.format_exc()
_write_error(tb)
finally:
lock.release()