Skip to content

Commit

Permalink
Add support for filtering objects in the Cython parser.
Browse files Browse the repository at this point in the history
I think this makes the new parser complete and can actually be used now.
  • Loading branch information
marineam committed Aug 25, 2010
1 parent 6deb1eb commit 354008b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
20 changes: 19 additions & 1 deletion python/nagcat/_object_parser_c.pyx
Expand Up @@ -120,12 +120,13 @@ cdef class ObjectParser:

# Cython >= 0.12 uses the 3.x style bytes type for its
# raw byte string instead of the 2.x style str type.
cdef dict _objects
cdef dict _objects, _object_select
cdef bytes _buffer
cdef char *_pos

def __init__(self, object_file, object_types=(), object_select=()):
self._objects = {}
self._object_select = dict(object_select)

try:
fd = open(object_file)
Expand All @@ -139,6 +140,23 @@ cdef class ObjectParser:
self._pos = NULL
self._buffer = None

# filter data after the fact, dunno if filtering during is
# faster or slower yet, in python it seemed slower.
if object_types or object_select:
for objtype in self._objects:
if object_types and objtype not in object_types:
del self._objects[objtype]
else:
self._objects[objtype] = filter(
self._select_filter,
self._objects[objtype])

def _select_filter(self, obj):
for key, value in self._object_select.iteritems():
if key in obj and obj[key] != value:
return False
return True

cdef int _parse(self) except -1:
while self._pos:
self._parse_object()
Expand Down
33 changes: 28 additions & 5 deletions python/nagcat/unittests/test_nagios_objects.py
Expand Up @@ -41,12 +41,20 @@ class ObjectsPyTestCase(unittest.TestCase):
'host_name': 'host1',
'alias': 'Host 1',
},
{
'host_name': 'host2',
'alias': 'Host 2',
},
],
'service': [
{
'service_description': "Service 1",
'host_name': 'host1',
},
{
'service_description': "Service 2",
'host_name': 'host2',
},
],
}

Expand All @@ -68,18 +76,18 @@ def mkfile(self, objects):
file_path = self.mktemp()
file_obj = open(file_path, 'w')
for obj_type, seq in objects.iteritems():
if self.status:
file_obj.write("%sstatus {\n" % obj_type)
else:
file_obj.write("define %s {\n" % obj_type)
for obj in seq:
if self.status:
file_obj.write("%sstatus {\n" % obj_type)
else:
file_obj.write("define %s {\n" % obj_type)
for attr, value in obj.iteritems():
value = self.escape(value)
if self.status:
file_obj.write(" %s=%s\n" % (attr, value))
else:
file_obj.write(" %s %s\n" % (attr, value))
file_obj.write(" }\n")
file_obj.write(" }\n")
file_obj.close()
return file_path

Expand All @@ -98,6 +106,21 @@ def testEscape(self):
parsed = self.todict(parser)
self.assertEquals(parsed, objects)

def testFilterTypes(self):
parser = self.parser(self.mkfile(self.objects),
object_types=('host',))
parsed = self.todict(parser)
expect = {'host': self.objects['host']}
self.assertEquals(parsed, expect)

def testFilterValues(self):
parser = self.parser(self.mkfile(self.objects),
object_select={'host_name': "host1"})
parsed = self.todict(parser)
expect = {'host': self.objects['host'][:1],
'service': self.objects['service'][:1]}
self.assertEquals(parsed, expect)


class StatusPyTestCase(ObjectsPyTestCase):

Expand Down

0 comments on commit 354008b

Please sign in to comment.