Package Bio :: Package config :: Module Registry
[hide private]
[frames] | no frames]

Source Code for Module Bio.config.Registry

  1  # Copyright 2002 by Jeffrey Chang, Andrew Dalke.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5   
  6  # This is based on some older code by Andrew Dalke. 
  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  """ 
18 -class Registry:
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
45 - def _autoload(self):
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
53 - def _load(self, path):
54 if path is None: 55 return 56 import _support 57 # Get a list of all the modules in that path. 58 modulenames = _support.find_submodules(path) 59 modulenames = filter(lambda x: not x.startswith("_"), modulenames) 60 modulenames.sort() 61 62 # Now load each one of the modules and look for 63 # RegisterableObject objects in them. 64 65 for name in modulenames: 66 module = _support.load_module(name) 67 for name, obj in module.__dict__.items(): 68 if name.startswith("_") or \ 69 not isinstance(obj, RegisterableObject): 70 continue 71 self.register(obj)
72
73 - def register(self, obj):
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
91 - def __getitem__(self, name):
92 self._autoload() 93 return self._name_table[name] # raises KeyError for unknown entries
94
95 - def get(self, name, default=None):
96 self._autoload() 97 return self._name_table.get(name, default)
98
99 - def keys(self):
100 self._autoload() 101 return self._name_table.keys()
102 - def values(self):
103 self._autoload() 104 return self._name_table.values()
105 - def items(self):
106 self._autoload() 107 return self._name_table.items()
108
109 - def __str__(self):
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
118 -class RegisterableObject:
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 """
126 - def __init__(self, name, abbrev, doc):
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
139 -class RegisterableGroup(RegisterableObject):
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 """
149 - def __init__(self, name, abbrev, doc):
150 RegisterableObject.__init__(self, name, abbrev, doc) 151 self.objs = []
152 - def add(self, obj, index=None):
153 if index is None: 154 index = len(self.objs) 155 self.objs.insert(index, obj)
156 - def add_after(self, obj, after):
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)
163 - def add_before(self, obj, before):
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