This repository has been archived by the owner on Mar 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbases.py
75 lines (56 loc) · 2.25 KB
/
bases.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import utils
import json
import logging
logger = logging.getLogger(__name__)
class BaseTelegramModule(object):
"""Base class which represents module
Attributes:
_telegram_api(:class:`api.TelegramAPI`): Telegram API object which is used in module
_tr(:class:`api.LangAPI`): LangAPI object which is used in module
friendly_name(str): friendly name that will be shown in list
disabled(bool): when this evaluates to ``True``, module is considered disabled
Args:
telegram_api(:class:`api.TelegramAPI`): Telegram API object to use in module
lang(:class:`api.LangAPI`): LangAPI object to use in module
"""
disabled = False
@utils.log(logger, print_ret=False)
def __init__(self, telegram_api, lang):
if self.disabled:
raise InterruptedError('Module is disabled')
self._telegram_api = telegram_api
self._tr = lang
self.friendly_name = None
def help(self, message, args, lang):
"""Answers to `/help` message
Args:
message(:class:`telegram.Message`): received message
args(list): message arguments
lang(str|NoneType): user language (``None`` if cannot be determined)
"""
self._telegram_api.send_text_message(message.chat_id,
self._tr(lang, 'help'),
reply_to=message.message_id)
class JSONAPI(object):
"""This class represents base for APIs that work with JSON files
Attributes:
raw_data: read-only raw data from file
Args:
directory(str): directory to use for finding a file
name(str): name of file without ``.json` extension
Raises:
FileNotFoundError: when specified file cannot be found
"""
@utils.log(logger, print_ret=False)
def __init__(self, directory, name):
with open('./{0}/{1}.json'.format(directory, name), 'r') as f:
self._raw_data = json.load(f)
@utils.log(logger)
def __getitem__(self, item):
return self._raw_data.get(item.lower(), None)
@utils.log(logger)
def __getattr__(self, item):
return self.__getitem__(item)
@property
def raw_data(self):
return self._raw_data