Exemplo n.º 1
0
def _msg_err(receiver, stringtuple):
    """
    Helper function for returning an error to the caller.

    Args:
        receiver (Object): object to get the error message.
        stringtuple (tuple): tuple with two strings - one for the
            _IN_GAME_ERRORS mode (with the traceback) and one with the
            production string (with a timestamp) to be shown to the user.

    """
    string = "{traceback}\n{errmsg}\n(Traceback was logged {timestamp})."
    timestamp = logger.timeformat()
    tracestring = format_exc()
    logger.log_trace()
    if _IN_GAME_ERRORS:
        receiver.msg(
            string.format(traceback=tracestring,
                          errmsg=stringtuple[0].strip(),
                          timestamp=timestamp).strip())
    else:
        receiver.msg(
            string.format(traceback=tracestring.splitlines()[-1],
                          errmsg=stringtuple[1].strip(),
                          timestamp=timestamp).strip())
Exemplo n.º 2
0
def _msg_err(receiver, string):
    """
    Helper function for returning an error to the caller.

    Args:
        receiver (Object): object to get the error message.
        string (str): string which will be shown to the user.

    """
    receiver.msg(string.format(_nomulti=True, timestamp=logger.timeformat()).strip())
Exemplo n.º 3
0
def _msg_err(receiver, stringtuple):
    """
    Helper function for returning an error to the caller.

    Args:
        receiver (Object): object to get the error message.
        stringtuple (tuple): tuple with two strings - one for the
        _IN_GAME_ERRORS mode (with the traceback) and one with the
        production string (with a timestamp) to be shown to the user.

    """
    string = "{traceback}\n{errmsg}\n(Traceback was logged {timestamp})."
    timestamp = logger.timeformat()
    tracestring = format_exc()
    logger.log_trace()
    if _IN_GAME_ERRORS:
        receiver.msg(string.format(traceback=tracestring,
                                   errmsg=stringtuple[0].strip(),
                                   timestamp=timestamp).strip())
    else:
        receiver.msg(string.format(traceback=tracestring.splitlines()[-1],
                                   errmsg=stringtuple[1].strip(),
                                   timestamp=timestamp).strip())
Exemplo n.º 4
0
def import_cmdset(path, cmdsetobj, emit_to_obj=None, no_logging=False):
    """
    This helper function is used by the cmdsethandler to load a cmdset
    instance from a python module, given a python_path. It's usually accessed
    through the cmdsethandler's add() and add_default() methods.
    path - This is the full path to the cmdset object on python dot-form

    Args:
        path (str): The path to the command set to load.
        cmdsetobj (CmdSet): The database object/typeclass on which this cmdset is to be
            assigned (this can be also channels and exits, as well as accounts
            but there will always be such an object)
        emit_to_obj (Object, optional): If given, error is emitted to
            this object (in addition to logging)
        no_logging (bool, optional): Don't log/send error messages.
            This can be useful if import_cmdset is just used to check if
            this is a valid python path or not.
    Returns:
        cmdset (CmdSet): The imported command set. If an error was
            encountered, `commands.cmdsethandler._ErrorCmdSet` is returned
            for the benefit of the handler.

    """
    python_paths = [path] + [
        "%s.%s" % (prefix, path)
        for prefix in _CMDSET_PATHS if not path.startswith(prefix)
    ]
    errstring = ""
    for python_path in python_paths:

        if "." in path:
            modpath, classname = python_path.rsplit(".", 1)
        else:
            raise ImportError(
                "The path '%s' is not on the form modulepath.ClassName" % path)

        try:
            # first try to get from cache
            cmdsetclass = _CACHED_CMDSETS.get(python_path, None)

            if not cmdsetclass:
                try:
                    module = import_module(modpath, package="evennia")
                except ImportError as exc:
                    if len(trace()) > 2:
                        # error in module, make sure to not hide it.
                        dum, dum, tb = sys.exc_info()
                        raise exc.with_traceback(tb)
                    else:
                        # try next suggested path
                        errstring += _("\n(Unsuccessfully tried '%s')." %
                                       python_path)
                        continue
                try:
                    cmdsetclass = getattr(module, classname)
                except AttributeError as exc:
                    if len(trace()) > 2:
                        # Attribute error within module, don't hide it
                        dum, dum, tb = sys.exc_info()
                        raise exc.with_traceback(tb)
                    else:
                        errstring += _("\n(Unsuccessfully tried '%s')." %
                                       python_path)
                        continue
                _CACHED_CMDSETS[python_path] = cmdsetclass

            # instantiate the cmdset (and catch its errors)
            if callable(cmdsetclass):
                cmdsetclass = cmdsetclass(cmdsetobj)
            return cmdsetclass
        except ImportError as err:
            logger.log_trace()
            errstring += _ERROR_CMDSET_IMPORT
            if _IN_GAME_ERRORS:
                errstring = errstring.format(path=python_path,
                                             traceback=format_exc(),
                                             timestamp=logger.timeformat())
            else:
                errstring = errstring.format(path=python_path,
                                             traceback=err,
                                             timestamp=logger.timeformat())
            break
        except KeyError:
            logger.log_trace()
            errstring += _ERROR_CMDSET_KEYERROR
            errstring = errstring.format(classname=classname,
                                         path=python_path,
                                         timestamp=logger.timeformat())
            break
        except SyntaxError as err:
            logger.log_trace()
            errstring += _ERROR_CMDSET_SYNTAXERROR
            if _IN_GAME_ERRORS:
                errstring = errstring.format(path=python_path,
                                             traceback=format_exc(),
                                             timestamp=logger.timeformat())
            else:
                errstring = errstring.format(path=python_path,
                                             traceback=err,
                                             timestamp=logger.timeformat())
            break
        except Exception as err:
            logger.log_trace()
            errstring += _ERROR_CMDSET_EXCEPTION
            if _IN_GAME_ERRORS:
                errstring = errstring.format(path=python_path,
                                             traceback=format_exc(),
                                             timestamp=logger.timeformat())
            else:
                errstring = errstring.format(path=python_path,
                                             traceback=err,
                                             timestamp=logger.timeformat())
            break

    if errstring:
        # returning an empty error cmdset
        errstring = errstring.strip()
        if not no_logging:
            logger.log_err(errstring)
            if emit_to_obj and not ServerConfig.objects.conf(
                    "server_starting_mode"):
                emit_to_obj.msg(errstring)
        err_cmdset = _ErrorCmdSet()
        err_cmdset.errmessage = errstring
        return err_cmdset
    return None  # undefined error
