Home | Trees | Indices | Help |
|
---|
|
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 """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 #40 41 return ElemObjClass(name) 42 43 #======================================================= 44 # 45 """ 46 Makes a comment element 47 """ 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 """22 """ 23 Cannot Initialize a true abstact class! 24 """ 25 raise TypeError, "an abstract class cannot be instantiated"26 27 28 #======================================================= 29 #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_ = None61 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 #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 #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 #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 #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 collectorDict151 152 #======================================================= 153 #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 collectorDict165 166 #======================================================= 167 #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 collection179 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 """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 None207 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 """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 None238
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0alpha3 on Thu Sep 07 21:19:42 2006 | http://epydoc.sourceforge.net |