2121import os
2222import pycurl
2323import re
24+ import six
2425import sys
2526import threading
2627
3031 from urllib import urlencode
3132 from urlparse import urlparse
3233
34+ from ovirtsdk4 import version
3335from ovirtsdk4 .http import Response
3436
3537
36- def get_version ():
37- from ovirtsdk4 import version
38- return version .VERSION
39-
40-
4138class Error (Exception ):
4239 """
4340 General exception which is thrown by SDK,
@@ -47,22 +44,20 @@ class Error(Exception):
4744 def __init__ (self , message , code = None , fault = None ):
4845 """
4946 Creates an instance of Error class.
50- """
51- super (Error , self ).__init__ (message )
52- """
53- Creates an instance of Error class.
54- """
55- self .code = code
56- """
57- An error code associated to the error. For HTTP related
47+
48+ `message`:: The exception message.
49+
50+ `code`:: An error code associated to the error. For HTTP related
5851 errors, this will be the HTTP response code returned by the server.
5952 For example, if retrieving of a virtual machine fails because it
6053 doesn't exist this attribute will contain the integer value 404. Note
61- that this may be `nil` if the error is not HTTP related."""
62- self .fault = fault
63- """
64- The `Fault` object associated to the error.
54+ that this may be `nil` if the error is not HTTP related.
55+
56+ `fault`:: The `Fault` object associated to the error.
6557 """
58+ super (Error , self ).__init__ (message )
59+ self .code = code
60+ self .fault = fault
6661
6762
6863class AuthError (Error ):
@@ -235,87 +230,70 @@ def __init__(
235230
236231 This method supports the following parameters:
237232
238- `url` \n
239- A string containing the base URL of the server, usually
233+ `url`:: A string containing the base URL of the server, usually
240234 something like `https://server.example.com/ovirt-engine/api`.
241235
242- `username` \n
243- The name of the user, something like `admin@internal`.
236+ `username`:: The name of the user, something like `admin@internal`.
244237
245- `password` \n
246- The name password of the user.
238+ `password`:: The name password of the user.
247239
248- `token` \n
249- The token to be used to access API. Optionally, user can
240+ `token`:: : The token to be used to access API. Optionally, user can
250241 use token, instead of username and password to access API. If user
251242 don't specify `token` parameter, SDK will automatically create one.
252243
253- `insecure` \n
254- A boolean flag that indicates if the server TLS
244+ `insecure`:: A boolean flag that indicates if the server TLS
255245 certificate and host name should be checked.
256246
257- `ca_file` \n
258- A PEM file containing the trusted CA certificates. The
247+ `ca_file`:: A PEM file containing the trusted CA certificates. The
259248 certificate presented by the server will be verified using these CA
260249 certificates. If `ca_file` parameter is not set, system wide
261250 CA certificate store is used.
262251
263- `debug` \n
264- A boolean flag indicating if debug output should be
252+ `debug`:: A boolean flag indicating if debug output should be
265253 generated. If the value is `True` and the `log` parameter isn't
266254 `None` then the data sent to and received from the server will
267255 be written to the log. Be aware that user names and passwords will
268256 also be written, so handle it with care.
269257
270- `log` \n
271- The logger where the log messages will be written.
258+ `log`:: The logger where the log messages will be written.
272259
273- `kerberos` \n
274- A boolean flag indicating if Kerberos
260+ `kerberos`:: A boolean flag indicating if Kerberos
275261 authentication should be used instead of the default basic
276262 authentication.
277263
278- `timeout` \n
279- The maximum total time to wait for the response, in
264+ `timeout`:: The maximum total time to wait for the response, in
280265 seconds. A value of zero (the default) means wait for ever. If
281266 the timeout expires before the response is received an exception
282267 will be raised.
283268
284- `compress` \n
285- A boolean flag indicating if the SDK should ask
269+ `compress`:: A boolean flag indicating if the SDK should ask
286270 the server to send compressed responses. The default is `True`.
287271 Note that this is a hint for the server, and that it may return
288272 uncompressed data even when this parameter is set to `True`.
289273 Note that compression will be disabled if user pass `debug`
290274 parameter set to `true`, so the debug messages are in plain text.
291275
292- `sso_url` \n
293- A string containing the base SSO URL of the serve.
276+ `sso_url`:: A string containing the base SSO URL of the serve.
294277 Default SSO url is computed from the `url` if no `sso_url` is provided.
295278
296- `sso_revoke_url` \n
297- A string containing the base URL of the SSO
279+ `sso_revoke_url`:: A string containing the base URL of the SSO
298280 revoke service. This needs to be specified only when using
299281 an external authentication service. By default this URL
300282 is automatically calculated from the value of the `url` parameter,
301283 so that SSO token revoke will be performed using the SSO service
302284 that is part of the engine.
303285
304- `sso_token_name` \n
305- The token name in the JSON SSO response returned
286+ `sso_token_name`:: The token name in the JSON SSO response returned
306287 from the SSO server. Default value is `access_token`.
307288
308- `headers` \n
309- A dictionary with headers which should be send with every
289+ `headers`:: A dictionary with headers which should be send with every
310290 request.
311291
312- `connections` \n
313- The maximum number of connections to open to the host.
292+ `connections`:: The maximum number of connections to open to the host.
314293 If the value is `0` (the default) then the number of connections will
315294 be unlimited.
316295
317- `pipeline` \n
318- The maximum number of request to put in an HTTP pipeline
296+ `pipeline`:: The maximum number of request to put in an HTTP pipeline
319297 without waiting for the response. If the value is `0` (the default)
320298 then pipelining is disabled.
321299 """
@@ -382,8 +360,7 @@ def send(self, request):
382360
383361 This method supports the following parameters.
384362
385- `request` \n
386- The Request object containing the details of the HTTP
363+ `request`:: The Request object containing the details of the HTTP
387364 request to send.
388365
389366 The returned value is a Request object containing the details of the
@@ -468,7 +445,7 @@ def __send(self, request):
468445 for header_name , header_value in headers_dict .items ():
469446 header_lines .append ('%s: %s' % (header_name , header_value ))
470447
471- header_lines .append ('User-Agent: PythonSDK/%s' % get_version () )
448+ header_lines .append ('User-Agent: PythonSDK/%s' % version . VERSION )
472449 header_lines .append ('Version: 4' )
473450 header_lines .append ('Content-Type: application/xml' )
474451 header_lines .append ('Accept: application/xml' )
@@ -691,7 +668,7 @@ def _get_sso_response(self, url, params=''):
691668
692669 # Prepare headers:
693670 header_lines = [
694- 'User-Agent: PythonSDK/%s' % get_version () ,
671+ 'User-Agent: PythonSDK/%s' % version . VERSION ,
695672 'Accept: application/json'
696673 ]
697674 curl .setopt (pycurl .HTTPHEADER , header_lines )
@@ -765,7 +742,7 @@ def test(self, raise_exception=False):
765742 return True
766743 except Error :
767744 if raise_exception :
768- raise
745+ six . reraise ( * sys . exc_info ())
769746 return False
770747 except Exception as exception :
771748 if raise_exception :
@@ -819,7 +796,7 @@ def close(self, logout=True):
819796 """
820797 Releases the resources used by this connection.
821798
822- `logout` A boolean, which specify if token should be revoked,
799+ `logout`:: A boolean, which specify if token should be revoked,
823800 and so user should be logged out.
824801 """
825802
@@ -841,10 +818,10 @@ def _build_url(self, path='', query=None):
841818
842819 This method supports the following parameters:
843820
844- `path` The path that will be added to the base URL. The default is an
821+ `path`:: The path that will be added to the base URL. The default is an
845822 empty string.
846823
847- `query` A dictionary containing the query parameters to add to the
824+ `query`:: A dictionary containing the query parameters to add to the
848825 URL. The keys of the dictionary should be strings containing the names
849826 of the parameters, and the values should be strings containing the
850827 values.
@@ -864,7 +841,7 @@ def check_xml_content_type(self, response):
864841 XML then it does nothing. If it isn't XML then it raises an
865842 exception.
866843
867- `response` The HTTP response to check.
844+ `response`:: The HTTP response to check.
868845 """
869846 return self ._check_content_type (
870847 self .__XML_CONTENT_TYPE_RE ,
@@ -878,7 +855,7 @@ def check_json_content_type(self, response):
878855 JSON then it does nothing. If it isn't JSON then it raises an
879856 exception.
880857
881- `response` The HTTP response to check.
858+ `response`:: The HTTP response to check.
882859 """
883860 return self ._check_content_type (
884861 self .__JSON_CONTENT_TYPE_RE ,
@@ -891,10 +868,10 @@ def _check_content_type(self, expected_re, expected_name, headers):
891868 Checks the given content type and raises an exception if it isn't the
892869 expected one.
893870
894- `expected_re` The regular expression used to check the expected
871+ `expected_re`:: The regular expression used to check the expected
895872 content type.
896- `expected_name` The name of the expected content type.
897- `headers` The HTTP headers to check.
873+ `expected_name`:: The name of the expected content type.
874+ `headers`:: The HTTP headers to check.
898875 """
899876 content_type = self ._get_header_value (headers , 'content-type' )
900877 if expected_re .match (content_type ) is None :
@@ -915,7 +892,7 @@ def _read_reponse(self, context):
915892 """
916893 Read the response.
917894
918- `context` tuple which contains cur easy, response body,
895+ `context`:: tuple which contains cur easy, response body,
919896 response headers, original request
920897 """
921898 # Extract the response code and body:
@@ -952,14 +929,14 @@ def __parse_error(self, error):
952929 elif e_code == pycurl .E_OPERATION_TIMEOUTED :
953930 clazz = TimeoutError
954931
955- raise clazz (error_msg ). with_traceback ( sys .exc_info ()[2 ])
932+ six . reraise ( clazz , clazz (error_msg ), sys .exc_info ()[2 ])
956933
957934 def _get_header_value (self , headers , name ):
958935 """
959936 Return header value by its name.
960937
961- `headers` list of headers
962- `name` name of the header
938+ `headers`:: list of headers
939+ `name`:: name of the header
963940 """
964941 return next (
965942 (h .split (':' )[1 ].strip () for h in headers if h .lower ().startswith (name )),
@@ -981,7 +958,7 @@ def _curl_debug(self, debug_type, data):
981958 # some as arrays of bytes, so we need to check the type of the
982959 # provided data and convert it to strings before trying to
983960 # manipulate it with the "replace", "strip" and "split" methods:
984- text = data .decode ('utf-8' ) if type (data ) is bytes else data
961+ text = data .decode ('utf-8' ) if isinstance (data , bytes ) else data
985962
986963 # Split the debug data into lines and send a debug message for
987964 # each line:
@@ -1007,7 +984,8 @@ class ConnectionBuilder(object):
1007984 equivalent to calling the constructor of the `Connection`
1008985 class. Typical use will be like this:
1009986
1010- ```python
987+ [source,python]
988+ ----
1011989 # Create the builder once:
1012990 builder = ConnectionBuilder(
1013991 url='https://enginer40.example.com/ovirt-engine/api',
@@ -1023,7 +1001,7 @@ class ConnectionBuilder(object):
10231001 # Create and use a second connection:
10241002 with builder.build() as connection:
10251003 ...
1026- ```
1004+ ----
10271005 """
10281006
10291007 def __init__ (self , ** kwargs ):
@@ -1051,7 +1029,7 @@ def build(self):
10511029# import ovirtsdk4 as sdk
10521030# vm = sdk.types.Vm()
10531031#
1054- import ovirtsdk4 . readers as readers # noqa: E402, F401
1055- import ovirtsdk4 . writers as writers # noqa: E402, F401
1056- import ovirtsdk4 . types as types # noqa: E402, F401
1057- import ovirtsdk4 . services as services # noqa: E402, F401
1032+ from ovirtsdk4 import readers # noqa: F401 E402
1033+ from ovirtsdk4 import services # noqa: F401 E402
1034+ from ovirtsdk4 import types # noqa: F401 E402
1035+ from ovirtsdk4 import writers # noqa: F401 E402
0 commit comments