Add Black and Flake8

This commit is contained in:
Alex RootJunior 2019-06-29 19:53:18 +03:00
parent 5b9d82f1ca
commit 7c51b1a7d7
124 changed files with 5841 additions and 3772 deletions

View file

@ -7,7 +7,7 @@ from aiogram.dispatcher.handler import CancelHandler, current_handler
from aiogram.dispatcher.middlewares import BaseMiddleware
from aiogram.utils.exceptions import Throttled
TOKEN = 'BOT TOKEN HERE'
TOKEN = "BOT TOKEN HERE"
loop = asyncio.get_event_loop()
@ -28,9 +28,9 @@ def rate_limit(limit: int, key=None):
"""
def decorator(func):
setattr(func, 'throttling_rate_limit', limit)
setattr(func, "throttling_rate_limit", limit)
if key:
setattr(func, 'throttling_key', key)
setattr(func, "throttling_key", key)
return func
return decorator
@ -41,7 +41,7 @@ class ThrottlingMiddleware(BaseMiddleware):
Simple middleware
"""
def __init__(self, limit=DEFAULT_RATE_LIMIT, key_prefix='antiflood_'):
def __init__(self, limit=DEFAULT_RATE_LIMIT, key_prefix="antiflood_"):
self.rate_limit = limit
self.prefix = key_prefix
super(ThrottlingMiddleware, self).__init__()
@ -59,8 +59,8 @@ class ThrottlingMiddleware(BaseMiddleware):
dispatcher = Dispatcher.get_current()
# If handler was configured, get rate limit and key from handler
if handler:
limit = getattr(handler, 'throttling_rate_limit', self.rate_limit)
key = getattr(handler, 'throttling_key', f"{self.prefix}_{handler.__name__}")
limit = getattr(handler, "throttling_rate_limit", self.rate_limit)
key = getattr(handler, "throttling_key", f"{self.prefix}_{handler.__name__}")
else:
limit = self.rate_limit
key = f"{self.prefix}_message"
@ -85,7 +85,7 @@ class ThrottlingMiddleware(BaseMiddleware):
handler = current_handler.get()
dispatcher = Dispatcher.get_current()
if handler:
key = getattr(handler, 'throttling_key', f"{self.prefix}_{handler.__name__}")
key = getattr(handler, "throttling_key", f"{self.prefix}_{handler.__name__}")
else:
key = f"{self.prefix}_message"
@ -94,7 +94,7 @@ class ThrottlingMiddleware(BaseMiddleware):
# Prevent flooding
if throttled.exceeded_count <= 2:
await message.reply('Too many requests! ')
await message.reply("Too many requests! ")
# Sleep.
await asyncio.sleep(delta)
@ -104,17 +104,19 @@ class ThrottlingMiddleware(BaseMiddleware):
# If current message is not last with current key - do not send message
if thr.exceeded_count == throttled.exceeded_count:
await message.reply('Unlocked.')
await message.reply("Unlocked.")
@dp.message_handler(commands=['start'])
@rate_limit(5, 'start') # this is not required but you can configure throttling manager for current handler using it
@dp.message_handler(commands=["start"])
@rate_limit(
5, "start"
) # this is not required but you can configure throttling manager for current handler using it
async def cmd_test(message: types.Message):
# You can use this command every 5 seconds
await message.reply('Test passed! You can use this command every 5 seconds.')
await message.reply("Test passed! You can use this command every 5 seconds.")
if __name__ == '__main__':
if __name__ == "__main__":
# Setup middleware
dp.middleware.setup(ThrottlingMiddleware())