Package pyxsd :: Package transforms :: Module transform
[hide private]
[frames] | no frames]

Source Code for Module pyxsd.transforms.transform

  1  """ 
  2  See the transforms manual on the `pyXSD website <http://pyXSD.org/>`_ 
  3  or in the /doc directory of the installation. 
  4   
  5  In the transform manual: 
  6   
  7  - Basic and advanced tutorials on writing transfomers 
  8  - An overview of this class 
  9  - Basic use instructions 
 10  - Documentation on included transform libraries 
 11   
 12  """ 
13 -class Transform (object):
14 """ 15 The base abstract class for all transforms. All methods should 16 mix into the usable transform classes. Contains methods to 17 retrieve elements from the tree. 18 """ 19 #======================================================= 20 #
21 - def __init__(self):
22 """ 23 Cannot Initialize a true abstact class! 24 """ 25 raise TypeError, "an abstract class cannot be instantiated"
26 27 28 #======================================================= 29 #
30 - def makeElemObj(self, name):
31 """ 32 Creates a new element that contains the proper tree stucture 33 """ 34 class ElemObjClass(object): 35 def __init__(self, name): 36 self._children_ = [] 37 self._attribs_ = {} 38 self._name_ = name 39 self._value_ = None
40 41 return ElemObjClass(name) 42 43 #======================================================= 44 # 45 """ 46 Makes a comment element 47 """
48 - def makeCommentElem(self, comment):
49 obj = self.makeElemObj('_comment_') 50 obj._value_ = comment 51 return obj
52 53 #======================================================= 54 # 55 # 56 """ 57 The *walk* function walks through the tree structure, and 58 runs a provided visitor function on all elements. 59 """
60 - def walk(self, instance, visitor, *args, **kwargs):
61 62 if isinstance(instance, list): 63 for item in instance: 64 self.walk(item, visitor, *args, **kwargs) 65 66 if isinstance(instance, dict): 67 for item in instance.values(): 68 self.walk(item, visitor, *args, **kwargs) 69 70 if not hasattr(instance, '_children_'): return 71 if not hasattr(instance, '_attribs_'): return 72 73 getName = lambda x: x.name 74 75 elemNames = map(lambda x: x._name_, instance._children_) 76 attrNames = instance._attribs_.keys() 77 78 visitor(instance, attrNames, elemNames, *args, **kwargs) 79 80 for el in instance._children_: 81 82 self.walk(el, visitor, *args, **kwargs)
83 84 #======================================================= 85 #
86 - def classCollector(self, instance, attrNames, elemNames, collectorDict):
87 """ 88 Visitor function to make a dictionary that associates a class with 89 its instances. The class name is the key, and the value is the list 90 of associated instances. SEE *getInstancesByClassName* 91 """ 92 className = instance.__class__.__name__ 93 94 collection = collectorDict.get(className, None) 95 96 if collection == None: 97 collection = [] 98 collectorDict[className] = collection 99 100 collection.append[instance]
101 102 #======================================================= 103 #
104 - def tagCollector(self, instance, attrNames, elemNames, collectorDict):
105 """ 106 A visitor function that is used to make a dictionary that associates 107 a tag name with its children. SEE *getAllSubElements* 108 """ 109 110 for i, tagName in list(enumerate(elemNames)): 111 obj = instance._children_[i] 112 if obj == None: continue 113 114 115 collection = collectorDict.get(tagName, None) 116 117 if collection == None: 118 119 collection = [] 120 collectorDict[tagName] = collection 121 122 collection.append(obj)
123 124 125 #======================================================= 126 #
127 - def tagFinder(self, instance, attrNames, elemNames, collection, name):
128 """ 129 A visitor function to collect all tags with a particular name, and 130 put them into a list. SEE *getElementsByName* 131 """ 132 for i, tagName in list(enumerate(elemNames)): 133 if name == tagName: 134 obj = instance._children_[i] 135 if not obj==None: 136 collection.append(obj)
137 138 #======================================================= 139 #
140 - def getInstancesByClassName(self, root):
141 """ 142 Function to use the *walk* function with the *classCollector* 143 visitor function. Used to associate a class name with the class' 144 instances. 145 """ 146 collectorDict = {} 147 148 self.walk(root, self.classCollector, collectorDict) 149 150 return collectorDict
151 152 #======================================================= 153 #
154 - def getAllSubElements(self, root):
155 """ 156 Function to use the *walk* function with the *tagCollector* 157 visitor function to make a dictionary that associates all 158 elements with their sub-Elements. 159 """ 160 collectorDict = {} 161 162 self.walk(root, self.tagCollector, collectorDict) 163 164 return collectorDict
165 166 #======================================================= 167 #
168 - def getElementsByName(self, root, name):
169 """ 170 Function to use the *walk* function with the *tagFinder* 171 visitor function to make a list containing all elements 172 with a particular name. 173 """ 174 collection = [] 175 176 self.walk(root, self.tagFinder, collection, name) 177 178 return collection
179 180 #======================================================= 181 182 183 184 #======================================================= 185 # 186 """Finds an element from a given tagName. Returns the first one found, 187 or returns None. This function is an alternative to the walk/vistor functions. 188 SEE *getElementsByName* 189 """
190 - def find(self, tagName, baseElem):
191 192 if baseElem._name_ == tagName: 193 194 return baseElem 195 196 for child in baseElem._children_: 197 198 returnedElement = self.find(tagName, child) 199 200 if not returnedElement == None: 201 202 return returnedElement 203 204 continue 205 206 return None
207 208 #======================================================= 209 # 210 """ 211 Finds all elements with a given tagName. Returns a list of elements or None. 212 This function is an alternative to the walk/vistor functions. 213 SEE *getElementsByName* 214 """
215 - def findAll(self, tagName, baseElem):
216 217 found = [] 218 219 if baseElem._name_ == tagName: 220 221 found.append(baseElem) 222 223 for child in baseElem._children_: 224 225 returnedElement = self.findAll(tagName, child) 226 227 if not returnedElement == None: 228 229 found.extend(returnedElement) 230 231 continue 232 233 234 if len(found) > 0: 235 return found 236 237 return None
238