[svn] commit: r162 - in /experiments/likun-bigtool: lib/__init__.py lib/bigtool.py lib/command.py lib/exception.py lib/moduleinfo.py lib/mycollections.py test_command.py
BIND 10 source code commits
bind10-changes at lists.isc.org
Thu Oct 29 07:54:13 UTC 2009
Author: zhanglikun
Date: Thu Oct 29 07:54:13 2009
New Revision: 162
Log:
1. Add class OrderedDict, to make sure bigtool can run on python3.0.
2. Refined some code.
Added:
experiments/likun-bigtool/lib/mycollections.py
Modified:
experiments/likun-bigtool/lib/__init__.py
experiments/likun-bigtool/lib/bigtool.py
experiments/likun-bigtool/lib/command.py
experiments/likun-bigtool/lib/exception.py
experiments/likun-bigtool/lib/moduleinfo.py
experiments/likun-bigtool/test_command.py
Modified: experiments/likun-bigtool/lib/__init__.py
==============================================================================
--- experiments/likun-bigtool/lib/__init__.py (original)
+++ experiments/likun-bigtool/lib/__init__.py Thu Oct 29 07:54:13 2009
@@ -1,1 +1,1 @@
-__all__ = ['exception', 'moduleinfo', 'command', 'bigtool']
+__all__ = ['mycollections', 'exception', 'moduleinfo', 'command', 'bigtool']
Modified: experiments/likun-bigtool/lib/bigtool.py
==============================================================================
--- experiments/likun-bigtool/lib/bigtool.py (original)
+++ experiments/likun-bigtool/lib/bigtool.py Thu Oct 29 07:54:13 2009
@@ -1,19 +1,28 @@
import sys
import readline
-import collections
from cmd import Cmd
from lib.exception import *
from lib.moduleinfo import ModuleInfo
+from lib.moduleinfo import ParamInfo
from lib.command import BigToolCmd
+from xml.dom import minidom
+
+try:
+ from collections import OrderedDict
+except ImportError:
+ from lib.mycollections import OrderedDict
+
CONST_BIGTOOL_HELP = """Bigtool, verstion 0.1
usage: <module name> <command name> [param1 = value1 [, param2 = value2]]
-Type Tab character to get the hint of module/command/parameters.
+Type Tab character to get the hint of module/command/paramters.
Type \"help(? h)\" for help on bigtool.
Type \"<module_name> help\" for help on the specific module.
Type \"<module_name> <command_name> help\" for help on the specific command.
\nAvailable module names: """
+CONST_COMMAND_NODE = "command"
+
class BigTool(Cmd):
"""simple bigtool command example."""
@@ -21,7 +30,7 @@
Cmd.__init__(self)
self.prompt = '> '
self.ruler = '-'
- self.modules = collections.OrderedDict()
+ self.modules = OrderedDict()
self.add_module_info(ModuleInfo("help", desc = "Get help for bigtool"))
@@ -36,13 +45,8 @@
command_info = module_info.get_command_with_name(cmd.command)
manda_params = command_info.get_mandatory_param_names()
all_params = command_info.get_param_names()
- if command_info.need_instance_param():
- inst_name = module_info.get_instance_param_name()
- if inst_name:
- manda_params.append(inst_name)
- all_params.append(inst_name)
-
- # If help is inputed, don't do further parameters validation.
+
+ # If help is inputed, don't do further paramters validation.
for val in cmd.params.keys():
if val == "help":
return
@@ -67,7 +71,7 @@
if cmd.command == "help" or ("help" in cmd.params.keys()):
self._handle_help(cmd)
else:
- self._temp_print_parse_result(cmd)
+ self._write_xml(cmd)
def add_module_info(self, module_info):
self.modules[module_info.name] = module_info
@@ -165,11 +169,6 @@
if command in module_info.get_command_names():
cmd_info = module_info.get_command_with_name(command)
params = cmd_info.get_param_names()
- if cmd_info.need_instance_param():
- inst_name = module_info.get_instance_param_name()
- if inst_name:
- params.append(inst_name)
-
hint = []
if text:
hint = [val for val in params if val.startswith(text)]
@@ -205,14 +204,6 @@
isinstance(ept, CmdUnknownParamSyntaxError):
self.modules[ept.module].command_help(ept.command)
-
- def _temp_print_parse_result(self, cmd):
- print("command line is parsed:\nmodule_name: %s\ncommand_name: %s\n"\
- "parameters:" % (cmd.module, cmd.command))
-
- for name in cmd.params.keys():
- print("\t%s \t%s" % (name, cmd.params[name]))
-
def _append_space_to_hint(self):
"""Append one space at the end of complete hint."""
@@ -225,9 +216,29 @@
self.modules[cmd.module].module_help()
else:
self.modules[cmd.module].command_help(cmd.command)
-
-
-
-
-
-
+
+
+ def _write_xml(self, cmd):
+ '''The format of the xml message is:
+ <command name = ''>
+ <module name = ''>
+ <param name = '' value = ''>
+ <param name = '' value = ''>
+ <param name = '' value = ''>
+ </module>
+ </command>
+ '''
+ xmldoc = minidom.Document()
+ module_node = self.modules[cmd.module].write_xml(xmldoc, cmd.module)
+
+ command_info = self.modules[cmd.module].get_command_with_name(cmd.command)
+ command_node = command_info.write_xml(xmldoc, cmd.command)
+ for param_name,param_value in cmd.params.items():
+ param_node = ParamInfo.write_xml(xmldoc, param_name, param_value)
+ module_node.appendChild(param_node)
+
+ command_node.appendChild(module_node)
+ xmldoc.appendChild(command_node)
+ print(xmldoc.toxml())
+
+
Modified: experiments/likun-bigtool/lib/command.py
==============================================================================
--- experiments/likun-bigtool/lib/command.py (original)
+++ experiments/likun-bigtool/lib/command.py Thu Oct 29 07:54:13 2009
@@ -1,98 +1,100 @@
-import re
-import collections
-from lib.exception import *
-
-
-param_name_str = "^\s*(?P<param_name>[\w]+)\s*=\s*"
-param_value_str = "(?P<param_value>[\w\.]+)"
-param_value_with_quota_str = "[\"\'](?P<param_value>[\w\., ]+)[\"\']"
-next_params_str = "(?P<blank>\s*)(?P<comma>,?)(?P<next_params>.*)$"
-
-PARAM_WITH_QUOTA_PATTERN = re.compile(param_name_str +
- param_value_with_quota_str +
- next_params_str)
-PARAM_PATTERN = re.compile(param_name_str + param_value_str + next_params_str)
-
-# Used for module and command name
-MODULE_CMD_NAME_PATTERN = re.compile("^\s*(?P<name>[\w]+)(?P<blank>\s*)(?P<others>.*)$")
-
-class BigToolCmd:
- """ This class will parse the command line usr input into three part
- module name, cmmand, parameters
- the first two parts are strings and parameter is one hash,
- parameter part is optional
-
- Example: zone reload, zone_name=example.com
- module == zone
- command == reload
- params == [zone_name = 'example.com']
- """
-
- def __init__(self, cmd):
- self.params = collections.OrderedDict()
- self.module = ''
- self.command = ''
- self._parse_cmd(cmd)
-
- def _parse_cmd(self, text_str):
- # Get module name
- groups = MODULE_CMD_NAME_PATTERN.match(text_str)
- if not groups:
- raise CmdModuleNameFormatError
-
- self.module = groups.group('name')
- cmd_str = groups.group('others')
- if cmd_str:
- if not groups.group('blank'):
- raise CmdModuleNameFormatError
- else:
- raise CmdMissCommandNameFormatError(self.module)
-
- # Get command name
- groups = MODULE_CMD_NAME_PATTERN.match(cmd_str)
- if (not groups):
- raise CmdCommandNameFormatError(self.module)
-
- self.command = groups.group('name')
- param_str = groups.group('others')
- if param_str:
- if not groups.group('blank'):
- raise CmdCommandNameFormatError(self.module)
-
- self._parse_params(param_str)
-
-
- def _parse_params(self, param_text):
- """convert a=b,c=d into one hash """
-
- # Check parameter name "help"
- param = MODULE_CMD_NAME_PATTERN.match(param_text)
- if param and param.group('name') == "help":
- self.params["help"] = "help"
- return
-
- while True:
- if not param_text.strip():
- break
-
- groups = PARAM_PATTERN.match(param_text) or \
- PARAM_WITH_QUOTA_PATTERN.match(param_text)
-
- if not groups:
- raise CmdParamFormatError(self.module, self.command)
- else:
- self.params[groups.group('param_name')] = groups.group('param_value')
- param_text = groups.group('next_params')
- if not param_text or (not param_text.strip()):
- break
-
- if not groups.group('blank') and \
- not groups.group('comma'):
- raise CmdParamFormatError(self.module, self.command)
-
-
-
-
-
-
-
+import re
+from lib.exception import *
+try:
+ from collections import OrderedDict
+except ImportError:
+ from lib.mycollections import OrderedDict
+
+param_name_str = "^\s*(?P<param_name>[\w]+)\s*=\s*"
+param_value_str = "(?P<param_value>[\w\.]+)"
+param_value_with_quota_str = "[\"\'](?P<param_value>[\w\., ]+)[\"\']"
+next_params_str = "(?P<blank>\s*)(?P<comma>,?)(?P<next_params>.*)$"
+
+PARAM_WITH_QUOTA_PATTERN = re.compile(param_name_str +
+ param_value_with_quota_str +
+ next_params_str)
+PARAM_PATTERN = re.compile(param_name_str + param_value_str + next_params_str)
+
+# Used for module and command name
+NAME_PATTERN = re.compile("^\s*(?P<name>[\w]+)(?P<blank>\s*)(?P<others>.*)$")
+
+class BigToolCmd:
+ """ This class will parse the command line usr input into three part
+ module name, cmmand, parameters
+ the first two parts are strings and parameter is one hash,
+ parameter part is optional
+
+ Example: zone reload, zone_name=example.com
+ module == zone
+ command == reload
+ params == [zone_name = 'example.com']
+ """
+
+ def __init__(self, cmd):
+ self.params = OrderedDict()
+ self.module = ''
+ self.command = ''
+ self._parse_cmd(cmd)
+
+ def _parse_cmd(self, text_str):
+ # Get module name
+ groups = NAME_PATTERN.match(text_str)
+ if not groups:
+ raise CmdModuleNameFormatError
+
+ self.module = groups.group('name')
+ cmd_str = groups.group('others')
+ if cmd_str:
+ if not groups.group('blank'):
+ raise CmdModuleNameFormatError
+ else:
+ raise CmdMissCommandNameFormatError(self.module)
+
+ # Get command name
+ groups = NAME_PATTERN.match(cmd_str)
+ if (not groups):
+ raise CmdCommandNameFormatError(self.module)
+
+ self.command = groups.group('name')
+ param_str = groups.group('others')
+ if param_str:
+ if not groups.group('blank'):
+ raise CmdCommandNameFormatError(self.module)
+
+ self._parse_params(param_str)
+
+
+ def _parse_params(self, param_text):
+ """convert a=b,c=d into one hash """
+
+ # Check parameter name "help"
+ param = NAME_PATTERN.match(param_text)
+ if param and param.group('name') == "help":
+ self.params["help"] = "help"
+ return
+
+ while True:
+ if not param_text.strip():
+ break
+
+ groups = PARAM_PATTERN.match(param_text) or \
+ PARAM_WITH_QUOTA_PATTERN.match(param_text)
+
+ if not groups:
+ raise CmdParamFormatError(self.module, self.command)
+ else:
+ self.params[groups.group('param_name')] = groups.group('param_value')
+ param_text = groups.group('next_params')
+ if not param_text or (not param_text.strip()):
+ break
+
+ if not groups.group('blank') and \
+ not groups.group('comma'):
+ raise CmdParamFormatError(self.module, self.command)
+
+
+
+
+
+
+
Modified: experiments/likun-bigtool/lib/exception.py
==============================================================================
--- experiments/likun-bigtool/lib/exception.py (original)
+++ experiments/likun-bigtool/lib/exception.py Thu Oct 29 07:54:13 2009
@@ -100,4 +100,4 @@
return str("Parameter '%s' is missed for command '%s' of moudule '%s'" %
(self.param, self.command, self.module))
-
+
Modified: experiments/likun-bigtool/lib/moduleinfo.py
==============================================================================
--- experiments/likun-bigtool/lib/moduleinfo.py (original)
+++ experiments/likun-bigtool/lib/moduleinfo.py Thu Oct 29 07:54:13 2009
@@ -1,167 +1,189 @@
-import collections
-
-# Define value type
-STRING_TYPE = "string"
-LIST_TYPE = "list"
-INT_TYPE = "int"
-
-class ParamInfo:
- """The parameter of one command
- each command parameter have four attributes,
- parameter name, parameter type, parameter value, and parameter description
- """
- def __init__(self, name, desc = '', type = STRING_TYPE,
- optional = False, value = ''):
- self.name = name
- self.type = type
- self.value = value #Save parameter's value
- self.desc = desc
- self.optional = optional
-
- def is_optional(self):
- return self.optional
-
- def __str__(self):
- return str("\t%s <type: %s> \t(%s)" % (self.name, self.type, self.desc))
-
-
-class CommandInfo:
- """One command which provide by one bind10 module, it has zero or
- more parameters
- """
-
- def __init__(self, name, desc = "", need_inst_param = True):
- self.name = name
- # Wether command needs parameter "instance_name"
- self.need_inst_param = need_inst_param
- self.desc = desc
- self.params = collections.OrderedDict()
- # Set default parameter "help"
- self.add_param(ParamInfo("help",
- desc = "Get help for command",
- optional = True))
-
- def __str__(self):
- return str("%s \t(%s)" % (self.name, self.desc))
-
-
- def add_param(self, paraminfo):
- self.params[paraminfo.name] = paraminfo
-
-
- def has_param_with_name(self, param_name):
- return param_name in self.params
-
-
- def get_param_with_name(self, param_name):
- return self.params[param_name]
-
-
- def get_params(self):
- return list(self.params.values())
-
-
- def get_param_names(self):
- return list(self.params.keys())
-
-
- def get_mandatory_param_names(self):
- all_names = self.params.keys()
- return [name for name in all_names
- if not self.params[name].is_optional()]
-
-
- def need_instance_param(self):
- return self.need_inst_param
-
-
- def command_help(self, inst_name, inst_type, inst_desc):
- print("Command ", self)
- print("\t\thelp (Get help for command)")
-
- params = self.params.copy()
- del params["help"]
- if self.need_inst_param:
- params[inst_name] = ParamInfo(name = inst_name, type = inst_type,
- desc = inst_desc)
- if len(params) == 0:
- print("\tNo parameters for the command")
- return
-
- print("\n\tMandatory parameters:")
- find_optional = False
- for info in params.values():
- if not info.is_optional():
- print("\t", info)
- else:
- find_optional = True
-
- if find_optional:
- print("\n\tOptional parameters:")
- for info in params.values():
- if info.is_optional():
- print("\t", info)
-
-
-class ModuleInfo:
- """Define the information of one module, include module name,
- module supporting commands, instance name and the value type of instance name
- """
-
- def __init__(self, name, inst_name = "", inst_type = STRING_TYPE,
- inst_desc = "", desc = ""):
- self.name = name
- self.inst_name = inst_name
- self.inst_type = inst_type
- self.inst_desc = inst_desc
- self.desc = desc
- self.commands = collections.OrderedDict()
- # Add defaut help command
- self.add_command(CommandInfo(name = "help",
- desc = "Get help for module",
- need_inst_param = False))
-
- def __str__(self):
- return str("%s \t%s" % (self.name, self.desc))
-
- def add_command(self, commandInfo):
- self.commands[commandInfo.name] = commandInfo
-
-
- def has_command_with_name(self, command_name):
- return command_name in self.commands
-
-
- def get_command_with_name(self, command_name):
- return self.commands[command_name]
-
-
- def get_commands(self):
- return list(self.commands.values())
-
-
- def get_command_names(self):
- return list(self.commands.keys())
-
-
- def get_instance_param_name(self):
- return self.inst_name
-
-
- def get_instance_param_type(self):
- return self.inst_type
-
-
- def module_help(self):
- print("Module ", self, "\nAvailable commands:")
- for k in self.commands.keys():
- print("\t", self.commands[k])
-
-
- def command_help(self, command):
- self.commands[command].command_help(self.inst_name,
- self.inst_type,
- self.inst_desc)
-
-
-
+try:
+ from collections import OrderedDict
+except ImportError:
+ from lib.mycollections import OrderedDict
+
+# Define value type
+STRING_TYPE = "string"
+LIST_TYPE = "list"
+INT_TYPE = "int"
+
+MODULE_NODE_NAME = 'module'
+COMMAND_NODE_NAME = 'command'
+PARAM_NODE_NAME = 'param'
+
+
+class ParamInfo:
+ """The parameter of one command
+ each command parameter have four attributes,
+ parameter name, parameter type, parameter value, and parameter description
+ """
+ def __init__(self, name, desc = '', type = STRING_TYPE,
+ optional = False, value = '', default_value = ''):
+ self.name = name
+ self.type = type
+ self.value = value
+ self.default_value = default_value
+ self.desc = desc
+ self.is_optional = optional
+
+ def __str__(self):
+ return str("\t%s <type: %s> \t(%s)" % (self.name, self.type, self.desc))
+
+ def write_xml(xmldoc, name, value):
+ node = xmldoc.createElement(PARAM_NODE_NAME)
+ node.setAttribute('name', name)
+ node.setAttribute('value', value)
+ return node
+
+class CommandInfo:
+ """One command which provide by one bind10 module, it has zero or
+ more parameters
+ """
+
+ def __init__(self, name, desc = "", need_inst_param = True):
+ self.name = name
+ # Wether command needs parameter "instance_name"
+ self.need_inst_param = need_inst_param
+ self.desc = desc
+ self.params = OrderedDict()
+ # Set default parameter "help"
+ self.add_param(ParamInfo("help",
+ desc = "Get help for command",
+ optional = True))
+
+ def __str__(self):
+ return str("%s \t(%s)" % (self.name, self.desc))
+
+
+ def add_param(self, paraminfo):
+ self.params[paraminfo.name] = paraminfo
+
+
+ def has_param_with_name(self, param_name):
+ return param_name in self.params
+
+
+ def get_param_with_name(self, param_name):
+ return self.params[param_name]
+
+
+ def get_params(self):
+ return list(self.params.values())
+
+
+ def get_param_names(self):
+ return list(self.params.keys())
+
+
+ def get_mandatory_param_names(self):
+ all_names = self.params.keys()
+ return [name for name in all_names
+ if not self.params[name].is_optional]
+
+
+ def need_instance_param(self):
+ return self.need_inst_param
+
+
+ def write_xml(self, xmldoc, command_name):
+ node = xmldoc.createElement(COMMAND_NODE_NAME)
+ node.setAttribute('name', command_name)
+ return node
+
+
+ def command_help(self, inst_name, inst_type, inst_desc):
+ print("Command ", self)
+ print("\t\thelp (Get help for command)")
+
+ params = self.params.copy()
+ del params["help"]
+
+ if len(params) == 0:
+ print("\tNo parameters for the command")
+ return
+
+ print("\n\tMandatory parameters:")
+ mandatory_infos = []
+ for info in params.values():
+ if not info.is_optional:
+ print("\t", info)
+ mandatory_infos.append(info)
+
+ optional_infos = [info for info in params.values()
+ if info not in mandatory_infos]
+ if len(optional_infos) > 0:
+ print("\n\tOptional parameters:")
+ for info in optional_infos:
+ print("\t", info)
+
+
+class ModuleInfo:
+ """Define the information of one module, include module name,
+ module supporting commands, instance name and the value type of instance name
+ """
+
+ def __init__(self, name, inst_name = "", inst_type = STRING_TYPE,
+ inst_desc = "", desc = ""):
+ self.name = name
+ self.inst_name = inst_name
+ self.inst_type = inst_type
+ self.inst_desc = inst_desc
+ self.desc = desc
+ self.commands = OrderedDict()
+ self.add_command(CommandInfo(name = "help",
+ desc = "Get help for module",
+ need_inst_param = False))
+
+ def __str__(self):
+ return str("%s \t%s" % (self.name, self.desc))
+
+ def add_command(self, command_info):
+ self.commands[command_info.name] = command_info
+ if command_info.need_instance_param():
+ command_info.add_param(ParamInfo(name = self.inst_name,
+ type = self.inst_type,
+ desc = self.inst_desc))
+
+
+ def has_command_with_name(self, command_name):
+ return command_name in self.commands
+
+
+ def get_command_with_name(self, command_name):
+ return self.commands[command_name]
+
+
+ def get_commands(self):
+ return list(self.commands.values())
+
+
+ def get_command_names(self):
+ return list(self.commands.keys())
+
+
+ def get_instance_param_name(self):
+ return self.inst_name
+
+
+ def get_instance_param_type(self):
+ return self.inst_type
+
+
+ def module_help(self):
+ print("Module ", self, "\nAvailable commands:")
+ for k in self.commands.keys():
+ print("\t", self.commands[k])
+
+
+ def command_help(self, command):
+ self.commands[command].command_help(self.inst_name,
+ self.inst_type,
+ self.inst_desc)
+
+ def write_xml(self, xmldoc, module_name):
+ node = xmldoc.createElement(MODULE_NODE_NAME)
+ node.setAttribute('name', module_name)
+ return node
+
+
Modified: experiments/likun-bigtool/test_command.py
==============================================================================
--- experiments/likun-bigtool/test_command.py (original)
+++ experiments/likun-bigtool/test_command.py Thu Oct 29 07:54:13 2009
@@ -1,188 +1,190 @@
-import unittest
-from lib import command
-from lib import bigtool
-from lib import exception
-
-from lib.moduleinfo import *
-from lib.exception import *
-
-
-class TestBigToolCmd(unittest.TestCase):
-
- def my_assert_raise(self, exception_type, cmd_line):
- self.assertRaises(exception_type, command.BigToolCmd, cmd_line)
-
-
- def testCommandWithoutParameter(self):
- cmd = command.BigToolCmd("zone add")
- assert cmd.module == "zone"
- assert cmd.command == "add"
- self.assertEqual(len(cmd.params), 0)
-
-
- def testCommandWithParameters(self):
- lines = {"zone add zone_name = cnnic.cn, file = cnnic.cn.file master=1.1.1.1",
- "zone add zone_name = \"cnnic.cn\", file ='cnnic.cn.file' master=1.1.1.1 ",
- "zone add zone_name = 'cnnic.cn\", file ='cnnic.cn.file' master=1.1.1.1, " }
-
- for cmd_line in lines:
- cmd = command.BigToolCmd(cmd_line)
- assert cmd.module == "zone"
- assert cmd.command == "add"
- assert cmd.params["zone_name"] == "cnnic.cn"
- assert cmd.params["file"] == "cnnic.cn.file"
- assert cmd.params["master"] == '1.1.1.1'
-
-
- def testCommandWithListParam(self):
- cmd = command.BigToolCmd("zone set zone_name='cnnic.cn', master='1.1.1.1, 2.2.2.2'")
- assert cmd.params["master"] == '1.1.1.1, 2.2.2.2'
-
-
- def testCommandWithHelpParam(self):
- cmd = command.BigToolCmd("zone add help")
- assert cmd.params["help"] == "help"
-
- cmd = command.BigToolCmd("zone add help *&)&)*&&$#$^%")
- assert cmd.params["help"] == "help"
- self.assertEqual(len(cmd.params), 1)
-
-
- def testCmdModuleNameFormatError(self):
- self.my_assert_raise(CmdModuleNameFormatError, "zone=good")
- self.my_assert_raise(CmdModuleNameFormatError, "zo/ne")
- self.my_assert_raise(CmdModuleNameFormatError, "")
- self.my_assert_raise(CmdModuleNameFormatError, "=zone")
- self.my_assert_raise(CmdModuleNameFormatError, "zone,")
-
-
- def testCmdMissCommandNameFormatError(self):
- self.my_assert_raise(CmdMissCommandNameFormatError, "zone")
- self.my_assert_raise(CmdMissCommandNameFormatError, "zone ")
- self.my_assert_raise(CmdMissCommandNameFormatError, "help ")
-
-
- def testCmdCommandNameFormatError(self):
- self.my_assert_raise(CmdCommandNameFormatError, "zone =d")
- self.my_assert_raise(CmdCommandNameFormatError, "zone z=d")
- self.my_assert_raise(CmdCommandNameFormatError, "zone z-d ")
- self.my_assert_raise(CmdCommandNameFormatError, "zone zdd/")
- self.my_assert_raise(CmdCommandNameFormatError, "zone zdd/ \"")
-
-
- def testCmdParamFormatError(self):
- self.my_assert_raise(CmdParamFormatError, "zone load load")
- self.my_assert_raise(CmdParamFormatError, "zone load load=")
- self.my_assert_raise(CmdParamFormatError, "zone load load==dd")
- self.my_assert_raise(CmdParamFormatError, "zone load , zone_name=dd zone_file=d" )
- self.my_assert_raise(CmdParamFormatError, "zone load zone_name=dd zone_file" )
- self.my_assert_raise(CmdParamFormatError, "zone zdd \"")
-
-
-class TestValidateCmd(unittest.TestCase):
-
- def _create_bigtool(self):
- """Create one bigtool"""
-
- tool = bigtool.BigTool()
- zone_file_param = ParamInfo(name = "zone_file")
- load_cmd = CommandInfo(name = "load")
- load_cmd.add_param(zone_file_param)
-
- param_master = ParamInfo(name = "master", optional = True)
- param_allow_update = ParamInfo(name = "allow_update", optional = True)
- set_cmd = CommandInfo(name = "set")
- set_cmd.add_param(param_master)
- set_cmd.add_param(param_allow_update)
-
- reload_all_cmd = CommandInfo(name = "reload_all", need_inst_param = False)
-
- zone_module = ModuleInfo(name = "zone", inst_name = "zone_name")
- zone_module.add_command(load_cmd)
- zone_module.add_command(set_cmd)
- zone_module.add_command(reload_all_cmd)
-
- tool.add_module_info(zone_module)
- return tool
-
-
- def setUp(self):
- self.bigtool = self._create_bigtool()
-
-
- def no_assert_raise(self, cmd_line):
- cmd = command.BigToolCmd(cmd_line)
- self.bigtool.validate_cmd(cmd)
-
-
- def my_assert_raise(self, exception_type, cmd_line):
- cmd = command.BigToolCmd(cmd_line)
- self.assertRaises(exception_type, self.bigtool.validate_cmd, cmd)
-
-
- def testValidateSuccess(self):
- self.no_assert_raise("zone load zone_file='cn' zone_name='cn'")
- self.no_assert_raise("zone load zone_file='cn', zone_name='cn', ")
- self.no_assert_raise("zone help ")
- self.no_assert_raise("zone load help ")
- self.no_assert_raise("zone help help='dd' ")
- self.no_assert_raise("zone set allow_update='1.1.1.1' zone_name='cn'")
- self.no_assert_raise("zone set zone_name='cn'")
- self.no_assert_raise("zone reload_all")
-
-
- def testCmdUnknownModuleSyntaxError(self):
- self.my_assert_raise(CmdUnknownModuleSyntaxError, "zoned d")
- self.my_assert_raise(CmdUnknownModuleSyntaxError, "dd dd ")
-
-
- def testCmdUnknownCmdSyntaxError(self):
- self.my_assert_raise(CmdUnknownCmdSyntaxError, "zone dd")
-
- def testCmdMissParamSyntaxError(self):
- self.my_assert_raise(CmdMissParamSyntaxError, "zone load zone_file='cn'")
- self.my_assert_raise(CmdMissParamSyntaxError, "zone load zone_name='cn'")
- self.my_assert_raise(CmdMissParamSyntaxError, "zone set allow_update='1.1.1.1'")
- self.my_assert_raise(CmdMissParamSyntaxError, "zone set ")
-
- def testCmdUnknownParamSyntaxError(self):
- self.my_assert_raise(CmdUnknownParamSyntaxError, "zone load zone_d='cn'")
- self.my_assert_raise(CmdUnknownParamSyntaxError, "zone reload_all zone_name = 'cn'")
-
-
-class TestNameSequence(unittest.TestCase):
- """
- Test if the module/command/parameters is saved in the order creation
- """
-
- def _create_bigtool(self):
- """Create one bigtool"""
-
- self._cmd = CommandInfo(name = "load")
- self.module = ModuleInfo(name = "zone")
- self.tool = bigtool.BigTool()
- for random_str in self.random_names:
- self._cmd.add_param(ParamInfo(name = random_str))
- self.module.add_command(CommandInfo(name = random_str))
- self.tool.add_module_info(ModuleInfo(name = random_str))
-
- def setUp(self):
- self.random_names = ['1erdfeDDWsd', '3fe', '2009erd', 'Fe231', 'tere142', 'rei8WD']
- self._create_bigtool()
-
- def testSequence(self):
- param_names = self._cmd.get_param_names()
- cmd_names = self.module.get_command_names()
- module_names = self.tool.get_module_names()
-
- i = 0
- while i < len(self.random_names):
- assert self.random_names[i] == param_names[i+1]
- assert self.random_names[i] == cmd_names[i+1]
- assert self.random_names[i] == module_names[i+1]
- i = i + 1
-
-
-if __name__== "__main__":
- unittest.main()
-
+import unittest
+from lib import command
+from lib import bigtool
+from lib import exception
+from lib.moduleinfo import *
+from lib.exception import *
+try:
+ from collections import OrderedDict
+except ImportError:
+ from lib.mycollections import OrderedDict
+
+class TestCmdLex(unittest.TestCase):
+
+ def my_assert_raise(self, exception_type, cmd_line):
+ self.assertRaises(exception_type, command.BigToolCmd, cmd_line)
+
+
+ def testCommandWithoutParameter(self):
+ cmd = command.BigToolCmd("zone add")
+ assert cmd.module == "zone"
+ assert cmd.command == "add"
+ self.assertEqual(len(cmd.params), 0)
+
+
+ def testCommandWithParameters(self):
+ lines = {"zone add zone_name = cnnic.cn, file = cnnic.cn.file master=1.1.1.1",
+ "zone add zone_name = \"cnnic.cn\", file ='cnnic.cn.file' master=1.1.1.1 ",
+ "zone add zone_name = 'cnnic.cn\", file ='cnnic.cn.file' master=1.1.1.1, " }
+
+ for cmd_line in lines:
+ cmd = command.BigToolCmd(cmd_line)
+ assert cmd.module == "zone"
+ assert cmd.command == "add"
+ assert cmd.params["zone_name"] == "cnnic.cn"
+ assert cmd.params["file"] == "cnnic.cn.file"
+ assert cmd.params["master"] == '1.1.1.1'
+
+
+ def testCommandWithListParam(self):
+ cmd = command.BigToolCmd("zone set zone_name='cnnic.cn', master='1.1.1.1, 2.2.2.2'")
+ assert cmd.params["master"] == '1.1.1.1, 2.2.2.2'
+
+
+ def testCommandWithHelpParam(self):
+ cmd = command.BigToolCmd("zone add help")
+ assert cmd.params["help"] == "help"
+
+ cmd = command.BigToolCmd("zone add help *&)&)*&&$#$^%")
+ assert cmd.params["help"] == "help"
+ self.assertEqual(len(cmd.params), 1)
+
+
+ def testCmdModuleNameFormatError(self):
+ self.my_assert_raise(CmdModuleNameFormatError, "zone=good")
+ self.my_assert_raise(CmdModuleNameFormatError, "zo/ne")
+ self.my_assert_raise(CmdModuleNameFormatError, "")
+ self.my_assert_raise(CmdModuleNameFormatError, "=zone")
+ self.my_assert_raise(CmdModuleNameFormatError, "zone,")
+
+
+ def testCmdMissCommandNameFormatError(self):
+ self.my_assert_raise(CmdMissCommandNameFormatError, "zone")
+ self.my_assert_raise(CmdMissCommandNameFormatError, "zone ")
+ self.my_assert_raise(CmdMissCommandNameFormatError, "help ")
+
+
+ def testCmdCommandNameFormatError(self):
+ self.my_assert_raise(CmdCommandNameFormatError, "zone =d")
+ self.my_assert_raise(CmdCommandNameFormatError, "zone z=d")
+ self.my_assert_raise(CmdCommandNameFormatError, "zone z-d ")
+ self.my_assert_raise(CmdCommandNameFormatError, "zone zdd/")
+ self.my_assert_raise(CmdCommandNameFormatError, "zone zdd/ \"")
+
+
+ def testCmdParamFormatError(self):
+ self.my_assert_raise(CmdParamFormatError, "zone load load")
+ self.my_assert_raise(CmdParamFormatError, "zone load load=")
+ self.my_assert_raise(CmdParamFormatError, "zone load load==dd")
+ self.my_assert_raise(CmdParamFormatError, "zone load , zone_name=dd zone_file=d" )
+ self.my_assert_raise(CmdParamFormatError, "zone load zone_name=dd zone_file" )
+ self.my_assert_raise(CmdParamFormatError, "zone zdd \"")
+
+
+class TestCmdSyntax(unittest.TestCase):
+
+ def _create_bigtool(self):
+ """Create one bigtool"""
+
+ tool = bigtool.BigTool()
+ zone_file_param = ParamInfo(name = "zone_file")
+ load_cmd = CommandInfo(name = "load")
+ load_cmd.add_param(zone_file_param)
+
+ param_master = ParamInfo(name = "master", optional = True)
+ param_allow_update = ParamInfo(name = "allow_update", optional = True)
+ set_cmd = CommandInfo(name = "set")
+ set_cmd.add_param(param_master)
+ set_cmd.add_param(param_allow_update)
+
+ reload_all_cmd = CommandInfo(name = "reload_all", need_inst_param = False)
+
+ zone_module = ModuleInfo(name = "zone", inst_name = "zone_name")
+ zone_module.add_command(load_cmd)
+ zone_module.add_command(set_cmd)
+ zone_module.add_command(reload_all_cmd)
+
+ tool.add_module_info(zone_module)
+ return tool
+
+
+ def setUp(self):
+ self.bigtool = self._create_bigtool()
+
+
+ def no_assert_raise(self, cmd_line):
+ cmd = command.BigToolCmd(cmd_line)
+ self.bigtool.validate_cmd(cmd)
+
+
+ def my_assert_raise(self, exception_type, cmd_line):
+ cmd = command.BigToolCmd(cmd_line)
+ self.assertRaises(exception_type, self.bigtool.validate_cmd, cmd)
+
+
+ def testValidateSuccess(self):
+ self.no_assert_raise("zone load zone_file='cn' zone_name='cn'")
+ self.no_assert_raise("zone load zone_file='cn', zone_name='cn', ")
+ self.no_assert_raise("zone help ")
+ self.no_assert_raise("zone load help ")
+ self.no_assert_raise("zone help help='dd' ")
+ self.no_assert_raise("zone set allow_update='1.1.1.1' zone_name='cn'")
+ self.no_assert_raise("zone set zone_name='cn'")
+ self.no_assert_raise("zone reload_all")
+
+
+ def testCmdUnknownModuleSyntaxError(self):
+ self.my_assert_raise(CmdUnknownModuleSyntaxError, "zoned d")
+ self.my_assert_raise(CmdUnknownModuleSyntaxError, "dd dd ")
+
+
+ def testCmdUnknownCmdSyntaxError(self):
+ self.my_assert_raise(CmdUnknownCmdSyntaxError, "zone dd")
+
+ def testCmdMissParamSyntaxError(self):
+ self.my_assert_raise(CmdMissParamSyntaxError, "zone load zone_file='cn'")
+ self.my_assert_raise(CmdMissParamSyntaxError, "zone load zone_name='cn'")
+ self.my_assert_raise(CmdMissParamSyntaxError, "zone set allow_update='1.1.1.1'")
+ self.my_assert_raise(CmdMissParamSyntaxError, "zone set ")
+
+ def testCmdUnknownParamSyntaxError(self):
+ self.my_assert_raise(CmdUnknownParamSyntaxError, "zone load zone_d='cn'")
+ self.my_assert_raise(CmdUnknownParamSyntaxError, "zone reload_all zone_name = 'cn'")
+
+
+class TestNameSequence(unittest.TestCase):
+ """
+ Test if the module/command/parameters is saved in the order creation
+ """
+
+ def _create_bigtool(self):
+ """Create one bigtool"""
+
+ self._cmd = CommandInfo(name = "load")
+ self.module = ModuleInfo(name = "zone")
+ self.tool = bigtool.BigTool()
+ for random_str in self.random_names:
+ self._cmd.add_param(ParamInfo(name = random_str))
+ self.module.add_command(CommandInfo(name = random_str))
+ self.tool.add_module_info(ModuleInfo(name = random_str))
+
+ def setUp(self):
+ self.random_names = ['1erdfeDDWsd', '3fe', '2009erd', 'Fe231', 'tere142', 'rei8WD']
+ self._create_bigtool()
+
+ def testSequence(self):
+ param_names = self._cmd.get_param_names()
+ cmd_names = self.module.get_command_names()
+ module_names = self.tool.get_module_names()
+
+ i = 0
+ while i < len(self.random_names):
+ assert self.random_names[i] == param_names[i+1]
+ assert self.random_names[i] == cmd_names[i+1]
+ assert self.random_names[i] == module_names[i+1]
+ i = i + 1
+
+
+if __name__== "__main__":
+ unittest.main()
+
More information about the bind10-changes
mailing list