1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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]
|