From 9055a9baf141b6e78f94ead153119372551688b8 Mon Sep 17 00:00:00 2001 From: surbhi Date: Sat, 1 Feb 2025 23:58:40 +0530 Subject: [PATCH] Use six in common files - 2 --- src/class_singleWorker.py | 6 ++++-- src/class_smtpDeliver.py | 7 ++++--- src/class_sqlThread.py | 2 ++ src/depends.py | 5 +++-- src/l10n.py | 6 +++--- src/namecoin.py | 5 +++-- src/protocol.py | 4 ++-- src/shared.py | 3 ++- src/upnp.py | 11 ++++++----- 9 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/class_singleWorker.py b/src/class_singleWorker.py index 83009c37..da9d64c6 100644 --- a/src/class_singleWorker.py +++ b/src/class_singleWorker.py @@ -12,6 +12,9 @@ from binascii import hexlify, unhexlify from struct import pack from subprocess import call # nosec +from six.moves import configparser, queue +from six.moves.reprlib import repr + import defaults import helper_inbox import helper_msgcoding @@ -28,8 +31,7 @@ import tr from addresses import decodeAddress, decodeVarint, encodeVarint from bmconfigparser import config from helper_sql import sqlExecute, sqlQuery -from network import knownnodes, StoppableThread, invQueue -from six.moves import configparser, queue +from network import StoppableThread, invQueue, knownnodes def sizeof_fmt(num, suffix='h/s'): diff --git a/src/class_smtpDeliver.py b/src/class_smtpDeliver.py index 9e3b8ab3..634dee17 100644 --- a/src/class_smtpDeliver.py +++ b/src/class_smtpDeliver.py @@ -4,9 +4,10 @@ SMTP client thread for delivering emails # pylint: disable=unused-variable import smtplib -import urlparse from email.header import Header -from email.mime.text import MIMEText + +from six.moves import email_mime_text +from six.moves.urllib import parse as urlparse import queues import state @@ -55,7 +56,7 @@ class smtpDeliver(StoppableThread): u = urlparse.urlparse(dest) to = urlparse.parse_qs(u.query)['to'] client = smtplib.SMTP(u.hostname, u.port) - msg = MIMEText(body, 'plain', 'utf-8') + msg = email_mime_text(body, 'plain', 'utf-8') msg['Subject'] = Header(subject, 'utf-8') msg['From'] = fromAddress + '@' + SMTPDOMAIN toLabel = map( diff --git a/src/class_sqlThread.py b/src/class_sqlThread.py index 7df9e253..144976f6 100644 --- a/src/class_sqlThread.py +++ b/src/class_sqlThread.py @@ -9,6 +9,8 @@ import sys import threading import time +from six.moves.reprlib import repr + try: import helper_sql import helper_startup diff --git a/src/depends.py b/src/depends.py index d966d5fe..15ddc94a 100755 --- a/src/depends.py +++ b/src/depends.py @@ -7,6 +7,8 @@ import os import re import sys +import six + # Only really old versions of Python don't have sys.hexversion. We don't # support them. The logging module was introduced in Python 2.3 if not hasattr(sys, 'hexversion') or sys.hexversion < 0x20300F0: @@ -18,7 +20,6 @@ if not hasattr(sys, 'hexversion') or sys.hexversion < 0x20300F0: import logging # noqa:E402 import subprocess # nosec B404 - from importlib import import_module # We can now use logging so set up a simple configuration @@ -438,7 +439,7 @@ def check_dependencies(verbose=False, optional=False): 'PyBitmessage requires Python 2.7.4 or greater' ' (but not Python 3+)') has_all_dependencies = False - if sys.hexversion >= 0x3000000: + if six.PY3: logger.error( 'PyBitmessage does not support Python 3+. Python 2.7.4' ' or greater is required. Python 2.7.18 is recommended.') diff --git a/src/l10n.py b/src/l10n.py index fe02d3f4..441562af 100644 --- a/src/l10n.py +++ b/src/l10n.py @@ -3,9 +3,9 @@ import logging import os import re -import sys import time +import six from six.moves import range from bmconfigparser import config @@ -61,7 +61,7 @@ if not re.search(r'\d', time.strftime(time_format)): # It seems some systems lie about the encoding they use # so we perform comprehensive decoding tests -elif sys.version_info[0] == 2: +elif six.PY2: try: # Check day names for i in range(7): @@ -118,7 +118,7 @@ def formatTimestamp(timestamp=None): except ValueError: timestring = time.strftime(time_format) - if sys.version_info[0] == 2: + if six.PY2: return timestring.decode(encoding) return timestring diff --git a/src/namecoin.py b/src/namecoin.py index a16cb3d7..595067e5 100644 --- a/src/namecoin.py +++ b/src/namecoin.py @@ -4,12 +4,13 @@ Namecoin queries # pylint: disable=too-many-branches,protected-access import base64 -import httplib import json import os import socket import sys +from six.moves import http_client as httplib + import defaults from addresses import decodeAddress from bmconfigparser import config @@ -296,7 +297,7 @@ def lookupNamecoinFolder(): """ app = "namecoin" - from os import path, environ + from os import environ, path if sys.platform == "darwin": if "HOME" in environ: dataFolder = path.join(os.environ["HOME"], diff --git a/src/protocol.py b/src/protocol.py index 96c980bb..78c32de0 100644 --- a/src/protocol.py +++ b/src/protocol.py @@ -16,8 +16,8 @@ from struct import Struct, pack, unpack import defaults import highlevelcrypto import state -from addresses import ( - encodeVarint, decodeVarint, decodeAddress, varintDecodeError) +from addresses import (decodeAddress, decodeVarint, encodeVarint, + varintDecodeError) from bmconfigparser import config from debug import logger from helper_sql import sqlExecute diff --git a/src/shared.py b/src/shared.py index b85ddb20..2df198be 100644 --- a/src/shared.py +++ b/src/shared.py @@ -15,6 +15,8 @@ import subprocess # nosec B404 import sys from binascii import hexlify +from six.moves.reprlib import repr + # Project imports. import highlevelcrypto import state @@ -23,7 +25,6 @@ from bmconfigparser import config from debug import logger from helper_sql import sqlQuery - myECCryptorObjects = {} MyECSubscriptionCryptorObjects = {} # The key in this dictionary is the RIPE hash which is encoded diff --git a/src/upnp.py b/src/upnp.py index 42ff0c6d..cf06f8af 100644 --- a/src/upnp.py +++ b/src/upnp.py @@ -4,22 +4,23 @@ Complete UPnP port forwarding implementation in separate thread. Reference: http://mattscodecave.com/posts/using-python-and-upnp-to-forward-a-port """ -import httplib import re import socket import time -import urllib2 from random import randint -from urlparse import urlparse from xml.dom.minidom import Document # nosec B408 + from defusedxml.minidom import parseString +from six.moves import http_client as httplib +from six.moves.urllib.parse import urlparse +from six.moves.urllib.request import urlopen import queues import state import tr from bmconfigparser import config from debug import logger -from network import connectionpool, knownnodes, StoppableThread +from network import StoppableThread, connectionpool, knownnodes from network.node import Peer @@ -108,7 +109,7 @@ class Router: # pylint: disable=old-style-class logger.error("UPnP: missing location header") # get the profile xml file and read it into a variable - directory = urllib2.urlopen(header['location']).read() + directory = urlopen(header['location']).read() # create a DOM object that represents the `directory` document dom = parseString(directory) -- 2.47.2