Skip to content

Commit 2703cb2

Browse files
committed
Introduction of PyFunceble v3.3.2 (Teal Blauwbok: Tick)
New: - Introduction of an option for sanity check into `PyFunceble.load_config`. This is mainly for those who may use the API. Fixed: - Warnings from dnspython. We now use the `resolve()` method instead of the `query()` method. - Warnings from domain2idna. We now use `domain2idna.domain2idna()` instead of `domain2idna.get()`. Improved: - The way we detect if a package is the `dev` one. - The tests which relate to the syntax check. - The tests which relate to the DNS resolver. - The way we get the latest user agent. We now ensure that our file was downloaded before trying to get it. This is mainly thought for those who may use the API. - The presence of SQLAlchemy in the migration from the old to the new layout. Removed: - All occurrences of the old database connection method.
2 parents 6b15e70 + e8c2775 commit 2703cb2

File tree

13 files changed

+290
-141
lines changed

13 files changed

+290
-141
lines changed

PyFunceble/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,9 @@ def is_url(subject): # pragma: no cover
672672
return None
673673

674674

675-
def load_config(generate_directory_structure=False, custom=None): # pragma: no cover
675+
def load_config(
676+
generate_directory_structure=False, custom=None, sanity_check=False
677+
): # pragma: no cover
676678
"""
677679
Load the configuration.
678680
@@ -683,6 +685,9 @@ def load_config(generate_directory_structure=False, custom=None): # pragma: no
683685
:param dict custom:
684686
A dict with the configuration index (from .PyFunceble.yaml) to update.
685687
688+
:param bool sanity_check:
689+
Tell us to run the safety check of the configuration.
690+
686691
.. note::
687692
If :code:`config` is given, the given :code:`dict` overwrite
688693
the last value of the given indexes in the configuration.
@@ -706,6 +711,9 @@ def load_config(generate_directory_structure=False, custom=None): # pragma: no
706711
else:
707712
LOADER.set_custom_config(custom)
708713

714+
if sanity_check:
715+
cconfig.Preset().init_all()
716+
709717
if generate_directory_structure:
710718
output.Constructor()
711719

PyFunceble/abstracts/package.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Package:
6464
:type: str
6565
"""
6666

67-
VERSION = "3.3.1. (Teal Blauwbok: Grub)"
67+
VERSION = "3.3.2. (Teal Blauwbok: Tick)"
6868
"""
6969
Sets the package version.
7070
@@ -213,7 +213,9 @@ def is_local_dev(cls):
213213
Checks if the local version is the development version.
214214
"""
215215

216-
return "dev" in Package.VERSION
216+
return cls.split_versions(Package.VERSION, return_non_digits=True)[
217+
-1
218+
].startswith("dev")
217219

218220
@classmethod
219221
def is_local_cloned(cls): # pragma: no cover

PyFunceble/check.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
ip_network,
5959
)
6060

61-
from domain2idna import get as domain2idna
61+
import domain2idna
6262

6363
import PyFunceble
6464

