Merge remote-tracking branch 'origin/dev-1.x' into dev-1.x

This commit is contained in:
Alex Root Junior 2018-01-23 23:39:59 +02:00
commit f7ef84acbc
19 changed files with 98 additions and 40 deletions

View file

@ -4,7 +4,8 @@ try:
from .bot import Bot
except ImportError as e:
if e.name == 'aiohttp':
warnings.warn('Dependencies is not installed!', category=ImportWarning)
warnings.warn('Dependencies are not installed!',
category=ImportWarning)
else:
raise

View file

@ -132,6 +132,32 @@ class RegexpFilter(Filter):
return bool(self.regexp.search(message.text))
class RegexpCommandsFilter(AsyncFilter):
"""
Check commands by regexp in message
"""
def __init__(self, regexp_commands):
self.regexp_commands = [re.compile(command, flags=re.IGNORECASE | re.MULTILINE) for command in regexp_commands]
async def check(self, message):
if not message.is_command():
return False
command = message.text.split()[0][1:]
command, _, mention = command.partition('@')
if mention and mention != (await message.bot.me).username:
return False
for command in self.regexp_commands:
search = command.search(message.text)
if search:
message.conf['regexp_command'] = search
return True
return False
class ContentTypeFilter(Filter):
"""
Check message content type

View file

@ -1,5 +1,5 @@
"""
Need setup task factory:
You need to setup task factory:
>>> from aiogram.utils import context
>>> loop = asyncio.get_event_loop()
>>> loop.set_task_factory(context.task_factory)

View file

@ -51,7 +51,8 @@ def start_pooling(*args, **kwargs):
return start_polling(*args, **kwargs)
def start_polling(dispatcher, *, loop=None, skip_updates=False, on_startup=None, on_shutdown=None):
def start_polling(dispatcher, *, loop=None, skip_updates=False,
on_startup=None, on_shutdown=None):
log.warning('Start bot with long-polling.')
if loop is None:
loop = dispatcher.loop
@ -59,7 +60,7 @@ def start_polling(dispatcher, *, loop=None, skip_updates=False, on_startup=None,
loop.set_task_factory(context.task_factory)
try:
loop.run_until_complete(_startup(dispatcher, skip_updates=skip_updates, callback=on_startup))
loop.run_until_complete(_startup(dispatcher, skip_updates, on_startup))
loop.create_task(dispatcher.start_polling(reset_webhook=True))
loop.run_forever()
except (KeyboardInterrupt, SystemExit):
@ -69,8 +70,8 @@ def start_polling(dispatcher, *, loop=None, skip_updates=False, on_startup=None,
log.warning("Goodbye!")
def start_webhook(dispatcher, webhook_path, *, loop=None, skip_updates=None, on_startup=None, on_shutdown=None,
check_ip=False, **kwargs):
def start_webhook(dispatcher, webhook_path, *, loop=None, skip_updates=None,
on_startup=None, on_shutdown=None, check_ip=False, **kwargs):
log.warning('Start bot with webhook.')
if loop is None:
loop = dispatcher.loop

View file

@ -137,7 +137,8 @@ class Item:
"""
Helper item
If value is not configured it will be generated automatically based on variable name
If a value is not provided,
it will be automatically generated based on a variable's name
"""
def __init__(self, value=None):
@ -156,7 +157,7 @@ class Item:
class ListItem(Item):
"""
This item always is list
This item is always a list
You can use &, | and + operators for that.
"""
@ -179,7 +180,7 @@ class ItemsList(list):
"""
Patch for default list
This class provide +, &, |, +=, &=, |= operators for extending the list
This class provides +, &, |, +=, &=, |= operators for extending the list
"""
def __init__(self, *seq):

View file

@ -18,6 +18,8 @@ HTML_QUOTES_MAP = {
'"': '"'
}
_HQS = HTML_QUOTES_MAP.keys() # HQS for HTML QUOTES SYMBOLS
def _join(*content, sep=' '):
return sep.join(map(str, content))
@ -38,21 +40,22 @@ def quote_html(content):
"""
Quote HTML symbols
All <, > and & symbols that are not a part of a tag or an HTML entity
must be replaced with the corresponding HTML entities (< with &lt;, > with &gt; and & with &amp;).
All <, >, & and " symbols that are not a part of a tag or
an HTML entity must be replaced with the corresponding HTML entities
(< with &lt; > with &gt; & with &amp and " with &quot).
:param content: str
:return: str
"""
new_content = ''
for symbol in content:
new_content += HTML_QUOTES_MAP[symbol] if symbol in '<>&"' else symbol
new_content += HTML_QUOTES_MAP[symbol] if symbol in _HQS else symbol
return new_content
def text(*content, sep=' '):
"""
Join all elements with separator
Join all elements with a separator
:param content:
:param sep:
@ -168,7 +171,7 @@ def hlink(title, url):
:param url:
:return:
"""
return "<a href=\"{0}\">{1}</a>".format(url, quote_html(title))
return '<a href="{0}">{1}</a>'.format(url, quote_html(title))
def escape_md(*content, sep=' '):

View file

@ -9,7 +9,8 @@ from .helper import Helper, HelperMode, Item
class Version:
def __init__(self, major=0, minor=0, maintenance=0, stage='final', build=0):
def __init__(self, major=0, minor=0,
maintenance=0, stage='final', build=0):
self.__raw_version = None
self.__version = None
@ -86,7 +87,8 @@ class Version:
if git_changeset:
sub = '.dev{0}'.format(git_changeset)
elif version[3] != Stage.FINAL:
mapping = {Stage.ALPHA: 'a', Stage.BETA: 'b', Stage.RC: 'rc', Stage.DEV: 'dev'}
mapping = {Stage.ALPHA: 'a', Stage.BETA: 'b',
Stage.RC: 'rc', Stage.DEV: 'dev'}
sub = mapping[version[3]] + str(version[4])
return str(main + sub)