[misc] Add flake8 to tox and fix existing issues

This commit is contained in:
Thomas Lindner 2020-07-17 19:59:25 +02:00
parent 761a4f9e0a
commit fc324f102d
9 changed files with 23 additions and 22 deletions

View file

@ -23,7 +23,7 @@ def send_email(to, subject, sender='kibicara', body=''):
Example:
```
from kibicara import email
email.send_email('abc@de.fg', 'My Email subject', body='Hi this is a mail body.')
email.send_email('abc@de.fg', 'Email subject', body='Hi this is a mail body.')
```
Args:

View file

@ -20,7 +20,7 @@ class Message:
Examples:
```
message = Message('Message sent by a user from platform xyz', xyz_message_id=123)
message = Message('Message sent from platform xyz', xyz_message_id=123)
```
Args:
@ -183,7 +183,8 @@ class Spawner:
@classmethod
async def init_all(cls):
""" Instantiate and start a bot for every row in the corresponding ORM model. """
""" Instantiate and start a bot for every row in the corresponding ORM model.
"""
for spawner in cls.__instances:
await spawner._init()

View file

@ -4,13 +4,11 @@
#
# SPDX-License-Identifier: 0BSD
from argparse import ArgumentParser
from asyncio import run as asyncio_run
from email.parser import BytesParser
from email.policy import default
from fastapi import status
from kibicara.config import args, config
from kibicara.model import Hood
from kibicara.platforms.email.model import Email
from logging import getLogger
from ormantic import NoMatch

View file

@ -55,7 +55,7 @@ async def get_email(email_id: int, hood=Depends(get_hood)):
async def get_subscriber(subscriber_id: int, hood=Depends(get_hood)):
try:
return await EmailSubscriber.objects.get(id=subscriber_id, hood=hood)
return await EmailSubscribers.objects.get(id=subscriber_id, hood=hood)
except NoMatch:
return HTTPException(status_code=status.HTTP_404_NOT_FOUND)
@ -203,11 +203,8 @@ async def email_message_create(
:param hood: Hood the Email bot belongs to.
:return: returns status code 201 if the message is accepted by the censor.
"""
for email in await Email.objects.filter(hood=hood).all():
if message.secret == email.secret:
# check API secret
logger.warning(str(message))
logger.warning(str(email))
for receiver in await Email.objects.filter(hood=hood).all():
if message.secret == receiver.secret:
# pass message.text to bot.py
if await spawner.get(hood).publish(Message(message.text)):
logger.warning("Message was accepted: " + message.text)

View file

@ -4,7 +4,6 @@
from aiogram import Bot, Dispatcher, exceptions, types
from asyncio import gather, sleep, CancelledError
from kibicara.config import config
from kibicara.platformapi import Censor, Message, Spawner
from kibicara.platforms.telegram.model import Telegram, TelegramUser
from logging import getLogger
@ -22,7 +21,7 @@ class TelegramBot(Censor):
try:
self.bot = Bot(token=telegram_model.api_token)
self.dp = self._create_dispatcher()
except exceptions.ValidationError as e:
except exceptions.ValidationError:
self.telegram_model.enabled = False
finally:
self.enabled = self.telegram_model.enabled
@ -80,7 +79,7 @@ class TelegramBot(Censor):
% (user_id, self.telegram_model.hood.name, e.timeout)
)
await sleep(e.timeout)
return await self._send_message(user_id, text)
return await self._send_message(user_id, message)
except exceptions.UserDeactivated:
logger.error(
'Target [ID:%s] (%s): user is deactivated'

View file

@ -5,12 +5,11 @@
from aiogram.bot.api import check_token
from aiogram import exceptions
from fastapi import APIRouter, Depends, HTTPException, Response, status
from kibicara.config import config
from kibicara.platforms.telegram.bot import spawner
from kibicara.platforms.telegram.model import Telegram
from kibicara.webapi.hoods import get_hood
from logging import getLogger
from sqlite3 import IntegrityError, OperationalError
from sqlite3 import IntegrityError
from ormantic.exceptions import NoMatch
from pydantic import BaseModel, validator

View file

@ -31,10 +31,10 @@ class TwitterBot(Censor):
if self.twitter_model.successful_verified:
if self.twitter_model.mentions_since_id is None:
logger.debug('since_id is None in model, fetch newest mention id')
mentions = await self._poll_mentions()
await self._poll_mentions()
if self.twitter_model.dms_since_id is None:
logger.debug('since_id is None in model, fetch newest dm id')
dms = await self._poll_direct_messages()
await self._poll_direct_messages()
logger.debug('Starting Twitter bot: %s' % self.twitter_model.__dict__)
await gather(self.poll(), self.push())
else:

View file

@ -18,7 +18,8 @@ def test_email_subscribe_unsubscribe(client, hood_id, receive_email):
mail = receive_email()
body = mail['body']
confirm_url = findall(
r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',
r'http[s]?://'
r'(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',
body,
)[0]
response = client.post(urlparse(confirm_url).path)
@ -55,7 +56,6 @@ Message-ID: <B5F50812.F55DFD8B@example.com>
Date: Tue, 16 Jun 2020 06:53:19 -0700
Reply-To: "Test" <test@example.com>
From: "Test" <test@example.com>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1.17) Gecko/20080914 Thunderbird/2.0.0.17
MIME-Version: 1.0
To: <hood@localhost>
Subject: Chat: test

View file

@ -1,9 +1,14 @@
[tox]
envlist = py38, black, pytest
envlist = py38, flake8, black, pytest
[testenv]
deps = .
[testenv:flake8]
deps = flake8
commands =
flake8 kibicara tests
[testenv:black]
deps = black
commands =
@ -14,3 +19,5 @@ deps = pytest
commands =
pytest
[flake8]
max_line_length = 88