Skip to content

Commit

Permalink
Fix issue with some circular or unnecessary imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
funilrys committed Mar 17, 2024
1 parent 754ecb2 commit 7c9c0b0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
17 changes: 10 additions & 7 deletions PyFunceble/config/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand Down
9 changes: 3 additions & 6 deletions PyFunceble/downloader/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -277,18 +276,16 @@ 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()

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()
10 changes: 4 additions & 6 deletions PyFunceble/query/dns/nameserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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:
Expand Down Expand Up @@ -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.
Expand All @@ -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(
[
Expand Down
4 changes: 3 additions & 1 deletion PyFunceble/query/dns/query_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion PyFunceble/query/requests/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7c9c0b0

Please sign in to comment.