1
2
3
4
5
6
7
8 """This module implements some base classes used in the Registry
9 system for Biopython.
10
11
12 Classes:
13 Registry Implements a Biopython Registry.
14 RegisterableObject Base class for objects in the Registry.
15 RegisterableGroup Base class for groups of objects in the Registry.
16
17 """
19 """This is a dictionary-like object for storing and retrieving
20 objects in a registry.
21
22 Methods:
23 register Add a RegisterableObject into the Registry.
24 Dictionary interface:
25 __getitem__
26 get
27 keys
28 values
29 items
30
31 """
32 - def __init__(self, name, load_path=None):
33 """Registry(name[, load_path])
34
35 Create a new registry. name is the name of the registry.
36 load_path is an optional path (e.g. Bio.config.dbdefs) that
37 contains objects for the registry.
38
39 """
40 self._name = name
41 self._load_path = load_path
42 self._name_table, self._abbrev_table = {}, {}
43 self._autoloaded = self._autoloading = 0
44
46 if self._autoloaded or self._autoloading:
47 return
48 self._autoloading = 1
49 self._load(self._load_path)
50 self._autoloading = 0
51 self._autoloaded = 1
52
72
74 """S.register(obj)
75
76 Add an object to the registry. obj must be a
77 RegisterableObject object.
78
79 """
80 self._autoload()
81 name, abbrev = obj.name, obj.abbrev
82 abbrev = abbrev or name
83 if self._name_table.has_key(name):
84 raise ValueError("%r is a duplicate entry" % (name,))
85 if self._abbrev_table.has_key(abbrev):
86 raise ValueError("%r is a duplicate entry" % (abbrev,))
87
88 self._name_table[name] = obj
89 self._abbrev_table[abbrev] = obj
90
94
95 - def get(self, name, default=None):
98
108
110 objs = self.keys()
111 objs.sort()
112 if not objs:
113 return self._name
114 obj_str = ', '.join(map(repr, objs))
115 return "%s, exporting %s" % (self._name, obj_str)
116 __repr__ = __str__
117
119 """This is a base class for objects that can be added to a registry.
120
121 Members:
122 name The name of the object.
123 abbrev An abbreviation for the name
124
125 """
127 """RegisterableObject(name, abbrev, doc)"""
128 import re
129
130 self.name = name
131 self.abbrev = abbrev or name
132 _legal_abbrev = re.compile(r"[a-zA-Z][a-zA-Z0-9_]*$")
133 check_abbrev = _legal_abbrev.match
134 if not check_abbrev(self.abbrev):
135 raise ValueError, "abbrev name of %r is not allowed" % self.abbrev
136 self.__doc__ = doc
137
138
140 """This is a base class for a RegisterableObject that groups many
141 objects together.
142
143 Methods:
144 add Add an object to the end of the group.
145 add_after Add an object to the group after another object.
146 add_before Add an object to the group before another object.
147
148 """
152 - def add(self, obj, index=None):
157 for i in range(len(self.objs)):
158 if self.objs[i] == after:
159 break
160 else:
161 raise ValueError, "I couldn't find the insertion point"
162 self.add(obj, i+1)
164 for i in range(len(self.objs)):
165 if self.objs[i] == before:
166 break
167 else:
168 raise ValueError, "I couldn't find the insertion point"
169 self.add(obj, i)
170