| 16 | |
| 17 | |
| 18 | class AbstractEdits: |
| 19 | default_kwargs = { |
| 20 | "line": "hello world", |
| 21 | "cursor_offset": 5, |
| 22 | "cut_buffer": "there", |
| 23 | } |
| 24 | |
| 25 | def __init__(self, simple_edits=None, cut_buffer_edits=None): |
| 26 | self.simple_edits = {} if simple_edits is None else simple_edits |
| 27 | self.cut_buffer_edits = ( |
| 28 | {} if cut_buffer_edits is None else cut_buffer_edits |
| 29 | ) |
| 30 | self.awaiting_config = {} |
| 31 | |
| 32 | def add(self, key, func, overwrite=False): |
| 33 | if key in self: |
| 34 | if overwrite: |
| 35 | del self[key] |
| 36 | else: |
| 37 | raise ValueError(f"key {key!r} already has a mapping") |
| 38 | params = getargspec(func) |
| 39 | args = {k: v for k, v in self.default_kwargs.items() if k in params} |
| 40 | r = func(**args) |
| 41 | if len(r) == 2: |
| 42 | if hasattr(func, "kills"): |
| 43 | raise ValueError( |
| 44 | "function %r returns two values, but has a " |
| 45 | "kills attribute" % (func,) |
| 46 | ) |
| 47 | self.simple_edits[key] = func |
| 48 | elif len(r) == 3: |
| 49 | if not hasattr(func, "kills"): |
| 50 | raise ValueError( |
| 51 | "function %r returns three values, but has " |
| 52 | "no kills attribute" % (func,) |
| 53 | ) |
| 54 | self.cut_buffer_edits[key] = func |
| 55 | else: |
| 56 | raise ValueError(f"return type of function {func!r} not recognized") |
| 57 | |
| 58 | def add_config_attr(self, config_attr, func): |
| 59 | if config_attr in self.awaiting_config: |
| 60 | raise ValueError( |
| 61 | f"config attribute {config_attr!r} already has a mapping" |
| 62 | ) |
| 63 | self.awaiting_config[config_attr] = func |
| 64 | |
| 65 | def call(self, key, **kwargs): |
| 66 | func = self[key] |
| 67 | params = getargspec(func) |
| 68 | args = {k: v for k, v in kwargs.items() if k in params} |
| 69 | return func(**args) |
| 70 | |
| 71 | def __contains__(self, key): |
| 72 | return key in self.simple_edits or key in self.cut_buffer_edits |
| 73 | |
| 74 | def __getitem__(self, key): |
| 75 | if key in self.simple_edits: |
nothing calls this directly
no outgoing calls
no test coverage detected