Replace MethodType to Callable

This commit is contained in:
László Károlyi 2020-04-11 16:04:27 +02:00
parent d57021e492
commit dfd1d68c40
Signed by: karolyi
GPG key ID: 2DCAF25E55735BFE
2 changed files with 10 additions and 11 deletions

View file

@ -14,9 +14,9 @@ This module is for Python 3.6 and above!
INSTALLATION
============================
You can install the package with pip:
You can install the package with pip::
pip install py3-validate-email
python -m pip install py3-validate-email
USAGE
@ -45,20 +45,20 @@ The function :code:`validate_email_or_fail()` works exactly like :code:`validate
Auto-updater
============================
The package contains an auto-updater for downloading and updating the built-in blacklist.txt. It will run on each module load (and installation), but will try to update the content if the file is older than 5 days, and if the content is not the same that's already downloaded.
The package contains an auto-updater for downloading and updating the built-in blacklist.txt. It will run on each module load (and installation), but will try to update the content only if the file is older than 5 days, and if the content is not the same that's already downloaded.
The update can be triggered manually::
from validate_email.updater import update_builtin_blacklist
update_builtin_blacklist(force: bool = False, background: bool = True,
callback: MethodType = None) -> Optional[Thread]
callback: Callable = None) -> Optional[Thread]
:code:`force`: forces the update even if the downloaded/installed file is fresh enough.
:code:`background`: starts the update in a ``Thread`` so it won't make your code hang while it's updating. If you set this to true, the function will return the Thread used for starting the update so you can ``join()`` it if necessary.
:code:`callback`: An optional function to be called when the update is done.
:code:`callback`: An optional `Callable` (function/method) to be called when the update is done.
TODOs and BUGS
============================

View file

@ -4,8 +4,7 @@ from pathlib import Path
from tempfile import gettempdir, gettempprefix
from threading import Thread
from time import time
from types import MethodType
from typing import Optional
from typing import Callable, Optional
from urllib.error import HTTPError
from urllib.request import Request, urlopen
@ -36,7 +35,7 @@ class BlacklistUpdater(object):
"""
_refresh_when_older_than: int = 5 * 24 * 60 * 60 # 5 days
_on_update_callback: MethodType = None
_on_update_callback: Callable = None
_is_install_time: bool = False
@property
@ -123,11 +122,11 @@ class BlacklistUpdater(object):
BLACKLIST_FILEPATH_TMP.touch()
return
raise
if type(self._on_update_callback) is MethodType:
if self._on_update_callback:
self._on_update_callback()
def process(
self, force: bool = False, callback: Optional[MethodType] = None):
self, force: bool = False, callback: Optional[Callable] = None):
'Start optionally updating the blacklist.txt file.'
# Locking to avoid multi-process update on multi-process startup
self._on_update_callback = callback
@ -137,7 +136,7 @@ class BlacklistUpdater(object):
def update_builtin_blacklist(
force: bool = False, background: bool = True,
callback: MethodType = None) -> Optional[Thread]:
callback: Callable = None) -> Optional[Thread]:
"""
Update and reload the built-in blacklist. Return the `Thread` used
to do the background update, so it can be `join()`-ed.