Resolves #45 creating to_data abstractmethod on TelegramObject (new base class)

This commit is contained in:
leandrotoledo 2015-07-19 23:06:04 -03:00
parent ded002a0c8
commit 859f04e566
18 changed files with 150 additions and 161 deletions

View file

@ -18,17 +18,18 @@ from .contact import Contact
from .location import Location from .location import Location
from .chataction import ChatAction from .chataction import ChatAction
from .userprofilephotos import UserProfilePhotos from .userprofilephotos import UserProfilePhotos
from .replymarkup import ReplyMarkup
from .replykeyboardmarkup import ReplyKeyboardMarkup from .replykeyboardmarkup import ReplyKeyboardMarkup
from .replykeyboardhide import ReplyKeyboardHide from .replykeyboardhide import ReplyKeyboardHide
from .forcereply import ForceReply from .forcereply import ForceReply
from .replymarkup import ReplyMarkup
from .inputfile import InputFile from .inputfile import InputFile
from .error import TelegramError from .error import TelegramError
from .emoji import Emoji from .emoji import Emoji
from .base import TelegramObject
from .bot import Bot from .bot import Bot
__all__ = ['Bot', 'Emoji', 'TelegramError', 'InputFile', 'ReplyMarkup', __all__ = ['Bot', 'Emoji', 'TelegramError', 'InputFile', 'ReplyMarkup',
'ForceReply', 'ReplyKeyboardHide', 'ReplyKeyboardMarkup', 'ForceReply', 'ReplyKeyboardHide', 'ReplyKeyboardMarkup',
'UserProfilePhotos', 'ChatAction', 'Location', 'Contact', 'UserProfilePhotos', 'ChatAction', 'Location', 'Contact',
'Video', 'Sticker', 'Document', 'Audio', 'PhotoSize', 'GroupChat', 'Video', 'Sticker', 'Document', 'Audio', 'PhotoSize', 'GroupChat',
'Update', 'Message', 'User'] 'Update', 'Message', 'User', 'TelegramObject']

View file

@ -1,11 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
import json from .base import TelegramObject
from .telegram_boject_base import Base
class Audio(Base): class Audio(TelegramObject):
def __init__(self, def __init__(self,
file_id, file_id,
duration, duration,
@ -23,11 +22,11 @@ class Audio(Base):
mime_type=data.get('mime_type', None), mime_type=data.get('mime_type', None),
file_size=data.get('file_size', None)) file_size=data.get('file_size', None))
def to_json(self): def to_data(self):
json_data = {'file_id': self.file_id, data = {'file_id': self.file_id,
'duration': self.duration} 'duration': self.duration}
if self.mime_type: if self.mime_type:
json_data['mime_type'] = self.mime_type data['mime_type'] = self.mime_type
if self.file_size: if self.file_size:
json_data['file_size'] = self.file_size data['file_size'] = self.file_size
return json.dumps(json_data) return data

View file

@ -1,26 +1,28 @@
#!/usr/bin/env python
import json
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
from .error import TelegramError
class Base(object): class TelegramObject(object):
"""Base class for most telegram object""" """Base class for most telegram object"""
__metaclass__ = ABCMeta __metaclass__ = ABCMeta
def __str__(self): def __str__(self):
return self.to_json() return self.to_data()
def __getitem__(self, item): def __getitem__(self, item):
try: return self.__dict__[item]
return self.__dict__[item]
except KeyError as e:
raise TelegramError(str(e))
@staticmethod @staticmethod
def de_json(data): def de_json(data):
pass raise NotImplementedError
def to_json(self):
return json.dumps(self.to_data())
@abstractmethod @abstractmethod
def to_json(self): def to_data(self):
pass return

View file

@ -1,11 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
import json from .base import TelegramObject
from .telegram_boject_base import Base
class Contact(Base): class Contact(TelegramObject):
def __init__(self, def __init__(self,
phone_number, phone_number,
first_name, first_name,
@ -23,11 +22,11 @@ class Contact(Base):
last_name=data.get('last_name', None), last_name=data.get('last_name', None),
user_id=data.get('user_id', None)) user_id=data.get('user_id', None))
def to_json(self): def to_data(self):
json_data = {'phone_number': self.phone_number, data = {'phone_number': self.phone_number,
'first_name': self.first_name} 'first_name': self.first_name}
if self.last_name: if self.last_name:
json_data['last_name'] = self.last_name data['last_name'] = self.last_name
if self.user_id: if self.user_id:
json_data['user_id'] = self.user_id data['user_id'] = self.user_id
return json.dumps(json_data) return data

