Replace MethodType to Callable
This commit is contained in:
parent
d57021e492
commit
dfd1d68c40
10
README.rst
10
README.rst
|
@ -14,9 +14,9 @@ This module is for Python 3.6 and above!
|
||||||
INSTALLATION
|
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
|
USAGE
|
||||||
|
@ -45,20 +45,20 @@ The function :code:`validate_email_or_fail()` works exactly like :code:`validate
|
||||||
|
|
||||||
Auto-updater
|
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::
|
The update can be triggered manually::
|
||||||
|
|
||||||
from validate_email.updater import update_builtin_blacklist
|
from validate_email.updater import update_builtin_blacklist
|
||||||
|
|
||||||
update_builtin_blacklist(force: bool = False, background: bool = True,
|
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:`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:`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
|
TODOs and BUGS
|
||||||
============================
|
============================
|
||||||
|
|
|
@ -4,8 +4,7 @@ from pathlib import Path
|
||||||
from tempfile import gettempdir, gettempprefix
|
from tempfile import gettempdir, gettempprefix
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from time import time
|
from time import time
|
||||||
from types import MethodType
|
from typing import Callable, Optional
|
||||||
from typing import Optional
|
|
||||||
from urllib.error import HTTPError
|
from urllib.error import HTTPError
|
||||||
from urllib.request import Request, urlopen
|
from urllib.request import Request, urlopen
|
||||||
|
|
||||||
|
@ -36,7 +35,7 @@ class BlacklistUpdater(object):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_refresh_when_older_than: int = 5 * 24 * 60 * 60 # 5 days
|
_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
|
_is_install_time: bool = False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -123,11 +122,11 @@ class BlacklistUpdater(object):
|
||||||
BLACKLIST_FILEPATH_TMP.touch()
|
BLACKLIST_FILEPATH_TMP.touch()
|
||||||
return
|
return
|
||||||
raise
|
raise
|
||||||
if type(self._on_update_callback) is MethodType:
|
if self._on_update_callback:
|
||||||
self._on_update_callback()
|
self._on_update_callback()
|
||||||
|
|
||||||
def process(
|
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.'
|
'Start optionally updating the blacklist.txt file.'
|
||||||
# Locking to avoid multi-process update on multi-process startup
|
# Locking to avoid multi-process update on multi-process startup
|
||||||
self._on_update_callback = callback
|
self._on_update_callback = callback
|
||||||
|
@ -137,7 +136,7 @@ class BlacklistUpdater(object):
|
||||||
|
|
||||||
def update_builtin_blacklist(
|
def update_builtin_blacklist(
|
||||||
force: bool = False, background: bool = True,
|
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
|
Update and reload the built-in blacklist. Return the `Thread` used
|
||||||
to do the background update, so it can be `join()`-ed.
|
to do the background update, so it can be `join()`-ed.
|
||||||
|
|
Loading…
Reference in New Issue