/usr/share/pyshared/soaplib/serializers/primitive.py is in python-soaplib 0.8.1-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 | from soaplib.xml import ns, create_xml_element, create_xml_subelement
from soaplib.etimport import ElementTree
import datetime
import re
import cStringIO
import pytz
from pytz import FixedOffset
#######################################################
# Utility Functions
#######################################################
string_encoding = 'utf-8'
_datetime_pattern = r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})[T ](?P<hr>\d{2}):(?P<min>\d{2}):(?P<sec>\d{2})(?P<fractional_sec>\.\d+)?'
_local_re = re.compile(_datetime_pattern)
_utc_re = re.compile(_datetime_pattern + 'Z')
_offset_re = re.compile(_datetime_pattern + r'(?P<tz_hr>[+-]\d{2}):(?P<tz_min>\d{2})')
def _is_null_element(element):
for k in element.keys():
if k.endswith('null'):
return True
return False
def _element_to_datetime(element):
# expect ISO formatted dates
#
text = element.text
if not text:
return None
def parse_date(date_match, tz=None):
fields = date_match.groupdict(0)
year, month, day, hr, min, sec = [ int(fields[x]) for x in
("year", "month", "day", "hr", "min", "sec")]
# use of decimal module here (rather than float) might be better
# here, if willing to require python 2.4 or higher
microsec = int(float(fields.get("fractional_sec", 0)) * 10**6)
return datetime.datetime(year, month, day, hr, min, sec, microsec, tz)
match = _utc_re.match(text)
if match:
return parse_date(match, tz=pytz.utc)
match = _offset_re.match(text)
if match:
tz_hr, tz_min = [int(match.group(x)) for x in "tz_hr", "tz_min"]
return parse_date(match, tz=FixedOffset(tz_hr*60 + tz_min, {}))
match = _local_re.match(text)
if match:
return parse_date(match)
raise Exception("DateTime [%s] not in known format"%text)
def _element_to_string(element):
text = element.text
if text:
return text.decode(string_encoding)
else:
return None
def _element_to_integer(element):
i = element.text
if not i:
return None
try:
return int(str(i))
except:
try: return long(i)
except: return None
def _element_to_float(element):
f = element.text
if f is None:
return None
return float(f)
def _element_to_unicode(element):
u = element.text
if not u:
return None
try:
u = str(u)
return u.encode(string_encoding)
except:
return u
def _unicode_to_xml(value, name, cls, nsmap):
retval = create_xml_element(name, nsmap)
if value == None:
return Null.to_xml(value,name,nsmap)
if type(value) == unicode:
retval.text = value
else:
retval.text = unicode(value,string_encoding)
retval.set(
nsmap.get('xsi') + 'type',
"%s:%s" % (cls.get_namespace_id(), cls.get_datatype()))
return retval
def _generic_to_xml(value, name, cls, nsmap):
retval = create_xml_element(name, nsmap)
if value:
retval.text = value
retval.set(
nsmap.get('xsi') + 'type',
"%s:%s" % (cls.get_namespace_id(), cls.get_datatype()))
return retval
def _get_datatype(cls, typename, nsmap):
if nsmap is not None:
return nsmap.get(cls.get_namespace_id()) + typename
return typename
class Any:
@classmethod
def to_xml(cls,value,name='retval',nsmap=ns):
if type(value) == str:
value = ElementTree.fromstring(value)
e = create_xml_element(name, nsmap)
e.append(value)
return e
@classmethod
def from_xml(cls,element):
children = element.getchildren()
if children:
return element.getchildren()[0]
return None
@classmethod
def get_datatype(cls, nsmap=None):
return _get_datatype(cls, 'anyType', nsmap)
@classmethod
def get_namespace_id(cls):
return 'xs'
@classmethod
def add_to_schema(cls,added_params,nsmap):
pass
class String:
@classmethod
def to_xml(cls,value,name='retval',nsmap=ns):
e = _unicode_to_xml(value, name, cls, nsmap)
return e
@classmethod
def from_xml(cls,element):
return _element_to_unicode(element)
@classmethod
def get_datatype(cls, nsmap=None):
return _get_datatype(cls, 'string', nsmap)
@classmethod
def get_namespace_id(cls):
return 'xs'
@classmethod
def add_to_schema(cls,added_params,nsmap):
pass
class Fault(Exception):
def __init__(self, faultcode = 'Server', faultstring = None, detail = None, name = 'ExceptionFault'):
self.faultcode = faultcode
self.faultstring = faultstring
self.detail = detail
self.name = name
@classmethod
def to_xml(cls, value, name, nsmap=ns):
fault = create_xml_element(name, nsmap)
create_xml_subelement(fault, 'faultcode').text = value.faultcode
create_xml_subelement(fault, 'faultstring').text = value.faultstring
detail = create_xml_subelement(fault, 'detail').text = value.detail
return fault
@classmethod
def from_xml(cls, element):
code = _element_to_string(element.find('faultcode'))
string = _element_to_string(element.find('faultstring'))
detail_element = element.find('detail')
if detail_element is not None:
if len(detail_element.getchildren()):
detail = ElementTree.tostring(detail_element)
else:
detail = _element_to_string(element.find('detail'))
else:
detail = ''
return Fault(faultcode = code, faultstring = string, detail = detail)
@classmethod
def get_datatype(cls, nsmap=None):
return _get_datatype(cls, 'ExceptionFaultType', nsmap)
@classmethod
def get_namespace_id(cls):
return 'tns'
@classmethod
def add_to_schema(cls,schema_dict,nsmap):
complexTypeNode = create_xml_element('complexType', nsmap)
complexTypeNode.set('name', cls.get_datatype())
sequenceNode = create_xml_subelement(complexTypeNode, 'sequence')
faultTypeElem = create_xml_subelement(sequenceNode,'element')
faultTypeElem.set('name','detail')
faultTypeElem.set(nsmap.get('xsi') + 'type', 'xs:string')
faultTypeElem = create_xml_subelement(sequenceNode,'element')
faultTypeElem.set('name','message')
faultTypeElem.set(nsmap.get('xsi') + 'type', 'xs:string')
schema_dict[cls.get_datatype()] = complexTypeNode
typeElementItem = create_xml_element('element', nsmap)
typeElementItem.set('name', 'ExceptionFaultType')
typeElementItem.set(nsmap.get('xsi') + 'type', cls.get_datatype(nsmap))
schema_dict['%sElement'%(cls.get_datatype(nsmap))] = typeElementItem
def __str__(self):
io = cStringIO.StringIO()
io.write("*"*80)
io.write("\r\n")
io.write(" Recieved soap fault \r\n")
io.write(" FaultCode %s \r\n"%self.faultcode)
io.write(" FaultString %s \r\n"%self.faultstring)
io.write(" FaultDetail \r\n")
if self.detail is not None:
io.write(self.detail)
return io.getvalue()
class Integer:
@classmethod
def to_xml(cls,value,name='retval',nsmap=ns):
e = _generic_to_xml(str(value), name, cls, nsmap)
return e
@classmethod
def from_xml(cls,element):
return _element_to_integer(element)
@classmethod
def get_datatype(cls, nsmap=None):
return _get_datatype(cls, 'int', nsmap)
@classmethod
def get_namespace_id(cls):
return 'xs'
@classmethod
def add_to_schema(cls,added_params,nsmap):
pass
class Double:
@classmethod
def to_xml(cls,value,name='retval',nsmap=ns):
e = _generic_to_xml(str(value), name, cls, nsmap)
return e
@classmethod
def from_xml(cls,element):
return _element_to_integer(element)
@classmethod
def get_datatype(cls, nsmap=None):
return _get_datatype(cls, 'double', nsmap)
@classmethod
def get_namespace_id(cls):
return 'xs'
@classmethod
def add_to_schema(cls,added_params,nsmap):
pass
class DateTime:
@classmethod
def to_xml(cls,value,name='retval',nsmap=ns):
if type(value) == datetime.datetime:
value = value.isoformat('T')
e = _generic_to_xml(value, name, cls, nsmap)
return e
@classmethod
def from_xml(cls,element):
return _element_to_datetime(element)
@classmethod
def get_datatype(cls, nsmap=None):
return _get_datatype(cls, 'dateTime', nsmap)
@classmethod
def get_namespace_id(cls):
return 'xs'
@classmethod
def add_to_schema(cls,added_params,nsmap):
pass
class Float:
@classmethod
def to_xml(cls,value,name='retval',nsmap=ns):
e = _generic_to_xml(str(value), name, cls, nsmap)
return e
@classmethod
def from_xml(cls,element):
return _element_to_float(element)
@classmethod
def get_datatype(cls, nsmap=None):
return _get_datatype(cls, 'float', nsmap)
@classmethod
def get_namespace_id(cls):
return 'xs'
@classmethod
def add_to_schema(cls,added_params,nsmap):
pass
class Null:
@classmethod
def to_xml(cls,value,name='retval',nsmap=ns):
element = create_xml_element(name, nsmap)
element.set(cls.get_datatype(nsmap),'1')
return element
@classmethod
def from_xml(cls,element):
return None
@classmethod
def get_datatype(cls, nsmap=None):
return _get_datatype(cls, 'null', nsmap)
@classmethod
def get_namespace_id(cls):
return 'xs'
@classmethod
def add_to_schema(cls,added_params,nsmap):
pass
class Boolean:
@classmethod
def to_xml(cls,value,name='retval',nsmap=ns):
# applied patch from Julius Volz
#e = _generic_to_xml(str(value).lower(),name,cls.get_datatype(nsmap))
if value == None:
return Null.to_xml('', name, nsmap)
else:
e = _generic_to_xml(str(bool(value)).lower(), name, cls, nsmap)
return e
@classmethod
def from_xml(cls,element):
s = _element_to_string(element)
if s == None:
return None
if s and s.lower()[0] == 't':
return True
return False
@classmethod
def get_datatype(cls, nsmap=None):
return _get_datatype(cls, 'boolean', nsmap)
@classmethod
def get_namespace_id(cls):
return 'xs'
@classmethod
def add_to_schema(cls,added_params,nsmap):
pass
class Array:
def __init__(self,serializer,type_name=None,namespace_id='tns'):
self.serializer = serializer
self.namespace_id = namespace_id
if not type_name:
self.type_name = '%sArray'%self.serializer.get_datatype()
else:
self.type_name = type_name
def to_xml(self,values,name='retval',nsmap=ns):
res = create_xml_element(name, nsmap)
typ = self.get_datatype(nsmap)
if values == None:
values = []
res.set('type',
"%s:%s" % (self.get_namespace_id(), self.get_datatype()))
for value in values:
serializer = self.serializer
if value == None:
serializer = Null
res.append(
serializer.to_xml(value, serializer.get_datatype(), nsmap))
return res
def from_xml(self,element):
results = []
for child in element.getchildren():
results.append(self.serializer.from_xml(child))
return results
def get_datatype(self, nsmap=None):
return _get_datatype(self, self.type_name, nsmap)
def get_namespace_id(self):
return self.namespace_id
def add_to_schema(self,schema_dict,nsmap):
typ = self.get_datatype()
self.serializer.add_to_schema(schema_dict, nsmap)
if not schema_dict.has_key(typ):
complexTypeNode = create_xml_element(
nsmap.get('xs') + 'complexType', nsmap)
complexTypeNode.set('name',self.get_datatype())
sequenceNode = create_xml_subelement(
complexTypeNode, nsmap.get('xs') + 'sequence')
elementNode = create_xml_subelement(
sequenceNode, nsmap.get('xs') + 'element')
elementNode.set('minOccurs','0')
elementNode.set('maxOccurs','unbounded')
elementNode.set('type',
"%s:%s" % (self.namespace_id, self.serializer.get_datatype()))
elementNode.set('name',self.serializer.get_datatype())
typeElement = create_xml_element(
nsmap.get('xs') + 'element', nsmap)
typeElement.set('name',typ)
typeElement.set('type',
"%s:%s" % (self.namespace_id, self.get_datatype()))
schema_dict['%sElement'%(self.get_datatype(nsmap))] = typeElement
schema_dict[self.get_datatype(nsmap)] = complexTypeNode
class Repeating(object):
def __init__(self,serializer,type_name=None,namespace_id='tns'):
self.serializer = serializer
self.namespace_id = namespace_id
def to_xml(self,values,name='retval',nsmap=ns):
if values == None:
values = []
res = []
for value in values:
serializer = self.serializer
if value == None:
serializer = Null
res.append(
serializer.to_xml(value,name,nsmap)
)
return res
def get_namespace_id(self):
return self.namespace_id
def from_xml(self,*elements):
results = []
for child in elements:
results.append(self.serializer.from_xml(child))
return results
def add_to_schema(self,schema_dict,nsmap):
raise Exception("The Repeating serializer is experimental and not supported for wsdl generation")
|