Add the ability to examine obj/attr with wildcards.

This commit is contained in:
Greg Taylor 2007-05-24 02:58:59 +00:00
parent 88dcdc95eb
commit 39b640e948
2 changed files with 115 additions and 41 deletions

View file

@ -1,3 +1,4 @@
import re
from django.db import models from django.db import models
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
import defines_global import defines_global
@ -24,12 +25,43 @@ class Attribute(models.Model):
list_display = ('object', 'name', 'value',) list_display = ('object', 'name', 'value',)
search_fields = ['name'] search_fields = ['name']
"""
BEGIN COMMON METHODS
"""
def get_name(self): def get_name(self):
""" """
Returns an attribute's name. Returns an attribute's name.
""" """
return self.name return self.name
def get_value(self):
"""
Returns an attribute's value.
"""
return self.value
def get_object(self):
"""
Returns the object that the attribute resides on.
"""
return self.object
def is_hidden(self):
"""
Returns True if the attribute is hidden.
"""
return self.is_hidden
def get_attrline(self):
"""
Best described as a __str__ method for in-game. Renders the attribute's
name and value as per MUX.
"""
return "%s%s%s: %s" % (ansi.ansi["hilite"],
self.get_name(),
ansi.ansi["normal"],
self.get_value())
class Object(models.Model): class Object(models.Model):
""" """
The Object class is very generic representation of a THING, PLAYER, EXIT, The Object class is very generic representation of a THING, PLAYER, EXIT,
@ -384,6 +416,21 @@ class Object(models.Model):
else: else:
return True return True
def attribute_namesearch(self, searchstr):
"""
Searches the object's attributes for name matches against searchstr
via regular expressions. Returns a list.
searchstr: (str) A string (maybe with wildcards) to search for.
"""
# Retrieve the list of attributes for this object.
attrs = Attribute.objects.filter(object=self)
# Compile a regular expression that is converted from the user's
# wild-carded search string.
match_exp = re.compile(functions_general.wildcard_to_regexp(searchstr), re.IGNORECASE)
# If the regular expression search returns a match object, add to results.
return [attr for attr in attrs if match_exp.search(attr.name)]
def has_flag(self, flag): def has_flag(self, flag):
""" """
Does our object have a certain flag? Does our object have a certain flag?

View file

@ -178,11 +178,28 @@ def cmd_examine(cdat):
session = cdat['session'] session = cdat['session']
pobject = session.get_pobject() pobject = session.get_pobject()
args = cdat['uinput']['splitted'][1:] args = cdat['uinput']['splitted'][1:]
attr_search = False
if len(args) == 0: if len(args) == 0:
# If no arguments are provided, examine the invoker's location.
target_obj = pobject.get_location() target_obj = pobject.get_location()
else: else:
results = functions_db.local_and_global_search(pobject, ' '.join(args)) # Look for a slash in the input, indicating an attribute search.
attr_split = args[0].split("/")
# If the splitting by the "/" character returns a list with more than 1
# entry, it's an attribute match.
if len(attr_split) > 1:
attr_search = True
# Strip the object search string from the input with the
# object/attribute pair.
searchstr = attr_split[0]
# Just in case there's a slash in an attribute name.
attr_searchstr = '/'.join(attr_split[1:])
else:
searchstr = ' '.join(args)
results = functions_db.local_and_global_search(pobject, searchstr)
if len(results) > 1: if len(results) > 1:
session.msg("More than one match found (please narrow target):") session.msg("More than one match found (please narrow target):")
@ -194,6 +211,16 @@ def cmd_examine(cdat):
return return
else: else:
target_obj = results[0] target_obj = results[0]
if attr_search:
attr_matches = target_obj.attribute_namesearch(attr_searchstr)
if attr_matches:
for attribute in attr_matches:
session.msg(attribute.get_attrline())
else:
session.msg("No matching attributes found.")
# End attr_search if()
else:
session.msg("%s\r\n%s" % ( session.msg("%s\r\n%s" % (
target_obj.get_name(fullname=True), target_obj.get_name(fullname=True),
target_obj.get_description(no_parsing=True), target_obj.get_description(no_parsing=True),
@ -203,7 +230,7 @@ def cmd_examine(cdat):
session.msg("Zone: %s" % (target_obj.get_zone(),)) session.msg("Zone: %s" % (target_obj.get_zone(),))
for attribute in target_obj.get_all_attributes(): for attribute in target_obj.get_all_attributes():
session.msg("%s%s%s: %s" % (ansi.ansi["hilite"], attribute.get_name(), ansi.ansi["normal"], attribute.value)) session.msg(attribute.get_attrline())
con_players = [] con_players = []
con_things = [] con_things = []