From 5657438e3c699ece4f5e169540c35ab8f89a2949 Mon Sep 17 00:00:00 2001 From: blitzmann Date: Fri, 14 Apr 2017 21:24:36 -0400 Subject: [PATCH] Change up effect classes for better efficiency --- eos/db/gamedata/effect.py | 13 ++------ eos/db/gamedata/item.py | 3 +- eos/gamedata.py | 70 ++++++++++++++++----------------------- 3 files changed, 34 insertions(+), 52 deletions(-) diff --git a/eos/db/gamedata/effect.py b/eos/db/gamedata/effect.py index 861c3fa64..e43bffa22 100644 --- a/eos/db/gamedata/effect.py +++ b/eos/db/gamedata/effect.py @@ -22,7 +22,7 @@ from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.orm import mapper, synonym, relation, deferred from eos.db import gamedata_meta -from eos.gamedata import Effect, EffectInfo +from eos.gamedata import Effect, ItemEffect typeeffects_table = Table("dgmtypeeffects", gamedata_meta, Column("typeID", Integer, ForeignKey("invtypes.typeID"), primary_key=True, index=True), @@ -36,19 +36,12 @@ effects_table = Table("dgmeffects", gamedata_meta, Column("isAssistance", Boolean), Column("isOffensive", Boolean)) -mapper(EffectInfo, effects_table, +mapper(Effect, effects_table, properties={ "ID" : synonym("effectID"), "name" : synonym("effectName"), "description": deferred(effects_table.c.description) }) -mapper(Effect, typeeffects_table, - properties={ - "ID" : synonym("effectID"), - "info": relation(EffectInfo, lazy=False) - }) +mapper(ItemEffect, typeeffects_table) -Effect.name = association_proxy("info", "name") -Effect.description = association_proxy("info", "description") -Effect.published = association_proxy("info", "published") diff --git a/eos/db/gamedata/item.py b/eos/db/gamedata/item.py index 1e5d7421a..ad6882435 100644 --- a/eos/db/gamedata/item.py +++ b/eos/db/gamedata/item.py @@ -21,6 +21,7 @@ from sqlalchemy import Column, String, Integer, Boolean, ForeignKey, Table, Floa from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.orm import relation, mapper, synonym, deferred from sqlalchemy.orm.collections import attribute_mapped_collection +from eos.db.gamedata.effect import typeeffects_table from eos.db import gamedata_meta from eos.gamedata import Attribute, Effect, Group, Icon, Item, MetaType, Traits @@ -47,7 +48,7 @@ mapper(Item, items_table, "group" : relation(Group, backref="items"), "icon" : relation(Icon), "_Item__attributes": relation(Attribute, collection_class=attribute_mapped_collection('name')), - "effects" : relation(Effect, collection_class=attribute_mapped_collection('name')), + "effects": relation(Effect, secondary=typeeffects_table, collection_class=attribute_mapped_collection('name')), "metaGroup" : relation(MetaType, primaryjoin=metatypes_table.c.typeID == items_table.c.typeID, uselist=False), diff --git a/eos/gamedata.py b/eos/gamedata.py index 8700229dc..27a202167 100644 --- a/eos/gamedata.py +++ b/eos/gamedata.py @@ -33,8 +33,6 @@ except ImportError: from logbook import Logger pyfalog = Logger(__name__) -# Keep a list of handlers that fail to import so we don't keep trying repeatedly. -badHandlers = [] class Effect(EqBase): @@ -162,51 +160,41 @@ class Effect(EqBase): Grab the handler, type and runTime from the effect code if it exists, if it doesn't, set dummy values and add a dummy handler """ - global badHandlers - # Skip if we've tried to import before and failed - if self.handlerName not in badHandlers: - try: - self.__effectModule = effectModule = __import__('eos.effects.' + self.handlerName, fromlist=True) - self.__handler = getattr(effectModule, "handler", effectDummy) - self.__runTime = getattr(effectModule, "runTime", "normal") - self.__activeByDefault = getattr(effectModule, "activeByDefault", True) - t = getattr(effectModule, "type", None) + pyfalog.debug("Generate effect handler for {}".format(self.name)) - t = t if isinstance(t, tuple) or t is None else (t,) - self.__type = t - except (ImportError) as e: - # Effect probably doesn't exist, so create a dummy effect and flag it with a warning. - self.__handler = effectDummy - self.__runTime = "normal" - self.__activeByDefault = True - self.__type = None - pyfalog.debug("ImportError generating handler: {0}", e) - badHandlers.append(self.handlerName) - except (AttributeError) as e: - # Effect probably exists but there is an issue with it. Turn it into a dummy effect so we can continue, but flag it with an error. - self.__handler = effectDummy - self.__runTime = "normal" - self.__activeByDefault = True - self.__type = None - pyfalog.error("AttributeError generating handler: {0}", e) - badHandlers.append(self.handlerName) - except Exception as e: - self.__handler = effectDummy - self.__runTime = "normal" - self.__activeByDefault = True - self.__type = None - pyfalog.critical("Exception generating handler:") - pyfalog.critical(e) - badHandlers.append(self.handlerName) + try: + self.__effectModule = effectModule = __import__('eos.effects.' + self.handlerName, fromlist=True) + self.__handler = getattr(effectModule, "handler", effectDummy) + self.__runTime = getattr(effectModule, "runTime", "normal") + self.__activeByDefault = getattr(effectModule, "activeByDefault", True) + t = getattr(effectModule, "type", None) - self.__generated = True - else: - # We've already failed on this one, just pass a dummy effect back + t = t if isinstance(t, tuple) or t is None else (t,) + self.__type = t + except (ImportError) as e: + # Effect probably doesn't exist, so create a dummy effect and flag it with a warning. self.__handler = effectDummy self.__runTime = "normal" self.__activeByDefault = True self.__type = None + pyfalog.debug("ImportError generating handler: {0}", e) + except (AttributeError) as e: + # Effect probably exists but there is an issue with it. Turn it into a dummy effect so we can continue, but flag it with an error. + self.__handler = effectDummy + self.__runTime = "normal" + self.__activeByDefault = True + self.__type = None + pyfalog.error("AttributeError generating handler: {0}", e) + except Exception as e: + self.__handler = effectDummy + self.__runTime = "normal" + self.__activeByDefault = True + self.__type = None + pyfalog.critical("Exception generating handler:") + pyfalog.critical(e) + + self.__generated = True def getattr(self, key): if not self.__generated: @@ -471,7 +459,7 @@ class MetaData(EqBase): pass -class EffectInfo(EqBase): +class ItemEffect(EqBase): pass