|
33 | 33 | __all__ = ["add", "add_macro", "add_map", "b", "clear", "codecs", "decode", "encode", "ensure_str", "examples", "guess", |
34 | 34 | "isb", "generate_strings_from_regex", "get_alphabet_from_mask", "handle_error", "is_native", |
35 | 35 | "list_categories", "list_encodings", "list_macros", "lookup", "maketrans", "os", "rank", "re", "register", |
36 | | - "remove", "remove_macro", "reset", "s2i", "search", "stopfunc", "BytesIO", "MASKS", "PY3", "_input", |
37 | | - "_stripl", "CodecMacro"] |
| 36 | + "remove", "reset", "s2i", "search", "stopfunc", "BytesIO", "MASKS", "PY3", "_input", "_stripl", "CodecMacro"] |
38 | 37 | CODECS_REGISTRY = None |
39 | 38 | CODECS_CATEGORIES = ["native", "custom"] |
40 | 39 | MASKS = { |
@@ -83,7 +82,7 @@ def __new__(cls, name): |
83 | 82 | self.codecs = [lookup(e, False) for e in self.codecs] # lookup(e, False) |
84 | 83 | self.parameters = {'name': name, 'category': "macro"} # ^ means that macros won't be nestable |
85 | 84 | # test examples to check that the chain of encodings works |
86 | | - for action, examples in (self.codecs[0].parameters.get('examples', {}) or {}).items(): |
| 85 | + for action, examples in (self.codecs[0].parameters.get('examples', {}) or {'enc-dec(': ["T3st str!"]}).items(): |
87 | 86 | if re.match(r"enc(-dec)?\(", action): |
88 | 87 | for e in (examples.keys() if action.startswith("enc(") else examples or []): |
89 | 88 | rd = re.match(r"\@random(?:\{(\d+(?:,(\d+))*?)\})?$", e) |
@@ -298,17 +297,23 @@ def add_macro(mname, *encodings): |
298 | 297 | :param mname: macro name |
299 | 298 | :param encodings: encoding names of the encodings to be chained with the macro |
300 | 299 | """ |
| 300 | + global PERS_MACROS |
301 | 301 | # check for name clash with alreday existing macros and codecs |
302 | 302 | if mname in MACROS or mname in PERS_MACROS: |
303 | 303 | raise ValueError("Macro name already exists") |
304 | 304 | try: |
305 | 305 | ci = lookup(mname, False) |
306 | 306 | raise ValueError("Macro name clashes with codec '%s'" % ci.name) |
307 | 307 | except LookupError: |
308 | | - #TODO: test if the encodings sequence can work, using an example from the first codec |
| 308 | + pass |
| 309 | + try: |
309 | 310 | PERS_MACROS[mname] = encodings |
| 311 | + CodecMacro(mname) |
310 | 312 | with open(PERS_MACROS_FILE, 'w') as f: |
311 | | - json.dump(PERS_MACROS, f) |
| 313 | + json.dump(PERS_MACROS, f, indent=2) |
| 314 | + except ValueError: |
| 315 | + del PERS_MACROS[mname] |
| 316 | + raise |
312 | 317 | codecs.add_macro = add_macro |
313 | 318 |
|
314 | 319 |
|
@@ -551,8 +556,8 @@ def __get_value(token, position, case_changed=False): |
551 | 556 |
|
552 | 557 | def clear(): |
553 | 558 | """ Clear codext's local registry of search functions. """ |
554 | | - global __codecs_registry |
555 | | - __codecs_registry = [] |
| 559 | + global __codecs_registry, MACROS, PERS_MACROS |
| 560 | + __codecs_registry, MACROS, PERS_MACROS = [], {}, {} |
556 | 561 | codecs.clear = clear |
557 | 562 |
|
558 | 563 |
|
@@ -648,34 +653,32 @@ def list_macros(): |
648 | 653 | return sorted(list(set(list(MACROS.keys()) + list(PERS_MACROS.keys())))) |
649 | 654 |
|
650 | 655 |
|
651 | | -def remove(encoding): |
652 | | - """ Remove all search functions matching the input encoding name from codext's local registry. """ |
| 656 | +def remove(name): |
| 657 | + """ Remove all search functions matching the input encoding name from codext's local registry or any macro with the |
| 658 | + given name. """ |
| 659 | + global __codecs_registry, MACROS, PERS_MACROS |
653 | 660 | tbr = [] |
654 | 661 | for search_function in __codecs_registry: |
655 | | - if search_function(encoding) is not None: |
| 662 | + if search_function(name) is not None: |
656 | 663 | tbr.append(search_function) |
657 | 664 | for search_function in tbr: |
658 | 665 | __codecs_registry.remove(search_function) |
659 | | -codecs.remove = remove |
660 | | - |
661 | | - |
662 | | -def remove_macro(name): |
663 | | - """ Remove the given macro from the macro registries. """ |
664 | 666 | try: |
665 | 667 | del MACROS[name] |
666 | 668 | except KeyError: |
667 | 669 | pass |
668 | 670 | try: |
669 | 671 | del PERS_MACROS[name] |
670 | 672 | with open(PERS_MACROS_FILE, 'w') as f: |
671 | | - json.dump(PERS_MACROS, f) |
| 673 | + json.dump(PERS_MACROS, f, indent=2) |
672 | 674 | except KeyError: |
673 | 675 | pass |
| 676 | +codecs.remove = remove |
674 | 677 |
|
675 | 678 |
|
676 | 679 | def reset(): |
677 | 680 | """ Reset codext's local registry of search functions and macros. """ |
678 | | - global CODECS_REGISTRY, MACROS, PERS_MACROS, __codecs_registry |
| 681 | + global __codecs_registry, CODECS_REGISTRY, MACROS, PERS_MACROS |
679 | 682 | clear() |
680 | 683 | d = os.path.dirname(__file__) |
681 | 684 | for pkg in sorted(os.listdir(d)): |
|
0 commit comments