def cast_param_type_from_string(cls, param_value, param_type): ''' Parameters are saved in a text format, so we have to cast them to the appropriate type on loading. This function does exactly that. @param param_value: the actual value of the parameter, in a string format @param param_type: the wanted type @returns something: the casted param_value ''' if param_type in cls._type_converter: return cls._type_converter[param_type](param_value) elif param_type == cls.TYPE_BOOL: if param_value == "True": return True elif param_value == "False": return False else: raise Exception("Unrecognized bool value '%s'" % param_type) elif param_type == cls.TYPE_PASSWORD: if param_value == -1: return None return Keyring().get_password(int(param_value)) elif param_type == cls.TYPE_LIST_OF_STRINGS: the_list = param_value.split(",") if not isinstance(the_list, list): the_list = [the_list] return the_list else: raise NotImplemented("I don't know what type is '%s'" % param_type)
def cast_param_type_to_string(self, param_type, param_value): ''' Inverse of cast_param_type_from_string @param param_value: the actual value of the parameter @param param_type: the type of the parameter (password...) @returns something: param_value casted to string ''' if param_type == GenericBackend.TYPE_PASSWORD: if param_value is None: return str(-1) else: return str(Keyring().set_password( "GTG stored password -" + self.get_id(), param_value)) elif param_type == GenericBackend.TYPE_LIST_OF_STRINGS: if param_value == []: return "" return reduce(lambda a, b: a + "," + b, param_value) else: return str(param_value)