Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ka_factory.py
diff options
context:
space:
mode:
Diffstat (limited to 'ka_factory.py')
-rw-r--r--ka_factory.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/ka_factory.py b/ka_factory.py
new file mode 100644
index 0000000..21f50d4
--- /dev/null
+++ b/ka_factory.py
@@ -0,0 +1,73 @@
+# coding: UTF8
+# Copyright 2009 Thomas Jourdan
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+import random
+import ka_extensionpoint
+
+class LayerFactory(object):
+ """
+ inv: self.factory_keys is not None
+ """
+ def __init__(self, category):
+ """ LayerFactory is a singleton.
+ Please call ka_factory.get_factory(category) to retrieve the instance.
+ """
+ self.category = category
+ self.factory_keys = ka_extensionpoint.list_extensions(self.category)
+ self.factory_keys = map(lambda x: x.replace(self.category + '_', ''), \
+ self.factory_keys)
+
+ def count(self):
+ """
+ post: __return__ > 0
+ """
+ return len(self.factory_keys)
+
+ def keys(self):
+ """
+ """
+ return self.factory_keys
+
+ def create(self, factory_key, trunk):
+ """
+ pre: factory_key in self.factory_keys
+ post: __return__ is not None
+ """
+ return ka_extensionpoint.create(self.category + '_' + factory_key, trunk)
+
+ def create_random(self, key_filter, trunk):
+ """
+ pre: key_filter is not None
+ pre: len(key_filter) > 0
+ post: __return__ is not None
+ """
+ permitted = [x for x in self.factory_keys if x in key_filter]
+ return self.create(random.choice(permitted), trunk)
+
+_factory_index = {}
+
+def get_factory(category):
+ """
+ pre: category is not None
+ post: __return__ is not None
+ """
+# global _factory_index
+# if _factory_index is None:
+# _factory_index = {}
+ if not _factory_index.has_key(category):
+ _factory_index[category] = LayerFactory(category)
+ return _factory_index[category]