
On 01/15/2014 12:05 AM, CrÃstian Viana wrote:
According to the PEP8 Style Guide[1], imports should be grouped in the following order:
1) standard library imports 2) related third party imports 3) local application/library specific imports
For Kimchi, that means:
1) standard Python libraries 2) CherryPy, Cheetah, Libvirt, etc. 3) util, kimchi.*, everything developed by our team
Conditional/exceptional imports should be moved to the end of their corresponding groups.
And according to some examples from the Style Guide, the "import ..." lines should come before the "from .... import" lines inside the same group.
Here is an example:
""" import os import sys from glob import iglob
try: from collections import OrderedDict except ImportError: from ordereddict import OrderedDict
import cherrypy
import kimchi.model from kimchi.exception import NotFoundError
if something(): import kimchi.control """
[1]: http://www.python.org/dev/peps/pep-0008/#imports
Signed-off-by: CrÃstian Viana <vianac@linux.vnet.ibm.com> --- src/kimchi/asynctask.py | 4 +++- src/kimchi/auth.py | 4 +++- src/kimchi/control/base.py | 4 +++- src/kimchi/control/utils.py | 2 +- src/kimchi/featuretests.py | 5 +++-- src/kimchi/kvmusertests.py | 4 +++- src/kimchi/mockmodel.py | 3 ++- src/kimchi/model.py | 10 +++++----- src/kimchi/networkxml.py | 2 -- src/kimchi/osinfo.py | 2 -- src/kimchi/root.py | 4 +++- src/kimchi/server.py | 5 ++++- src/kimchi/template.py | 2 +- src/kimchi/utils.py | 2 +- src/kimchi/xmlutils.py | 2 -- tests/test_mockmodel.py | 6 ++++-- tests/test_networkxml.py | 2 -- tests/test_plugin.py | 2 -- tests/test_rest.py | 2 -- tests/test_server.py | 2 +- tests/utils.py | 6 +++--- 21 files changed, 40 insertions(+), 35 deletions(-)
diff --git a/src/kimchi/asynctask.py b/src/kimchi/asynctask.py index 4ff76e4..3b7d505 100644 --- a/src/kimchi/asynctask.py +++ b/src/kimchi/asynctask.py @@ -20,11 +20,13 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-import cherrypy import threading import traceback
+import cherrypy + + from kimchi.exception import OperationFailed
diff --git a/src/kimchi/auth.py b/src/kimchi/auth.py index 242fdcf..d0a1318 100644 --- a/src/kimchi/auth.py +++ b/src/kimchi/auth.py @@ -23,11 +23,13 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import base64 -import cherrypy import PAM import re
PAM is also a third-party module. It comes in PyPAM/python-pam package
+import cherrypy + + from kimchi import template from kimchi.exception import OperationFailed
diff --git a/src/kimchi/control/base.py b/src/kimchi/control/base.py index 185c8d8..031bf59 100644 --- a/src/kimchi/control/base.py +++ b/src/kimchi/control/base.py @@ -22,10 +22,12 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-import cherrypy import urllib2
+import cherrypy + + import kimchi.template from kimchi.control.utils import get_class_name, internal_redirect, model_fn from kimchi.control.utils import parse_request, validate_method diff --git a/src/kimchi/control/utils.py b/src/kimchi/control/utils.py index c3c5f8e..894f35b 100644 --- a/src/kimchi/control/utils.py +++ b/src/kimchi/control/utils.py @@ -23,10 +23,10 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #
-import cherrypy import json
+import cherrypy from jsonschema import Draft3Validator, ValidationError
diff --git a/src/kimchi/featuretests.py b/src/kimchi/featuretests.py index a5755a2..e8d654d 100644 --- a/src/kimchi/featuretests.py +++ b/src/kimchi/featuretests.py @@ -20,13 +20,14 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-import cherrypy -import libvirt import os import subprocess import threading
+import cherrypy +import libvirt + from kimchi import config
diff --git a/src/kimchi/kvmusertests.py b/src/kimchi/kvmusertests.py index 3d69eb4..82e3862 100644 --- a/src/kimchi/kvmusertests.py +++ b/src/kimchi/kvmusertests.py @@ -20,11 +20,13 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-import libvirt
import psutil
psutil is also a third-party module. It comes in python-psutil package
import uuid
+import libvirt + + from kimchi.rollbackcontext import RollbackContext
diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py index 1f5178f..494a757 100644 --- a/src/kimchi/mockmodel.py +++ b/src/kimchi/mockmodel.py @@ -20,7 +20,6 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-import cherrypy import copy
import disks
disks is a kimchi module. So it should be "from kimchi import disks"
import glob @@ -41,6 +40,8 @@ except ImportError: import ImageDraw
+import cherrypy + import kimchi.model from kimchi import config from kimchi import network as knetwork diff --git a/src/kimchi/model.py b/src/kimchi/model.py index 671af02..0125a93 100644 --- a/src/kimchi/model.py +++ b/src/kimchi/model.py @@ -20,7 +20,6 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-import cherrypy import copy import disks
disks is a kimchi module. So it should be "from kimchi import disks"
import fnmatch @@ -40,10 +39,6 @@ import sys import threading import time import uuid - - -from cherrypy.process.plugins import BackgroundTask -from cherrypy.process.plugins import SimplePlugin from collections import defaultdict from xml.etree import ElementTree
@@ -54,6 +49,11 @@ except ImportError: from ordereddict import OrderedDict
+import cherrypy +from cherrypy.process.plugins import BackgroundTask +from cherrypy.process.plugins import SimplePlugin + + from kimchi import config from kimchi import netinfo from kimchi import network as knetwork diff --git a/src/kimchi/networkxml.py b/src/kimchi/networkxml.py index 63cb210..61a9ba0 100644 --- a/src/kimchi/networkxml.py +++ b/src/kimchi/networkxml.py @@ -22,8 +22,6 @@
import ipaddr import lxml.etree as ET - - from lxml.builder import E
diff --git a/src/kimchi/osinfo.py b/src/kimchi/osinfo.py index 0509622..5ed5186 100644 --- a/src/kimchi/osinfo.py +++ b/src/kimchi/osinfo.py @@ -22,8 +22,6 @@
import copy import os - - from distutils.version import LooseVersion
diff --git a/src/kimchi/root.py b/src/kimchi/root.py index 3cc6321..ae06bc3 100644 --- a/src/kimchi/root.py +++ b/src/kimchi/root.py @@ -21,10 +21,12 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-import cherrypy import json
+import cherrypy + + from kimchi import auth from kimchi import template from kimchi.config import get_api_schema_file diff --git a/src/kimchi/server.py b/src/kimchi/server.py index b820263..53fe889 100644 --- a/src/kimchi/server.py +++ b/src/kimchi/server.py @@ -22,12 +22,15 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #
-import cherrypy import logging import logging.handlers import os import sslcert
sscert is a kimchi module. So it should be "from kimchi import sslcert"
+ +import cherrypy + + from kimchi import auth from kimchi import config from kimchi import model diff --git a/src/kimchi/template.py b/src/kimchi/template.py index 1f19c4a..3f94d24 100644 --- a/src/kimchi/template.py +++ b/src/kimchi/template.py @@ -21,12 +21,12 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-import cherrypy import errno import json import os
+import cherrypy from Cheetah.Template import Template
diff --git a/src/kimchi/utils.py b/src/kimchi/utils.py index af245c6..ac7dee5 100644 --- a/src/kimchi/utils.py +++ b/src/kimchi/utils.py @@ -21,11 +21,11 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #
-import cherrypy import os import urllib2
+import cherrypy from cherrypy.lib.reprconf import Parser
diff --git a/src/kimchi/xmlutils.py b/src/kimchi/xmlutils.py index 51ff0ec..e368e9d 100644 --- a/src/kimchi/xmlutils.py +++ b/src/kimchi/xmlutils.py @@ -21,8 +21,6 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import libxml2 - - from xml.etree import ElementTree
diff --git a/tests/test_mockmodel.py b/tests/test_mockmodel.py index f167dc6..3e0701f 100644 --- a/tests/test_mockmodel.py +++ b/tests/test_mockmodel.py @@ -20,16 +20,18 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-import cherrypy import json import os import time import unittest
+import cherrypy + + import kimchi.mockmodel -from utils import get_free_port, patch_auth, request, run_server from kimchi.control.base import Collection, Resource +from utils import get_free_port, patch_auth, request, run_server
test_server = None diff --git a/tests/test_networkxml.py b/tests/test_networkxml.py index 42b3ea9..fc97f6a 100644 --- a/tests/test_networkxml.py +++ b/tests/test_networkxml.py @@ -26,8 +26,6 @@ import unittest
import kimchi.networkxml as nxml import utils - - from kimchi.xmlutils import xpath_get_text
diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 42c87a9..f12b11f 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -24,8 +24,6 @@ import json import os import sys import unittest - - from functools import partial
diff --git a/tests/test_rest.py b/tests/test_rest.py index 8732781..89bc616 100644 --- a/tests/test_rest.py +++ b/tests/test_rest.py @@ -25,8 +25,6 @@ import json import os import time import unittest - - from functools import partial
diff --git a/tests/test_server.py b/tests/test_server.py index 734a618..adbf770 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -25,8 +25,8 @@ import os import unittest
-import utils import kimchi.mockmodel +import utils
#utils.silence_server()
diff --git a/tests/utils.py b/tests/utils.py index 79fc2e2..452a001 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -22,19 +22,19 @@ #
import base64 -import cherrypy import httplib import os import socket import sys import threading import unittest - - from contextlib import closing from lxml import etree
+import cherrypy + + import kimchi.server import kimchi.model