Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Commit fa3d543

Browse files
committed
Merge branch 'add-typing' into dev
2 parents a88485a + 97ba72a commit fa3d543

23 files changed

+191
-113
lines changed

.gitlab-ci.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
image: python:3.9
1+
image: python:3.10
22

33
stages:
44
- lint
@@ -17,11 +17,6 @@ lint:
1717
script:
1818
- pytest --flake8 --isort --black -m "flake8 or isort or black" twootfeed
1919

20-
python-3.6:
21-
stage: tests
22-
extends: .python
23-
image: python:3.6
24-
2520
python-3.7:
2621
stage: tests
2722
extends: .python
@@ -33,6 +28,11 @@ python-3.8:
3328
image: python:3.8
3429

3530
python-3.9:
31+
stage: tests
32+
extends: .python
33+
image: python:3.9
34+
35+
python-3.10:
3636
stage: tests
3737
before_script:
3838
- pip install -e .[test]

Makefile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ include Makefile.config
22
-include Makefile.custom.config
33
.SILENT:
44

5+
check: type-check lint test
6+
57
clean:
8+
rm -rf .mypy_cache
9+
rm -rf .pytest_cache
10+
11+
clean-all: clean
612
rm -fr $(VENV)
713
rm -fr *.egg-info
814
rm -fr .eggs
9-
rm -rf .pytest_cache
1015
rm -fr build
1116
rm -rf dist
17+
rm -rf *.log
1218

1319
create-mastodon-cli:
1420
$(PYTHON) $(FLASK_APP)/utils/create_mastodon_client.py
@@ -42,3 +48,7 @@ venv:
4248

4349
test:
4450
$(PYTEST) $(FLASK_APP) --cov $(FLASK_APP) --cov-report term-missing $(PYTEST_ARGS)
51+
52+
type-check:
53+
echo 'Running mypy...'
54+
$(MYPY) $(FLASK_APP)

Makefile.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ FLASK = $(VENV)/bin/flask
1818
PYTEST = $(VENV)/bin/py.test
1919
GUNICORN = $(VENV)/bin/gunicorn
2020
BLACK = $(VENV)/bin/black
21+
MYPY = $(VENV)/bin/mypy
2122

2223
#Sphinx Docs
2324
SPHINXOPTS ?=

mypy.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Global options:
2+
3+
[mypy]
4+
python_version = 3.10
5+
disallow_untyped_defs = True
6+
ignore_missing_imports = True
7+
8+
9+
# Per-module options:

setup.cfg

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ classifiers =
1616
License :: OSI Approved :: MIT License
1717
Operating System :: POSIX :: Linux
1818
Programming Language :: Python :: 3
19-
Programming Language :: Python :: 3.6
2019
Programming Language :: Python :: 3.7
2120
Programming Language :: Python :: 3.8
2221
Programming Language :: Python :: 3.9
22+
Programming Language :: Python :: 3.10
2323
Topic :: Internet :: WWW/HTTP
2424
project_urls =
2525
Documentation = https://samr1.github.io/python-twootfeed
@@ -29,27 +29,33 @@ packages = find:
2929
zip_safe = false
3030
setup_requires = pytest-runner
3131
install_requires =
32-
beautifulsoup4==4.10.0
32+
beautifulsoup4==4.11.1
3333
feedgenerator==2.0.0
34-
Flask==2.0.2
34+
Flask==2.1.3
3535
gunicorn==20.1.0
3636
Mastodon.py==1.5.1
37-
pytz==2021.3
38-
PyYAML==5.4.1
39-
tweepy==4.0.1
37+
pytz==2022.1
38+
PyYAML==6.0.0
39+
tweepy==4.10.0
4040
tests_require =
41+
mypy
4142
pytest-cov
4243
pytest-flake8
4344
pytest-isort
4445
pytest-black
46+
types-pytz
47+
types-PyYAML
4548
include_package_data = True
4649

4750
[options.extras_require]
4851
test =
52+
mypy
4953
pytest-cov
5054
pytest-flake8
5155
pytest-isort
5256
pytest-black
57+
types-pytz
58+
types-PyYAML
5359
doc =
5460
sphinx
5561
sphinx_rtd_theme

twootfeed/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
mastodon_api = get_mastodon_api(param, app_log)
2727

2828

29-
def create_app():
29+
def create_app() -> Flask:
3030
app = Flask(__name__)
3131
app_log.setLevel(logging.DEBUG if app.debug else logging.INFO)
3232

@@ -37,7 +37,7 @@ def create_app():
3737
app.register_blueprint(twitter_bp)
3838