@@ -134,7 +134,7 @@ def is_url(
134134
# We have to convert the domain to IDNA.
135135

136136
# We convert the initial base to IDNA.
137-
base = domain2idna(base)
137+
base = domain2idna.domain2idna(base)
138138

139139
if ":" in base:
140140
# The port is explicitly given.
@@ -424,7 +424,7 @@ def is_ipv6(self):
424424
except ValueError:
425425
return False
426426

427-
def is_ip_range(self): # pragma: no cover
427+
def is_ip_range(self):
428428
"""
429429
Checks if the given subject is a valid IPv4 or IPv6 range.
430430
@@ -464,7 +464,7 @@ def is_ipv6_range(self):
464464
return 0 <= block <= 128
465465
return False
466466

467-
def is_reserved_ip(self): # pragma: no cover
467+
def is_reserved_ip(self):
468468
"""
469469
Checks if the given subject is a reserved IPv4 or IPv6.
470470

PyFunceble/core/file.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
from tempfile import NamedTemporaryFile
5353

54-
from domain2idna import get as domain2idna
54+
import domain2idna
5555
from sqlalchemy.orm.exc import NoResultFound
5656

5757
import PyFunceble
@@ -264,7 +264,7 @@ def test(self, subject):
264264
"""
265265

266266
if PyFunceble.CONFIGURATION.idna_conversion:
267-
subject = domain2idna(subject)
267+
subject = domain2idna.domain2idna(subject)
268268

269269
if isinstance(PyFunceble.CONFIGURATION.cooldown_time, (float, int)):
270270
PyFunceble.sleep(PyFunceble.CONFIGURATION.cooldown_time)

PyFunceble/core/multiprocess.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@
5656
from tempfile import NamedTemporaryFile
5757
from traceback import format_exc
5858

59+
import domain2idna
5960
from colorama import Fore, Style
6061
from colorama import init as initiate_colorama
61-
from domain2idna import get as domain2idna
6262

6363
import PyFunceble
6464

@@ -169,7 +169,7 @@ def test(
169169
PyFunceble.INTERN.update(intern)
170170

171171
if PyFunceble.CONFIGURATION.idna_conversion:
172-
subject = domain2idna(subject)
172+
subject = domain2idna.domain2idna(subject)
173173

174174
if not self.should_be_ignored(
175175
subject,

PyFunceble/core/simple.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
limitations under the License.
5151
"""
5252

53-
from domain2idna import get as domain2idna
53+
import domain2idna
5454

5555
import PyFunceble
5656

@@ -70,7 +70,7 @@ def __init__(self, subject):
7070
self.mining = PyFunceble.engine.Mining("simple")
7171

7272
if PyFunceble.CONFIGURATION.idna_conversion:
73-
self.subject = domain2idna(subject)
73+
self.subject = domain2idna.domain2idna(subject)
7474
else:
7575
self.subject = subject
7676

PyFunceble/engine/database/migrations/old2new.py

Lines changed: 27 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@
5252

5353
from datetime import datetime
5454
from multiprocessing import active_children
55-
from os import sep as directory_separator
5655

57-
import pymysql
58-
import pymysql.cursors
5956
from colorama import Fore, Style
6057
from sqlalchemy.exc import IntegrityError
6158
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
@@ -98,58 +95,25 @@ def authorized(self):
9895
[self.does_table_exists(x) for x in self.old_tables]
9996
)
10097

101-
def __get_rows(self, statement, limit=20):
98+
@classmethod
99+
def __get_rows(cls, statement, limit=20):
102100
"""
103101
Get the row of the database.
104102
"""
105103

106104
statement += f" LIMIT {limit}"
107105

108106
while True:
109-
fetcher_connection = self.get_old_connection()
110-
with fetcher_connection.cursor() as cursor:
111-
cursor.execute(statement)
112-
113-
db_result = cursor.fetchall()
114-
115-
fetcher_connection.close()
107+
with PyFunceble.engine.database.loader.session.Session() as db_session:
108+
db_result = db_session.execute(statement)
109+
db_result = [dict(x) for x in db_result.fetchall()]
116110

117111
if not db_result:
118112
break
119113

120114
for result in db_result:
121115
yield result
122116

123-
def get_old_connection(self):
124-
"""
125-
Provides a connection, the old way.
126-
"""
127-
128-
if (
129-
directory_separator not in self.credentials["host"]
130-
or "/" not in self.credentials["host"]
131-
):
132-
return pymysql.connect(
133-
host=self.credentials["host"],
134-
port=self.credentials["port"],
135-
user=self.credentials["username"],
136-
password=self.credentials["password"],
137-
db=self.credentials["name"],
138-
charset=self.credentials["charset"],
139-
cursorclass=pymysql.cursors.DictCursor,
140-
autocommit=True,
141-
)
142-
143-
return pymysql.connect(
144-
unix_socket=self.credentials["host"],
145-
user=self.credentials["username"],
146-
password=self.credentials["password"],
147-
db=self.credentials["name"],
148-
charset=self.credentials["charset"],
149-
cursorclass=pymysql.cursors.DictCursor,
150-
autocommit=True,
151-
)
152-
153117
def does_table_exists(self, table_name):
154118
"""
155119
Checks if the table exists.
@@ -158,25 +122,23 @@ def does_table_exists(self, table_name):
158122
The name of the table to check.
159123
"""
160124

161-
old_connection = self.get_old_connection()
162-
with old_connection.cursor() as cursor:
125+
with PyFunceble.engine.database.loader.session.Session() as db_session:
163126
statement = (
164127
"SELECT COUNT(*) "
165128
"FROM information_schema.tables "
166-
"WHERE table_schema = %(database_name)s "
167-
"AND table_name = %(table_name)s "
129+
"WHERE table_schema = :database_name "
130+
"AND table_name = :table_name "
168131
)
169132

