mirror of
https://github.com/jlu5/PyLink.git
synced 2025-01-23 18:54:05 +01:00
exec: add 'pieval' and 'peval' to evaluate expressions pretty-printed
This commit is contained in:
parent
c67c0aa2e6
commit
716ac681b6
@ -1,6 +1,7 @@
|
||||
"""
|
||||
exec.py: Provides commands for executing raw code and debugging PyLink.
|
||||
"""
|
||||
import pprint
|
||||
|
||||
from pylinkirc import utils, world
|
||||
from pylinkirc.log import log
|
||||
@ -15,6 +16,8 @@ import pylinkirc
|
||||
import importlib
|
||||
|
||||
exec_locals_dict = {}
|
||||
PPRINT_MAX_LINES = 20
|
||||
PPRINT_WIDTH = 200
|
||||
|
||||
def _exec(irc, source, args, locals_dict=None):
|
||||
"""<code>
|
||||
@ -60,7 +63,7 @@ def iexec(irc, source, args):
|
||||
"""
|
||||
_exec(irc, source, args, locals_dict=exec_locals_dict)
|
||||
|
||||
def _eval(irc, source, args, locals_dict=None):
|
||||
def _eval(irc, source, args, locals_dict=None, pretty_print=False):
|
||||
"""<Python expression>
|
||||
|
||||
Admin-only. Evaluates the given Python expression and returns the result.
|
||||
@ -84,9 +87,30 @@ def _eval(irc, source, args, locals_dict=None):
|
||||
|
||||
log.info('(%s) Evaluating %r for %s', irc.name, args,
|
||||
irc.getHostmask(source))
|
||||
irc.reply(repr(eval(args, globals(), locals_dict)))
|
||||
|
||||
result = eval(args, globals(), locals_dict)
|
||||
|
||||
if pretty_print:
|
||||
lines = pprint.pformat(result, width=PPRINT_WIDTH, compact=True).splitlines()
|
||||
for line in lines[:PPRINT_MAX_LINES]:
|
||||
irc.reply(line)
|
||||
if len(lines) > PPRINT_MAX_LINES:
|
||||
irc.reply('Suppressing %s more line(s) of output.' % (len(lines) - PPRINT_MAX_LINES))
|
||||
else:
|
||||
irc.reply(repr(result))
|
||||
|
||||
utils.add_cmd(_eval, 'eval')
|
||||
|
||||
@utils.add_cmd
|
||||
def peval(irc, source, args):
|
||||
"""<Python expression>
|
||||
|
||||
Admin-only. This command is the same as 'eval', except that results are pretty formatted.
|
||||
|
||||
\x02**WARNING: THIS CAN BE DANGEROUS IF USED IMPROPERLY!**\x02
|
||||
"""
|
||||
_eval(irc, source, args, pretty_print=True)
|
||||
|
||||
@utils.add_cmd
|
||||
def ieval(irc, source, args):
|
||||
"""<Python expression>
|
||||
@ -101,6 +125,16 @@ def ieval(irc, source, args):
|
||||
"""
|
||||
_eval(irc, source, args, locals_dict=exec_locals_dict)
|
||||
|
||||
@utils.add_cmd
|
||||
def pieval(irc, source, args):
|
||||
"""<Python expression>
|
||||
|
||||
Admin-only. This command is the same as 'ieval', except that results are pretty formatted.
|
||||
|
||||
\x02**WARNING: THIS CAN BE DANGEROUS IF USED IMPROPERLY!**\x02
|
||||
"""
|
||||
_eval(irc, source, args, locals_dict=exec_locals_dict, pretty_print=True)
|
||||
|
||||
@utils.add_cmd
|
||||
def raw(irc, source, args):
|
||||
"""<text>
|
||||
|
Loading…
Reference in New Issue
Block a user