View file

@ -1,11 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
import json from .base import TelegramObject
from .telegram_boject_base import Base
class Document(Base): class Document(TelegramObject):
def __init__(self, def __init__(self,
file_id, file_id,
thumb, thumb,
@ -32,13 +31,13 @@ class Document(Base):
mime_type=data.get('mime_type', None), mime_type=data.get('mime_type', None),
file_size=data.get('file_size', None)) file_size=data.get('file_size', None))
def to_json(self): def to_data(self):
json_data = {'file_id': self.file_id, data = {'file_id': self.file_id,
'thumb': self.thumb.to_json()} 'thumb': self.thumb.to_data()}
if self.file_name: if self.file_name:
json_data['file_name'] = self.file_name data['file_name'] = self.file_name
if self.mime_type: if self.mime_type:
json_data['mime_type'] = self.mime_type data['mime_type'] = self.mime_type
if self.file_size: if self.file_size:
json_data['file_size'] = self.file_size data['file_size'] = self.file_size
return json.dumps(json_data) return data

View file

@ -1,11 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
import json from .base import TelegramObject
from .replymarkup import ReplyMarkup
class ForceReply(ReplyMarkup): class ForceReply(TelegramObject):
def __init__(self, def __init__(self,
force_reply=True, force_reply=True,
selective=None): selective=None):
@ -17,8 +16,8 @@ class ForceReply(ReplyMarkup):
return ForceReply(force_reply=data.get('force_reply', None), return ForceReply(force_reply=data.get('force_reply', None),
selective=data.get('selective', None)) selective=data.get('selective', None))
def to_json(self): def to_data(self):
json_data = {'force_reply': self.force_reply} data = {'force_reply': self.force_reply}
if self.selective: if self.selective:
json_data['selective'] = self.selective data['selective'] = self.selective
return json.dumps(json_data) return data

View file

@ -1,11 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
import json from .base import TelegramObject
from .telegram_boject_base import Base
class GroupChat(Base): class GroupChat(TelegramObject):
def __init__(self, def __init__(self,
id, id,
title): title):
@ -17,7 +16,7 @@ class GroupChat(Base):
return GroupChat(id=data.get('id', None), return GroupChat(id=data.get('id', None),
title=data.get('title', None)) title=data.get('title', None))
def to_json(self): def to_data(self):
json_data = {'id': self.id, data = {'id': self.id,
'title': self.title} 'title': self.title}
return json.dumps(json_data) return data

View file

@ -1,11 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
import json from .base import TelegramObject
from .telegram_boject_base import Base
class Location(Base): class Location(TelegramObject):
def __init__(self, def __init__(self,
longitude, longitude,
latitude): latitude):
@ -17,7 +16,7 @@ class Location(Base):
return Location(longitude=data.get('longitude', None), return Location(longitude=data.get('longitude', None),
latitude=data.get('latitude', None)) latitude=data.get('latitude', None))
def to_json(self): def to_data(self):
json_data = {'longitude': self.longitude, data = {'longitude': self.longitude,
'latitude': self.latitude} 'latitude': self.latitude}
return json.dumps(json_data) return data

View file

