<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , unicode_literals <EOL> import time <EOL> from datetime import datetime <EOL> import base64 <EOL> import copy <EOL> import six <EOL> from wechatpy . utils import to_text , to_binary , ObjectDict , timezone <EOL> default_timezone = timezone ( '<STR_LIT>' ) <EOL> class FieldDescriptor ( object ) : <EOL> def __init__ ( self , field ) : <EOL> self . field = field <EOL> self . attr_name = field . name <EOL> def __get__ ( self , instance , instance_type = None ) : <EOL> if instance is not None : <EOL> value = instance . _data . get ( self . attr_name ) <EOL> if value is None : <EOL> value = copy . deepcopy ( self . field . default ) <EOL> instance . _data [ self . attr_name ] = value <EOL> if isinstance ( value , dict ) : <EOL> value = ObjectDict ( value ) <EOL> if value and not isinstance ( value , ( dict , list , tuple ) ) and six . callable ( self . field . converter ) : <EOL> value = self . field . converter ( value ) <EOL> return value <EOL> return self . field <EOL> def __set__ ( self , instance , value ) : <EOL> instance . _data [ self . attr_name ] = value <EOL> class BaseField ( object ) : <EOL> converter = None <EOL> def __init__ ( self , name , default = None ) : <EOL> self . name = name <EOL> self . default = default <EOL> def to_xml ( self , value ) : <EOL> raise NotImplementedError ( ) <EOL> def __repr__ ( self ) : <EOL> _repr = '<STR_LIT>' . format ( <EOL> klass = self . __class__ . __name__ , <EOL> name = repr ( self . name ) <EOL> ) <EOL> if six . PY2 : <EOL> return to_binary ( _repr ) <EOL> else : <EOL> return to_text ( _repr ) <EOL> def add_to_class ( self , klass , name ) : <EOL> self . klass = klass <EOL> klass . _fields [ name ] = self <EOL> setattr ( klass , name , FieldDescriptor ( self ) ) <EOL> class StringField ( BaseField ) : <EOL> def __to_text ( self , value ) : <EOL> return to_text ( value ) <EOL> converter = __to_text <EOL> def to_xml ( self , value ) : <EOL> value = self . converter ( value ) <EOL> tpl = '<STR_LIT>' <EOL> return tpl . format ( name = self . name , value = value ) <EOL> class IntegerField ( BaseField ) : <EOL> converter = int <EOL> def to_xml ( self , value ) : <EOL> value = self . converter ( value ) if value is not None else self . default <EOL> tpl = '<STR_LIT>' <EOL> return tpl . format ( name = self . name , value = value ) <EOL> class DateTimeField ( BaseField ) : <EOL> def __converter ( self , value ) : <EOL> v = int ( value ) <EOL> return datetime . fromtimestamp ( v , tz = default_timezone ) <EOL> converter = __converter <EOL> def to_xml ( self , value ) : <EOL> value = time . mktime ( datetime . timetuple ( value ) ) <EOL> value = int ( value ) <EOL> tpl = '<STR_LIT>' <EOL> return tpl . format ( name = self . name , value = value ) <EOL> class FloatField ( BaseField ) : <EOL> converter = float <EOL> def to_xml ( self , value ) : <EOL> value = self . converter ( value ) if value is not None else self . default <EOL> tpl = '<STR_LIT>' <EOL> return tpl . format ( name = self . name , value = value ) <EOL> class ImageField ( StringField ) : <EOL> def to_xml ( self , value ) : <EOL> value = self . converter ( value ) <EOL> tpl = """<STR_LIT>""" <EOL> return tpl . format ( value = value ) <EOL> class VoiceField ( StringField ) : <EOL> def to_xml ( self , value ) : <EOL> value = self . converter ( value ) <EOL> tpl = """<STR_LIT>""" <EOL> return tpl . format ( value = value ) <EOL> class VideoField ( StringField ) : <EOL> def to_xml ( self , value ) : <EOL> media_id = self . converter ( value [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT:title>' in value : <EOL> title = self . converter ( value [ '<STR_LIT:title>' ] ) <EOL> if '<STR_LIT:description>' in value : <EOL> description = self . converter ( value [ '<STR_LIT:description>' ] ) <EOL> tpl = """<STR_LIT>""" <EOL> return tpl . format ( <EOL> media_id = media_id , <EOL> title = title , <EOL> description = description <EOL> ) <EOL> class MusicField ( StringField ) : <EOL> def to_xml ( self , value ) : <EOL> thumb_media_id = self . converter ( value [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT:title>' in value : <EOL> title = self . converter ( value [ '<STR_LIT:title>' ] ) <EOL> if '<STR_LIT:description>' in value : <EOL> description = self . converter ( value [ '<STR_LIT:description>' ] ) <EOL> if '<STR_LIT>' in value : <EOL> music_url = self . converter ( value [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in value : <EOL> hq_music_url = self . converter ( value [ '<STR_LIT>' ] ) <EOL> tpl = """<STR_LIT>""" <EOL> return tpl . format ( <EOL> thumb_media_id = thumb_media_id , <EOL> title = title , <EOL> description = description , <EOL> music_url = music_url , <EOL> hq_music_url = hq_music_url <EOL> ) <EOL> class ArticlesField ( StringField ) : <EOL> def to_xml ( self , articles ) : <EOL> article_count = len ( articles ) <EOL> items = [ ] <EOL> for article in articles : <EOL> title = self . converter ( article . get ( '<STR_LIT:title>' , '<STR_LIT>' ) ) <EOL> description = self . converter ( article . get ( '<STR_LIT:description>' , '<STR_LIT>' ) ) <EOL> image = self . converter ( article . get ( '<STR_LIT:image>' , '<STR_LIT>' ) ) <EOL> url = self . converter ( article . get ( '<STR_LIT:url>' , '<STR_LIT>' ) ) <EOL> item_tpl = """<STR_LIT>""" <EOL> item = item_tpl . format ( <EOL> title = title , <EOL> description = description , <EOL> image = image , <EOL> url = url <EOL> ) <EOL> items . append ( item ) <EOL> items_str = '<STR_LIT:\n>' . join ( items ) <EOL> tpl = """<STR_LIT>""" <EOL> return tpl . format ( <EOL> article_count = article_count , <EOL> items = items_str <EOL> ) <EOL> class Base64EncodeField ( StringField ) : <EOL> def __base64_encode ( self , text ) : <EOL> return to_text ( base64 . b64encode ( to_binary ( text ) ) ) <EOL> converter = __base64_encode <EOL> class Base64DecodeField ( StringField ) : <EOL> def __base64_decode ( self , text ) : <EOL> return to_text ( base64 . b64decode ( to_binary ( text ) ) ) <EOL> converter = __base64_decode <EOL> class HardwareField ( StringField ) : <EOL> def to_xml ( self , value = None ) : <EOL> value = value or { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:action>' : '<STR_LIT>' } <EOL> tpl = """<STR_LIT>""" <EOL> return tpl . format ( <EOL> name = self . name , <EOL> view = value . get ( '<STR_LIT>' ) , <EOL> action = value . get ( '<STR_LIT:action>' ) <EOL> ) </s>
