Browse Source

renamed camelCase methods to underscored in the python config classes
started with unittests for config/python


git-svn-id: svn://bind10.isc.org/svn/bind10/branches/parkinglot@719 e5f2f494-b856-4b98-b285-d166d9295462

Jelte Jansen 15 years ago
parent
commit
300ebfaf17

+ 1 - 0
configure.ac

@@ -185,6 +185,7 @@ AC_OUTPUT([src/bin/cfgmgr/b10-cfgmgr.py
            src/bin/auth/config.h
            src/bin/parkinglot/config.h
            src/lib/config/cpp/data_def_unittests_config.h
+           src/lib/config/python/isc/config/config_test
            src/lib/dns/cpp/gen-rdatacode.py
           ], [
            chmod +x src/bin/cfgmgr/run_b10-cfgmgr.sh

+ 1 - 1
src/bin/bind10/bind10.py.in

@@ -460,7 +460,7 @@ def main():
     # In our main loop, we check for dead processes or messages 
     # on the c-channel.
     wakeup_fd = wakeup_pipe[0]
-    ccs_fd = boss_of_bind.ccs.getSocket().fileno()
+    ccs_fd = boss_of_bind.ccs.get_socket().fileno()
     while boss_of_bind.runnable:
         # XXX: get time for next restart for timeout
 

+ 13 - 13
src/lib/config/python/isc/config/ccsession.py

@@ -28,22 +28,22 @@ import isc
 class CCSession:
     def __init__(self, spec_file_name, config_handler, command_handler):
         self._data_definition = isc.config.DataDefinition(spec_file_name)
-        self._module_name = self._data_definition.getModuleName()
+        self._module_name = self._data_definition.get_module_name()
         
-        self.setConfigHandler(config_handler)
-        self.setCommandHandler(command_handler)
+        self.set_config_handler(config_handler)
+        self.set_command_handler(command_handler)
 
         self._session = Session()
         self._session.group_subscribe(self._module_name, "*")
 
-        self.__sendSpec()
-        self.__getFullConfig()
+        self.__send_spec()
+        self.__get_full_config()
 
-    def getSocket(self):
+    def get_socket(self):
         """Returns the socket from the command channel session"""
         return self._session._socket
     
-    def getSession(self):
+    def get_session(self):
         """Returns the command-channel session that is used, so the
            application can use it directly"""
         return self._session
@@ -51,7 +51,7 @@ class CCSession:
     def close(self):
         self._session.close()
 
-    def checkCommand(self):
+    def check_command(self):
         """Check whether there is a command on the channel.
            Call the command callback function if so"""
         msg, env = self._session.group_recvmsg(False)
@@ -66,7 +66,7 @@ class CCSession:
             self._session.group_reply(env, answer)
 
     
-    def setConfigHandler(self, config_handler):
+    def set_config_handler(self, config_handler):
         """Set the config handler for this module. The handler is a
            function that takes the full configuration and handles it.
            It should return either { "result": [ 0 ] } or
@@ -74,19 +74,19 @@ class CCSession:
         self._config_handler = config_handler
         # should we run this right now since we've changed the handler?
 
-    def setCommandHandler(self, command_handler):
+    def set_command_handler(self, command_handler):
         """Set the command handler for this module. The handler is a
            function that takes a command as defined in the .spec file
            and return either { "result": [ 0, (result) ] } or
            { "result": [ <error_number>. "error message" ] }"""
         self._command_handler = command_handler
 
-    def __sendSpec(self):
+    def __send_spec(self):
         """Sends the data specification to the configuration manager"""
-        self._session.group_sendmsg(self._data_definition.getDefinition(), "ConfigManager")
+        self._session.group_sendmsg(self._data_definition.get_definition(), "ConfigManager")
         answer, env = self._session.group_recvmsg(False)
         
-    def __getFullConfig(self):
+    def __get_full_config(self):
         """Asks the configuration manager for the current configuration, and call the config handler if set"""
         self._session.group_sendmsg({ "command": [ "get_config", { "module_name": self._module_name } ] }, "ConfigManager")
         answer, env = self._session.group_recvmsg(False)

+ 16 - 0
src/lib/config/python/isc/config/config_test.in

@@ -0,0 +1,16 @@
+#! /bin/sh
+
+PYTHON_EXEC=${PYTHON_EXEC:-@PYTHON@}
+export PYTHON_EXEC
+
+CONFIG_PATH=@abs_top_srcdir@/src/lib/config/python/isc/config/
+
+PYTHONPATH=@abs_top_srcdir@/pyshared
+export PYTHONPATH
+
+CONFIG_TESTDATA_PATH=@abs_top_srcdir@/src/lib/config/testdata
+export CONFIG_TESTDATA_PATH
+
+cd ${BIND10_PATH}
+exec ${PYTHON_EXEC} -O ${CONFIG_PATH}/datadefinition_test.py $*
+

+ 17 - 18
src/lib/config/python/isc/config/datadefinition.py

@@ -21,18 +21,17 @@ import ast
 
 # file objects are passed around as _io.TextIOWrapper objects
 # import that so we can check those types
-import _io
 
 class DataDefinitionError(Exception):
     pass
 
 class DataDefinition:
     def __init__(self, spec_file, check = True):
-        if type(spec_file) == _io.TextIOWrapper:
-            self._data_spec = __read_data_spec_file(spec_file)
+        if hasattr(spec_file, 'read'):
+            self._data_spec = self.__read_data_spec_file(spec_file)
         elif type(spec_file) == str:
             file = open(spec_file)
-            self._data_spec = self.__readDataSpecFile(file)
+            self._data_spec = self.__read_data_spec_file(file)
             file.close()
         else:
             raise DataDefinitionError("Not a str or file-like object")
@@ -43,13 +42,13 @@ class DataDefinition:
         # "TODO"
         return True
 
-    def __readDataSpecFile(self, file, check = True):
+    def __read_data_spec_file(self, file, check = True):
         """Reads the data spec from the given file object.
            If check is True, check whether it is of the correct form.
            If it is not, an DataDefinitionError exception is raised"""
-        if type(file) != _io.TextIOWrapper:
+        if not hasattr(file, 'read'):
             raise DataDefinitionError("Not a file-like object:" + str(type(file)))
-        str = file.read()
+        str = file.read(-1)
         # TODO catch error here and reraise as a less ugly exception
         data_spec = ast.literal_eval(str)
         if check:
@@ -58,10 +57,10 @@ class DataDefinition:
             pass
         return data_spec
 
-    def getDefinition(self):
+    def get_definition(self):
         return self._data_spec
 
-    def getModuleName(self):
+    def get_module_name(self):
         return self._data_spec["data_specification"]["module_name"]
 
 def _check(data_spec):
@@ -78,11 +77,11 @@ def _check(data_spec):
     if "module_name" not in data_spec:
         raise DataDefinitionError("no module_name in data_specification")
     if "config_data" in data_spec:
-        _checkConfigSpec(data_spec["config_data"])
+        _check_config_spec(data_spec["config_data"])
     if "commands" in data_spec:
-        _checkCommandSpec(data_spec["commands"])
+        _check_command_spec(data_spec["commands"])
 
-def _checkConfigSpec(config_data):
+def _check_config_spec(config_data):
     # config data is a list of items represented by dicts that contain
     # things like "item_name", depending on the type they can have
     # specific subitems
@@ -92,9 +91,9 @@ def _checkConfigSpec(config_data):
     if type(config_data) != list:
         raise DataDefinitionError("config_data is not a list of items")
     for config_item in config_data:
-        _checkItemSpec(config_item)
+        _check_item_spec(config_item)
 
-def _checkCommandSpec(commands):
+def _check_command_spec(commands):
     """Checks the list that contains a set of commands. Raises a
        DataDefinitionError is there is an error"""
     if type(commands) != list:
@@ -116,10 +115,10 @@ def _checkCommandSpec(commands):
             for command_arg in command["command_args"]:
                 if type(command_arg) != dict:
                     raise DataDefinitionError("command argument not a dict in " + command_name)
-                _checkItemSpec(command_arg)
+                _check_item_spec(command_arg)
     pass
 
-def _checkItemSpec(config_item):
+def _check_item_spec(config_item):
     """Checks the dict that defines one config item
        (i.e. containing "item_name", "item_type", etc.
        Raises a DataDefinitionError if there is an error"""
@@ -157,7 +156,7 @@ def _checkItemSpec(config_item):
             raise DataDefinitionError("no list_item_spec in list item " + item_name)
         if type(config_item["list_item_spec"]) != dict:
             raise DataDefinitionError("list_item_spec in " + item_name + " is not a dict")
-        _checkItemSpec(config_item["list_item_spec"])
+        _check_item_spec(config_item["list_item_spec"])
     if item_type == "map":
         if "map_item_spec" not in config_item:
             raise DataDefinitionError("no map_item_sepc in map item " + item_name)
@@ -166,5 +165,5 @@ def _checkItemSpec(config_item):
         for map_item in config_item["map_item_spec"]:
             if type(map_item) != dict:
                 raise DataDefinitionError("map_item_spec element is not a dict")
-            _checkItemSpec(map_item)
+            _check_item_spec(map_item)
 

+ 54 - 0
src/lib/config/python/isc/config/datadefinition_test.py

@@ -0,0 +1,54 @@
+# Copyright (C) 2009  Internet Systems Consortium.
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
+# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
+# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
+# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
+# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
+# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
+# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+#
+# Tests for the datadefinition module
+#
+
+import unittest
+import os
+from isc.config import DataDefinition
+
+
+class TestDataDefinition(unittest.TestCase):
+
+    def setUp(self):
+        self.assert_('CONFIG_TESTDATA_PATH' in os.environ)
+        self.data_path = os.environ['CONFIG_TESTDATA_PATH']
+
+    def spec_file(self, filename):
+        return(self.data_path + os.sep + filename)
+
+    def spec1(self, dd):
+        data_def = dd.get_definition()
+        self.assert_('data_specification' in data_def)
+        data_spec = data_def['data_specification']
+        self.assert_('module_name' in data_spec)
+        self.assertEqual(data_spec['module_name'], "Spec1")
+        
+    def test_open_file_name(self):
+        dd = DataDefinition(self.spec_file("spec1.spec"))
+        self.spec1(dd)
+
+    def test_open_file_obj(self):
+        file1 = open(self.spec_file("spec1.spec"))
+        dd = DataDefinition(file1)
+        self.spec1(dd)
+
+    def test_bad_specfiles(self):
+        self.assertRaises(DataDefinition(self.spec_file("spec3.spec")))
+
+if __name__ == '__main__':
+    unittest.main()