From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-621199-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	by finch.gentoo.org (Postfix) with ESMTP id 548D21381F3
	for <garchives@archives.gentoo.org>; Fri, 23 Aug 2013 13:52:21 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id EED74E0C20;
	Fri, 23 Aug 2013 13:52:04 +0000 (UTC)
Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183])
	(using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id 18C42E0C1F
	for <gentoo-commits@lists.gentoo.org>; Fri, 23 Aug 2013 13:52:03 +0000 (UTC)
Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163])
	(using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id F277333EBCD
	for <gentoo-commits@lists.gentoo.org>; Fri, 23 Aug 2013 13:52:02 +0000 (UTC)
Received: from localhost.localdomain (localhost [127.0.0.1])
	by hornbill.gentoo.org (Postfix) with ESMTP id AF6C7E546B
	for <gentoo-commits@lists.gentoo.org>; Fri, 23 Aug 2013 13:52:00 +0000 (UTC)
From: "André Erdmann" <dywi@mailerd.de>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "André Erdmann" <dywi@mailerd.de>
Message-ID: <1377256633.1bd7938e8f3d9ba59fae9bdae378ec2120c3ca8b.dywi@gentoo>
Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/util/
X-VCS-Repository: proj/R_overlay
X-VCS-Files: roverlay/util/namespace.py
X-VCS-Directories: roverlay/util/
X-VCS-Committer: dywi
X-VCS-Committer-Name: André Erdmann
X-VCS-Revision: 1bd7938e8f3d9ba59fae9bdae378ec2120c3ca8b
X-VCS-Branch: master
Date: Fri, 23 Aug 2013 13:52:00 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Archives-Salt: a27d3759-1761-4fd0-8bcf-a8ccfde0522a
X-Archives-Hash: 44fd7c0f93a878c8c4b35af2830465f4

commit:     1bd7938e8f3d9ba59fae9bdae378ec2120c3ca8b
Author:     André Erdmann <dywi <AT> mailerd <DOT> de>
AuthorDate: Fri Aug 23 11:17:13 2013 +0000
Commit:     André Erdmann <dywi <AT> mailerd <DOT> de>
CommitDate: Fri Aug 23 11:17:13 2013 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=1bd7938e

roverlay/util/namespace:

This is (mostly) a copy of the package rules' namespace object, but aims to be
generic. Supports creation of objects with hashable args / kwarg items only,
currently.

---
 roverlay/util/namespace.py | 144 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 144 insertions(+)

diff --git a/roverlay/util/namespace.py b/roverlay/util/namespace.py
new file mode 100644
index 0000000..86d102f
--- /dev/null
+++ b/roverlay/util/namespace.py
@@ -0,0 +1,144 @@
+# R overlay -- generic namespace
+# -*- coding: utf-8 -*-
+# Copyright (C) 2013 André Erdmann <dywi@mailerd.de>
+# Distributed under the terms of the GNU General Public License;
+# either version 2 of the License, or (at your option) any later version.
+
+#import weakref
+
+import roverlay.util.common
+import roverlay.util.objects
+
+DEBUG_GET_OBJECT = False
+
+
+if DEBUG_GET_OBJECT:
+   def debug_get_object ( msg, cls, args, kwargs ):
+      print (
+         "[ObjectNamespace] {:<17} :: {}, {}, {}".format (
+            msg, cls, args, kwargs
+         )
+      )
+   # --- end of debug_get_object (...) ---
+# -- end if
+
+
+class Namespaceable ( object ):
+   @classmethod
+   def from_namespace ( cls, namespace, *args, **kwargs ):
+      return namespace.get_object_v ( cls, args, kwargs )
+   # --- end of from_namespace (...) ---
+
+   def __init__ ( self, *args, **kwargs ):
+      super ( Namespaceable, self ).__init__()
+   # --- end of __init__ (...) ---
+
+# --- end of Namespaceable ---
+
+class AbstractNamespace ( object ):
+
+   def __init__ ( self, *args, **kwargs ):
+      super ( AbstractNamespace, self ).__init__()
+   # --- end of __init__ (...) ---
+
+   @roverlay.util.objects.abstractmethod
+   def zap ( self, zap_object_db=False ):
+      pass
+   # --- end of zap (...) ---
+
+   def get_dict_hash ( self, d ):
+      # note that this doesn't handle "recursive" dicts
+      return roverlay.util.common.get_dict_hash ( d )
+   # --- end of get_dict_hash (...) ---
+
+   @roverlay.util.objects.abstractmethod
+   def get_object_v ( self, cls, args, kwargs ):
+      """Returns the desired object.
+
+      The object will be created if it does not already exist in the
+      object db of this namespace.
+
+      !!! The object has to be "shareable", i.e. it must not be modified
+          after constructing it (unless such a side-effect is intentional).
+
+      arguments:
+      * cls      --
+      * *args    --
+      * **kwargs --
+      """
+      pass
+   # --- end of get_object_v (...) ---
+
+   def get_object ( self, cls, *args, **kwargs ):
+      """Like get_object_v(), but accepts a variable number of args."""
+      return self.get_object_v ( cls, args, kwargs )
+   # --- end of get_object (...) ---
+
+# --- end of AbstractNamespace ---
+
+
+class NullNamespace ( AbstractNamespace ):
+
+   def zap ( self, *args, **kwargs ):
+      pass
+   # --- end of zap (...) ---
+
+   def get_object_v ( self, cls, args, kwargs ):
+      return cls ( *args, **kwargs )
+   # --- end of get_object_v (...) ---
+
+# --- end of NullNamespace ---
+
+
+class SimpleNamespace ( AbstractNamespace ):
+   """A namespace that caches all created objects."""
+
+   def zap ( self, zap_object_db=False ):
+      if zap_object_db:
+         self._objects.clear()
+   # --- end of zap (...) ---
+
+   def __init__ ( self ):
+      super ( SimpleNamespace, self ).__init__()
+
+      # object db
+      #  dict (
+      #     class => dict (
+      #        tuple(hash(args),hash(kwargs)) => instance
+      #     )
+      #  )
+      #
+      self._objects = dict()
+   # --- end of __init__ (...) ---
+
+   def get_object_v ( self, cls, args, kwargs ):
+      ident = (
+         hash ( args ) if args else 0,
+         self.get_dict_hash ( kwargs ) if kwargs else 0,
+      )
+
+      objects = self._objects.get ( cls, None )
+
+      if objects is None:
+         if DEBUG_GET_OBJECT:
+            debug_get_object ( "miss/new cls, obj", cls, args, kwargs )
+
+         c = cls ( *args, **kwargs )
+         self._objects [cls] = { ident : c }
+
+      else:
+         c = objects.get ( ident, None )
+
+         if c is None:
+            if DEBUG_GET_OBJECT:
+               debug_get_object ( "miss/new obj", cls, args, kwargs )
+
+            c = cls ( *args, **kwargs )
+            objects [ident] = c
+         elif DEBUG_GET_OBJECT:
+            debug_get_object ( "hit/exist", cls, args, kwargs )
+
+      return c
+   # --- end of get_object_v (...) ---
+
+# --- end of SimpleNamespace ---