diff --git a/plugins/exec.py b/plugins/exec.py index 1848ae1..5a93752 100644 --- a/plugins/exec.py +++ b/plugins/exec.py @@ -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): """ @@ -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): """ 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): + """ + + 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): """ @@ -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): + """ + + 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): """