1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
commit dd1f0e84f8e321789264aec5ada0f1cb4d9ac8af
Author: Ondrej Mular <omular@redhat.com>
Date: Tue Nov 21 08:40:55 2017 +0100
Port to python3
diff --git a/pyagentx/__init__.py b/pyagentx/__init__.py
index efeef10..d4fd627 100644
--- a/pyagentx/__init__.py
+++ b/pyagentx/__init__.py
@@ -1,5 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
+from __future__ import (
+ absolute_import,
+ division,
+ print_function,
+)
import logging
diff --git a/pyagentx/agent.py b/pyagentx/agent.py
index b6c0e2a..2db39db 100644
--- a/pyagentx/agent.py
+++ b/pyagentx/agent.py
@@ -1,5 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
+from __future__ import (
+ absolute_import,
+ division,
+ print_function,
+)
# --------------------------------------------
import logging
@@ -11,8 +16,11 @@ logger.addHandler(NullHandler())
# --------------------------------------------
import time
-import Queue
import inspect
+try:
+ import queue
+except ImportError:
+ import Queue as queue
import pyagentx
from pyagentx.updater import Updater
@@ -57,18 +65,18 @@ class Agent(object):
pass
def start(self):
- queue = Queue.Queue(maxsize=20)
+ update_queue = queue.Queue(maxsize=20)
self.setup()
# Start Updaters
for u in self._updater_list:
logger.debug('Starting updater [%s]' % u['oid'])
t = u['class']()
- t.agent_setup(queue, u['oid'], u['freq'])
+ t.agent_setup(update_queue, u['oid'], u['freq'])
t.start()
self._threads.append(t)
# Start Network
oid_list = [u['oid'] for u in self._updater_list]
- t = Network(queue, oid_list, self._sethandlers)
+ t = Network(update_queue, oid_list, self._sethandlers)
t.start()
self._threads.append(t)
# Do nothing ... just wait for someone to stop you
diff --git a/pyagentx/network.py b/pyagentx/network.py
index 9711398..f30edad 100644
--- a/pyagentx/network.py
+++ b/pyagentx/network.py
@@ -1,5 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
+from __future__ import (
+ absolute_import,
+ division,
+ print_function,
+)
# --------------------------------------------
import logging
@@ -13,7 +18,10 @@ logger.addHandler(NullHandler())
import socket
import time
import threading
-import Queue
+try:
+ import queue
+except ImportError:
+ import Queue as queue
import pyagentx
from pyagentx.pdu import PDU
@@ -21,10 +29,10 @@ from pyagentx.pdu import PDU
class Network(threading.Thread):
- def __init__(self, queue, oid_list, sethandlers):
+ def __init__(self, update_queue, oid_list, sethandlers):
threading.Thread.__init__(self)
self.stop = threading.Event()
- self._queue = queue
+ self._queue = update_queue
self._oid_list = oid_list
self._sethandlers = sethandlers
@@ -84,7 +92,7 @@ class Network(threading.Thread):
update_oid = item['oid']
update_data = item['data']
# clear values with prefix oid
- for oid in self.data.keys():
+ for oid in list(self.data.keys()):
if oid.startswith(update_oid):
del(self.data[oid])
# insert updated value
@@ -94,7 +102,7 @@ class Network(threading.Thread):
'value':row['value']}
# recalculate reverse index if data changed
self.data_idx = sorted(self.data.keys(), key=lambda k: tuple(int(part) for part in k.split('.')))
- except Queue.Empty:
+ except queue.Empty:
break
diff --git a/pyagentx/pdu.py b/pyagentx/pdu.py
index 0af8e82..ac02a77 100644
--- a/pyagentx/pdu.py
+++ b/pyagentx/pdu.py
@@ -1,5 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
+from __future__ import (
+ absolute_import,
+ division,
+ print_function,
+)
# --------------------------------------------
import logging
@@ -68,10 +73,11 @@ class PDU(object):
def encode_octet(self, octet):
+ octet = octet.encode("utf-8")
buf = struct.pack('!L', len(octet))
- buf += str(octet)
+ buf += octet
padding = ( 4 - ( len(octet) % 4 ) ) % 4
- buf += chr(0)* padding
+ buf += chr(0).encode() * padding
return buf
@@ -107,7 +113,7 @@ class PDU(object):
def encode(self):
- buf = ''
+ buf = b''
if self.type == pyagentx.AGENTX_OPEN_PDU:
# timeout
buf += struct.pack('!BBBB', 5, 0, 0, 0)
@@ -169,7 +175,7 @@ class PDU(object):
sub_ids.append(t[0])
oid = '.'.join(str(i) for i in sub_ids)
return oid, ret['include']
- except Exception, e:
+ except Exception as e:
logger.exception('Invalid packing OID header')
logger.debug('%s' % pprint.pformat(self.decode_buf))
@@ -196,7 +202,7 @@ class PDU(object):
buf = self.decode_buf[:l]
self.decode_buf = self.decode_buf[l+padding:]
return buf
- except Exception, e:
+ except Exception as e:
logger.exception('Invalid packing octet header')
@@ -204,7 +210,7 @@ class PDU(object):
try:
vtype,_ = struct.unpack('!HH', self.decode_buf[:4])
self.decode_buf = self.decode_buf[4:]
- except Exception, e:
+ except Exception as e:
logger.exception('Invalid packing value header')
oid,_ = self.decode_oid()
if vtype in [pyagentx.TYPE_INTEGER, pyagentx.TYPE_COUNTER32, pyagentx.TYPE_GAUGE32, pyagentx.TYPE_TIMETICKS]:
@@ -252,7 +258,7 @@ class PDU(object):
context = self.decode_octet()
logger.debug('Context: %s' % context)
return ret
- except Exception, e:
+ except Exception as e:
logger.exception('Invalid packing: %d' % len(self.decode_buf))
logger.debug('%s' % pprint.pformat(self.decode_buf))
diff --git a/pyagentx/sethandler.py b/pyagentx/sethandler.py
index 30a2db5..97839b2 100644
--- a/pyagentx/sethandler.py
+++ b/pyagentx/sethandler.py
@@ -1,5 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
+from __future__ import (
+ absolute_import,
+ division,
+ print_function,
+)
# --------------------------------------------
import logging
diff --git a/pyagentx/updater.py b/pyagentx/updater.py
index 5fb06d4..711f87e 100644
--- a/pyagentx/updater.py
+++ b/pyagentx/updater.py
@@ -1,5 +1,10 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
+from __future__ import (
+ absolute_import,
+ division,
+ print_function,
+)
# --------------------------------------------
import logging
@@ -12,7 +17,11 @@ logger.addHandler(NullHandler())
import time
import threading
-import Queue
+try:
+ import queue
+except ImportError:
+ import Queue as queue
+
import pyagentx
@@ -39,7 +48,7 @@ class Updater(threading.Thread):
self.update()
self._queue.put_nowait({'oid': self._oid,
'data':self._data})
- except Queue.Full:
+ except queue.Full:
logger.error('Queue full')
except:
logger.exception('Unhandled update exception')
|