Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to python 3 #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions pyagentx/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ def emit(self, record):
# --------------------------------------------

import time
import Queue
import inspect
import queue as Queue

import pyagentx
from pyagentx.updater import Updater
Expand All @@ -31,7 +30,7 @@ def __init__(self):
self._threads = []

def register(self, oid, class_, freq=10):
if Updater not in inspect.getmro(class_):
if Updater not in class_.__bases__:
raise AgentError('Class given isn\'t an updater')
# cleanup and test oid
try:
Expand Down
14 changes: 7 additions & 7 deletions pyagentx/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def emit(self, record):
import socket
import time
import threading
import Queue
import queue as Queue

import pyagentx
from pyagentx.pdu import PDU
Expand Down Expand Up @@ -84,16 +84,16 @@ def _get_updates(self):
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
for row in update_data.values():
for row in list(update_data.values()):
oid = "%s.%s" % (update_oid, row['name'])
self.data[oid] = {'name': oid, 'type':row['type'],
'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('.')))
self.data_idx = sorted(list(self.data.keys()), key=lambda k: tuple(int(part) for part in k.split('.')))
except Queue.Empty:
break

Expand Down Expand Up @@ -221,17 +221,17 @@ def _start_network(self):


elif request.type == pyagentx.AGENTX_COMMITSET_PDU:
for handler in self._sethandlers.values():
for handler in list(self._sethandlers.values()):
handler.network_commit(request.session_id, request.transaction_id)
logger.info("Received COMMITSET PDU")

elif request.type == pyagentx.AGENTX_UNDOSET_PDU:
for handler in self._sethandlers.values():
for handler in list(self._sethandlers.values()):
handler.network_undo(request.session_id, request.transaction_id)
logger.info("Received UNDOSET PDU")

elif request.type == pyagentx.AGENTX_CLEANUPSET_PDU:
for handler in self._sethandlers.values():
for handler in list(self._sethandlers.values()):
handler.network_cleanup(request.session_id, request.transaction_id)
logger.info("Received CLEANUP PDU")

Expand Down
17 changes: 9 additions & 8 deletions pyagentx/pdu.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ def encode_oid(self, oid, include=0):

def encode_octet(self, octet):
buf = struct.pack('!L', len(octet))
buf += str(octet)
buf += octet
padding = ( 4 - ( len(octet) % 4 ) ) % 4
buf += chr(0)* padding
# buf += chr(0)* padding
buf += b'0' * padding
return buf


Expand Down Expand Up @@ -107,14 +108,14 @@ def encode_header(self, pdu_type, payload_length=0, flags=0):


def encode(self):
buf = ''
buf = b''
if self.type == pyagentx.AGENTX_OPEN_PDU:
# timeout
buf += struct.pack('!BBBB', 5, 0, 0, 0)
# agent OID
buf += struct.pack('!L', 0)
# Agent Desc
buf += self.encode_octet('MyAgent')
buf += self.encode_octet(b'MyAgent')

elif self.type == pyagentx.AGENTX_PING_PDU:
# No extra data
Expand Down Expand Up @@ -169,7 +170,7 @@ def decode_oid(self):
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))

Expand All @@ -196,15 +197,15 @@ def decode_octet(self):
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')


def decode_value(self):
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]:
Expand Down Expand Up @@ -252,7 +253,7 @@ def decode_header(self):
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))

Expand Down
2 changes: 1 addition & 1 deletion pyagentx/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def emit(self, record):

import time
import threading
import Queue
import queue as Queue

import pyagentx

Expand Down