Exemplo n.º 1
0
def match_failed(matches, search=None, search_for=None, show=False, names=str):
    """
    Utility method to handled failed matches. Will return a message if a
    search for a single match has failed and return False if the search
    succeeded.

    :param matches: The result of the match search.
    :type matches: list

    :param search: The string used to search.
    :type search: str

    :param search_for: A string describing what type of thing was being
        searched for. This should be the singular form of the word.
    :type search_for: str

    :param show: If true, will show the list of possible matches in the
        case of an ambiguous match.
    :type show: bool

    :param names: A function to use to convert match objects to string names.
    :type names: function

    :return: Message if match failed, or False if match did not fail.
    :rtype: str or bool
    """
    if len(matches) == 1:
        return False
    elif len(matches) > 1:
        if search is not None:
            if search_for is not None:
                msg = ("Multiple %s match '%s'" %
                       (str_utils.inflection.plural(search_for), search))
            else:
                msg = "Multiple matches for '%s'" % search
        else:
            if search_for is not None:
                msg = "Multiple %s found"
                msg %= str_utils.inflection.plural(search_for)
            else:
                msg = "Multiple matches"
        if show:
            msg += ': ' + str_utils.english_list(matches, formatter=names)
        else:
            msg += '.'
    else:
        if search is not None:
            if search_for is not None:
                msg = "No %s called '%s' was found." % (search_for, search)
            else:
                msg = "No '%s' was found." % search
        else:
            if search_for is not None:
                msg = "No matching %s found."
                msg %= str_utils.inflection.plural(search_for)
            else:
                msg = "No match found."
    return msg
Exemplo n.º 2
0
def match_failed(matches, search=None, search_for=None, show=False, names=str):
    """
    Utility method to handled failed matches. Will return a message if a
    search for a single match has failed and return False if the search
    succeeded.

    :param matches: The result of the match search.
    :type matches: list

    :param search: The string used to search.
    :type search: str

    :param search_for: A string describing what type of thing was being
        searched for. This should be the singular form of the word.
    :type search_for: str

    :param show: If true, will show the list of possible matches in the
        case of an ambiguous match.
    :type show: bool

    :param names: A function to use to convert match objects to string names.
    :type names: function

    :return: Message if match failed, or False if match did not fail.
    :rtype: str or bool
    """
    if len(matches) == 1:
        return False
    elif len(matches) > 1:
        if search is not None:
            if search_for is not None:
                msg = ("Multiple %s match '%s'"
                       % (str_utils.inflection.plural(search_for), search))
            else:
                msg = "Multiple matches for '%s'" % search
        else:
            if search_for is not None:
                msg = "Multiple %s found"
                msg %= str_utils.inflection.plural(search_for)
            else:
                msg = "Multiple matches"
        if show:
            msg += ': ' + str_utils.english_list(matches, formatter=names)
        else:
            msg += '.'
    else:
        if search is not None:
            if search_for is not None:
                msg = "No %s called '%s' was found." % (search_for, search)
            else:
                msg = "No '%s' was found." % search
        else:
            if search_for is not None:
                msg = "No matching %s found."
                msg %= str_utils.inflection.plural(search_for)
            else:
                msg = "No match found."
    return msg
Exemplo n.º 3
0
 def render(self, viewer=None):
     if viewer is not None:
         rendered_vals = [self.format % viewer._format_msg((v,))
                          for v in self.values]
     else:
         rendered_vals = [self.format % v for v in self.values]
     return str_utils.english_list(rendered_vals,
                                   nothingstr=self.nothingstr,
                                   andstr=self.andstr,
                                   commastr=self.commastr,
                                   finalcommastr=self.finalcommastr)
Exemplo n.º 4
0
 def render(self, viewer=None):
     if viewer is not None:
         rendered_vals = [
             self.format % viewer._format_msg((v, )) for v in self.values
         ]
     else:
         rendered_vals = [self.format % v for v in self.values]
     return str_utils.english_list(rendered_vals,
                                   nothingstr=self.nothingstr,
                                   andstr=self.andstr,
                                   commastr=self.commastr,
                                   finalcommastr=self.finalcommastr)
Exemplo n.º 5
0
    def run(self, this, actor, args):
        """
        @type this: mudslingcore.objects.Player
        @type actor: mudslingcore.objects.Player
        @type args: dict
        """
        from mudslingcore.objects import Player

        if not self.argwords:
            # List roles
            msg = "{yRoles: {m%s"
            actor.msg(msg % string.english_list(self.game.db.roles, 'none'))
            return

        matches = actor.match_obj_of_type(self.argstr, cls=Player)
        if self.match_failed(matches, self.argstr, 'player', show=True):
            return
        player = matches[0]

        msg = ("{yRoles for {c%s{y: {m%s" %
               (player.nn, string.english_list(player.get_roles(), 'none')))
        actor.msg(msg)
