diff --git a/PyFunceble/config/loader.py b/PyFunceble/config/loader.py index 50d719c0..f91e1e8a 100644 --- a/PyFunceble/config/loader.py +++ b/PyFunceble/config/loader.py @@ -65,13 +65,11 @@ import PyFunceble.cli.storage import PyFunceble.storage from PyFunceble.config.compare import ConfigComparison -from PyFunceble.downloader.iana import IANADownloader -from PyFunceble.downloader.public_suffix import PublicSuffixDownloader -from PyFunceble.downloader.user_agents import UserAgentsDownloader from PyFunceble.helpers.dict import DictHelper from PyFunceble.helpers.environment_variable import EnvironmentVariableHelper from PyFunceble.helpers.file import FileHelper from PyFunceble.helpers.merge import Merge +from PyFunceble.utils.system import LateImport class ConfigLoader: @@ -294,12 +292,16 @@ def install_missing_infrastructure_files( return self - @classmethod + @LateImport( + "PyFunceble.downloader.iana<-IANADownloader", + "PyFunceble.downloader.public_suffix<-PublicSuffixDownloader", + "PyFunceble.downloader.user_agents<-UserAgentsDownloader", + ) def download_dynamic_infrastructure_files( - cls, + self, ) -> "ConfigLoader": """ - Downloads all the dynamicly (generated) infrastructure files. + Downloads all the dynamically (generated) infrastructure files. .. note:: Downloaded if missing: @@ -309,7 +311,8 @@ def download_dynamic_infrastructure_files( ## pragma: no cover ## Underlying download methods already tested. - if not cls.is_already_loaded(): + if not self.is_already_loaded(): + # pylint: disable=undefined-variable # LateImport IANADownloader().start() PublicSuffixDownloader().start() UserAgentsDownloader().start() diff --git a/PyFunceble/downloader/base.py b/PyFunceble/downloader/base.py index 851ee944..e0af29fe 100644 --- a/PyFunceble/downloader/base.py +++ b/PyFunceble/downloader/base.py @@ -53,10 +53,10 @@ import datetime import os from typing import Optional -import importlib import PyFunceble.downloader.exceptions import PyFunceble.exceptions +import PyFunceble.factory import PyFunceble.storage from PyFunceble.helpers.dict import DictHelper from PyFunceble.helpers.download import DownloadHelper @@ -73,7 +73,6 @@ class DownloaderBase: the download mechanism but also the generation or update of that JSON file. """ - DOWNTIME_INDEX: Optional[str] = None """ Used to set/track the download time of the current file. @@ -277,7 +276,6 @@ def start(self) -> None: Starts the download process. """ - if self.authorized and self.is_last_download_expired(): if not hasattr(self, "destination") or not self.destination: raise PyFunceble.downloader.exceptions.NoDownloadDestinationGiven() @@ -285,10 +283,9 @@ def start(self) -> None: if not hasattr(self, "download_link") or not self.download_link: raise PyFunceble.downloader.exceptions.NoDownloadLinkGiven() - importlib.import_module("PyFunceble.factory") - if DownloadHelper( - self.download_link, session=PyFunceble.factory.Requester + self.download_link, + session=PyFunceble.factory.Requester, ).download_text(destination=self.destination): self.set_current_downtime() self.save_all_downtimes() diff --git a/PyFunceble/query/dns/nameserver.py b/PyFunceble/query/dns/nameserver.py index a1dd53df..bbcfece9 100644 --- a/PyFunceble/query/dns/nameserver.py +++ b/PyFunceble/query/dns/nameserver.py @@ -58,7 +58,6 @@ import PyFunceble.facility import PyFunceble.storage from PyFunceble.checker.syntax.domain import DomainSyntaxChecker -from PyFunceble.checker.syntax.url import URLSyntaxChecker from PyFunceble.converter.url2netloc import Url2Netloc @@ -79,13 +78,13 @@ class Nameservers: protocol: Optional[str] = None - domain_syntax_checker: DomainSyntaxChecker = DomainSyntaxChecker() - url_syntax_checker: URLSyntaxChecker = URLSyntaxChecker() + domain_syntax_checker: Optional[DomainSyntaxChecker] = None url2netloc: Url2Netloc = Url2Netloc() def __init__( self, nameserver: Optional[List[str]] = None, protocol: str = "TCP" ) -> None: + self.domain_syntax_checker = DomainSyntaxChecker() self.protocol = protocol if nameserver is not None: @@ -125,8 +124,7 @@ def split_nameserver_from_port( return ":".join(splitted[:-1]), default_port return nameserver, default_port - @classmethod - def get_ip_from_nameserver(cls, nameserver: str) -> List[str]: + def get_ip_from_nameserver(self, nameserver: str) -> List[str]: """ Given a nameserver, this method resolve it in order to get the IP to contact. @@ -141,7 +139,7 @@ def get_ip_from_nameserver(cls, nameserver: str) -> List[str]: result = [] - if cls.domain_syntax_checker.set_subject(nameserver).is_valid(): + if self.domain_syntax_checker.set_subject(nameserver).is_valid(): try: result.extend( [ diff --git a/PyFunceble/query/dns/query_tool.py b/PyFunceble/query/dns/query_tool.py index 6c666115..97789dca 100644 --- a/PyFunceble/query/dns/query_tool.py +++ b/PyFunceble/query/dns/query_tool.py @@ -96,7 +96,7 @@ class DNSQueryTool: x.name: x.value for x in dns.rdatatype.RdataType } - nameservers: Nameservers = Nameservers() + nameservers: Optional[Nameservers] = None _query_record_type: int = dns.rdatatype.RdataType.ANY _subject: Optional[str] = None @@ -120,6 +120,8 @@ def __init__( trust_server: Optional[bool] = None, delay: Optional[bool] = None, ) -> None: + self.nameservers = Nameservers() + if nameservers is not None: self.nameservers.set_nameservers(nameservers) else: # pragma: no cover ## I'm not playing with system resolver. diff --git a/PyFunceble/query/requests/requester.py b/PyFunceble/query/requests/requester.py index c00c253c..eef2fe65 100644 --- a/PyFunceble/query/requests/requester.py +++ b/PyFunceble/query/requests/requester.py @@ -61,10 +61,10 @@ import PyFunceble.facility import PyFunceble.storage -from PyFunceble.dataset.user_agent import UserAgentDataset from PyFunceble.query.dns.query_tool import DNSQueryTool from PyFunceble.query.requests.adapter.http import RequestHTTPAdapter from PyFunceble.query.requests.adapter.https import RequestHTTPSAdapter +from PyFunceble.utils.system import LateImport class Requester: @@ -523,6 +523,7 @@ def get_timeout(self) -> float: return self.timeout + @LateImport("PyFunceble.dataset.user_agent<-UserAgentDataset") def get_session(self) -> requests.Session: """ Provides a new session. @@ -551,6 +552,7 @@ def get_session(self) -> requests.Session: ), ) + # pylint: disable=undefined-variable # LateImport custom_headers = {"User-Agent": UserAgentDataset().get_latest()} session.headers.update(custom_headers)