3939
@app.route('/')
40-
def index_page():
40+
def index_page() -> str:
4141
message = (
4242
'The RSS feeds are available on these urls : \r\n'
4343
'for Twitter : http://localhost:5000/_keywords_ or '

twootfeed/__main__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
# source: http://docs.gunicorn.org/en/stable/custom.html
2-
from __future__ import unicode_literals
3-
42
import multiprocessing
3+
from typing import Dict, Optional
54

65
import gunicorn.app.base
6+
from flask import Flask
77
from twootfeed import create_app, param
88

99

1010
class StandaloneApplication(gunicorn.app.base.BaseApplication):
11-
def __init__(self, current_app, options=None):
11+
def __init__(
12+
self, current_app: Flask, options: Optional[Dict] = None
13+
) -> None:
1214
self.options = options or {}
1315
self.application = current_app
1416
super().__init__()
1517

16-
def load_config(self):
18+
def load_config(self) -> None:
1719
config = {
1820
key: value
1921
for key, value in self.options.items()
@@ -22,15 +24,15 @@ def load_config(self):
2224
for key, value in config.items():
2325
self.cfg.set(key.lower(), value)
2426

25-
def load(self):
27+
def load(self) -> Flask:
2628
return self.application
2729

2830

29-
def number_of_workers():
31+
def number_of_workers() -> int:
3032
return (multiprocessing.cpu_count() * 2) + 1
3133

3234

33-
def main():
35+
def main() -> None:
3436
app = create_app()
3537
options = {
3638
'bind': f"{param['app']['host']}:{param['app']['port']}",

twootfeed/mastodon/generate_toots_feed.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from html import unescape
2+
from typing import Dict, List, Optional, Tuple
23

34
import pytz
45
from bs4 import BeautifulSoup
6+
from mastodon import Mastodon
57
from twootfeed.utils.feed_generation import generate_feed
68

79

8-
def format_toot(toot, text_length_limit):
10+
def format_toot(toot: Dict, text_length_limit: int) -> Dict:
911
rss_toot = {
1012
'display_name': toot['account']['display_name'],
1113
'screen_name': toot['account']['username'],
@@ -25,7 +27,7 @@ def format_toot(toot, text_length_limit):
2527
if source:
2628
rss_toot['htmltext'] += '<i>Source: {}</i>'.format(source.get('name'))
2729

28-
medialist = toot.get('media_attachments')
30+
medialist = toot.get('media_attachments', [])
2931
if len(medialist) > 0:
3032
rss_toot['htmltext'] += '<br>'
3133
for media in medialist:
@@ -53,8 +55,12 @@ def format_toot(toot, text_length_limit):
5355

5456

5557
def generate_mastodon_feed(
56-
result, param, feed_title, feed_link, feed_desc=None
57-
):
58+
result: List[Dict],
59+
param: Dict,
60+
feed_title: str,
61+
feed_link: str,
62+
feed_desc: Optional[str] = None,
63+
) -> str:
5864
text_length_limit = int(param['feed'].get('text_length_limit', 100))
5965
f = generate_feed(feed_title, feed_link, param, feed_desc)
6066

@@ -86,7 +92,9 @@ def generate_mastodon_feed(
8692
return xml
8793

8894

89-
def get_next_toots(api, first_toots, max_items):
95+
def get_next_toots(
96+
api: Mastodon, first_toots: List[Dict], max_items: int
97+
) -> List[Dict]:
9098
if len(first_toots) == 0:
9199
return first_toots
92100
result = first_toots
@@ -104,7 +112,12 @@ def get_next_toots(api, first_toots, max_items):
104112
return result
105113

106114

107-
def generate_xml(api, param, query_feed=None, favorites=False):
115+
def generate_xml(
116+
api: Mastodon,
117+
param: Dict,
118+
query_feed: Optional[Dict] = None,
119+
favorites: bool = False,
120+
) -> Tuple[str, int]:
108121
if api:
109122
max_items = param['feed']['max_items']
110123
if query_feed:

twootfeed/mastodon/get_api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import os
2+
from logging import Logger
3+
from typing import Dict
24

35
from mastodon import Mastodon
46
from twootfeed.utils.config import default_directory
57

68

7-
def get_mastodon_api(param, app_log):
9+
def get_mastodon_api(param: Dict, app_log: Logger) -> Mastodon:
810
mastodon_api = None
911

1012
try:

twootfeed/mastodon/routes.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Tuple
2+
13
from flask import Blueprint
24
from twootfeed import mastodon_api, param as mastodon_param
35
from twootfeed.mastodon.generate_toots_feed import generate_xml
@@ -6,26 +8,26 @@
68

79

810
@mastodon_bp.route('/toots/<hashtag>', methods=['GET'])
9-
def tootfeed_hashtag(hashtag):
11+
def tootfeed_hashtag(hashtag: str) -> Tuple[str, int]:
1012
"""generate a rss feed from parsed mastodon search"""
1113
return generate_xml(mastodon_api, mastodon_param, {'hashtag': hashtag})
1214

1315

1416
@mastodon_bp.route('/toots/search/<query_feed>', methods=['GET'])
1517
@mastodon_bp.route('/toot_search/<query_feed>', methods=['GET'])
16-
def tootfeed(query_feed):
18+
def tootfeed(query_feed: str) -> Tuple[str, int]:
1719
"""generate a rss feed from parsed mastodon search"""
1820
return generate_xml(mastodon_api, mastodon_param, {'query': query_feed})
1921

2022

2123
@mastodon_bp.route('/toots/favorites', methods=['GET'])
2224
@mastodon_bp.route('/toot_favorites', methods=['GET'])
23-
def toot_favorites_feed():
25+
def toot_favorites_feed() -> Tuple[str, int]:
2426
"""generate an rss feed authenticated user's favorites"""
2527
return generate_xml(mastodon_api, mastodon_param, favorites=True)
2628

2729

2830
@mastodon_bp.route('/toots/bookmarks', methods=['GET'])
29-
def toot_bookmarks_feed():
31+
def toot_bookmarks_feed() -> Tuple[str, int]:
3032
"""generate an rss feed authenticated user's bookmarks"""
3133
return generate_xml(mastodon_api, mastodon_param)

0 commit comments

Comments
 (0)