@ -1,11 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
import json from .base import TelegramObject
from .telegram_boject_base import Base
class Message(Base): class Message(TelegramObject):
def __init__(self, def __init__(self,
message_id, message_id,
from_user, from_user,
@ -164,43 +163,43 @@ class Message(Base):
delete_chat_photo=data.get('delete_chat_photo', None), delete_chat_photo=data.get('delete_chat_photo', None),
group_chat_created=data.get('group_chat_created', None)) group_chat_created=data.get('group_chat_created', None))
def to_json(self): def to_data(self):
json_data = {'message_id': self.message_id, data = {'message_id': self.message_id,
'from': self.from_user.to_json(), 'from': self.from_user.to_data(),
'date': self.date, 'date': self.date,
'chat': self.chat.to_json()} 'chat': self.chat.to_data()}
if self.forward_from: if self.forward_from:
json_data['forward_from'] = self.forward_from data['forward_from'] = self.forward_from
if self.forward_date: if self.forward_date:
json_data['forward_date'] = self.forward_date data['forward_date'] = self.forward_date
if self.reply_to_message: if self.reply_to_message:
json_data['reply_to_message'] = self.reply_to_message data['reply_to_message'] = self.reply_to_message
if self.text: if self.text:
json_data['text'] = self.text data['text'] = self.text
if self.audio: if self.audio:
json_data['audio'] = self.audio.to_json() data['audio'] = self.audio.to_data()
if self.document: if self.document:
json_data['document'] = self.document.to_json() data['document'] = self.document.to_data()
if self.photo: if self.photo:
json_data['photo'] = self.photo.to_json() data['photo'] = self.photo.to_data()
if self.sticker: if self.sticker:
json_data['sticker'] = self.sticker.to_json() data['sticker'] = self.sticker.to_data()
if self.video: if self.video:
json_data['video'] = self.video.to_json() data['video'] = self.video.to_data()
if self.contact: if self.contact:
json_data['contact'] = self.contact.to_json() data['contact'] = self.contact.to_data()
if self.location: if self.location:
json_data['location'] = self.location.to_json() data['location'] = self.location.to_data()
if self.new_chat_participant: if self.new_chat_participant:
json_data['new_chat_participant'] = self.new_chat_participant data['new_chat_participant'] = self.new_chat_participant
if self.left_chat_participant: if self.left_chat_participant:
json_data['left_chat_participant'] = self.left_chat_participant data['left_chat_participant'] = self.left_chat_participant
if self.new_chat_title: if self.new_chat_title:
json_data['new_chat_title'] = self.new_chat_title data['new_chat_title'] = self.new_chat_title
if self.new_chat_photo: if self.new_chat_photo:
json_data['new_chat_photo'] = self.new_chat_photo data['new_chat_photo'] = self.new_chat_photo
if self.delete_chat_photo: if self.delete_chat_photo:
json_data['delete_chat_photo'] = self.delete_chat_photo data['delete_chat_photo'] = self.delete_chat_photo
if self.group_chat_created: if self.group_chat_created:
json_data['group_chat_created'] = self.group_chat_created data['group_chat_created'] = self.group_chat_created
return json.dumps(json_data) return data

View file

@ -1,11 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
import json from .base import TelegramObject
from .telegram_boject_base import Base
class PhotoSize(Base): class PhotoSize(TelegramObject):
def __init__(self, def __init__(self,
file_id, file_id,
width, width,
@ -23,10 +22,10 @@ class PhotoSize(Base):
height=data.get('height', None), height=data.get('height', None),
file_size=data.get('file_size', None)) file_size=data.get('file_size', None))
def to_json(self): def to_data(self):
json_data = {'file_id': self.file_id, data = {'file_id': self.file_id,
'width': self.width, 'width': self.width,
'height': self.height} 'height': self.height}
if self.file_size: if self.file_size:
json_data['file_size'] = self.file_size data['file_size'] = self.file_size
return json.dumps(json_data) return data

View file

@ -1,7 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
import json
from .replymarkup import ReplyMarkup from .replymarkup import ReplyMarkup
@ -17,8 +16,8 @@ class ReplyKeyboardHide(ReplyMarkup):
return ReplyKeyboardHide(hide_keyboard=data.get('hide_keyboard', None), return ReplyKeyboardHide(hide_keyboard=data.get('hide_keyboard', None),
selective=data.get('selective', None)) selective=data.get('selective', None))
def to_json(self): def to_data(self):
json_data = {'hide_keyboard': self.hide_keyboard} data = {'hide_keyboard': self.hide_keyboard}
if self.selective: if self.selective:
json_data['selective'] = self.selective data['selective'] = self.selective
return json.dumps(json_data) return data

View file

@ -1,7 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
import json
from .replymarkup import ReplyMarkup from .replymarkup import ReplyMarkup
@ -27,12 +26,12 @@ class ReplyKeyboardMarkup(ReplyMarkup):
), ),
selective=data.get('selective', None)) selective=data.get('selective', None))
def to_json(self): def to_data(self):
json_data = {'keyboard': self.keyboard} data = {'keyboard': self.keyboard}
if self.resize_keyboard: if self.resize_keyboard:
json_data['resize_keyboard'] = self.resize_keyboard data['resize_keyboard'] = self.resize_keyboard
if self.one_time_keyboard: if self.one_time_keyboard:
json_data['one_time_keyboard'] = self.one_time_keyboard data['one_time_keyboard'] = self.one_time_keyboard
if self.selective: if self.selective:
json_data['selective'] = self.selective data['selective'] = self.selective
return json.dumps(json_data) return data

View file

