# HG changeset patch # User Dan Smith dsmith@danplanet.com # Date 1333739555 25200 # Node ID 21a443c222d1cfdb043b07f858a7b58d5fcc26d6 # Parent 0f5732297a9aabd5dc2e7cb603e0e8f6def94195 FIXME: radio settings Bug #00
diff -r 0f5732297a9a -r 21a443c222d1 chirp/chirp_common.py --- a/chirp/chirp_common.py Fri Apr 06 10:39:28 2012 -0700 +++ b/chirp/chirp_common.py Fri Apr 06 12:12:35 2012 -0700 @@ -778,6 +778,12 @@ def get_features(self): return RadioFeatures()
+ def get_settings(self): + return None + + def set_settings(self, settings): + raise Exception("Not implemented") + def _get_name_raw(*args): cls = args[-1] return "%s %s" % (cls.VENDOR, cls.MODEL) diff -r 0f5732297a9a -r 21a443c222d1 chirp/settings.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chirp/settings.py Fri Apr 06 12:12:35 2012 -0700 @@ -0,0 +1,176 @@ +# Copyright 2012 Dan Smith dsmith@danplanet.com +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see http://www.gnu.org/licenses/. + +from chirp import chirp_common + +class InvalidValueError(Exception): + pass + +class InternalError(Exception): + pass + +class RadioSettingValue: + def __init__(self): + self.set_value(None) + + def set_value(self, value): + raise Exception("Unimplemented") + + def get_value(self): + return self._current + + def __trunc__(self): + return int(self.get_value()) + + def __str__(self): + return str(self.get_value()) + +class RadioSettingValueInteger(RadioSettingValue): + def __init__(self, min, max, current, step=1): + self._min = min + self._max = max + self._step = step + self.set_value(current) + + def set_value(self, value): + try: + value = int(value) + except: + raise InvalidValueError("An integer is required") + if value > self._max or value < self._min: + raise InvalidValueError("Value %i not in range %i-%i" % (value, + self._min, + self._max)) + self._current = value + + def get_min(self): + return self._min + + def get_max(self): + return self._max + + def get_step(self): + return self._step + +class RadioSettingValueBoolean(RadioSettingValue): + def __init__(self, current): + self.set_value(current) + + def set_value(self, value): + self._current = bool(value) + + def __str__(self): + return str(bool(self.get_value())) + +class RadioSettingValueList(RadioSettingValue): + def __init__(self, options, current): + self._options = options + self.set_value(current) + + def set_value(self, value): + if not value in self._options: + raise InvalidValueError("%s is not valid for this setting" % value) + self._current = value + + def get_options(self): + return self._options + + def __trunc__(self): + return self._options.index(self._current) + +class RadioSettingValueString(RadioSettingValue): + def __init__(self, minlength, maxlength, current, + autopad=True): + self._minlength = minlength + self._maxlength = maxlength + self._charset = chirp_common.CHARSET_ASCII + self._autopad = autopad + self._autostrip = autostrip + self.set_value(current) + + def set_charset(self, charset): + self._charset = charset + + def set_value(self, value): + if len(value) < self._minlength or len(value) > self._maxlength: + raise InvalidValueError("Value must be between %i and %i chars" % (\ + self._minlength, self._maxlength)) + if self._autopad: + value = value.ljust(self._maxlength) + for char in value: + if char not in self._charset: + raise InvalidValueError("Value contains invalid " + + "character `%s'" % char) + self._current = value + + def __str__(self): + return self._current.rstrip() + +class RadioSettingGroup: + def __init__(self, name, shortname): + self._name = name # Setting identifier + self._shortname = shortname # Short human-readable name/description + self.__doc__ = name # Longer explanation/documentation + self._elements = {} + self._element_order = [] + + def get_name(self): + return self._name + + def get_shortname(self): + return self._shortname + + def add_element(self, element): + self._elements[element.get_name()] = element + self._element_order.append(element.get_name()) + + def get_element(self, name): + return self._elements[name] + + def get_elements(self): + return [self._elements[name] for name in self._element_order] + + def get_element_names(self): + return self._elemetn_order + + def set_doc(self, doc): + self.__doc__ = doc + + def __str__(self): + s = "{Settings Group %s:\n" % self._name + for element in self._elements.values(): + s += "%s: %s\n" % (element.get_name(), element.get_value()) + s += "}" + return s + +class RadioSetting(RadioSettingGroup): + def __init__(self, name, value): + self._name = name + self._value = value + + if not isinstance(value, RadioSettingValue): + raise InternalError("Incorrect type") + + def set_value(self, value): + self._value.set_value(value) + + def get_value(self): + return self._value + + def __str__(self): + return str(self._value) + + def __repr__(self): + return "[RadioSetting %s:%s]" % (self._name, self._value)