Exemplo n.º 6
0
    def run(self, this, actor, args):
        """
        @type this: mudslingcore.objects.Player
        @type actor: mudslingcore.objects.Player
        @type args: dict
        """
        from mudslingcore.objects import Player

        if not self.argwords:
            # List roles
            msg = "{yRoles: {m%s"
            actor.msg(msg % string.english_list(self.game.db.roles, 'none'))
            return

        matches = actor.match_obj_of_type(self.argstr, cls=Player)
        if self.match_failed(matches, self.argstr, 'player', show=True):
            return
        player = matches[0]

        msg = ("{yRoles for {c%s{y: {m%s"
               % (player.nn, string.english_list(player.get_roles(), 'none')))
        actor.msg(msg)
Exemplo n.º 7
0
    def run(self, this, actor, args):
        """
        @type this: mudslingcore.objects.Player
        @type actor: mudslingcore.objects.Player
        @type args: dict
        """
        try:
            role = self.game.db.match_role(args['role'])
        except MatchError as e:
            actor.msg(e.message)
            return

        msg = "{yPermissions for role {m%s{y: {g%s"
        perms = string.english_list(role.perms, nothingstr="No perms")
        actor.msg(msg % (role.name, perms))
Exemplo n.º 8
0
    def run(self, this, actor, args):
        """
        @type this: mudslingcore.objects.Player
        @type actor: mudslingcore.objects.Player
        @type args: dict
        """
        try:
            role = self.game.db.match_role(args['role'])
        except MatchError as e:
            actor.msg(e.message)
            return

        msg = "{yPermissions for role {m%s{y: {g%s"
        perms = string.english_list(role.perms, nothingstr="No perms")
        actor.msg(msg % (role.name, perms))
Exemplo n.º 9
0
    def run(self, this, actor, args):
        """
        @type this: mudslingcore.objects.Player
        @type actor: mudslingcore.objects.Player
        @type args: dict
        """
        from mudslingcore.objects import Player

        matches = actor.match_obj_of_type(self.argstr, cls=Player)
        if self.match_failed(matches, self.argstr, 'player', show=True):
            return
        #: @type: Player
        player = matches[0]
        perms = set()
        for role in player.get_roles():
            perms |= role.perms
        perms = string.english_list(perms)

        msg = "{yPerms for {c%s{y: {g%s"
        actor.msg(msg % (player.nn, perms))
Exemplo n.º 10
0
    def run(self, this, actor, args):
        """
        @type this: mudslingcore.objects.Player
        @type actor: mudslingcore.objects.Player
        @type args: dict
        """
        from mudslingcore.objects import Player

        matches = actor.match_obj_of_type(self.argstr, cls=Player)
        if self.match_failed(matches, self.argstr, 'player', show=True):
            return
        #: @type: Player
        player = matches[0]
        perms = set()
        for role in player.get_roles():
            perms |= role.perms
        perms = string.english_list(perms)

        msg = "{yPerms for {c%s{y: {g%s"
        actor.msg(msg % (player.nn, perms))
Exemplo n.º 11
0
    def match_syntax(self, argstr):
        """
        Determine if the input matches the command syntax.

        :param argstr: The unparsed command string, excluding the command name.
        :type argstr: str

        :rtype: bool
        """
        if '_syntax' not in self.__class__.__dict__:
            self._compile_syntax()

        parsed = {}
        for syntax in self._syntax:
            parsed = syntax.parse(argstr)
            if isinstance(parsed, dict):
                break
        if parsed:
            self.args = parsed
            # Check for 'this' in any of the validators.
            for argName, valid in self.arg_parsers.iteritems():
                if isinstance(valid, str) and valid.lower() == 'this':
                    matches = self.actor.match_object(parsed[argName])
                    if self.obj not in matches:
                        return False
                    elif len(matches) > 1:
                        msg = "Multiple matches for '%s': %s"
                        msg %= (parsed[argName],
                                string.english_list(
                                    matches,
                                    formatter=lambda o: self.actor.name_for(o)
                                ))
                        raise mudsling.errors.AmbiguousMatch(
                            msg=msg,
                            query=parsed[argName],
                            matches=matches
                        )
            return True
        return False