def create_object(typeclass=None, key=None, location=None, home=None, permissions=None, locks=None, aliases=None, tags=None, destination=None, report_to=None, nohome=False): """ Create a new in-game object. Kwargs: typeclass (class or str): Class or python path to a typeclass. key (str): Name of the new object. If not set, a name of #dbref will be set. home (Object or str): Obj or #dbref to use as the object's home location. permissions (str): A comma-separated string of permissions. locks (str): one or more lockstrings, separated by semicolons. aliases (list): A list of alternative keys. tags (list): List of tag keys (using no category). destination (Object or str): Obj or #dbref to use as an Exit's target. report_to (Object): The object to return error messages to. nohome (bool): This allows the creation of objects without a default home location; only used when creating the default location itself or during unittests. Returns: object (Object): A newly created object of the given typeclass. Raises: ObjectDB.DoesNotExist: If trying to create an Object with `location` or `home` that can't be found. """ global _ObjectDB if not _ObjectDB: from evennia.objects.models import ObjectDB as _ObjectDB typeclass = typeclass if typeclass else settings.BASE_OBJECT_TYPECLASS if isinstance(typeclass, basestring): # a path is given. Load the actual typeclass typeclass = class_from_module(typeclass, settings.TYPECLASS_PATHS) # Setup input for the create command. We use ObjectDB as baseclass here # to give us maximum freedom (the typeclasses will load # correctly when each object is recovered). location = dbid_to_obj(location, _ObjectDB) destination = dbid_to_obj(destination, _ObjectDB) home = dbid_to_obj(home, _ObjectDB) if not home: try: home = dbid_to_obj(settings.DEFAULT_HOME, _ObjectDB) if not nohome else None except _ObjectDB.DoesNotExist: raise _ObjectDB.DoesNotExist( "settings.DEFAULT_HOME (= '%s') does not exist, or the setting is malformed." % settings.DEFAULT_HOME) # create new instance new_object = typeclass(db_key=key, db_location=location, db_destination=destination, db_home=home, db_typeclass_path=typeclass.path) # store the call signature for the signal new_object._createdict = dict(key=key, location=location, destination=destination, home=home, typeclass=typeclass.path, permissions=permissions, locks=locks, aliases=aliases, tags=tags, report_to=report_to, nohome=nohome) # this will trigger the save signal which in turn calls the # at_first_save hook on the typeclass, where the _createdict can be # used. new_object.save() return new_object
def create_object( typeclass=None, key=None, location=None, home=None, permissions=None, locks=None, aliases=None, tags=None, destination=None, report_to=None, nohome=False, attributes=None, nattributes=None, ): """ Create a new in-game object. Kwargs: typeclass (class or str): Class or python path to a typeclass. key (str): Name of the new object. If not set, a name of #dbref will be set. home (Object or str): Obj or #dbref to use as the object's home location. permissions (list): A list of permission strings or tuples (permstring, category). locks (str): one or more lockstrings, separated by semicolons. aliases (list): A list of alternative keys or tuples (aliasstring, category). tags (list): List of tag keys or tuples (tagkey, category) or (tagkey, category, data). destination (Object or str): Obj or #dbref to use as an Exit's target. report_to (Object): The object to return error messages to. nohome (bool): This allows the creation of objects without a default home location; only used when creating the default location itself or during unittests. attributes (list): Tuples on the form (key, value) or (key, value, category), (key, value, lockstring) or (key, value, lockstring, default_access). to set as Attributes on the new object. nattributes (list): Non-persistent tuples on the form (key, value). Note that adding this rarely makes sense since this data will not survive a reload. Returns: object (Object): A newly created object of the given typeclass. Raises: ObjectDB.DoesNotExist: If trying to create an Object with `location` or `home` that can't be found. """ global _ObjectDB if not _ObjectDB: from evennia.objects.models import ObjectDB as _ObjectDB typeclass = typeclass if typeclass else settings.BASE_OBJECT_TYPECLASS # convenience converters to avoid common usage mistake permissions = make_iter(permissions) if permissions is not None else None locks = make_iter(locks) if locks is not None else None aliases = make_iter(aliases) if aliases is not None else None tags = make_iter(tags) if tags is not None else None attributes = make_iter(attributes) if attributes is not None else None if isinstance(typeclass, str): # a path is given. Load the actual typeclass typeclass = class_from_module(typeclass, settings.TYPECLASS_PATHS) # Setup input for the create command. We use ObjectDB as baseclass here # to give us maximum freedom (the typeclasses will load # correctly when each object is recovered). location = dbid_to_obj(location, _ObjectDB) destination = dbid_to_obj(destination, _ObjectDB) home = dbid_to_obj(home, _ObjectDB) if not home: try: home = dbid_to_obj(settings.DEFAULT_HOME, _ObjectDB) if not nohome else None except _ObjectDB.DoesNotExist: raise _ObjectDB.DoesNotExist( "settings.DEFAULT_HOME (= '%s') does not exist, or the setting is malformed." % settings.DEFAULT_HOME) # create new instance new_object = typeclass( db_key=key, db_location=location, db_destination=destination, db_home=home, db_typeclass_path=typeclass.path, ) # store the call signature for the signal new_object._createdict = dict( key=key, location=location, destination=destination, home=home, typeclass=typeclass.path, permissions=permissions, locks=locks, aliases=aliases, tags=tags, report_to=report_to, nohome=nohome, attributes=attributes, nattributes=nattributes, ) # this will trigger the save signal which in turn calls the # at_first_save hook on the typeclass, where the _createdict can be # used. new_object.save() signals.SIGNAL_OBJECT_POST_CREATE.send(sender=new_object) return new_object
def create_object(typeclass=None, key=None, location=None, home=None, permissions=None, locks=None, aliases=None, destination=None, report_to=None, nohome=False): """ Create a new in-game object. keywords: typeclass - class or python path to a typeclass key - name of the new object. If not set, a name of #dbref will be set. home - obj or #dbref to use as the object's home location permissions - a comma-separated string of permissions locks - one or more lockstrings, separated by semicolons aliases - a list of alternative keys destination - obj or #dbref to use as an Exit's target nohome - this allows the creation of objects without a default home location; only used when creating the default location itself or during unittests """ global _ObjectDB if not _ObjectDB: from evennia.objects.models import ObjectDB as _ObjectDB typeclass = typeclass if typeclass else settings.BASE_OBJECT_TYPECLASS if isinstance(typeclass, basestring): # a path is given. Load the actual typeclass typeclass = class_from_module(typeclass, settings.TYPECLASS_PATHS) # Setup input for the create command. We use ObjectDB as baseclass here # to give us maximum freedom (the typeclasses will load # correctly when each object is recovered). location = dbid_to_obj(location, _ObjectDB) destination = dbid_to_obj(destination, _ObjectDB) home = dbid_to_obj(home, _ObjectDB) if not home: try: home = dbid_to_obj(settings.DEFAULT_HOME, _ObjectDB) if not nohome else None except _ObjectDB.DoesNotExist: raise _ObjectDB.DoesNotExist( "settings.DEFAULT_HOME (= '%s') does not exist, or the setting is malformed." % settings.DEFAULT_HOME) # create new instance new_object = typeclass(db_key=key, db_location=location, db_destination=destination, db_home=home, db_typeclass_path=typeclass.path) # store the call signature for the signal new_object._createdict = { "key": key, "location": location, "destination": destination, "home": home, "typeclass": typeclass.path, "permissions": permissions, "locks": locks, "aliases": aliases, "destination": destination, "report_to": report_to, "nohome": nohome } # this will trigger the save signal which in turn calls the # at_first_save hook on the typeclass, where the _createdict can be # used. new_object.save() return new_object