From 9df783cf9d6a183a82d2ad19d51995a0dedfa16f Mon Sep 17 00:00:00 2001 From: Alexander Kukushkin Date: Wed, 15 Jul 2015 12:03:28 +0200 Subject: [PATCH] use six module to support python 2 and 3 instead of conditional imports --- helpers/api.py | 10 ++-------- helpers/dcs.py | 7 +------ helpers/postgresql.py | 8 +++----- requirements-py2.txt | 1 + requirements-py3.txt | 1 + tests/test_api.py | 6 +----- tests/test_patroni.py | 6 +----- 7 files changed, 10 insertions(+), 29 deletions(-) diff --git a/helpers/api.py b/helpers/api.py index e8998792..4274f786 100644 --- a/helpers/api.py +++ b/helpers/api.py @@ -1,17 +1,11 @@ import json import logging import psycopg2 -import sys +from six.moves.BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer +from six.moves.socketserver import ThreadingMixIn from threading import Thread -if sys.hexversion >= 0x03000000: - from http.server import BaseHTTPRequestHandler, HTTPServer - from socketserver import ThreadingMixIn -else: - from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer - from SocketServer import ThreadingMixIn - logger = logging.getLogger(__name__) diff --git a/helpers/dcs.py b/helpers/dcs.py index a5e43488..abb60180 100644 --- a/helpers/dcs.py +++ b/helpers/dcs.py @@ -1,13 +1,8 @@ import abc -import sys from collections import namedtuple from helpers.utils import calculate_ttl, sleep - -if sys.hexversion >= 0x03000000: - from urllib.parse import urlparse, urlunparse, parse_qsl -else: - from urlparse import urlparse, urlunparse, parse_qsl +from six.moves.urllib_parse import urlparse, urlunparse, parse_qsl def parse_connection_string(value): diff --git a/helpers/postgresql.py b/helpers/postgresql.py index bbf5ffe9..63e2b24d 100644 --- a/helpers/postgresql.py +++ b/helpers/postgresql.py @@ -3,15 +3,13 @@ import os import psycopg2 import shutil import subprocess -import sys +import six from helpers.utils import sleep +from six.moves.urllib_parse import urlparse -if sys.hexversion >= 0x03000000: - from urllib.parse import urlparse +if six.PY3: long = int -else: - from urlparse import urlparse logger = logging.getLogger(__name__) diff --git a/requirements-py2.txt b/requirements-py2.txt index 012d87a1..12c71fd0 100644 --- a/requirements-py2.txt +++ b/requirements-py2.txt @@ -3,4 +3,5 @@ dnspython psycopg2 PyYAML requests +six kazoo>=2.2.1 diff --git a/requirements-py3.txt b/requirements-py3.txt index bf481736..a48403f9 100644 --- a/requirements-py3.txt +++ b/requirements-py3.txt @@ -3,4 +3,5 @@ dnspython3 psycopg2 PyYAML requests +six kazoo>=2.2.1 diff --git a/tests/test_api.py b/tests/test_api.py index 2079d881..07ad410b 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -4,11 +4,7 @@ import unittest from helpers.api import RestApiHandler, RestApiServer from test_postgresql import psycopg2_connect - -if sys.hexversion >= 0x03000000: - from io import BytesIO as IO -else: - from StringIO import StringIO as IO +from six import BytesIO as IO def throws(*args, **kwargs): diff --git a/tests/test_patroni.py b/tests/test_patroni.py index 8d74c8cb..a2d29e9c 100644 --- a/tests/test_patroni.py +++ b/tests/test_patroni.py @@ -11,16 +11,12 @@ import yaml from patroni import Patroni, main from helpers.dcs import Cluster, Member from helpers.zookeeper import ZooKeeper +from six.moves import BaseHTTPServer from test_etcd import requests_get, requests_put, requests_delete from test_ha import true, false from test_postgresql import Postgresql, subprocess_call, psycopg2_connect from test_zookeeper import MockKazooClient -if sys.hexversion >= 0x03000000: - import http.server as BaseHTTPServer -else: - import BaseHTTPServer - def nop(*args, **kwargs): pass