~fkfd/utab

f1658d8a6fa0cf511e625652e7c7a66b4775ef1e — Frederick Yin 2 years ago cd39b33
Type hinting taken to extemes
1 files changed, 16 insertions(+), 16 deletions(-)

M utab/__main__.py
M utab/__main__.py => utab/__main__.py +16 -16
@@ 20,10 20,10 @@ css_dir = Path(data_dir) / "css"
icons_dir = Path(data_dir) / "icons"
try:
    with open(template_fp) as f:
        template = f.read()
        template: str = f.read()
        f.close()
    with open(site_form_fp) as f:
        site_form = f.read()
        site_form: str = f.read()
        f.close()
except FileNotFoundError:
    print("One or more template files are not found.")


@@ 33,26 33,26 @@ except FileNotFoundError:
sites_fp = Path(data_dir) / "sites.csv"
config_fp = Path(data_dir) / "config.yml"
with open(config_fp) as f:
    config = yaml.load(f.read(), Loader=yaml.FullLoader)
    config: dict = yaml.load(f.read(), Loader=yaml.FullLoader)
    f.close()

sites_grid_dimensions = {"columns": config["columns"], "rows": config["rows"]}


def read_sites():
def read_sites() -> list:
    with open(sites_fp) as f:
        sites = list(csv.reader(f))
        f.close()
        return sites


def write_sites(data):
def write_sites(data: list):
    with open(sites_fp, "w") as f:
        csv.writer(f).writerows(data)
        f.close()


def append_site(site):
def append_site(site: list):
    with open(sites_fp, "a") as f:
        csv.writer(f).writerow(site)
        f.close()


@@ 70,7 70,7 @@ def index():


@app.route("/go/<path:url>")
def visit_site(url):
def visit_site(url: str):
    # log this visit, then redirect to the unescaped url
    sites = read_sites()
    for i, s in enumerate(sites):


@@ 97,13 97,13 @@ def add_site():
                return "A site with the same URL already exists.", 403
        # now we have ensured there isn't such URL in sites
        if favicon:
            favicon_src = favicon
            favicon_src: str = favicon
        else:
            # get its favicon url
            # fetch favicon url from the root page (w/o path):
            url_split = urllib.parse.urlsplit(url)
            root = url_split.scheme + "://" + url_split.netloc
            favicon_src = get_favicon_url(root)
            favicon_src: str = get_favicon_url(root)

        append_site([url, title, favicon_src, 0])
        return redirect("/", 302)


@@ 122,7 122,7 @@ def add_site():


@app.route("/edit/<path:url>")
def edit_site(url):
def edit_site(url: str):
    # /edit/ => /edit
    # /edit/<escaped_url> => form with original data pre-filled for user to modify
    # /edit/<escaped_url>?url=<escaped_new_url>&... => edit this site in database


@@ 168,7 168,7 @@ def select_site_to_edit():


@app.route("/delete/<path:url>")
def delete_site(url):
def delete_site(url: str):
    sites = read_sites()
    for i, s in enumerate(sites):
        if s[URL] == url:


@@ 187,13 187,13 @@ def search():
        return abort(400)
    if words[0].startswith("/"):
        try:
            engine = config["engines"][words[0][1:]]  # get engine from keyword
            engine: str = config["engines"][words[0][1:]]  # get engine from keyword
            query = " ".join(words[1:])  # strip engine from query
        except KeyError:
            engine = config["engines"][config["default_engine"]]
            engine: str = config["engines"][config["default_engine"]]
    else:
        try:
            engine = config["engines"][config["default_engine"]]
            engine: str = config["engines"][config["default_engine"]]
        except (KeyError, IndexError):
            return "No engines defined", 403



@@ 201,7 201,7 @@ def search():


@app.route("/css/<string:filename>")
def serve_css(filename):
def serve_css(filename: str):
    # serve static CSS because browsers forbid file:// scheme
    try:
        with open(css_dir / filename) as f:


@@ 215,7 215,7 @@ def serve_css(filename):


@app.route("/icons/<string:filename>")
def serve_icon(filename):
def serve_icon(filename: str):
    try:
        fp = icons_dir / filename
        with open(fp, "rb") as f: