From f811c4c4dcb2e3de55a6f2e3a730f88fb25e5b6d Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Mon, 5 Oct 2020 15:00:28 +0300 Subject: [PATCH] Added python3 imports --- src/api.py | 26 ++++++++++++++++++-------- src/bmconfigparser.py | 42 +++++++++++++++++++++++------------------- src/debug.py | 4 ++-- 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/src/api.py b/src/api.py index 641ce16e..c2123fd7 100644 --- a/src/api.py +++ b/src/api.py @@ -58,20 +58,30 @@ For further examples please reference `.tests.test_api`. """ import base64 -import ConfigParser import errno import hashlib -import httplib import json import random # nosec import socket import subprocess import time -import xmlrpclib from binascii import hexlify, unhexlify -from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer from struct import pack +try: + import httplib +except ImportError: + import http.client as httplib +try: + from xmlrpclib import Fault +except ImportError: + from xmlrpc.client import Fault +try: + from SimpleXMLRPCServer import ( + SimpleXMLRPCRequestHandler, SimpleXMLRPCServer) +except ImportError: + from xmlrpc.server import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer + import defaults import helper_inbox import helper_sent @@ -88,7 +98,7 @@ from addresses import ( decodeVarint, varintDecodeError ) -from bmconfigparser import BMConfigParser +from bmconfigparser import BMConfigParser, NoSectionError from debug import logger from helper_ackPayload import genAckPayload from helper_sql import SqlBulkExecute, sqlExecute, sqlQuery, sqlStoredProcedure @@ -163,7 +173,7 @@ class ErrorCodes(type): return result -class APIError(xmlrpclib.Fault): +class APIError(Fault): """ APIError exception class @@ -843,7 +853,7 @@ class BMRPCDispatcher(object): ' Use deleteAddress API call instead.') try: self.config.remove_section(address) - except ConfigParser.NoSectionError: + except NoSectionError: raise APIError( 13, 'Could not find this address in your keys.dat file.') self.config.save() @@ -860,7 +870,7 @@ class BMRPCDispatcher(object): address = addBMIfNotPresent(address) try: self.config.remove_section(address) - except ConfigParser.NoSectionError: + except NoSectionError: raise APIError( 13, 'Could not find this address in your keys.dat file.') self.config.save() diff --git a/src/bmconfigparser.py b/src/bmconfigparser.py index 328cf0c7..d0af5877 100644 --- a/src/bmconfigparser.py +++ b/src/bmconfigparser.py @@ -2,10 +2,17 @@ BMConfigParser class definition and default configuration settings """ -import ConfigParser import os import shutil from datetime import datetime +try: + from ConfigParser import ( + ConfigParser, InterpolationError, NoOptionError, NoSectionError, + SafeConfigParser) +except ImportError: + from configparser import ( + ConfigParser, InterpolationError, NoOptionError, NoSectionError) + SafeConfigParser = ConfigParser import state from singleton import Singleton @@ -42,7 +49,7 @@ BMConfigDefaults = { @Singleton -class BMConfigParser(ConfigParser.SafeConfigParser): +class BMConfigParser(SafeConfigParser): """ Singleton class inherited from :class:`ConfigParser.SafeConfigParser` with additional methods specific to bitmessage config. @@ -57,24 +64,24 @@ class BMConfigParser(ConfigParser.SafeConfigParser): raise TypeError("option values must be strings") if not self.validate(section, option, value): raise ValueError("Invalid value %s" % value) - return ConfigParser.ConfigParser.set(self, section, option, value) + return ConfigParser.set(self, section, option, value) def get(self, section, option, raw=False, variables=None): # pylint: disable=arguments-differ try: if section == "bitmessagesettings" and option == "timeformat": - return ConfigParser.ConfigParser.get( + return ConfigParser.get( self, section, option, raw, variables) try: return self._temp[section][option] except KeyError: pass - return ConfigParser.ConfigParser.get( + return ConfigParser.get( self, section, option, True, variables) - except ConfigParser.InterpolationError: - return ConfigParser.ConfigParser.get( + except InterpolationError: + return ConfigParser.get( self, section, option, True, variables) - except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e: + except (NoSectionError, NoOptionError) as e: try: return BMConfigDefaults[section][option] except (KeyError, ValueError, AttributeError): @@ -91,8 +98,7 @@ class BMConfigParser(ConfigParser.SafeConfigParser): """Return value as boolean, False on exceptions""" try: return self.getboolean(section, field) - except (ConfigParser.NoSectionError, ConfigParser.NoOptionError, - ValueError, AttributeError): + except (NoSectionError, NoOptionError, ValueError, AttributeError): return False def safeGetInt(self, section, field, default=0): @@ -100,23 +106,21 @@ class BMConfigParser(ConfigParser.SafeConfigParser): 0 if default missing""" try: return self.getint(section, field) - except (ConfigParser.NoSectionError, ConfigParser.NoOptionError, - ValueError, AttributeError): + except (NoSectionError, NoOptionError, ValueError, AttributeError): return default def safeGet(self, section, option, default=None): """Return value as is, default on exceptions, None if default missing""" try: return self.get(section, option) - except (ConfigParser.NoSectionError, ConfigParser.NoOptionError, - ValueError, AttributeError): + except (NoSectionError, NoOptionError, ValueError, AttributeError): return default def items(self, section, raw=False, variables=None): """Return section variables as parent, but override the "raw" argument to always True""" # pylint: disable=arguments-differ - return ConfigParser.ConfigParser.items(self, section, True, variables) + return ConfigParser.items(self, section, True, variables) @staticmethod def addresses(): @@ -125,21 +129,21 @@ class BMConfigParser(ConfigParser.SafeConfigParser): x for x in BMConfigParser().sections() if x.startswith('BM-')] def read(self, filenames): - ConfigParser.ConfigParser.read(self, filenames) + ConfigParser.read(self, filenames) for section in self.sections(): for option in self.options(section): try: if not self.validate( section, option, - ConfigParser.ConfigParser.get(self, section, option) + ConfigParser.get(self, section, option) ): try: newVal = BMConfigDefaults[section][option] except KeyError: continue - ConfigParser.ConfigParser.set( + ConfigParser.set( self, section, option, newVal) - except ConfigParser.InterpolationError: + except InterpolationError: continue def save(self): diff --git a/src/debug.py b/src/debug.py index cab07275..001c507e 100644 --- a/src/debug.py +++ b/src/debug.py @@ -35,7 +35,6 @@ Logging is thread-safe so you don't have to worry about locks, just import and log. """ -import ConfigParser import logging import logging.config import os @@ -43,6 +42,7 @@ import sys import helper_startup import state +from bmconfigparser import NoSectionError helper_startup.loadConfig() @@ -74,7 +74,7 @@ def configureLogging(): False, 'Loaded logger configuration from %s' % logging_config ) - except (OSError, ConfigParser.NoSectionError): + except (OSError, NoSectionError): if os.path.isfile(logging_config): fail_msg = \ 'Failed to load logger configuration from %s, using default' \