NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

decorators.py (2682B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\eveui\decorators.py
      2 import functools
      3 import weakref
      4 import locks
      5 
      6 def skip_if_destroyed(f):
      7 
      8     @functools.wraps(f)
      9     def wrapper(*args, **kwargs):
     10         self = args[0]
     11         if self.destroyed:
     12             return
     13         f(*args, **kwargs)
     14 
     15     return wrapper
     16 
     17 
     18 class lazy(object):
     19 
     20     def __init__(self, func):
     21         self.func = func
     22         self._cache = weakref.WeakKeyDictionary()
     23 
     24     def __get__(self, obj, type = None):
     25         if obj is None:
     26             return self
     27         element = self._get_cached_element(obj)
     28         if not element or element.destroyed and not obj.destroyed:
     29             element = self.func(obj)
     30             self._cache[obj] = weakref.ref(element)
     31         return element
     32 
     33     def _get_cached_element(self, obj):
     34         if obj not in self._cache:
     35             return None
     36         element_ref = self._cache[obj]
     37         return element_ref()
     38 
     39 
     40 def lock_and_set_pending():
     41 
     42     def Wrapper(func):
     43         runBoolName = 'locked_%s_running' % func.__name__
     44         pendingBoolName = 'locked_%s_pending' % func.__name__
     45 
     46         @functools.wraps(func)
     47         def FuncCalled(uiInstance):
     48             funcName = func.__name__
     49             lockName = '%s_%s' % (funcName, id(uiInstance))
     50             if is_destroyed(uiInstance):
     51                 reset_variables(uiInstance)
     52                 return
     53             if is_running(uiInstance) and is_pending(uiInstance):
     54                 return
     55             set_pending_value(uiInstance, True)
     56             with locks.TempLock(lockName):
     57                 if not is_pending(uiInstance) or is_destroyed(uiInstance):
     58                     reset_variables(uiInstance)
     59                     return
     60                 set_pending_value(uiInstance, False)
     61                 try:
     62                     set_running_value(uiInstance, True)
     63                     func(uiInstance)
     64                 finally:
     65                     set_running_value(uiInstance, False)
     66 
     67         def is_destroyed(uiInstance):
     68             return getattr(uiInstance, 'destroyed', False)
     69 
     70         def set_running_value(uiInstance, value):
     71             setattr(uiInstance, runBoolName, value)
     72 
     73         def set_pending_value(uiInstance, value):
     74             setattr(uiInstance, pendingBoolName, value)
     75 
     76         def is_running(uiInstance):
     77             return getattr(uiInstance, runBoolName, False)
     78 
     79         def is_pending(uiInstance):
     80             return getattr(uiInstance, pendingBoolName, False)
     81 
     82         def reset_variables(uiInstance):
     83             set_running_value(uiInstance, False)
     84             set_pending_value(uiInstance, False)
     85 
     86         return FuncCalled
     87 
     88     return Wrapper