def setvalue(cls, service, key, value): """ Set values for this service. :param CoreService service: service to get value list for :param str key: key to set value for :param value: value of key to set :return: nothing """ if key not in cls.keys: raise ValueError("key `%s` not in `%s`" % (key, cls.keys)) # this handles data conversion to int, string, and tuples if value: if key == "startidx": value = int(value) elif key == "meta": value = str(value) else: value = utils.make_tuple_fromstr(value, str) if key == "dirs": service.dirs = value elif key == "files": service.configs = value elif key == "cmdup": service.startup = value elif key == "cmddown": service.shutdown = value elif key == "cmdval": service.validate = value elif key == "meta": service.meta = value
def setvalue(self, key, value): """ Set values for this service. :param str key: key to set value for :param value: value of key to set :return: nothing """ if key not in self.keys: raise ValueError('key `%s` not in `%s`' % (key, self.keys)) # this handles data conversion to int, string, and tuples if value: if key == "startidx": value = int(value) elif key == "meta": value = str(value) else: value = utils.make_tuple_fromstr(value, str) if key == "dirs": self._dirs = value elif key == "files": self._configs = value elif key == "startidx": self._startindex = value elif key == "cmdup": self._startup = value elif key == "cmddown": self._shutdown = value elif key == "cmdval": self._validate = value elif key == "meta": self._meta = value elif key == "starttime": self._starttime = value
def value_to_params(doc, name, value): """ Helper to convert a parameter to a paramlist. Returns an XML paramlist, or None if the value does not expand to multiple values. :param xml.dom.minidom.Document doc: xml document :param name: name of element for params :param str value: value string to convert to tuple :return: xml document with added params or None, when an invalid value has been provided """ try: values = utils.make_tuple_fromstr(value, str) except SyntaxError: logger.exception("error in value string to param list") return None if not hasattr(values, "__iter__"): return None if len(values) < 2: return None return xmlutils.add_param_list_to_parent(doc, parent=None, name=name, values=values)
def test_make_tuple_fromstr(self): # given no_args = "()" one_arg = "('one',)" two_args = "('one', 'two')" unicode_args = u"('one', 'two', 'three')" # when no_args = utils.make_tuple_fromstr(no_args, str) one_arg = utils.make_tuple_fromstr(one_arg, str) two_args = utils.make_tuple_fromstr(two_args, str) unicode_args = utils.make_tuple_fromstr(unicode_args, str) # then assert no_args == () assert len(one_arg) == 1 assert len(two_args) == 2 assert len(unicode_args) == 3
def _value_to_params(value): """ Helper to convert a parameter to a parameter tuple. :param str value: value string to convert to tuple :return: parameter tuple, None otherwise """ try: values = utils.make_tuple_fromstr(value, str) if not hasattr(values, "__iter__"): return None if len(values) < 2: return None return values except SyntaxError: logging.exception("error in value string to param list") return None
def _value_to_params(value): """ Helper to convert a parameter to a parameter tuple. :param str value: value string to convert to tuple :return: parameter tuple, None otherwise """ try: values = utils.make_tuple_fromstr(value, str) if not hasattr(values, "__iter__"): return None if len(values) < 2: return None return values except SyntaxError: logger.exception("error in value string to param list") return None