|
| 1 | +class Config: |
| 2 | + """ |
| 3 | + Configuration for `check50` behavior. |
| 4 | +
|
| 5 | + This class stores user-defined configuration options that influence |
| 6 | + check50's output formatting. |
| 7 | +
|
| 8 | + For developers of `check50`, you can extend the `Config` class by adding new |
| 9 | + variables to the `__init__`, which will automatically generate new "setter" |
| 10 | + functions to modify the default values. Additionally, if the new |
| 11 | + configuration needs to be validated before the user can modify it, add your |
| 12 | + validation into the `_validators` dictionary. |
| 13 | + """ |
| 14 | + def __init__(self): |
| 15 | + self.truncate_len = 10 |
| 16 | + self.dynamic_truncate = True |
| 17 | + |
| 18 | + # Create boolean validators for your variables here (if needed): |
| 19 | + # A help message is not required. |
| 20 | + self._validators = { |
| 21 | + "truncate_len": (lambda val: isinstance(val, int) and val >= 1, |
| 22 | + "truncate_len must be a positive integer"), |
| 23 | + } |
| 24 | + |
| 25 | + # Dynamically generates setter functions based on variable names and |
| 26 | + # the type of the default values |
| 27 | + self._generate_setters() |
| 28 | + |
| 29 | + def _generate_setters(self): |
| 30 | + def create_method(name, func): |
| 31 | + setattr(self.__class__, name, func) |
| 32 | + |
| 33 | + def make_toggle(attr): |
| 34 | + """Factory for making functions like `toggle_<attr_name>()`""" |
| 35 | + def toggler(self): |
| 36 | + setattr(self, attr, not getattr(self, attr)) |
| 37 | + return toggler |
| 38 | + |
| 39 | + def make_setter(attr): |
| 40 | + """Factory for making functions like `set_<attr_name>(arg)`""" |
| 41 | + def setter(self, value): |
| 42 | + # Get the entry in the dict of validators. |
| 43 | + # Check to see if the value passes the validator, and if it |
| 44 | + # didn't, display the help message, if any. |
| 45 | + validator_entry = self._validators.get(attr) |
| 46 | + |
| 47 | + if validator_entry: |
| 48 | + if isinstance(validator_entry, tuple): |
| 49 | + validator, help = validator_entry |
| 50 | + else: |
| 51 | + validator, help = validator_entry, None |
| 52 | + |
| 53 | + if not validator(value): |
| 54 | + error_msg = f"invalid value for {attr}: {value}" |
| 55 | + if help: |
| 56 | + error_msg += f", {help}" |
| 57 | + raise ValueError(error_msg) |
| 58 | + |
| 59 | + setattr(self, attr, value) |
| 60 | + return setter |
| 61 | + |
| 62 | + for attribute_name in dir(self): |
| 63 | + if attribute_name.startswith('_'): |
| 64 | + continue # skip "private" attributes (denoted with a prefix `_`) |
| 65 | + value = getattr(self, attribute_name) |
| 66 | + if callable(value): |
| 67 | + continue # skip functions/methods |
| 68 | + |
| 69 | + # For variables with the default boolean type, make a setter that |
| 70 | + # starts with `toggle_`. Otherwise, have it start with `set_`. |
| 71 | + if isinstance(value, bool): |
| 72 | + create_method(f"toggle_{attribute_name}", make_toggle(attribute_name)) |
| 73 | + else: |
| 74 | + create_method(f"set_{attribute_name}", make_setter(attribute_name)) |
| 75 | + |
| 76 | + |
| 77 | +config = Config() |
0 commit comments