|
| 1 | +# -*- coding: UTF-8 -*- |
| 2 | +"""Braille Codec - braille content encoding. |
| 3 | +
|
| 4 | +This codec: |
| 5 | +- en/decodes strings from str to str |
| 6 | +- en/decodes strings from bytes to bytes |
| 7 | +- decodes file content to str (read) |
| 8 | +- encodes file content from str to bytes (write) |
| 9 | +""" |
| 10 | +from codext.__common__ import * |
| 11 | + |
| 12 | + |
| 13 | +if PY3: |
| 14 | + ENCMAP = { |
| 15 | + # digits |
| 16 | + '0': '⠴', '1': '⠂', '2': '⠆', '3': '⠒', '4': '⠲', '5': '⠢', '6': '⠖', |
| 17 | + '7': '⠶', '8': '⠦', '9': '⠔', |
| 18 | + # letters |
| 19 | + 'a': '⠁', 'b': '⠃', 'c': '⠉', 'd': '⠙', 'e': '⠑', 'f': '⠋', 'g': '⠛', |
| 20 | + 'h': '⠓', 'i': '⠊', 'j': '⠚', 'k': '⠅', 'l': '⠇', 'm': '⠍', 'n': '⠝', |
| 21 | + 'o': '⠕', 'p': '⠏', 'q': '⠟', 'r': '⠗', 's': '⠎', 't': '⠞', 'u': '⠥', |
| 22 | + 'v': '⠧', 'w': '⠺', 'x': '⠭', 'y': '⠽', 'z': '⠵', |
| 23 | + # punctuation |
| 24 | + ' ': '⠀', '!': '⠮', '"': '⠐', '#': '⠼', '$': '⠫', '%': '⠩', '&': '⠯', |
| 25 | + ':': '⠱', ';': '⠰', '<': '⠣', '=': '⠿', '>': '⠜', '?': '⠹', '@': '⠈', |
| 26 | + "'": '⠄', '(': '⠷', ')': '⠾', '*': '⠡', '+': '⠬', ',': '⠠', '-': '⠤', |
| 27 | + '.': '⠨', '/': '⠌', '[': '⠪', '\\': '⠳', ']': '⠻', '^': '⠘', |
| 28 | + '_': '⠸', |
| 29 | + } |
| 30 | + DECMAP = {v: k for k, v in ENCMAP.items()} |
| 31 | + REPLACE_CHAR = "?" |
| 32 | + |
| 33 | + |
| 34 | + class BrailleError(ValueError): |
| 35 | + pass |
| 36 | + |
| 37 | + |
| 38 | + class BrailleDecodeError(BrailleError): |
| 39 | + pass |
| 40 | + |
| 41 | + |
| 42 | + class BrailleEncodeError(BrailleError): |
| 43 | + pass |
| 44 | + |
| 45 | + |
| 46 | + def braille_encode(text, errors="strict"): |
| 47 | + r = "" |
| 48 | + for i, c in enumerate(ensure_str(text)): |
| 49 | + try: |
| 50 | + r += ENCMAP[c] |
| 51 | + except KeyError: |
| 52 | + if errors == "strict": |
| 53 | + raise BrailleEncodeError("'braille' codec can't encode " |
| 54 | + "character '{}' in position {}" |
| 55 | + .format(c, i)) |
| 56 | + elif errors == "replace": |
| 57 | + r += REPLACE_CHAR |
| 58 | + elif errors == "ignore": |
| 59 | + continue |
| 60 | + else: |
| 61 | + raise ValueError("Unsupported error handling {}" |
| 62 | + .format(errors)) |
| 63 | + return r, len(text) |
| 64 | + |
| 65 | + |
| 66 | + def braille_decode(text, errors="strict"): |
| 67 | + r = "" |
| 68 | + for i, c in enumerate(ensure_str(text)): |
| 69 | + try: |
| 70 | + r += DECMAP[c] |
| 71 | + except KeyError: |
| 72 | + if errors == "strict": |
| 73 | + raise BrailleDecodeError("'braille' codec can't decode " |
| 74 | + "character '{}' in position {}" |
| 75 | + .format(c, i)) |
| 76 | + elif errors == "replace": |
| 77 | + r += REPLACE_CHAR |
| 78 | + elif errors == "ignore": |
| 79 | + continue |
| 80 | + else: |
| 81 | + raise ValueError("Unsupported error handling {}" |
| 82 | + .format(errors)) |
| 83 | + return r, len(text) |
| 84 | + |
| 85 | + |
| 86 | + add("braille", braille_encode, braille_decode) |
0 commit comments