@ -1,7 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
from .telegram_boject_base import Base
class ReplyMarkup(Base): from .base import TelegramObject
def to_json(self):
raise NotImplementedError
class ReplyMarkup(TelegramObject):
pass

View file

@ -1,11 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
import json from .base import TelegramObject
from .telegram_boject_base import Base
class Sticker(Base): class Sticker(TelegramObject):
def __init__(self, def __init__(self,
file_id, file_id,
width, width,
@ -32,11 +31,11 @@ class Sticker(Base):
thumb=thumb, thumb=thumb,
file_size=data.get('file_size', None)) file_size=data.get('file_size', None))
def to_json(self): def to_data(self):
json_data = {'file_id': self.file_id, data = {'file_id': self.file_id,
'width': self.width, 'width': self.width,
'height': self.height, 'height': self.height,
'thumb': self.thumb.to_json()} 'thumb': self.thumb.to_data()}
if self.file_size: if self.file_size:
json_data['file_size'] = self.file_size data['file_size'] = self.file_size
return json.dumps(json_data) return data

View file

@ -1,11 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
import json from .base import TelegramObject
from .telegram_boject_base import Base
class Update(Base): class Update(TelegramObject):
def __init__(self, def __init__(self,
update_id, update_id,
message=None): message=None):
@ -23,8 +22,8 @@ class Update(Base):
return Update(update_id=data.get('update_id', None), return Update(update_id=data.get('update_id', None),
message=message) message=message)
def to_json(self): def to_data(self):
json_data = {'update_id': self.update_id} data = {'update_id': self.update_id}
if self.message: if self.message:
json_data['message'] = self.message.to_json() data['message'] = self.message.to_data()
return json.dumps(json_data) return data

View file

@ -1,11 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
import json from .base import TelegramObject
from .telegram_boject_base import Base
class User(Base): class User(TelegramObject):
def __init__(self, def __init__(self,
id, id,
first_name, first_name,
@ -31,11 +30,11 @@ class User(Base):
last_name=data.get('last_name', None), last_name=data.get('last_name', None),
username=data.get('username', None)) username=data.get('username', None))
def to_json(self): def to_data(self):
json_data = {'id': self.id, data = {'id': self.id,
'first_name': self.first_name} 'first_name': self.first_name}
if self.last_name: if self.last_name:
json_data['last_name'] = self.last_name data['last_name'] = self.last_name
if self.username: if self.username:
json_data['username'] = self.username data['username'] = self.username
return json.dumps(json_data) return data

View file

@ -1,11 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
import json from .base import TelegramObject
from .telegram_boject_base import Base
class UserProfilePhotos(Base): class UserProfilePhotos(TelegramObject):
def __init__(self, def __init__(self,
total_count, total_count,
photos): photos):
@ -25,12 +24,12 @@ class UserProfilePhotos(Base):
return UserProfilePhotos(total_count=data.get('total_count', None), return UserProfilePhotos(total_count=data.get('total_count', None),
photos=photos) photos=photos)
def to_json(self): def to_data(self):
json_data = {} data = {}
if self.total_count: if self.total_count:
json_data['total_count'] = self.total_count data['total_count'] = self.total_count
if self.photos: if self.photos:
json_data['photos'] = [] data['photos'] = []
for photo in self.photos: for photo in self.photos:
json_data['photos'].append([x.to_json() for x in photo]) data['photos'].append([x.to_data() for x in photo])
return json.dumps(json_data) return data

View file

@ -1,11 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
import json from .base import TelegramObject
from .telegram_boject_base import Base
class Video(Base): class Video(TelegramObject):
def __init__(self, def __init__(self,
file_id, file_id,
width, width,
@ -41,16 +40,16 @@ class Video(Base):
file_size=data.get('file_size', None), file_size=data.get('file_size', None),
caption=data.get('caption', None)) caption=data.get('caption', None))
def to_json(self): def to_data(self):
json_data = {'file_id': self.file_id, data = {'file_id': self.file_id,
'width': self.width, 'width': self.width,
'height': self.height, 'height': self.height,
'duration': self.duration, 'duration': self.duration,
'thumb': self.thumb.to_json()} 'thumb': self.thumb.to_data()}
if self.mime_type: if self.mime_type:
json_data['mime_type'] = self.mime_type data['mime_type'] = self.mime_type
if self.file_size: if self.file_size:
json_data['file_size'] = self.file_size data['file_size'] = self.file_size
if self.caption: if self.caption:
json_data['caption'] = self.caption data['caption'] = self.caption
return json.dumps(json_data) return data