Reviewed-by: Aline Manera <alinefm(a)linux.vnet.ibm.com>
On 01/15/2014 12:05 AM, CrÃstian Viana wrote:
In order to make the code less ambiguous and more consistent, only
use
import aliases (i.e. 'import ... as ...') if it is needed to solve a
conflict in the imported class names. For example, if there is a class
named 'kimchi.model.file', the following import statement would lead to
an anbiguity when trying to use the type 'file' (because of the built-in
type 'file'):
from kimchi.model import file
One of the solutions to this conflict would be to use an alias, e.g.:
from kimchi.model import file as kfile
Unless it is necessary, do not use import aliases.
Signed-off-by: CrÃstian Viana <vianac(a)linux.vnet.ibm.com>
---
src/kimchi/model.py | 6 +++---
src/kimchi/networkxml.py | 4 ++--
tests/test_networkxml.py | 28 +++++++++++++++-------------
3 files changed, 20 insertions(+), 18 deletions(-)
diff --git a/src/kimchi/model.py b/src/kimchi/model.py
index f64a5fd..93a2764 100644
--- a/src/kimchi/model.py
+++ b/src/kimchi/model.py
@@ -47,7 +47,7 @@ from cherrypy.process.plugins import BackgroundTask
from kimchi import config
from kimchi import netinfo
-from kimchi import network as knetwork
+from kimchi import network
from kimchi import networkxml
from kimchi import vnc
from kimchi import xmlutils
@@ -787,7 +787,7 @@ class Model(object):
xml = network.XMLDesc(0)
subnet = self._get_network_from_xml(xml)['subnet']
subnet and net_addrs.append(ipaddr.IPNetwork(subnet))
- netaddr = knetwork.get_one_free_network(net_addrs)
+ netaddr = network.get_one_free_network(net_addrs)
if not netaddr:
raise OperationFailed("can not find a free IP address "
"for network '%s'" %
@@ -882,7 +882,7 @@ class Model(object):
# macvtap bridge
interface = interface or forward['interface'][0]
# exposing the network on linux bridge or macvtap interface
- interface_subnet = knetwork.get_dev_netaddr(interface)
+ interface_subnet = network.get_dev_netaddr(interface)
subnet = subnet if subnet else interface_subnet
# libvirt use format 192.168.0.1/24, standard should be 192.168.0.0/24
diff --git a/src/kimchi/networkxml.py b/src/kimchi/networkxml.py
index 61a9ba0..2fe674b 100644
--- a/src/kimchi/networkxml.py
+++ b/src/kimchi/networkxml.py
@@ -21,7 +21,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import ipaddr
-import lxml.etree as ET
+import lxml.etree
from lxml.builder import E
@@ -125,4 +125,4 @@ def create_vlan_tagged_bridge_xml(bridge, interface, vlan_id):
name='.'.join([interface, vlan_id]))),
type='bridge',
name=bridge)
- return ET.tostring(m)
+ return lxml.etree.tostring(m)
diff --git a/tests/test_networkxml.py b/tests/test_networkxml.py
index fc97f6a..445c36d 100644
--- a/tests/test_networkxml.py
+++ b/tests/test_networkxml.py
@@ -24,7 +24,7 @@ import ipaddr
import unittest
-import kimchi.networkxml as nxml
+import kimchi.networkxml
import utils
from kimchi.xmlutils import xpath_get_text
@@ -43,18 +43,18 @@ class NetworkXmlTests(unittest.TestCase):
"ip": "192.168.122.11"}
params = {}
- xml = nxml._get_dhcp_xml(**params)
+ xml = kimchi.networkxml._get_dhcp_xml(**params)
self.assertEquals("", xml)
params["range"] = dhcp_range
- xml = nxml._get_dhcp_xml(**params)
+ xml = kimchi.networkxml._get_dhcp_xml(**params)
start = xpath_get_text(xml, "/dhcp/range/@start")
end = xpath_get_text(xml, "/dhcp/range/@end")
self.assertEquals(dhcp_range['start'], start[0])
self.assertEquals(dhcp_range['end'], end[0])
params["hosts"] = [host1, host2]
- xml = nxml._get_dhcp_xml(**params)
+ xml = kimchi.networkxml._get_dhcp_xml(**params)
ip = xpath_get_text(xml, "/dhcp/host/@ip")
self.assertEquals(ip, [host1['ip'], host2['ip']])
@@ -65,12 +65,12 @@ class NetworkXmlTests(unittest.TestCase):
dhcp_range = {"start": "192.168.122.100", "end":
"192.168.122.254"}
params = {}
- xml = nxml._get_dhcp_xml(**params)
+ xml = kimchi.networkxml._get_dhcp_xml(**params)
self.assertEquals("", xml)
params["net"] = "192.168.122.0/255.255.255.0"
params["dhcp"] = {'range': dhcp_range}
- xml = nxml._get_ip_xml(**params)
+ xml = kimchi.networkxml._get_ip_xml(**params)
start = xpath_get_text(xml, "/ip/dhcp/range/@start")[0]
end = xpath_get_text(xml, "/ip/dhcp/range/@end")[0]
self.assertEquals(dhcp_range['start'], start)
@@ -84,7 +84,7 @@ class NetworkXmlTests(unittest.TestCase):
# test _get_ip_xml can accepts strings: '192.168.122.0/24',
# which is same as "192.168.122.0/255.255.255.0"
params["net"] = "192.168.122.0/24"
- xml = nxml._get_ip_xml(**params)
+ xml = kimchi.networkxml._get_ip_xml(**params)
netmask = xpath_get_text(xml, "/ip/@netmask")[0]
self.assertEquals(netmask,
str(ipaddr.IPNetwork(params["net"]).netmask))
@@ -95,12 +95,12 @@ class NetworkXmlTests(unittest.TestCase):
"""
params = {"mode": None}
- xml = nxml._get_forward_xml(**params)
+ xml = kimchi.networkxml._get_forward_xml(**params)
self.assertEquals("", xml)
params["mode"] = 'nat'
params["dev"] = 'eth0'
- xml = nxml._get_forward_xml(**params)
+ xml = kimchi.networkxml._get_forward_xml(**params)
mode = xpath_get_text(xml, "/forward/@mode")[0]
dev = xpath_get_text(xml, "/forward/@dev")[0]
self.assertEquals(params['mode'], mode)
@@ -113,7 +113,7 @@ class NetworkXmlTests(unittest.TestCase):
params = {"name": "test",
"forward": {"mode": "nat",
"dev": ""},
"net": "192.168.0.0/255.255.255.0"}
- xml = nxml.to_network_xml(**params)
+ xml = kimchi.networkxml.to_network_xml(**params)
name = xpath_get_text(xml, "/network/name")[0]
self.assertEquals(name, params['name'])
@@ -136,7 +136,7 @@ class NetworkXmlTests(unittest.TestCase):
params['forward']['dev'] = "eth0"
params['dhcp'] = {"range": {'start':
'192.168.0.1',
'end': '192.168.0.254'}}
- xml = nxml.to_network_xml(**params)
+ xml = kimchi.networkxml.to_network_xml(**params)
forward_dev = xpath_get_text(xml, "/network/forward/@dev")[0]
self.assertEquals(forward_dev, params['forward']['dev'])
@@ -148,7 +148,7 @@ class NetworkXmlTests(unittest.TestCase):
# test _get_ip_xml can accepts strings: '192.168.122.0/24',
# which is same as "192.168.122.0/255.255.255.0"
params["net"] = "192.168.0.0/24"
- xml = nxml.to_network_xml(**params)
+ xml = kimchi.networkxml.to_network_xml(**params)
netmask = xpath_get_text(xml, "/network/ip/@netmask")[0]
self.assertEquals(netmask,
str(ipaddr.IPNetwork(params["net"]).netmask))
@@ -169,5 +169,7 @@ class InterfaceXmlTests(unittest.TestCase):
</bridge>
</interface>
"""
- actual_xml = nxml.create_vlan_tagged_bridge_xml('br10', 'em1',
'10')
+ actual_xml = kimchi.networkxml.create_vlan_tagged_bridge_xml('br10',
+ 'em1',
+ '10')
self.assertEquals(actual_xml, utils.normalize_xml(expected_xml))