1
0
mirror of https://github.com/ihabunek/toot synced 2024-12-22 23:08:17 +01:00

Improve types

This commit is contained in:
Ivan Habunek 2024-01-13 20:00:45 +01:00
parent 965ffa1312
commit 9875209b30
No known key found for this signature in database
GPG Key ID: F5F0623FF5EBCB3D
3 changed files with 15 additions and 19 deletions

View File

@ -17,11 +17,11 @@ def get_config_file_path():
return join(get_config_dir(), TOOT_CONFIG_FILE_NAME)
def user_id(user):
def user_id(user: User):
return "{}@{}".format(user.username, user.instance)
def make_config(path):
def make_config(path: str):
"""Creates an empty toot configuration file."""
config = {
"apps": {},
@ -58,7 +58,7 @@ def save_config(config):
return json.dump(config, f, indent=True, sort_keys=True)
def extract_user_app(config, user_id):
def extract_user_app(config, user_id: str):
if user_id not in config['users']:
return None, None
@ -82,7 +82,7 @@ def get_active_user_app():
return None, None
def get_user_app(user_id):
def get_user_app(user_id: str):
"""Returns (User, App) for given user ID or (None, None) if user is not logged in."""
return extract_user_app(load_config(), user_id)
@ -93,7 +93,7 @@ def load_app(instance: str) -> Optional[App]:
return App(**config['apps'][instance])
def load_user(user_id, throw=False):
def load_user(user_id: str, throw=False):
config = load_config()
if user_id in config['users']:
@ -120,7 +120,7 @@ def save_app(app: App):
config['apps'][app.instance] = app._asdict()
def delete_app(config, app):
def delete_app(config, app: App):
with edit_config() as config:
config['apps'].pop(app.instance, None)

View File

@ -1,26 +1,22 @@
import click
import os
import re
import socket
import subprocess
import tempfile
import unicodedata
import warnings
from bs4 import BeautifulSoup
from typing import Any, Dict, List
import click
from toot.exceptions import ConsoleError
from typing import Any, Dict, Generator, List, Optional
from urllib.parse import urlparse, urlencode, quote, unquote
def str_bool(b):
def str_bool(b: bool) -> str:
"""Convert boolean to string, in the way expected by the API."""
return "true" if b else "false"
def str_bool_nullable(b):
def str_bool_nullable(b: Optional[bool]) -> Optional[str]:
"""Similar to str_bool, but leave None as None"""
return None if b is None else str_bool(b)
@ -34,7 +30,7 @@ def parse_html(html: str) -> BeautifulSoup:
return BeautifulSoup(html.replace("'", "'"), "html.parser")
def get_text(html):
def get_text(html: str) -> str:
"""Converts html to text, strips all tags."""
text = parse_html(html).get_text()
return unicodedata.normalize("NFKC", text)
@ -53,7 +49,7 @@ def html_to_paragraphs(html: str) -> List[List[str]]:
return [[get_text(line) for line in p] for p in paragraphs]
def format_content(content):
def format_content(content: str) -> Generator[str, None, None]:
"""Given a Status contents in HTML, converts it into lines of plain text.
Returns a generator yielding lines of content.
@ -76,9 +72,9 @@ def format_content(content):
EOF_KEY = "Ctrl-Z" if os.name == 'nt' else "Ctrl-D"
def multiline_input():
def multiline_input() -> str:
"""Lets user input multiple lines of text, terminated by EOF."""
lines = []
lines: List[str] = []
while True:
try:
lines.append(input())

View File

@ -4,7 +4,7 @@ import os
from datetime import datetime, timezone
def parse_datetime(value):
def parse_datetime(value: str) -> datetime:
"""Returns an aware datetime in local timezone"""
dttm = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f%z")