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 .chataction import ChatAction
from .userprofilephotos import UserProfilePhotos
from .replymarkup import ReplyMarkup
from .replykeyboardmarkup import ReplyKeyboardMarkup
from .replykeyboardhide import ReplyKeyboardHide
from .forcereply import ForceReply
from .replymarkup import ReplyMarkup
from .inputfile import InputFile
from .error import TelegramError
from .emoji import Emoji
from .base import TelegramObject
from .bot import Bot
__all__ = ['Bot', 'Emoji', 'TelegramError', 'InputFile', 'ReplyMarkup',
'ForceReply', 'ReplyKeyboardHide', 'ReplyKeyboardMarkup',
'UserProfilePhotos', 'ChatAction', 'Location', 'Contact',
'Video', 'Sticker', 'Document', 'Audio', 'PhotoSize', 'GroupChat',
'Update', 'Message', 'User']
'Update', 'Message', 'User', 'TelegramObject']

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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