Set the value of a property. Supports limitd property value types: `bool`, `int` and `str`. Args: property_name: Name of the property. property_val: Value of the property. If the property has `bool` type and this argument has `str` type, the `str` value will be parsed a
(self, property_name, property_val)
| 55 | return self._config[property_name] |
| 56 | |
| 57 | def set(self, property_name, property_val): |
| 58 | """Set the value of a property. |
| 59 | |
| 60 | Supports limitd property value types: `bool`, `int` and `str`. |
| 61 | |
| 62 | Args: |
| 63 | property_name: Name of the property. |
| 64 | property_val: Value of the property. If the property has `bool` type and |
| 65 | this argument has `str` type, the `str` value will be parsed as a `bool` |
| 66 | |
| 67 | Raises: |
| 68 | ValueError: if a `str` property_value fails to be parsed as a `bool`. |
| 69 | KeyError: if `property_name` is an invalid property name. |
| 70 | """ |
| 71 | if property_name not in self._config: |
| 72 | raise KeyError("%s is not a valid property name." % property_name) |
| 73 | |
| 74 | orig_val = self._config[property_name] |
| 75 | if isinstance(orig_val, bool): |
| 76 | if isinstance(property_val, str): |
| 77 | if property_val.lower() in ("1", "true", "t", "yes", "y", "on"): |
| 78 | property_val = True |
| 79 | elif property_val.lower() in ("0", "false", "f", "no", "n", "off"): |
| 80 | property_val = False |
| 81 | else: |
| 82 | raise ValueError( |
| 83 | "Invalid string value for bool type: %s" % property_val) |
| 84 | else: |
| 85 | property_val = bool(property_val) |
| 86 | elif isinstance(orig_val, int): |
| 87 | property_val = int(property_val) |
| 88 | elif isinstance(orig_val, str): |
| 89 | property_val = str(property_val) |
| 90 | else: |
| 91 | raise TypeError("Unsupported property type: %s" % type(orig_val)) |
| 92 | self._config[property_name] = property_val |
| 93 | self._save_to_file() |
| 94 | |
| 95 | # Invoke set-callback. |
| 96 | if property_name in self._set_callbacks: |
| 97 | self._set_callbacks[property_name](self._config) |
| 98 | |
| 99 | def set_callback(self, property_name, callback): |
| 100 | """Set a set-callback for given property. |