Exemplo n.º 5
0
def import_cmdset(path, cmdsetobj, emit_to_obj=None, no_logging=False):
    """
    This helper function is used by the cmdsethandler to load a cmdset
    instance from a python module, given a python_path. It's usually accessed
    through the cmdsethandler's add() and add_default() methods.
    path - This is the full path to the cmdset object on python dot-form

    Args:
        path (str): The path to the command set to load.
        cmdsetobj (CmdSet): The database object/typeclass on which this cmdset is to be
            assigned (this can be also channels and exits, as well as players
            but there will always be such an object)
        emit_to_obj (Object, optional): If given, error is emitted to
            this object (in addition to logging)
        no_logging (bool, optional): Don't log/send error messages.
            This can be useful if import_cmdset is just used to check if
            this is a valid python path or not.
    Returns:
        cmdset (CmdSet): The imported command set. If an error was
            encountered, `commands.cmdsethandler._ErrorCmdSet` is returned
            for the benefit of the handler.

    """
    python_paths = [path] + ["%s.%s" % (prefix, path)
                                    for prefix in _CMDSET_PATHS if not path.startswith(prefix)]
    errstring = ""
    for python_path in python_paths:

        if "." in  path:
            modpath, classname = python_path.rsplit(".", 1)
        else:
            raise ImportError("The path '%s' is not on the form modulepath.ClassName" % path)

        try:
            # first try to get from cache
            cmdsetclass = _CACHED_CMDSETS.get(python_path, None)

            if not cmdsetclass:
                try:
                    module = import_module(modpath, package="evennia")
                except ImportError:
                    if len(trace()) > 2:
                        # error in module, make sure to not hide it.
                        exc = sys.exc_info()
                        raise_(exc[1], None, exc[2])
                    else:
                        # try next suggested path
                        errstring += _("\n(Unsuccessfully tried '%s')." % python_path)
                        continue
                try:
                    cmdsetclass = getattr(module, classname)
                except AttributeError:
                    if len(trace()) > 2:
                        # Attribute error within module, don't hide it
                        exc = sys.exc_info()
                        raise_(exc[1], None, exc[2])
                    else:
                        errstring += _("\n(Unsuccessfully tried '%s')." % python_path)
                        continue
                _CACHED_CMDSETS[python_path] = cmdsetclass

            #instantiate the cmdset (and catch its errors)
            if callable(cmdsetclass):
                cmdsetclass = cmdsetclass(cmdsetobj)
            return cmdsetclass
        except ImportError as err:
            logger.log_trace()
            errstring += _ERROR_CMDSET_IMPORT
            if _IN_GAME_ERRORS:
                errstring = errstring.format(path=python_path, traceback=format_exc(), timestamp=logger.timeformat())
            else:
                errstring = errstring.format(path=python_path, traceback=err, timestamp=logger.timeformat())
            break
        except KeyError:
            logger.log_trace()
            errstring += _ERROR_CMDSET_KEYERROR
            errstring = errstring.format(classname=classname, path=python_path, timestamp=logger.timeformat())
            break
        except SyntaxError as err:
            logger.log_trace()
            errstring += _ERROR_CMDSET_SYNTAXERROR
            if _IN_GAME_ERRORS:
                errstring = errstring.format(path=python_path, traceback=format_exc(), timestamp=logger.timeformat())
            else:
                errstring = errstring.format(path=python_path, traceback=err, timestamp=logger.timeformat())
            break
        except Exception as err:
            logger.log_trace()
            errstring += _ERROR_CMDSET_EXCEPTION
            if _IN_GAME_ERRORS:
                errstring = errstring.format(path=python_path, traceback=format_exc(), timestamp=logger.timeformat())
            else:
                errstring = errstring.format(path=python_path, traceback=err, timestamp=logger.timeformat())
            break

    if errstring:
        # returning an empty error cmdset
        errstring = errstring.strip()
        if not no_logging:
            logger.log_err(errstring)
            if emit_to_obj and not ServerConfig.objects.conf("server_starting_mode"):
                emit_to_obj.msg(errstring)
        err_cmdset = _ErrorCmdSet()
        err_cmdset.errmessage = errstring
        return err_cmdset
    return None # undefined error