<s> """<STR_LIT>""" <EOL> from dateutil . parser import parse <EOL> from dateutil . tz import tzutc <EOL> from twisted . words . protocols . jabber . jid import InvalidFormat , JID <EOL> from twisted . words . xish import domish <EOL> NS_DELAY = '<STR_LIT>' <EOL> NS_JABBER_DELAY = '<STR_LIT>' <EOL> class Delay ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stamp , sender = None ) : <EOL> self . stamp = stamp <EOL> self . sender = sender <EOL> def toElement ( self , legacy = False ) : <EOL> """<STR_LIT>""" <EOL> if not self . stamp : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if self . stamp . tzinfo is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if legacy : <EOL> element = domish . Element ( ( NS_JABBER_DELAY , '<STR_LIT:x>' ) ) <EOL> stampFormat = '<STR_LIT>' <EOL> else : <EOL> element = domish . Element ( ( NS_DELAY , '<STR_LIT>' ) ) <EOL> stampFormat = '<STR_LIT>' <EOL> stamp = self . stamp . astimezone ( tzutc ( ) ) <EOL> element [ '<STR_LIT>' ] = stamp . strftime ( stampFormat ) <EOL> if self . sender : <EOL> element [ '<STR_LIT>' ] = self . sender . full ( ) <EOL> return element <EOL> @ staticmethod <EOL> def fromElement ( element ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> stamp = parse ( element [ u'<STR_LIT>' ] ) <EOL> if stamp . tzinfo is None : <EOL> stamp = stamp . replace ( tzinfo = tzutc ( ) ) <EOL> except ( KeyError , ValueError ) : <EOL> stamp = None <EOL> try : <EOL> sender = JID ( element [ u'<STR_LIT>' ] ) <EOL> except ( KeyError , InvalidFormat ) : <EOL> sender = None <EOL> delay = Delay ( stamp , sender ) <EOL> return delay <EOL> class DelayMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> delay = None <EOL> childParsers = { <EOL> ( NS_DELAY , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( NS_JABBER_DELAY , '<STR_LIT:x>' ) : '<STR_LIT>' , <EOL> } <EOL> def _childParser_delay ( self , element ) : <EOL> self . delay = Delay . fromElement ( element ) <EOL> def _childParser_legacyDelay ( self , element ) : <EOL> if not self . delay : <EOL> self . delay = Delay . fromElement ( element ) </s>
