Exemplo n.º 1
0
    async def execute(self):
        mdict = self.match_obj.groupdict()
        args = mdict.get("args", None)
        if not args:
            raise CommandException("@py requires arguments!")
        out = fmt.FormatList(self.executor)
        out.add(fmt.Line(f">>> {args}"))
        duration = ""
        ret = None

        try:
            # reroute standard output to game client console
            old_stdout = sys.stdout
            old_stderr = sys.stderr

            sys.stdout = self
            sys.stderr = self

            try:
                pycode_compiled = compile(args, "", "eval")
            except Exception:
                pycode_compiled = compile(args, "", "exec")

            measure_time = True

            if measure_time:
                t0 = time.time()
                ret = eval(pycode_compiled, {}, self.available_vars())
                t1 = time.time()
                duration = " (runtime ~ %.4f ms)" % ((t1 - t0) * 1000)
            else:
                ret = eval(pycode_compiled, {}, self.available_vars())
        except Exception:
            exc_type, exc_value, tb = sys.exc_info()
            trace = Traceback.extract(exc_type,
                                      exc_value,
                                      tb,
                                      show_locals=False)
            out.add(fmt.PyException(trace))
        finally:
            # return to old stdout
            sys.stdout = old_stdout
            sys.stderr = old_stderr

        out.add(fmt.PyDebug(repr(ret)))
        if duration:
            out.add(fmt.Line(duration))
        self.executor.send(out)
Exemplo n.º 2
0
    async def render_appearance(self,
                                entry: "TaskEntry",
                                viewer: "GameObject",
                                internal=False):
        parser = entry.parser
        out = fmt.FormatList(viewer)

        see_dbrefs = viewer.session.admin if viewer.session else True

        def format_name(obj, cmd=None):
            name = viewer.get_dub_or_keyphrase_for(obj)
            display = ansi_fun("hw", name)
            if cmd:
                display = send_menu(display, [(f"{cmd} {name}", cmd)])
            if see_dbrefs:
                display += f" ({obj.dbref})"
            return display

        if (nameformat := self.attributes.get_value("NAMEFORMAT")):
            result = await parser.evaluate(nameformat,
                                           executor=self,
                                           number_args={
                                               0: self.objid,
                                               1: self.name
                                           })
            out.add(fmt.Line(result))
Exemplo n.º 3
0
 def help(cls, entry):
     """
     This is called by the command-help system if help is called on this command.
     """
     enactor = entry.enactor
     if cls.__doc__:
         out = fmt.FormatList(enactor)
         out.add(fmt.Header(f"Help: {cls.name}"))
         out.add(fmt.Line(cls.__doc__))
         out.add(fmt.Footer())
         enactor.send(out)
     else:
         enactor.msg(text="Help is not implemented for this command.")
Exemplo n.º 4
0
 def msg(self, text, **kwargs):
     flist = fmt.FormatList(self, **kwargs)
     flist.add(fmt.Line(text))
     self.send(flist)
Exemplo n.º 5
0
            if cmd:
                display = send_menu(display, [(f"{cmd} {name}", cmd)])
            if see_dbrefs:
                display += f" ({obj.dbref})"
            return display

        if (nameformat := self.attributes.get_value("NAMEFORMAT")):
            result = await parser.evaluate(nameformat,
                                           executor=self,
                                           number_args={
                                               0: self.objid,
                                               1: self.name
                                           })
            out.add(fmt.Line(result))
        else:
            out.add(fmt.Line(format_name(self)))

        if internal and (idesc := self.attributes.get_value("IDESCRIBE")):
            idesc_eval = await parser.evaluate(idesc, executor=self)
            if (idescformat := self.attributes.get_value("IDESCFORMAT")):
                result = await parser.evaluate(idescformat,
                                               executor=self,
                                               number_args=(idesc_eval, ))
                out.add(fmt.Line(result))
            else:
                out.add(fmt.Line(idesc_eval))

        elif (desc := self.attributes.get_value("DESCRIBE")):
            try:
                desc_eval = await parser.evaluate(desc, executor=self)
            except Exception as err:
Exemplo n.º 6
0
 async def execute(self):
     out = fmt.FormatList(self.executor)
     out.add(fmt.Line("See you again!"))
     out.reason = "quit"
     self.executor.send(out)
     self.executor.terminate()