[Kimchi-devel] [PATCH] Organize python imports

Aline Manera alinefm at linux.vnet.ibm.com
Fri Dec 20 14:28:10 UTC 2013


On 12/19/2013 03:44 PM, Rodrigo Trujillo wrote:
> Follow this rule:
> 1) Import common modules
> import ...
> import ...
> from ... import ...
> from ... import ...
>
> 2) Import kimchi modules
> import kimchi.<mod>
> import kimchi.<mod>
> from kimchi import ...
> from kimchi import ...
>
> Signed-off-by: Rodrigo Trujillo <rodrigo.trujillo at linux.vnet.ibm.com>
> ---
>   plugins/sample/__init__.py |  8 ++++++--
>   plugins/sample/model.py    |  2 +-
>   src/kimchi/model.py        |  4 ++--
>   src/kimchi/server.py       |  2 +-
>   src/kimchi/sslcert.py      |  2 +-
>   src/kimchi/websocket.py    | 17 ++++++++++++++---
>   src/kimchi/websockify.py   | 15 +++++++++++++--
>   tests/test_exception.py    |  6 ++++--
>   tests/test_mockmodel.py    |  5 +++--
>   tests/test_model.py        | 18 ++++++++++--------
>   tests/test_networkxml.py   |  4 +++-
>   tests/test_osinfo.py       |  3 +++
>   tests/test_plugin.py       |  4 +++-
>   tests/test_rest.py         | 10 +++++++---
>   tests/test_server.py       |  4 ++--
>   tests/test_vmtemplate.py   |  2 ++
>   tests/utils.py             | 15 +++++++++------
>   17 files changed, 84 insertions(+), 37 deletions(-)
>
> diff --git a/plugins/sample/__init__.py b/plugins/sample/__init__.py
> index a20f5e6..7064904 100644
> --- a/plugins/sample/__init__.py
> +++ b/plugins/sample/__init__.py
> @@ -22,12 +22,16 @@
>
>   import json
>   import os
> +
> +
>   from cherrypy import expose
> -from kimchi.controller import Resource, Collection
> +
> +
> +from kimchi.controller import Collection, Resource
>   from model import Model
>
> -model = Model()
>
> +model = Model()
>
>   class Drawings(Resource):
>       def __init__(self):
> diff --git a/plugins/sample/model.py b/plugins/sample/model.py
> index f6da5d0..9a2f22f 100644
> --- a/plugins/sample/model.py
> +++ b/plugins/sample/model.py
> @@ -20,7 +20,7 @@
>   # License along with this library; if not, write to the Free Software
>   # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
>
> -from kimchi.exception import NotFoundError, InvalidOperation
> +from kimchi.exception import InvalidOperation, NotFoundError
>
>
>   class Model(object):
> diff --git a/src/kimchi/model.py b/src/kimchi/model.py
> index 73c18ac..1e4dac2 100644
> --- a/src/kimchi/model.py
> +++ b/src/kimchi/model.py
> @@ -42,9 +42,9 @@ import time
>   import uuid
>
>
> -from collections import defaultdict
>   from cherrypy.process.plugins import BackgroundTask
>   from cherrypy.process.plugins import SimplePlugin
> +from collections import defaultdict
>   from xml.etree import ElementTree
>
>
> @@ -69,7 +69,7 @@ from kimchi.networkxml import to_network_xml
>   from kimchi.objectstore import ObjectStore
>   from kimchi.scan import Scanner
>   from kimchi.screenshot import VMScreenshot
> -from kimchi.utils import kimchi_log, is_digit, get_enabled_plugins
> +from kimchi.utils import get_enabled_plugins, is_digit, kimchi_log
>   from kimchi.vmtemplate import VMTemplate
>
>
> diff --git a/src/kimchi/server.py b/src/kimchi/server.py
> index 6ff6fa0..114a3a0 100644
> --- a/src/kimchi/server.py
> +++ b/src/kimchi/server.py
> @@ -33,7 +33,7 @@ from kimchi import config
>   from kimchi import model
>   from kimchi import mockmodel
>   from kimchi.root import Root
> -from kimchi.utils import import_class, get_enabled_plugins
> +from kimchi.utils import get_enabled_plugins, import_class
>
>
>   LOGGING_LEVEL = {"debug": logging.DEBUG,
> diff --git a/src/kimchi/sslcert.py b/src/kimchi/sslcert.py
> index 70441f2..529699d 100644
> --- a/src/kimchi/sslcert.py
> +++ b/src/kimchi/sslcert.py
> @@ -28,7 +28,7 @@
>   import time
>
>
> -from M2Crypto import X509, EVP, RSA, ASN1
> +from M2Crypto import ASN1, EVP, RSA, X509
>

>   class SSLCert(object):
> diff --git a/src/kimchi/websocket.py b/src/kimchi/websocket.py
> index a98fc6d..945676d 100644
> --- a/src/kimchi/websocket.py
> +++ b/src/kimchi/websocket.py

This file (src/kimchi/websocket.py) was imported into kimchi. You should 
keep it as it is.

> @@ -16,9 +16,20 @@ as taken from http://docs.python.org/dev/library/ssl.html#certificates
>
>   '''
>
> -import os, sys, time, errno, signal, socket, traceback, select
> -import array, struct
> -from base64 import b64encode, b64decode
> +import array
> +import errno
> +import os
> +import select
> +import signal
> +import socket
> +import struct
> +import sys
> +import time
> +import traceback
> +
> +
> +from base64 import b64decode, b64encode
> +
>
>   # Imports that vary by python version

> diff --git a/src/kimchi/websockify.py b/src/kimchi/websockify.py
> index 1154d92..2857e7c 100755
> --- a/src/kimchi/websockify.py
> +++ b/src/kimchi/websockify.py
> @@ -11,15 +11,26 @@ as taken from http://docs.python.org/dev/library/ssl.html#certificates
>
>   '''

Same here.

src/kimchi/websockify.py was imported too. Keep it as it is.

> -import signal, socket, optparse, time, os, sys, subprocess
> +import optparse
> +import os
> +import signal
> +import socket
> +import subprocess
> +import sys
> +import time
> +
> +
>   from select import select
> -import websocket
>   try:
>       from urllib.parse import parse_qs, urlparse
>   except:
>       from cgi import parse_qs
>       from urlparse import urlparse
>
> +
> +import websocket
> +
> +
>   class WebSocketProxy(websocket.WebSocketServer):
>       """
>       Proxy traffic to and from a WebSockets client to a normal TCP
> diff --git a/tests/test_exception.py b/tests/test_exception.py
> index 9b5355a..cb60995 100644
> --- a/tests/test_exception.py
> +++ b/tests/test_exception.py
> @@ -20,14 +20,16 @@
>   # 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 unittest
> -import os
>   import json
> +import os
> +import unittest
> +
>
>   import kimchi.mockmodel
>   import kimchi.server
>   from utils import *

Replace * to the methods used.

>
> +
>   test_server = None
>   model = None
>   host = None
> diff --git a/tests/test_mockmodel.py b/tests/test_mockmodel.py
> index b819172..0830a24 100644
> --- a/tests/test_mockmodel.py
> +++ b/tests/test_mockmodel.py
> @@ -20,16 +20,17 @@
>   # 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 unittest
>   import cherrypy
>   import json
>   import os
> +import unittest
> +
>
>   import kimchi.mockmodel
>   import kimchi.controller
> -
>   from utils import *

Same here

> +
>   #utils.silence_server()
>   test_server = None
>   model = None
> diff --git a/tests/test_model.py b/tests/test_model.py
> index fb7d6dd..07b68b1 100644
> --- a/tests/test_model.py
> +++ b/tests/test_model.py
> @@ -21,21 +21,23 @@
>   # 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 unittest
> -import threading
>   import os
> -import time
> -import tempfile
> -import psutil
>   import platform
> +import psutil
> +import tempfile
> +import threading
> +import time
> +import unittest
>   import uuid

> +
> +import iso_gen
>   import kimchi.model
>   import kimchi.objectstore
> -from kimchi.exception import *
> -from kimchi import netinfo
>   import utils
> -import iso_gen
> +from kimchi import netinfo
> +from kimchi.exception import *

And here.

> +
>
>   class ModelTests(unittest.TestCase):
>       def setUp(self):
> diff --git a/tests/test_networkxml.py b/tests/test_networkxml.py
> index 4eeeaa2..3073bce 100644
> --- a/tests/test_networkxml.py
> +++ b/tests/test_networkxml.py
> @@ -20,10 +20,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 ipaddr
>   import unittest
> +
> +
>   import kimchi.networkxml as nxml
>   from kimchi.xmlutils import xpath_get_text
> -import ipaddr
>
>
>   class NetworkXmlTests(unittest.TestCase):
> diff --git a/tests/test_osinfo.py b/tests/test_osinfo.py
> index f92567d..1dcfdaf 100644
> --- a/tests/test_osinfo.py
> +++ b/tests/test_osinfo.py
> @@ -21,8 +21,11 @@
>   # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
>
>   import unittest
> +
> +
>   from kimchi.osinfo import *

Here too.

> +
>   class OSInfoTests(unittest.TestCase):
>       def test_default_lookup(self):
>           name, entry = lookup(None, None)
> diff --git a/tests/test_plugin.py b/tests/test_plugin.py
> index 20cc598..f12b11f 100644
> --- a/tests/test_plugin.py
> +++ b/tests/test_plugin.py
> @@ -20,17 +20,19 @@
>   # 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 unittest
>   import json
>   import os
>   import sys
> +import unittest

2 lines here.

>   from functools import partial
>
> +
>   import kimchi.mockmodel
>   import kimchi.server
>   import utils
>   from kimchi import config
>
> +
>   test_server = None
>   model = None
>   host = None
> diff --git a/tests/test_rest.py b/tests/test_rest.py
> index 73b5243..61369c3 100644
> --- a/tests/test_rest.py
> +++ b/tests/test_rest.py
> @@ -20,16 +20,20 @@
>   # 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 unittest
>   import json
> -import time
>   import os
> +import time
> +import unittest
> +
> +
>   from functools import partial
>
> +
>   import kimchi.mockmodel
>   import kimchi.server
> -from utils import *
>   from kimchi.asynctask import AsyncTask
> +from utils import *
> +

Replace * to methods used

>   test_server = None
>   model = None
> diff --git a/tests/test_server.py b/tests/test_server.py
> index 9bb0034..734a618 100644
> --- a/tests/test_server.py
> +++ b/tests/test_server.py
> @@ -20,12 +20,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 unittest
>   import json
>   import os
> +import unittest
>
> -import utils
>
> +import utils
>   import kimchi.mockmodel
>
>   #utils.silence_server()
> diff --git a/tests/test_vmtemplate.py b/tests/test_vmtemplate.py
> index 81382c7..92c7385 100644
> --- a/tests/test_vmtemplate.py
> +++ b/tests/test_vmtemplate.py
> @@ -23,9 +23,11 @@
>   import unittest
>   import uuid
>
> +
>   from kimchi.vmtemplate import *

Here too

>   from kimchi.xmlutils import xpath_get_text
>
> +
>   class VMTemplateTests(unittest.TestCase):
>       def test_minimal_construct(self):
>           fields = (('name', 'test'), ('os_distro', 'unknown'),
> diff --git a/tests/utils.py b/tests/utils.py
> index c114813..a7596e8 100644
> --- a/tests/utils.py
> +++ b/tests/utils.py
> @@ -21,16 +21,19 @@
>   # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
>   #
>
> -import httplib
> +import base64
>   import cherrypy
> -import threading
> -import time
> +import httplib
>   import os
> -import sys
>   import socket
> -from contextlib import closing
> +import sys
> +import threading
> +import time
>   import unittest
> -import base64
> +
> +
> +from contextlib import closing
> +
>
>   import kimchi.server
>   import kimchi.model




More information about the Kimchi-devel mailing list