summaryrefslogtreecommitdiff
path: root/virtinst
diff options
context:
space:
mode:
authorCole Robinson <crobinso@redhat.com>2014-12-09 08:22:51 -0500
committerCole Robinson <crobinso@redhat.com>2014-12-09 08:43:19 -0500
commitcff08223baacc938be2edd770dcddbbc2a265622 (patch)
tree779cb6baa1f51fca29a89e1a1a7d9eda6e5d7a7a /virtinst
parent81009caa54735b9ee8986cdc0b6de5b910ad6843 (diff)
downloadvirt-manager-cff08223baacc938be2edd770dcddbbc2a265622.tar.gz
util: Move uri_split to its own URISplit class
Does what uri_split did, but wraps it all up in an object that makes handling the data easier, and makes it easy to extend.
Diffstat (limited to 'virtinst')
-rw-r--r--virtinst/__init__.py1
-rw-r--r--virtinst/connection.py49
-rw-r--r--virtinst/uri.py105
-rw-r--r--virtinst/util.py35
4 files changed, 123 insertions, 67 deletions
diff --git a/virtinst/__init__.py b/virtinst/__init__.py
index e4160545..aaf6eeea 100644
--- a/virtinst/__init__.py
+++ b/virtinst/__init__.py
@@ -36,6 +36,7 @@ stable_defaults = _cliconfig.stable_defaults
from . import util
from virtinst import support
+from virtinst.uri import URISplit
from virtinst.osxml import OSXML
from virtinst.domainfeatures import DomainFeatures
diff --git a/virtinst/connection.py b/virtinst/connection.py
index 73b3494b..988b7730 100644
--- a/virtinst/connection.py
+++ b/virtinst/connection.py
@@ -26,6 +26,7 @@ from . import pollhelpers
from . import support
from . import util
from . import capabilities as CapabilitiesParser
+from . import URISplit
from .cli import VirtOptionString
from .guest import Guest
from .nodedev import NodeDevice
@@ -81,7 +82,7 @@ class VirtualConnection(object):
self._test_opts = {}
self._libvirtconn = None
- self._urisplits = util.uri_split(self._uri)
+ self._urisplits = URISplit(self._uri)
self._caps = None
self._support_cache = {}
@@ -162,7 +163,7 @@ class VirtualConnection(object):
self._libvirtconn = conn
if not self._open_uri:
self._uri = self._libvirtconn.getURI()
- self._urisplits = util.uri_split(self._uri)
+ self._urisplits = URISplit(self._uri)
def set_keep_alive(self, interval, count):
if hasattr(self._libvirtconn, "setKeepAlive"):
@@ -335,56 +336,40 @@ class VirtualConnection(object):
def is_remote(self):
return (hasattr(self, "_virtinst__fake_conn_remote") or
- self._urisplits[2])
+ self._urisplits.hostname)
def get_uri_hostname(self):
- return self._urisplits[2] or "localhost"
+ return self._urisplits.hostname or "localhost"
def get_uri_host_port(self):
- hostname = self.get_uri_hostname()
- port = None
-
- if hostname.startswith("[") and "]" in hostname:
- if "]:" in hostname:
- hostname, port = hostname.rsplit(":", 1)
- hostname = "".join(hostname[1:].split("]", 1))
- elif ":" in hostname:
- hostname, port = hostname.split(":", 1)
- return hostname, port
+ return self.get_uri_hostname(), self._urisplits.port
def get_uri_transport(self):
- scheme = self._urisplits[0]
- username = self._urisplits[1]
- offset = scheme.find("+")
- if offset != -1:
- return [scheme[offset + 1:], username]
+ if self._urisplits.transport:
+ return [self._urisplits.transport, self._urisplits.username]
return [None, None]
def get_uri_driver(self):
- scheme = self._urisplits[0]
- offset = scheme.find("+")
- if offset > 0:
- return scheme[:offset]
- return scheme
+ return self._urisplits.scheme
def is_session_uri(self):
- return self._urisplits[3] == "/session"
+ return self._urisplits.path == "/session"
def is_qemu(self):
- return self._urisplits[0].startswith("qemu")
+ return self._urisplits.scheme.startswith("qemu")
def is_qemu_system(self):
- return (self.is_qemu() and self._urisplits[3] == "/system")
+ return (self.is_qemu() and self._urisplits.path == "/system")
def is_qemu_session(self):
return (self.is_qemu() and self.is_session_uri())
def is_test(self):
- return self._urisplits[0].startswith("test")
+ return self._urisplits.scheme.startswith("test")
def is_xen(self):
- return (self._urisplits[0].startswith("xen") or
- self._urisplits[0].startswith("libxl"))
+ return (self._urisplits.scheme.startswith("xen") or
+ self._urisplits.scheme.startswith("libxl"))
def is_lxc(self):
- return self._urisplits[0].startswith("lxc")
+ return self._urisplits.scheme.startswith("lxc")
def is_openvz(self):
- return self._urisplits[0].startswith("openvz")
+ return self._urisplits.scheme.startswith("openvz")
def is_container(self):
return self.is_lxc() or self.is_openvz()
diff --git a/virtinst/uri.py b/virtinst/uri.py
new file mode 100644
index 00000000..c76f7ffb
--- /dev/null
+++ b/virtinst/uri.py
@@ -0,0 +1,105 @@
+#
+# Copyright 2014 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301 USA.
+#
+
+import re
+
+
+class URISplit(object):
+ """
+ Parse an arbitrary URI into its individual parts
+ """
+ def __init__(self, uri):
+ self.uri = uri
+
+ (self.scheme, self.username, self.hostname,
+ self.path, self.query, self.fragment) = self._split(self.uri)
+
+ self.transport = ''
+ if "+" in self.scheme:
+ self.scheme, self.transport = self.scheme.rsplit("+", 1)
+
+ self.port = ''
+ self.is_ipv6 = False
+ if self.hostname.startswith("[") and "]" in self.hostname:
+ if "]:" in self.hostname:
+ self.hostname, self.port = self.hostname.rsplit(":", 1)
+ self.hostname = "".join(self.hostname[1:].split("]", 1))
+ self.is_ipv6 = True
+ elif ":" in self.hostname:
+ self.hostname, self.port = self.hostname.split(":", 1)
+
+ self.host_is_ipv4_string = bool(re.match(self.hostname, "[0-9.]+"))
+
+
+ ###################
+ # Private helpers #
+ ###################
+
+ def _split(self, uri):
+ def splitnetloc(url, start=0):
+ for c in '/?#': # the order is important!
+ delim = url.find(c, start)
+ if delim >= 0:
+ break
+ else:
+ delim = len(url)
+ return url[start:delim], url[delim:]
+
+ username = netloc = query = fragment = ''
+ i = uri.find(":")
+ if i > 0:
+ scheme, uri = uri[:i].lower(), uri[i + 1:]
+ if uri[:2] == '//':
+ netloc, uri = splitnetloc(uri, 2)
+ offset = netloc.find("@")
+ if offset > 0:
+ username = netloc[0:offset]
+ netloc = netloc[offset + 1:]
+ if '#' in uri:
+ uri, fragment = uri.split('#', 1)
+ if '?' in uri:
+ uri, query = uri.split('?', 1)
+ else:
+ scheme = uri.lower()
+ return scheme, username, netloc, uri, query, fragment
+
+
+ ##############
+ # Public API #
+ ##############
+
+ def rebuild_uri(self):
+ ret = self.scheme
+ if self.transport:
+ ret += "+" % self.transport
+ ret += "://"
+ if self.username:
+ ret += self.username + "@"
+ if self.hostname:
+ host = self.hostname
+ if self.is_ipv6:
+ host = "[%s]" % self.hostname
+ ret += host
+ if self.port:
+ ret += ":" + self.port
+ if self.query:
+ ret += "?" + self.query
+ if self.fragment:
+ ret += "#" + self.fragment
+ return ret
diff --git a/virtinst/util.py b/virtinst/util.py
index a29d730a..54e09e9d 100644
--- a/virtinst/util.py
+++ b/virtinst/util.py
@@ -365,41 +365,6 @@ def xml_escape(xml):
return xml
-def uri_split(uri):
- """
- Parse a libvirt hypervisor uri into it's individual parts
- @returns: tuple of the form (scheme (ex. 'qemu', 'xen+ssh'), username,
- hostname, path (ex. '/system'), query,
- fragment)
- """
- def splitnetloc(url, start=0):
- for c in '/?#': # the order is important!
- delim = url.find(c, start)
- if delim >= 0:
- break
- else:
- delim = len(url)
- return url[start:delim], url[delim:]
-
- username = netloc = query = fragment = ''
- i = uri.find(":")
- if i > 0:
- scheme, uri = uri[:i].lower(), uri[i + 1:]
- if uri[:2] == '//':
- netloc, uri = splitnetloc(uri, 2)
- offset = netloc.find("@")
- if offset > 0:
- username = netloc[0:offset]
- netloc = netloc[offset + 1:]
- if '#' in uri:
- uri, fragment = uri.split('#', 1)
- if '?' in uri:
- uri, query = uri.split('?', 1)
- else:
- scheme = uri.lower()
- return scheme, username, netloc, uri, query, fragment
-
-
def is_error_nosupport(err):
"""
Check if passed exception indicates that the called libvirt command isn't