-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72821f8
commit d7c1500
Showing
5 changed files
with
141 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import logging | ||
import dataset | ||
|
||
import pyconfig | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class BeardDBTable(object): | ||
"""Placeholder for database object for beards. | ||
For use with async with. | ||
""" | ||
def __init__(self, beard, table_name, **kwargs): | ||
self.beard_name = type(beard).__name__ | ||
self.table_name = "{}_{}".format( | ||
self.beard_name, | ||
table_name | ||
) | ||
self.kwargs = kwargs | ||
|
||
def __enter__(self): | ||
self.db = dataset.connect(pyconfig.get('db_url')) | ||
self.db.__enter__() | ||
self.table = self.db.get_table(self.table_name, **self.kwargs) | ||
logger.debug("BeardDBTable initalised with: self.table: {}, self.db: {}".format( | ||
self.table, self.db)) | ||
return self | ||
|
||
def __exit__(self, error_type, error_value, traceback): | ||
self.db.__exit__(error_type, error_value, traceback) | ||
|
||
del self.table | ||
del self.db | ||
|
||
def __getattr__(self, name): | ||
"""If the normal getattr fails, try getattr(self.table, name).""" | ||
try: | ||
return getattr(self.table, name) | ||
except AttributeError: | ||
raise AttributeError( | ||
"Open table not found. Are you using BeardDBTable with with?") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import asyncio | ||
import logging | ||
|
||
|
||
class TelegramHandler(logging.Handler): | ||
"""A logging handler that posts directly to telegram""" | ||
|
||
def __init__(self, bot, parse_mode=None): | ||
self.bot = bot | ||
self.parse_mode = parse_mode | ||
super().__init__() | ||
|
||
def emit(self, record): | ||
coro = self.bot.sender.sendMessage( | ||
self.format(record), parse_mode=self.parse_mode) | ||
asyncio.ensure_future(coro) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import logging | ||
import re | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def regex_predicate(pattern): | ||
"""Returns a predicate function which returns True if pattern is matched.""" | ||
def retfunc(chat_handler, msg): | ||
try: | ||
logging.debug("Matching regex: '{}' in '{}'".format( | ||
pattern, msg['text'])) | ||
retmatch = re.match(pattern, msg['text']) | ||
logging.debug("Match: {}".format(retmatch)) | ||
return retmatch | ||
except KeyError: | ||
return False | ||
|
||
return retfunc | ||
|
||
|
||
# TODO make command_predicate in terms of regex_predicate | ||
def command_predicate(cmd): | ||
"""Returns a predicate coroutine which returns True if command is sent.""" | ||
async def retcoro(beard_chat_handler, msg): | ||
bot_username = await beard_chat_handler.get_username() | ||
pattern = r"^/{}(?:@{}|[^@]|$)".format( | ||
cmd, | ||
bot_username, | ||
) | ||
try: | ||
logging.debug("Matching regex: '{}' in '{}'".format( | ||
pattern, msg['text'])) | ||
retmatch = re.match(pattern, msg['text']) | ||
logging.debug("Match: {}".format(retmatch)) | ||
return retmatch | ||
except KeyError: | ||
return False | ||
|
||
return retcoro | ||
|
||
|
||
class Filters: | ||
"""Filters used to call plugin methods when particular types of | ||
messages are received. | ||
For usage, see description of the BeardChatHandler.__commands__ variable. | ||
""" | ||
@classmethod | ||
def text(cls, chat_handler, msg): | ||
"""Filters for text messages""" | ||
return "text" in msg | ||
|
||
@classmethod | ||
def document(cls, chat_handler, msg): | ||
"""Filters for sent documents""" | ||
return "document" in msg | ||
|
||
@classmethod | ||
def location(cls, chat_handler, msg): | ||
"""Filters for sent locations""" | ||
return "location" in msg |