Package suds :: Package xsd :: Module query
[hide private]
[frames] | no frames]

Source Code for Module suds.xsd.query

  1  # This program is free software; you can redistribute it and/or modify 
  2  # it under the terms of the (LGPL) GNU Lesser General Public License as 
  3  # published by the Free Software Foundation; either version 3 of the  
  4  # License, or (at your option) any later version. 
  5  # 
  6  # This program is distributed in the hope that it will be useful, 
  7  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
  8  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  9  # GNU Library Lesser General Public License for more details at 
 10  # ( http://www.gnu.org/licenses/lgpl.html ). 
 11  # 
 12  # You should have received a copy of the GNU Lesser General Public License 
 13  # along with this program; if not, write to the Free Software 
 14  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 15  # written by: Jeff Ortel ( jortel@redhat.com ) 
 16   
 17  """ 
 18  The I{query} module defines a class for performing schema queries. 
 19  """ 
 20   
 21  from logging import getLogger 
 22  from suds import * 
 23  from suds.sudsobject import * 
 24  from suds.xsd import qualify, isqref 
 25  from suds.xsd.sxbuiltin import Factory 
 26   
 27  log = getLogger(__name__) 
 28   
 29   
30 -class Query(Object):
31 """ 32 Schema query base class. 33 """ 34
35 - def __init__(self, ref=None):
36 """ 37 @param ref: The schema reference being queried. 38 @type ref: qref 39 """ 40 Object.__init__(self) 41 self.id = objid(self) 42 self.ref = ref 43 self.history = [] 44 self.resolved = False 45 if not isqref(self.ref): 46 raise Exception('%s, must be qref' % tostr(self.ref))
47
48 - def execute(self, schema):
49 """ 50 Execute this query using the specified schema. 51 @param schema: The schema associated with the query. The schema 52 is used by the query to search for items. 53 @type schema: L{schema.Schema} 54 @return: The item matching the search criteria. 55 @rtype: L{sxbase.SchemaObject} 56 """ 57 raise Exception, 'not-implemented by subclass'
58
59 - def filter(self, result):
60 """ 61 Filter the specified result based on query criteria. 62 @param result: A potential result. 63 @type result: L{sxbase.SchemaObject} 64 @return: True if result should be excluded. 65 @rtype: boolean 66 """ 67 if result is None: 68 return True 69 reject = ( result in self.history ) 70 if reject: 71 log.debug('result %s, rejected by\n%s', Repr(result), self) 72 return reject
73
74 - def result(self, result):
75 """ 76 Query result post processing. 77 @param result: A query result. 78 @type result: L{sxbase.SchemaObject} 79 """ 80 if result is None: 81 log.debug('%s, not-found', self.ref) 82 return 83 if self.resolved: 84 result = result.resolve() 85 log.debug('%s, found as: %s', self.ref, Repr(result)) 86 self.history.append(result) 87 return result
88 89
90 -class BlindQuery(Query):
91 """ 92 Schema query class that I{blindly} searches for a reference in 93 the specified schema. It may be used to find Elements and Types but 94 will match on an Element first. This query will also find builtins. 95 """ 96
97 - def execute(self, schema):
98 if schema.builtin(self.ref): 99 name = self.ref[0] 100 b = Factory.create(schema, name) 101 log.debug('%s, found builtin (%s)', self.id, name) 102 return b 103 result = None 104 for d in (schema.elements, schema.types): 105 result = d.get(self.ref) 106 if self.filter(result): 107 result = None 108 else: 109 break 110 if result is None: 111 eq = ElementQuery(self.ref) 112 eq.history = self.history 113 result = eq.execute(schema) 114 return self.result(result)
115 116
117 -class TypeQuery(Query):
118 """ 119 Schema query class that searches for Type references in 120 the specified schema. Matches on root types only. 121 """ 122
123 - def execute(self, schema):
124 if schema.builtin(self.ref): 125 name = self.ref[0] 126 b = Factory.create(schema, name) 127 log.debug('%s, found builtin (%s)', self.id, name) 128 return b 129 result = schema.types.get(self.ref) 130 if self.filter(result): 131 result = None 132 return self.result(result)
133 134
135 -class GroupQuery(Query):
136 """ 137 Schema query class that searches for Group references in 138 the specified schema. 139 """ 140
141 - def execute(self, schema):
142 result = schema.groups.get(self.ref) 143 if self.filter(result): 144 result = None 145 return self.result(result)
146 147
148 -class AttrQuery(Query):
149 """ 150 Schema query class that searches for Attribute references in 151 the specified schema. Matches on root Attribute by qname first, then searches 152 deep into the document. 153 """ 154
155 - def execute(self, schema):
156 result = schema.attributes.get(self.ref) 157 if self.filter(result): 158 result = self.__deepsearch(schema) 159 return self.result(result)
160
161 - def __deepsearch(self, schema):
162 from suds.xsd.sxbasic import Attribute 163 result = None 164 for e in schema.all: 165 result = e.find(self.ref, (Attribute,)) 166 if self.filter(result): 167 result = None 168 else: 169 break 170 return result
171 172
173 -class AttrGroupQuery(Query):
174 """ 175 Schema query class that searches for attributeGroup references in 176 the specified schema. 177 """ 178
179 - def execute(self, schema):
180 result = schema.agrps.get(self.ref) 181 if self.filter(result): 182 result = None 183 return self.result(result)
184 185
186 -class ElementQuery(Query):
187 """ 188 Schema query class that searches for Element references in 189 the specified schema. Matches on root Elements by qname first, then searches 190 deep into the document. 191 """ 192
193 - def execute(self, schema):
194 result = schema.elements.get(self.ref) 195 if self.filter(result): 196 result = self.__deepsearch(schema) 197 return self.result(result)
198
199 - def __deepsearch(self, schema):
200 from suds.xsd.sxbasic import Element 201 result = None 202 for e in schema.all: 203 result = e.find(self.ref, (Element,)) 204 if self.filter(result): 205 result = None 206 else: 207 break 208 return result
209