diff options
author | Nagappan Alagappan <nagappan@gmail.com> | 2013-10-18 16:12:30 -0700 |
---|---|---|
committer | Nagappan Alagappan <nagappan@gmail.com> | 2013-10-18 16:12:30 -0700 |
commit | deb6810a220443c04b35ee33fdd98b1319a09f9e (patch) | |
tree | 005452c397f8443ff6d307b95eac89c87e58bc40 | |
parent | 004ee6865f21c087629157ec8ec8601ed431cb97 (diff) |
Fix for python 3.x
Fix for python 3.x
QA Notes:
Testing Done: Verified with Python 3.3.2 and 2.7
Documentation Notes:
Bug Number:
Reviewed by:
Approved by:
Mailto:
-rw-r--r-- | ldtp/__init__.py | 75 | ||||
-rw-r--r-- | ldtp/client.py | 37 |
2 files changed, 70 insertions, 42 deletions
diff --git a/ldtp/__init__.py b/ldtp/__init__.py index acc0685..591e18f 100644 --- a/ldtp/__init__.py +++ b/ldtp/__init__.py @@ -39,9 +39,9 @@ if path == "": path = "." sys.path.append(path) -import state -import client -from client_exception import LdtpExecutionError +from ldtp import state +from ldtp import client +from ldtp.client_exception import LdtpExecutionError _t = None _pollEvents = None @@ -161,32 +161,43 @@ def stoplog(): _file_logger = None return 1 -class PollLogs: +class PollLogs(threading.Thread): """ Class to poll logs, NOTE: *NOT* for external use """ global _file_logger def __init__(self): - self._stop = False + super(PollLogs, self).__init__() + self.alive = True def __del__(self): """ Stop polling when destroying this class """ try: - self._stop = True + self.alive = False except: pass + def stop(self): + """ + Stop the thread + """ + try: + self.alive = False + self.join() + except: + pass + def run(self): - while not self._stop: + while self.alive: try: if not self.poll_server(): # Socket error break except: log(traceback.format_exc()) - self._stop = False + self.alive = False break def poll_server(self): @@ -243,33 +254,44 @@ def _populateNamespace(d): d[local_name] = getattr(client._client, method) d[local_name].__doc__ = client._client.system.methodHelp(method) -class PollEvents: +class PollEvents(threading.Thread): """ Class to poll callback events, NOTE: *NOT* for external use """ def __init__(self): - self._stop = False - # Initialize callback dictionary - self._callback = {} + super(PollEvents, self).__init__() + self.alive = True + # Initialize callback dictionary + self._callback = {} def __del__(self): """ - Stop callback when destroying this class + Stop polling when destroying this class """ try: - self._stop = True + self.alive = False except: pass + def stop(self): + """ + Stop the thread + """ + try: + self.alive = False + self.join() + except: + pass + def run(self): - while not self._stop: + while self.alive: try: if not self.poll_server(): # Socket error break except: log(traceback.format_exc()) - self._stop = False + self.alive = False break def poll_server(self): @@ -570,21 +592,18 @@ def windowuptime(window_name): _populateNamespace(globals()) _pollEvents = PollEvents() -_t1 = threading.Thread(target=_pollEvents.run, args=()) -_t1.daemon = True -_t1.start() +_pollEvents.daemon = True +_pollEvents.start() _pollLogs = PollLogs() -_t2 = threading.Thread(target=_pollLogs.run, args=()) -_t2.daemon = True -_t2.start() +_pollLogs.daemon = True +_pollLogs.start() @atexit.register def _stop_thread(): - if _t and _t.isAlive(): - _t._Thread__stop() - if _t1.isAlive(): - _t1._Thread__stop() - if _t2.isAlive(): - _t2._Thread__stop() + try: + _pollLogs.stop() + _pollEvents.stop() + except: + pass atexit.register(client._client.kill_daemon) diff --git a/ldtp/client.py b/ldtp/client.py index 4f4c8cf..920db67 100644 --- a/ldtp/client.py +++ b/ldtp/client.py @@ -26,15 +26,21 @@ import time import signal import platform import traceback -import xmlrpclib import subprocess from socket import error as SocketError -from client_exception import LdtpExecutionError, ERROR_CODE -from log import logger - +from ldtp.log import logger +from ldtp.client_exception import LdtpExecutionError, ERROR_CODE + +try: + import xmlrpclib +except ImportError: + import xmlrpc.client as xmlrpclib +_python3 = False _python26 = False if sys.version_info[:2] <= (2, 6): _python26 = True +if sys.version_info[:2] >= (3, 0): + _python3 = True _ldtp_windows_env = False if 'LDTP_DEBUG' in os.environ: _ldtp_debug = os.environ['LDTP_DEBUG'] @@ -102,7 +108,7 @@ class Transport(xmlrpclib.Transport): # @param host Target host. # @return A connection handle. - if not _python26: + if not _python26 and _python3: # Add to the class, only if > python 2.5 def make_connection(self, host): # create a HTTP connection object from a host descriptor @@ -128,15 +134,18 @@ class Transport(xmlrpclib.Transport): # Activestate python 2.5, use the old method return xmlrpclib.Transport.request( self, host, handler, request_body, verbose=verbose) - # Follwing implementation not supported in Python <= 2.6 - h = self.make_connection(host) - if verbose: - h.set_debuglevel(1) - - self.send_request(h, handler, request_body) - self.send_host(h, host) - self.send_user_agent(h) - self.send_content(h, request_body) + if not _python3: + # Follwing implementation not supported in Python <= 2.6 + h = self.make_connection(host) + if verbose: + h.set_debuglevel(1) + + self.send_request(h, handler, request_body) + self.send_host(h, host) + self.send_user_agent(h) + self.send_content(h, request_body) + else: + h=self.send_request(host, handler, request_body, bool(verbose)) response = h.getresponse() |