170-
cursor.execute(
133+
result = db_session.execute(
171134
statement,
172135
{
173136
"database_name": self.credentials["name"],
174137
"table_name": table_name,
175138
},
176139
)
177140

178-
result = cursor.fetchone()
179-
old_connection.close()
141+
result = dict(result.fetchone())
180142

181143
if result["COUNT(*)"] != 1:
182144
return False
@@ -287,12 +249,11 @@ def __tested_migration(self, data):
287249
except IntegrityError:
288250
pass
289251

290-
old_connection = self.get_old_connection()
291-
with old_connection.cursor() as cursor:
292-
statement = "DELETE FROM pyfunceble_tested WHERE id = %(status_id)s"
252+
with PyFunceble.engine.database.loader.session.Session() as db_session:
253+
statement = "DELETE FROM pyfunceble_tested WHERE id = :status_id"
254+
293255
# pylint: disable=no-member
294-
cursor.execute(statement, {"status_id": status.id})
295-
old_connection.close()
256+
db_session.execute(statement, {"status_id": status.id})
296257

297258
if self.autosave.authorized or PyFunceble.CONFIGURATION.print_dots:
298259
PyFunceble.LOGGER.info(f'Switched {data["tested"]} to SQLAlchemy.')
@@ -364,11 +325,10 @@ def __autocontinue_migration(self, data):
364325
except IntegrityError:
365326
pass
366327

367-
old_connection = self.get_old_connection()
368-
with old_connection.cursor() as cursor:
369-
statement = "DELETE FROM pyfunceble_auto_continue WHERE id = %(id)s"
370-
cursor.execute(statement, {"id": data["id"]})
371-
old_connection.close()
328+
with PyFunceble.engine.database.loader.session.Session() as db_session:
329+
statement = "DELETE FROM pyfunceble_auto_continue WHERE id = :id"
330+
331+
db_session.execute(statement, {"id": data["id"]})
372332

373333
if self.autosave.authorized or PyFunceble.CONFIGURATION.print_dots:
374334
PyFunceble.LOGGER.info(
@@ -440,11 +400,10 @@ def __whois_migration(self, data):
440400
except IntegrityError:
441401
pass
442402

443-
old_connection = self.get_old_connection()
444-
with old_connection.cursor() as cursor:
445-
statement = "DELETE FROM pyfunceble_whois WHERE id = %(id)s"
446-
cursor.execute(statement, {"id": data["id"]})
447-
old_connection.close()
403+
with PyFunceble.engine.database.loader.session.Session() as db_session:
404+
statement = "DELETE FROM pyfunceble_whois WHERE id = :id"
405+
406+
db_session.execute(statement, {"id": data["id"]})
448407

449408
if self.autosave.authorized or PyFunceble.CONFIGURATION.print_dots:
450409
PyFunceble.LOGGER.info(f'Switched {data["subject"]} (WHOIS) to SQLAlchemy.')
@@ -482,12 +441,12 @@ def __delete_old_tables(self):
482441
for table in self.old_tables:
483442
if self.does_table_exists(table):
484443
PyFunceble.LOGGER.info(f"Starting deletion of {table}.")
485-
old_connection = self.get_old_connection()
486-
with old_connection.cursor() as cursor:
444+
445+
with PyFunceble.engine.database.loader.session.Session() as db_session:
487446
statement = f"DROP TABLE {table}"
488-
cursor.execute(statement)
489-
old_connection.close()
490-
PyFunceble.LOGGER.info(f"Finished deletion of {table}.")
447+
448+
db_session.execute(statement)
449+
PyFunceble.LOGGER.info(f"Finished deletion of {table}.")
491450

492451
if self.autosave.authorized or PyFunceble.CONFIGURATION.print_dots:
493452
print(".", end="")

PyFunceble/engine/user_agent.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,16 @@ class UserAgent:
6060
"""
6161

6262
def __init__(self):
63-
self.dumped = PyFunceble.helpers.Dict().from_json_file(
63+
file_instance = PyFunceble.helpers.File(
6464
PyFunceble.CONFIG_DIRECTORY
6565
+ PyFunceble.abstracts.Infrastructure.USER_AGENT_FILENAME
6666
)
6767

68+
if not file_instance.exists():
69+
PyFunceble.downloader.UserAgents()
70+
71+
self.dumped = PyFunceble.helpers.Dict().from_json_file(file_instance.path)
72+
6873
def get(self):
6974
"""
7075
Provides the user agent to use.

0 commit comments

Comments
 (0)