From c0133052de575c2e277d51643b8c8d39bbf96fe3 Mon Sep 17 00:00:00 2001 From: Jesper Geertsen Jonsson Date: Fri, 30 Dec 2016 15:39:35 +0100 Subject: [PATCH] Refactor imports Improved compatibility, robustness, clear errors and some cleaning up. Also synced with the readme. --- CloudFlare/__init__.py | 10 +++++----- CloudFlare/cloudflare.py | 14 +++++++------- CloudFlare/read_configs.py | 5 ++++- CloudFlare/utils.py | 4 +++- README.md | 2 -- README.rst | 3 --- cli4/__main__.py | 3 ++- cli4/cli4.py | 5 +---- cli4/converters.py | 7 +------ setup.py | 12 +++++++++--- 10 files changed, 32 insertions(+), 33 deletions(-) diff --git a/CloudFlare/__init__.py b/CloudFlare/__init__.py index 60c8e7f..404bdbd 100644 --- a/CloudFlare/__init__.py +++ b/CloudFlare/__init__.py @@ -1,8 +1,8 @@ """ Cloudflare v4 API""" - -try: - from cloudflare import CloudFlare -except: - pass +from __future__ import absolute_import __version__ = '1.4.11' + +from .cloudflare import CloudFlare + +__all__ = ['CloudFlare'] diff --git a/CloudFlare/cloudflare.py b/CloudFlare/cloudflare.py index f38ed91..610d5ac 100644 --- a/CloudFlare/cloudflare.py +++ b/CloudFlare/cloudflare.py @@ -1,15 +1,15 @@ """ Cloudflare v4 API""" +from __future__ import absolute_import import json -import urllib import requests -from logger import Logger -from utils import user_agent, sanitize_secrets -from read_configs import read_configs -from api_v4 import api_v4 -from api_extras import api_extras -from exceptions import CloudFlareError, CloudFlareAPIError, CloudFlareInternalError +from .logger import Logger +from .utils import user_agent, sanitize_secrets +from .read_configs import read_configs +from .api_v4 import api_v4 +from .api_extras import api_extras +from .exceptions import CloudFlareError, CloudFlareAPIError, CloudFlareInternalError BASE_URL = 'https://api.cloudflare.com/client/v4' diff --git a/CloudFlare/read_configs.py b/CloudFlare/read_configs.py index 659811a..51bb1d3 100644 --- a/CloudFlare/read_configs.py +++ b/CloudFlare/read_configs.py @@ -2,7 +2,10 @@ import os import re -import ConfigParser +try: + import ConfigParser # py2 +except ImportError: + import configparser as ConfigParser # py3 def read_configs(): """ reading the config file for Cloudflare API""" diff --git a/CloudFlare/utils.py b/CloudFlare/utils.py index c5d5331..0164d26 100644 --- a/CloudFlare/utils.py +++ b/CloudFlare/utils.py @@ -1,8 +1,10 @@ """ misc utilities for Cloudflare API""" +from __future__ import absolute_import import sys import requests -from __init__ import __version__ + +from . import __version__ def user_agent(): """ misc utilities for Cloudflare API""" diff --git a/README.md b/README.md index 96f5159..e852bcc 100644 --- a/README.md +++ b/README.md @@ -298,7 +298,6 @@ The exception returns both an integer and textual message in one value. ```python import CloudFlare -import CloudFlare.exceptions ... try @@ -316,7 +315,6 @@ You can itterate over that array to see the additional error. ```python import sys import CloudFlare -import CloudFlare.exceptions ... try diff --git a/README.rst b/README.rst index 2d247a1..3de4725 100644 --- a/README.rst +++ b/README.rst @@ -140,7 +140,6 @@ A more complex example follows. .. code:: python import CloudFlare - import CloudFlare.exceptions def main(): zone_name = 'example.com' @@ -336,7 +335,6 @@ The exception returns both an integer and textual message in one value. .. code:: python import CloudFlare - import CloudFlare.exceptions ... try @@ -356,7 +354,6 @@ the additional error. import sys import CloudFlare - import CloudFlare.exceptions ... try diff --git a/cli4/__main__.py b/cli4/__main__.py index 70de4be..4563bcc 100755 --- a/cli4/__main__.py +++ b/cli4/__main__.py @@ -1,9 +1,10 @@ #!/usr/bin/env python """Cloudflare API via command line""" +from __future__ import absolute_import import sys -from cli4 import cli4 +from .cli4 import cli4 def main(args=None): """Cloudflare API via command line""" diff --git a/cli4/cli4.py b/cli4/cli4.py index 38b3d73..77d64be 100644 --- a/cli4/cli4.py +++ b/cli4/cli4.py @@ -1,7 +1,6 @@ #!/usr/bin/env python """Cloudflare API via command line""" -import os import sys import re import getopt @@ -11,11 +10,9 @@ except ImportError: yaml = None -import converters +from . import converters -sys.path.insert(0, os.path.abspath('..')) import CloudFlare -import CloudFlare.exceptions def dump_commands(cf): """dump a tree of all the known API commands""" diff --git a/cli4/converters.py b/cli4/converters.py index be821de..3b58ad3 100644 --- a/cli4/converters.py +++ b/cli4/converters.py @@ -1,12 +1,7 @@ -#!/usr/bin/env python """Cloudflare API via command line""" +from __future__ import absolute_import -import os -import sys - -sys.path.insert(0, os.path.abspath('..')) import CloudFlare -import CloudFlare.exceptions def convert_zones_to_identifier(cf, zone_name): """zone names to numbers""" diff --git a/setup.py b/setup.py index 5780b3e..24e66a8 100755 --- a/setup.py +++ b/setup.py @@ -1,8 +1,10 @@ #!/usr/bin/env python """Cloudflare API code - setup.py file""" - +import re from setuptools import setup, find_packages -from CloudFlare import __version__ + +_version_re = re.compile(r"__version__\s=\s'(.*)'") + def main(): """Cloudflare API code - setup.py file""" @@ -10,9 +12,12 @@ def main(): with open('README.rst') as read_me: long_description = read_me.read() + with open('CloudFlare/__init__.py', 'r') as f: + version = _version_re.search(f.read()).group(1) + setup( name='cloudflare', - version=__version__, + version=version, description='Python wrapper for the Cloudflare v4 API', long_description=long_description, author='Martin J. Levy', @@ -51,5 +56,6 @@ def main(): ] ) + if __name__ == '__main__': main()