def build_object(obj_key, level=None, caller=None, reset_location=True): """ Build objects of a model. Args: obj_key: (string) The key of the object. level: (number) The object's level. caller: (command caller) If provide, running messages will send to the caller. """ # Get object's information record = None typeclass_path = None try: record = get_object_record(obj_key) # get typeclass model typeclass_path = TYPECLASS_SET.get_module(record.typeclass) except Exception as e: ostring = "Can not get typeclass of %s: %s." % (obj_key, e) print(ostring) print(traceback.print_exc()) pass if not record or not typeclass_path: ostring = "Can not find the data of %s." % obj_key print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) return # Create object. try: name = getattr(record, "name", "") obj = create.create_object(typeclass_path, name) except Exception as e: ostring = "Can not create obj %s: %s" % (obj_key, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) return try: # Set data info. obj.set_data_key(record.key, level, reset_location=reset_location) except Exception as e: ostring = "Can not set data info to obj %s: %s" % (obj_key, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) return return obj
def build_object(obj_key, caller=None, set_location=True): """ Build objects of a model. Args: obj_key: (string) The key of the object. caller: (command caller) If provide, running messages will send to the caller. """ # Get object's information record = None typeclass_path = None try: record = get_object_record(obj_key) # get typeclass model typeclass_path = TYPECLASS_SET.get_module(record.typeclass) except Exception, e: ostring = "Can not get typeclass of %s: %s." % (obj_key, e) print(ostring) print(traceback.print_exc()) pass
def build_object(obj_key, caller=None, set_location=True): """ Build objects of a model. Args: obj_key: (string) The key of the object. caller: (command caller) If provide, running messages will send to the caller. """ # Get object's information record = None typeclass_path = None try: record = get_object_record(obj_key) # get typeclass model typeclass_path = TYPECLASS_SET.get_module(record.typeclass) except Exception, e: ostring = "Can not get typeclass of %s: %s." % (obj_key, e) print(ostring) print(traceback.print_exc()) pass
current_obj_keys.add(obj_key) # Create new objects. object_model_name = TYPECLASS("OBJECT").model_name object_model = apps.get_model(settings.WORLD_DATA_APP, object_model_name) for record in objects_data: if not record.key in current_obj_keys: # Create new objects. ostring = "Creating %s." % record.key print(ostring) if caller: caller.msg(ostring) try: object_record = object_model.objects.get(key=record.key) typeclass_path = TYPECLASS_SET.get_module( object_record.typeclass) obj = create.create_object(typeclass_path, object_record.name) count_create += 1 except Exception, e: ostring = "Can not create obj %s: %s" % (record.key, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) continue try: obj.set_data_key(record.key, getattr(record, "level", 0)) utils.set_obj_unique_type(obj, type_name) except Exception, e: ostring = "Can not set data info to obj %s: %s" % (record.key,
obj.set_data_key(record.key, set_location=set_location) except Exception, e: ostring = "Can not set data info to obj %s: %s" % (obj_key, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) return if record.typeclass == settings.TWO_WAY_EXIT_TYPECLASS_KEY: # If it's a two way exit, create the reverse exit. # Create object. try: obj = create.create_object( TYPECLASS_SET.get_module(settings.REVERSE_EXIT_TYPECLASS_KEY), record.name) except Exception, e: ostring = "Can not create obj %s: %s" % (obj_key, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) return try: # Set data info. obj.set_data_key(settings.REVERSE_EXIT_PREFIX + record.key) except Exception, e: ostring = "Can not set data info to obj %s: %s" % (obj_key, e) print(ostring)
def build_unique_objects(objects_data, type_name, caller=None): """ Build all objects in a model. Args: model_name: (string) The name of the data model. caller: (command caller) If provide, running messages will send to the caller. """ # new objects new_obj_keys = set(record.key for record in objects_data) # current objects current_objs = utils.search_obj_unique_type(type_name) # remove objects count_remove = 0 count_update = 0 count_create = 0 current_obj_keys = set() for obj in current_objs: obj_key = obj.get_data_key() if obj_key in current_obj_keys: # This object is duplcated. ostring = "Deleting %s" % obj_key print(ostring) if caller: caller.msg(ostring) # If default home will be removed, set default home to the Limbo. if obj.dbref == settings.DEFAULT_HOME: settings.DEFAULT_HOME = "#2" obj.delete() count_remove += 1 continue if not obj_key in new_obj_keys: # This object should be removed ostring = "Deleting %s" % obj_key print(ostring) if caller: caller.msg(ostring) # If default home will be removed, set default home to the Limbo. if obj.dbref == settings.DEFAULT_HOME: settings.DEFAULT_HOME = "#2" obj.delete() count_remove += 1 continue try: # set data obj.load_data() # put obj to its default location obj.reset_location() except Exception as e: ostring = "%s can not load data:%s" % (obj.dbref, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) current_obj_keys.add(obj_key) # Create new objects. object_model_name = TYPECLASS("OBJECT").model_name object_model = apps.get_model(settings.WORLD_DATA_APP, object_model_name) for record in objects_data: if not record.key in current_obj_keys: # Create new objects. ostring = "Creating %s." % record.key print(ostring) if caller: caller.msg(ostring) try: object_record = object_model.objects.get(key=record.key) typeclass_path = TYPECLASS_SET.get_module( object_record.typeclass) obj = create.create_object(typeclass_path, object_record.name) count_create += 1 except Exception as e: ostring = "Can not create obj %s: %s" % (record.key, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) continue try: obj.set_data_key(record.key) utils.set_obj_unique_type(obj, type_name) except Exception as e: ostring = "Can not set data info to obj %s: %s" % (record.key, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) continue ostring = "Removed %d object(s). Created %d object(s). Updated %d object(s). Total %d objects.\n"\ % (count_remove, count_create, count_update, len(objects_data)) print(ostring) if caller: caller.msg(ostring)
current_obj_keys.add(obj_key) # Create new objects. object_model_name = TYPECLASS("OBJECT").model_name object_model = apps.get_model(settings.WORLD_DATA_APP, object_model_name) for record in objects_data: if not record.key in current_obj_keys: # Create new objects. ostring = "Creating %s." % record.key print(ostring) if caller: caller.msg(ostring) try: object_record = object_model.objects.get(key=record.key) typeclass_path = TYPECLASS_SET.get_module(object_record.typeclass) obj = create.create_object(typeclass_path, object_record.name) count_create += 1 except Exception, e: ostring = "Can not create obj %s: %s" % (record.key, e) print(ostring) print(traceback.print_exc()) if caller: caller.msg(ostring) continue try: obj.set_data_key(record.key) utils.set_obj_unique_type(obj, type_name) except Exception, e: ostring = "Can not set data info to obj %s: %s" % (record.key, e)