mirror of
https://github.com/evennia/evennia.git
synced 2026-03-27 10:16:32 +01:00
Cleaning some unnecessary whitespace, overall cleanup of various source codes.
This commit is contained in:
parent
d4c97d7df8
commit
c0322c9eae
27 changed files with 1342 additions and 1318 deletions
|
|
@ -3,7 +3,7 @@ Custom manager for Objects.
|
|||
"""
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models.fields import exceptions
|
||||
from django.db.models.fields import exceptions
|
||||
from src.typeclasses.managers import TypedObjectManager
|
||||
from src.typeclasses.managers import returns_typeclass, returns_typeclass_list
|
||||
from src.utils import utils
|
||||
|
|
@ -17,11 +17,11 @@ AT_MULTIMATCH_INPUT = utils.mod_import(*settings.SEARCH_AT_MULTIMATCH_INPUT.rspl
|
|||
class ObjectManager(TypedObjectManager):
|
||||
"""
|
||||
This ObjectManager implementes methods for searching
|
||||
and manipulating Objects directly from the database.
|
||||
and manipulating Objects directly from the database.
|
||||
|
||||
Evennia-specific search methods (will return Typeclasses or
|
||||
lists of Typeclasses, whereas Django-general methods will return
|
||||
Querysets or database objects).
|
||||
Querysets or database objects).
|
||||
|
||||
dbref (converter)
|
||||
dbref_search
|
||||
|
|
@ -41,19 +41,19 @@ class ObjectManager(TypedObjectManager):
|
|||
copy_object
|
||||
|
||||
"""
|
||||
|
||||
|
||||
#
|
||||
# ObjectManager Get methods
|
||||
# ObjectManager Get methods
|
||||
#
|
||||
|
||||
# user/player related
|
||||
|
||||
|
||||
@returns_typeclass
|
||||
def get_object_with_user(self, user):
|
||||
"""
|
||||
Matches objects with obj.player.user matching the argument.
|
||||
A player<->user is a one-to-relationship, so this always
|
||||
returns just one result or None.
|
||||
returns just one result or None.
|
||||
|
||||
user - may be a user object or user id.
|
||||
"""
|
||||
|
|
@ -61,26 +61,26 @@ class ObjectManager(TypedObjectManager):
|
|||
uid = int(user)
|
||||
except TypeError:
|
||||
try:
|
||||
uid = user.id
|
||||
uid = user.id
|
||||
except:
|
||||
return None
|
||||
try:
|
||||
return self.get(db_player__user__id=uid)
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
# This returns typeclass since get_object_with_user and get_dbref does.
|
||||
|
||||
# This returns typeclass since get_object_with_user and get_dbref does.
|
||||
def get_object_with_player(self, search_string):
|
||||
"""
|
||||
Search for an object based on its player's name or dbref.
|
||||
"""
|
||||
Search for an object based on its player's name or dbref.
|
||||
This search
|
||||
is sometimes initiated by appending a * to the beginning of
|
||||
the search criterion (e.g. in local_and_global_search).
|
||||
the search criterion (e.g. in local_and_global_search).
|
||||
search_string: (string) The name or dbref to search for.
|
||||
"""
|
||||
search_string = to_unicode(search_string).lstrip('*')
|
||||
search_string = to_unicode(search_string).lstrip('*')
|
||||
dbref = self.dbref(search_string)
|
||||
if not dbref:
|
||||
if not dbref:
|
||||
# not a dbref. Search by name.
|
||||
player_matches = User.objects.filter(username__iexact=search_string)
|
||||
if player_matches:
|
||||
|
|
@ -105,17 +105,17 @@ class ObjectManager(TypedObjectManager):
|
|||
from src.objects.models import ObjAttribute
|
||||
lstring = ""
|
||||
if location:
|
||||
lstring = ", db_obj__db_location=location"
|
||||
lstring = ", db_obj__db_location=location"
|
||||
attrs = eval("ObjAttribute.objects.filter(db_key=attribute_name%s)" % lstring)
|
||||
return [attr.obj for attr in attrs]
|
||||
|
||||
|
||||
@returns_typeclass_list
|
||||
def get_objs_with_attr_match(self, attribute_name, attribute_value, location=None, exact=False):
|
||||
"""
|
||||
Returns all objects having the valid
|
||||
Returns all objects having the valid
|
||||
attrname set to the given value. Note that no conversion is made
|
||||
to attribute_value, and so it can accept also non-strings.
|
||||
"""
|
||||
"""
|
||||
from src.objects.models import ObjAttribute
|
||||
lstring = ""
|
||||
if location:
|
||||
|
|
@ -123,27 +123,27 @@ class ObjectManager(TypedObjectManager):
|
|||
attrs = eval("ObjAttribute.objects.filter(db_key=attribute_name%s)" % lstring)
|
||||
# since attribute values are pickled in database, we cannot search directly, but
|
||||
# must loop through the results. .
|
||||
if exact:
|
||||
if exact:
|
||||
return [attr.obj for attr in attrs if attribute_value == attr.value]
|
||||
else:
|
||||
return [attr.obj for attr in attrs if to_unicode(attribute_value) in str(attr.value)]
|
||||
|
||||
|
||||
@returns_typeclass_list
|
||||
def get_objs_with_db_property(self, property_name, location=None):
|
||||
"""
|
||||
Returns all objects having a given db field property.
|
||||
property_name = search string
|
||||
property_name = search string
|
||||
location - actual location object to restrict to
|
||||
|
||||
"""
|
||||
lstring = ""
|
||||
if location:
|
||||
lstring = ".filter(db_location=location)"
|
||||
lstring = ".filter(db_location=location)"
|
||||
try:
|
||||
return eval("self.exclude(db_%s=None)%s" % (property_name, lstring))
|
||||
except exceptions.FieldError:
|
||||
return []
|
||||
|
||||
|
||||
@returns_typeclass_list
|
||||
def get_objs_with_db_property_match(self, property_name, property_value, location, exact=False):
|
||||
"""
|
||||
|
|
@ -165,7 +165,7 @@ class ObjectManager(TypedObjectManager):
|
|||
def get_objs_with_key_or_alias(self, ostring, location, exact=False):
|
||||
"""
|
||||
Returns objects based on key or alias match
|
||||
"""
|
||||
"""
|
||||
lstring_key, lstring_alias, estring = "", "", "icontains"
|
||||
if location:
|
||||
lstring_key = ", db_location=location"
|
||||
|
|
@ -181,7 +181,7 @@ class ObjectManager(TypedObjectManager):
|
|||
return matches
|
||||
|
||||
# main search methods and helper functions
|
||||
|
||||
|
||||
@returns_typeclass_list
|
||||
def get_contents(self, location, excludeobj=None):
|
||||
"""
|
||||
|
|
@ -192,31 +192,40 @@ class ObjectManager(TypedObjectManager):
|
|||
if excludeobj:
|
||||
estring = ".exclude(db_key=excludeobj)"
|
||||
return eval("self.filter(db_location__id=location.id)%s" % estring)
|
||||
|
||||
|
||||
@returns_typeclass_list
|
||||
def object_search(self, ostring, caller=None,
|
||||
global_search=False,
|
||||
global_search=False,
|
||||
attribute_name=None, location=None):
|
||||
"""
|
||||
Search as an object and return results. The result is always an Object.
|
||||
If * is appended (player search, a Character controlled by this Player
|
||||
is looked for. The Character is returned, not the Player. Use player_search
|
||||
to find Player objects. Always returns a list.
|
||||
|
||||
|
||||
Arguments:
|
||||
ostring: (string) The string to compare names against.
|
||||
Can be a dbref. If name is appended by *, a player is searched for.
|
||||
caller: (Object) The object performing the search.
|
||||
global_search: Search all objects, not just the current location/inventory
|
||||
attribute_name: (string) Which attribute to search in each object.
|
||||
If None, the default 'key' attribute is used.
|
||||
location: If None, character.location will be used.
|
||||
Can be a dbref. If name is appended by *, a player is searched for.
|
||||
caller: (Object) The optional object performing the search.
|
||||
global_search (bool). Defaults to False. If a caller is defined, search will
|
||||
be restricted to the contents of caller.location unless global_search
|
||||
is True. If no caller is given (or the caller has no location), a
|
||||
global search is assumed automatically.
|
||||
attribute_name: (string) Which object attribute to match ostring against. If not
|
||||
set, the "key" and "aliases" properties are searched in order.
|
||||
location (Object): If set, this location's contents will be used to limit the search instead
|
||||
of the callers. global_search will override this argument
|
||||
|
||||
Returns:
|
||||
A list of matching objects (or a list with one unique match)
|
||||
|
||||
"""
|
||||
ostring = to_unicode(ostring, force_string=True)
|
||||
|
||||
if not ostring:
|
||||
return []
|
||||
return []
|
||||
|
||||
# Easiest case - dbref matching (always exact)
|
||||
# Easiest case - dbref matching (always exact)
|
||||
dbref = self.dbref(ostring)
|
||||
if dbref:
|
||||
dbref_match = self.dbref_search(dbref)
|
||||
|
|
@ -229,12 +238,12 @@ class ObjectManager(TypedObjectManager):
|
|||
# Test some common self-references
|
||||
|
||||
if location and ostring == 'here':
|
||||
return [location]
|
||||
return [location]
|
||||
if caller and ostring in ('me', 'self'):
|
||||
return [caller]
|
||||
if caller and ostring in ('*me', '*self'):
|
||||
return [caller]
|
||||
|
||||
if caller and ostring in ('*me', '*self'):
|
||||
return [caller]
|
||||
|
||||
# Test if we are looking for an object controlled by a
|
||||
# specific player
|
||||
|
||||
|
|
@ -244,24 +253,24 @@ class ObjectManager(TypedObjectManager):
|
|||
player_match = self.get_object_with_player(ostring)
|
||||
if player_match is not None:
|
||||
return [player_match]
|
||||
|
||||
|
||||
# Search for keys, aliases or other attributes
|
||||
|
||||
|
||||
search_locations = [None] # this means a global search
|
||||
if not global_search and location:
|
||||
# Test if we are referring to the current room
|
||||
if location and (ostring.lower() == location.key.lower()
|
||||
if location and (ostring.lower() == location.key.lower()
|
||||
or ostring.lower() in [alias.lower() for alias in location.aliases]):
|
||||
return [location]
|
||||
# otherwise, setup the locations to search in
|
||||
# otherwise, setup the locations to search in
|
||||
search_locations = [location]
|
||||
if caller:
|
||||
search_locations.append(caller)
|
||||
|
||||
|
||||
def local_and_global_search(ostring, exact=False):
|
||||
"Helper method for searching objects"
|
||||
matches = []
|
||||
for location in search_locations:
|
||||
"Helper method for searching objects"
|
||||
matches = []
|
||||
for location in search_locations:
|
||||
if attribute_name:
|
||||
# Attribute/property search. First, search for db_<attrname> matches on the model
|
||||
matches.extend(self.get_objs_with_db_property_match(attribute_name, ostring, location, exact))
|
||||
|
|
@ -269,14 +278,14 @@ class ObjectManager(TypedObjectManager):
|
|||
# Next, try Attribute matches
|
||||
matches.extend(self.get_objs_with_attr_match(attribute_name, ostring, location, exact))
|
||||
else:
|
||||
# No attribute/property named. Do a normal key/alias-search
|
||||
# No attribute/property named. Do a normal key/alias-search
|
||||
matches.extend(self.get_objs_with_key_or_alias(ostring, location, exact))
|
||||
return matches
|
||||
|
||||
# Search through all possibilities.
|
||||
|
||||
match_number = None
|
||||
matches = local_and_global_search(ostring, exact=True)
|
||||
matches = local_and_global_search(ostring, exact=True)
|
||||
if not matches:
|
||||
# if we have no match, check if we are dealing with an "N-keyword" query - if so, strip it.
|
||||
match_number, ostring = AT_MULTIMATCH_INPUT(ostring)
|
||||
|
|
@ -289,7 +298,7 @@ class ObjectManager(TypedObjectManager):
|
|||
elif len(matches) > 1:
|
||||
# multiple matches already. Run a fuzzy search. This catches partial matches (suggestions)
|
||||
matches = local_and_global_search(ostring, exact=False)
|
||||
|
||||
|
||||
# deal with the result
|
||||
if len(matches) > 1 and match_number != None:
|
||||
# We have multiple matches, but a N-type match number is available to separate them.
|
||||
|
|
@ -299,21 +308,21 @@ class ObjectManager(TypedObjectManager):
|
|||
pass
|
||||
# This is always a list.
|
||||
return matches
|
||||
|
||||
|
||||
#
|
||||
# ObjectManager Copy method
|
||||
#
|
||||
|
||||
def copy_object(self, original_object, new_key=None,
|
||||
new_location=None, new_player=None, new_home=None,
|
||||
new_location=None, new_player=None, new_home=None,
|
||||
new_permissions=None, new_locks=None, new_aliases=None, new_destination=None):
|
||||
"""
|
||||
Create and return a new object as a copy of the original object. All will
|
||||
be identical to the original except for the arguments given specifically
|
||||
be identical to the original except for the arguments given specifically
|
||||
to this method.
|
||||
|
||||
original_object (obj) - the object to make a copy from
|
||||
new_key (str) - name the copy differently from the original.
|
||||
new_key (str) - name the copy differently from the original.
|
||||
new_location (obj) - if not None, change the location
|
||||
new_home (obj) - if not None, change the Home
|
||||
new_aliases (list of strings) - if not None, change object aliases.
|
||||
|
|
@ -322,7 +331,7 @@ class ObjectManager(TypedObjectManager):
|
|||
|
||||
# get all the object's stats
|
||||
typeclass_path = original_object.typeclass_path
|
||||
if not new_key:
|
||||
if not new_key:
|
||||
new_key = original_object.key
|
||||
if not new_location:
|
||||
new_location = original_object.location
|
||||
|
|
@ -331,36 +340,36 @@ class ObjectManager(TypedObjectManager):
|
|||
if not new_player:
|
||||
new_player = original_object.player
|
||||
if not new_aliases:
|
||||
new_aliases = original_object.aliases
|
||||
new_aliases = original_object.aliases
|
||||
if not new_locks:
|
||||
new_locks = original_object.db_lock_storage
|
||||
if not new_permissions:
|
||||
new_permissions = original_object.permissions
|
||||
new_permissions = original_object.permissions
|
||||
if not new_destination:
|
||||
new_destination = original_object.destination
|
||||
|
||||
# create new object
|
||||
from src.utils import create
|
||||
|
||||
# create new object
|
||||
from src.utils import create
|
||||
from src.scripts.models import ScriptDB
|
||||
new_object = create.create_object(typeclass_path, key=new_key, location=new_location,
|
||||
home=new_home, player=new_player, permissions=new_permissions,
|
||||
home=new_home, player=new_player, permissions=new_permissions,
|
||||
locks=new_locks, aliases=new_aliases, destination=new_destination)
|
||||
if not new_object:
|
||||
return None
|
||||
return None
|
||||
|
||||
# copy over all attributes from old to new.
|
||||
# copy over all attributes from old to new.
|
||||
for attr in original_object.get_all_attributes():
|
||||
new_object.set_attribute(attr.key, attr.value)
|
||||
|
||||
# copy over all cmdsets, if any
|
||||
# copy over all cmdsets, if any
|
||||
for icmdset, cmdset in enumerate(original_object.cmdset.all()):
|
||||
if icmdset == 0:
|
||||
new_object.cmdset.add_default(cmdset)
|
||||
else:
|
||||
new_object.cmdset.add(cmdset)
|
||||
|
||||
# copy over all scripts, if any
|
||||
# copy over all scripts, if any
|
||||
for script in original_object.scripts.all():
|
||||
ScriptDB.objects.copy_script(script, new_obj=new_object.dbobj)
|
||||
|
||||
|
||||
return new_object
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue