Skip to content

Commit

Permalink
Add UTC class and change the wsse timestamps to use sax.date classes …
Browse files Browse the repository at this point in the history
…for propert formatting. Also, change wsse classes to have timestamps in UTC.

git-svn-id: http://svn.fedorahosted.org/svn/suds/trunk@663 0b8c961e-115e-4cb0-8d11-a7d6dae58e8c
  • Loading branch information
jortel committed Mar 3, 2010
1 parent e410a71 commit f8e2387
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion suds/__init__.py
Expand Up @@ -27,7 +27,7 @@
#

__version__ = '0.4'
__build__="(beta) R660-20100219"
__build__="(beta) R663-20100303"

#
# Exceptions
Expand Down
26 changes: 22 additions & 4 deletions suds/sax/date.py
Expand Up @@ -132,6 +132,7 @@ def __init__(self, time, adjusted=True):
@type adjusted: boolean
@raise ValueError: When I{time} is invalid.
"""
self.tz = Timezone()
if isinstance(time, dt.time):
self.time = time
return
Expand Down Expand Up @@ -245,7 +246,7 @@ def __offset(self, s):
if len(s) == len('-00:00'):
return int(s[:3])
if len(s) == 0:
return Timezone.local
return self.tz.local
if len(s) == 1:
return 0
raise Exception()
Expand All @@ -255,7 +256,10 @@ def __str__(self):

def __unicode__(self):
time = self.time.isoformat()
return '%s%+.2d:00' % (time, Timezone.local)
if self.tz.local:
return '%s%+.2d:00' % (time, self.tz.local)
else:
return '%sZ' % time


class DateTime(Date,Time):
Expand Down Expand Up @@ -319,6 +323,18 @@ def __unicode__(self):
return 'T'.join(s)


class UTC(DateTime):
"""
Represents current UTC time.
"""

def __init__(self, date=None):
if date is None:
date = dt.datetime.utcnow()
DateTime.__init__(self, date)
self.tz.local = 0


class Timezone:
"""
Timezone object used to do TZ conversions
Expand All @@ -327,9 +343,11 @@ class Timezone:
@cvar patten: The regex patten to match TZ.
@type patten: L{re.RegexObject}
"""

local = ( 0-time.timezone/60/60 )

pattern = re.compile('([zZ])|([\-\+][0-9]{2}:[0-9]{2})')

def __init__(self):
self.local = ( 0-time.timezone/60/60 )

@classmethod
def split(cls, s):
Expand Down
20 changes: 13 additions & 7 deletions suds/wsse.py
Expand Up @@ -22,6 +22,7 @@
from suds import *
from suds.sudsobject import Object
from suds.sax.element import Element
from suds.sax.date import UTC
from datetime import datetime, timedelta

try:
Expand Down Expand Up @@ -87,9 +88,14 @@ class Token(Object):
def now(cls):
return datetime.now()

@classmethod
def utc(cls):
return datetime.utcnow()

@classmethod
def sysdate(cls):
return cls.now().isoformat()
utc = UTC()
return str(utc)

def __init__(self):
Object.__init__(self)
Expand Down Expand Up @@ -144,11 +150,11 @@ def setcreated(self, dt=None):
"""
Set I{created}.
@param dt: The created date & time.
Set as datetime.now() when I{None}.
Set as datetime.utc() when I{None}.
@type dt: L{datetime}
"""
if dt is None:
self.created = Token.now()
self.created = Token.utc()
else:
self.created = dt

Expand All @@ -172,7 +178,7 @@ def xml(self):
root.append(n)
if self.created is not None:
n = Element('Created', ns=wsuns)
n.setText(self.created.isoformat())
n.setText(str(UTC(self.created)))
root.append(n)
return root

Expand All @@ -192,15 +198,15 @@ def __init__(self, validity=90):
@type validity: int
"""
Token.__init__(self)
self.created = Token.now()
self.created = Token.utc()
self.expires = self.created + timedelta(seconds=validity)

def xml(self):
root = Element("Timestamp", ns=wsuns)
created = Element('Created', ns=wsuns)
created.setText(self.created.isoformat())
created.setText(str(UTC(self.created)))
expires = Element('Expires', ns=wsuns)
expires.setText(self.expires.isoformat())
expires.setText(str(UTC(self.expires)))
root.append(created)
root.append(expires)
return root
1 change: 1 addition & 0 deletions tests/builtin.py
Expand Up @@ -16,6 +16,7 @@

import sys
sys.path.append('../')
import unittest
from suds.sax.date import Timezone as Tz
from suds.xsd.sxbuiltin import *
from unittest import TestCase
Expand Down

0 comments on commit f8e2387

Please sign in to comment.