| 811 | SUPPRESS_USAGE = "SUPPRESS"+"USAGE" |
| 812 | |
| 813 | class Values: |
| 814 | |
| 815 | def __init__(self, defaults=None): |
| 816 | if defaults: |
| 817 | for (attr, val) in defaults.items(): |
| 818 | setattr(self, attr, val) |
| 819 | |
| 820 | def __str__(self): |
| 821 | return str(self.__dict__) |
| 822 | |
| 823 | __repr__ = _repr |
| 824 | |
| 825 | def __eq__(self, other): |
| 826 | if isinstance(other, Values): |
| 827 | return self.__dict__ == other.__dict__ |
| 828 | elif isinstance(other, dict): |
| 829 | return self.__dict__ == other |
| 830 | else: |
| 831 | return NotImplemented |
| 832 | |
| 833 | def _update_careful(self, dict): |
| 834 | """ |
| 835 | Update the option values from an arbitrary dictionary, but only |
| 836 | use keys from dict that already have a corresponding attribute |
| 837 | in self. Any keys in dict without a corresponding attribute |
| 838 | are silently ignored. |
| 839 | """ |
| 840 | for attr in dir(self): |
| 841 | if attr in dict: |
| 842 | dval = dict[attr] |
| 843 | if dval is not None: |
| 844 | setattr(self, attr, dval) |
| 845 | |
| 846 | def _update_loose(self, dict): |
| 847 | """ |
| 848 | Update the option values from an arbitrary dictionary, |
| 849 | using all keys from the dictionary regardless of whether |
| 850 | they have a corresponding attribute in self or not. |
| 851 | """ |
| 852 | self.__dict__.update(dict) |
| 853 | |
| 854 | def _update(self, dict, mode): |
| 855 | if mode == "careful": |
| 856 | self._update_careful(dict) |
| 857 | elif mode == "loose": |
| 858 | self._update_loose(dict) |
| 859 | else: |
| 860 | raise ValueError("invalid update mode: %r" % mode) |
| 861 | |
| 862 | def read_module(self, modname, mode="careful"): |
| 863 | __import__(modname) |
| 864 | mod = sys.modules[modname] |
| 865 | self._update(vars(mod), mode) |
| 866 | |
| 867 | def read_file(self, filename, mode="careful"): |
| 868 | vars = {} |
| 869 | exec(open(filename).read(), vars) |
| 870 | self._update(vars, mode) |
no outgoing calls
searching dependent graphs…