Skip to content

Commit 593f5e5

Browse files
committed
Initial check-in
0 parents  commit 593f5e5

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.pyc
2+
.*.swp

genders.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python26
2+
3+
#from ctypes import CDLL, c_char_p, pointer, byref
4+
from ctypes import *
5+
6+
libgenders = CDLL("libgenders.so")
7+
8+
class Genders(object):
9+
def __init__(self, genders_file=None):
10+
pass
11+
# self.handle_create()
12+
13+
# if self.load_data(genders_file) != 0:
14+
# raise Exception(self.errormsg())
15+
16+
def handle_create(self):
17+
self._handle = c_void_p(libgenders.genders_handle_create())
18+
19+
if not self._handle:
20+
raise Exception("Error allocating memory")
21+
22+
def handle_destroy(self):
23+
if libgenders.genders_handle_destroy(self._handle) != 0:
24+
raise Exception(self.errormsg())
25+
26+
def load_data(self, genders_file=None):
27+
if libgenders.genders_load_data(self._handle, c_char_p(genders_file)) != 0:
28+
raise Exception(self.errormsg())
29+
30+
def isnode(self, node=None):
31+
node = c_char_p(node)
32+
return bool(libgenders.genders_isnode(self._handle, node))
33+
34+
def isattr(self, attr=None):
35+
attr = c_char_p(attr)
36+
return bool(libgenders.genders_isattr(self._handle, attr))
37+
38+
def isattrval(self, attr=None, val=None):
39+
attr = c_char_p(attr)
40+
val = c_char_p(val)
41+
return bool(libgenders.genders_isattrval(self._handle, attr, val))
42+
43+
def errnum(self):
44+
return libgenders.genders_errnum(self._handle)
45+
46+
def strerror(self, err):
47+
libgenders.genders_strerror.restype = c_char_p
48+
return libgenders.genders_strerror(err)
49+
50+
def errormsg(self):
51+
libgenders.genders_errormsg.restype = c_char_p
52+
return libgenders.genders_errormsg(self._handle)
53+
54+
def perror(self, msg=None):
55+
libgenders.genders_perror(self._handle, msg)
56+
57+
def nodelist_create(self):
58+
self.node_list = POINTER(POINTER(POINTER(c_char)))
59+
#libgenders.genders_nodelist_create.
60+
libgenders.genders_nodelist_create(self._handle, self.node_list)
61+
62+
def query(self, query_str):
63+
query_str = c_char_p(query_str)
64+
65+
#nodes = byref(c_char_p * 500)
66+
#nodes = pointer(c_char_p * 500)
67+
#nodes = POINTER(c_char_p * 500)
68+
node_buf = (c_char_p * 500)()
69+
libgenders.genders_query.argtypes = [c_int, POINTER(c_char_p), c_int, c_char_p]
70+
query_ret = libgenders.genders_query(self._handle, cast(node_buf, POINTER(c_char_p)), 500, query_str)
71+
print "ret:", query_ret
72+
73+
return node_buf

test-data/genders

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
host1 os=rhel5,testhost

test-data/genders.dup-attr

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
host1 attrdup,attrdup

tests/genders_core.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python26
2+
3+
from pprint import pprint as pp
4+
import sys
5+
import unittest
6+
7+
sys.path.insert(0, '.')
8+
sys.path.insert(0, '..')
9+
10+
import genders
11+
12+
class TestGendersCore(unittest.TestCase):
13+
14+
def setUp(self):
15+
self.genders = genders.Genders()
16+
17+
def test_handle_create(self):
18+
self.assertEqual(self.genders.handle_create(), None)
19+
20+
def test_handle_destroy(self):
21+
self.genders.handle_create()
22+
self.assertEqual(self.genders.handle_destroy(), None)
23+
24+
class TestGendersLoad(unittest.TestCase):
25+
26+
def setUp(self):
27+
self.genders = genders.Genders()
28+
self.genders.handle_create()
29+
30+
def test_load_data(self):
31+
self.assertEqual(self.genders.load_data("test-data/genders"), None)
32+
33+
# def test_load_data_fail(self):
34+
# self.assertRaises(Exception("genders file parse error"), self.genders.load_data("test-data/genders.dup-attr"))
35+
36+
class TestGendersTests(unittest.TestCase):
37+
38+
def setUp(self):
39+
self.genders = genders.Genders()
40+
self.genders.handle_create()
41+
self.genders.load_data("test-data/genders")
42+
43+
def test_isnode_true(self):
44+
self.assertTrue(self.genders.isnode("host1"))
45+
46+
def test_isnode_false(self):
47+
self.assertFalse(self.genders.isnode("Xhost1"))
48+
49+
if __name__ == '__main__':
50+
loader = unittest.TestLoader()
51+
suite = loader.loadTestsFromTestCase(TestGendersCore)
52+
suite.addTest(loader.loadTestsFromTestCase(TestGendersLoad))
53+
suite.addTest(loader.loadTestsFromTestCase(TestGendersTests))
54+
55+
unittest.TextTestRunner(verbosity=2).run(suite)
56+

0 commit comments

Comments
 (0)