* [gentoo-commits] proj/R_overlay:master commit in: roverlay/util/
@ 2013-08-29 12:36 99% André Erdmann
0 siblings, 0 replies; 1+ results
From: André Erdmann @ 2013-08-29 12:36 UTC (permalink / raw
To: gentoo-commits
commit: 758592e4107cae1a93c936062f0bda2bc9fc6dd5
Author: André Erdmann <dywi <AT> mailerd <DOT> de>
AuthorDate: Thu Aug 29 12:24:54 2013 +0000
Commit: André Erdmann <dywi <AT> mailerd <DOT> de>
CommitDate: Thu Aug 29 12:24:54 2013 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=758592e4
roverlay/util/objects: more weak referencing
This functionality will (primarily) be used for handling distmap/mirror
directory file collisions.
---
roverlay/util/objects.py | 154 +++++++++++++++++++++++++++++++++++++++++------
1 file changed, 135 insertions(+), 19 deletions(-)
diff --git a/roverlay/util/objects.py b/roverlay/util/objects.py
index 28cc4e8..ce4eccb 100644
--- a/roverlay/util/objects.py
+++ b/roverlay/util/objects.py
@@ -6,6 +6,85 @@
import weakref
+class ObjectDisappeared ( Exception ):
+ pass
+# --- end of ObjectDisappeared ---
+
+class SafeWeakRef ( weakref.ref ):
+
+ def deref_unsafe ( self ):
+ return super ( SafeWeakRef, self ).__call__()
+ # --- end of deref_unsafe (...) ---
+
+ def deref_safe ( self ):
+ obj = super ( SafeWeakRef, self ).__call__()
+ if obj is not None:
+ return obj
+ else:
+ raise ObjectDisappeared()
+ # --- end of deref_safe (...) ---
+
+ __call__ = deref_safe
+ deref = deref_safe
+
+ def __bool__ ( self ):
+ return self.deref_unsafe() is not None
+ # --- end of __bool__ (...) ---
+
+ def __repr__ ( self ):
+ obj = self.deref_unsafe()
+ if obj:
+ return "<{} at 0x{:x} to {!r} at 0x{:x}>".format (
+ self.__class__.__name__, id ( self ),
+ obj.__class__.__name__, id ( obj )
+ )
+ else:
+ return "<{} at 0x{:x} to None>".format (
+ self.__class__.__name__, id ( self )
+ )
+ # --- end of __repr__ (...) ---
+
+# --- end of SafeWeakRef ---
+
+
+class NoneRef ( object ):
+
+ def __init__ ( self, obj=None ):
+ super ( NoneRef, self ).__init__()
+ assert obj is None
+ # --- end of NoneRef (...) ---
+
+ def deref_unsafe ( self ):
+ return None
+ # --- end of deref_unsafe (...) ---
+
+ def deref_safe ( self ):
+ raise ObjectDisappeared()
+ # --- end of deref_safe (...) ---
+
+ __call__ = deref_safe
+ deref = deref_safe
+
+ def __bool__ ( self ):
+ return False
+ # --- end of __bool__ (...) ---
+
+ def __repr__ ( self ):
+ return "<NoneRef at 0x{:x}>".format ( id ( self ) )
+ # --- end of __repr__ (...) ---
+
+# --- end of NoneRef ---
+
+def get_object_ref ( obj ):
+ if obj is None:
+ return NoneRef()
+ elif hasattr ( obj, 'get_ref' ):
+ return obj.get_ref()
+ else:
+ return SafeWeakRef ( obj )
+# --- end of get_object_ref (...) ---
+
+
class MethodNotImplementedError ( NotImplementedError ):
def __init__ ( self, obj, method, msg=None ):
if isinstance ( obj, str ):
@@ -71,35 +150,72 @@ def not_implemented ( func=None ):
# --- end of not_implemented (...) ---
+class Referenceable ( object ):
+
+ CACHE_REF = False
+
+ def __init__ ( self, *args, **kwargs ):
+ super ( Referenceable, self ).__init__()
+ self._cached_selfref = None
+ if self.CACHE_REF:
+ self.cache_ref()
+ # --- end of __init__ (...) ---
+
+ def cache_ref ( self ):
+ self._cached_selfref = self.get_new_ref()
+ self.get_ref = self._get_cached_ref
+ # --- end of cache_ref (...) ---
+
+ def _get_cached_ref ( self ):
+ return self._cached_selfref
+ # --- end of _get_cached_ref (...) ---
+
+ def get_new_ref ( self ):
+ return SafeWeakRef ( self )
+ # --- end of get_new_ref (...) ---
+
+ get_ref = get_new_ref
+
+# --- end of Referenceable ---
+
+
+class ReferenceTree ( Referenceable ):
+
+ def __init__ ( self, parent ):
+ super ( ReferenceTree, self ).__init__()
+ self.parent_ref = None
+ self.set_parent ( parent )
+ # --- end of __init__ (...) ---
+
+ def set_parent ( self, parent ):
+ self.parent_ref = get_object_ref ( parent )
+ # --- end of set_parent (...) ---
+
+ def get_parent ( self ):
+ return self.parent_ref.deref_safe()
+ # --- end of get_parent (...) ---
+
+ get_upper = get_parent
+
+# --- end of ReferenceTree ---
+
+
class ObjectView ( object ):
- class ObjectDisappeared ( Exception ):
- pass
- # --- end of ObjectDisappeared ---
+ ObjectDisappeared = ObjectDisappeared
def __init__ ( self, obj ):
super ( ObjectView, self ).__init__()
- self.obj_ref = weakref.ref ( obj ) if obj is not None else None
+ self.obj_ref = get_object_ref ( obj )
+ self.deref_unsafe = self.obj_ref.deref_unsafe
+ self.deref_safe = self.obj_ref.deref_safe
+ self.deref = self.obj_ref.deref_safe
# --- end of __init__ (...) ---
def __bool__ ( self ):
- return ( self.obj_ref is not None and self.obj_ref() is not None )
+ return bool ( self.obj_ref )
# --- end of __bool__ (...) ---
- def deref_unsafe ( self ):
- return None if self.obj_ref is None else self.obj_ref()
- # --- end of deref_unsafe (...) ---
-
- def deref_safe ( self ):
- obj = None if self.obj_ref is None else self.obj_ref()
- if obj is None:
- raise self.__class__.ObjectDisappeared()
- else:
- return obj
- # --- end of deref_safe (...) ---
-
- deref = deref_safe
-
@abstractmethod
def update ( self ):
pass
^ permalink raw reply related [relevance 99%]
Results 1-1 of 1 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2013-08-29 12:36 99% [gentoo-commits] proj/R_overlay:master commit in: roverlay/util/ André Erdmann
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox