paul@380 | 1 | #!/usr/bin/env python |
paul@380 | 2 | |
paul@380 | 3 | """ |
paul@380 | 4 | POSIX character set conversion functions. |
paul@380 | 5 | |
paul@380 | 6 | Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk> |
paul@380 | 7 | |
paul@380 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@380 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@380 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@380 | 11 | version. |
paul@380 | 12 | |
paul@380 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@380 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@380 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@380 | 16 | details. |
paul@380 | 17 | |
paul@380 | 18 | You should have received a copy of the GNU General Public License along with |
paul@380 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@380 | 20 | """ |
paul@380 | 21 | |
paul@380 | 22 | from __builtins__.types import check_int, check_string |
paul@386 | 23 | from native import iconv, iconv_close, iconv_open, iconv_reset |
paul@386 | 24 | |
paul@386 | 25 | # Errors produced by iconv. |
paul@386 | 26 | |
paul@386 | 27 | EINVAL = 22 |
paul@386 | 28 | EILSEQ = 84 |
paul@380 | 29 | |
paul@380 | 30 | class ConverterError(Exception): |
paul@380 | 31 | |
paul@380 | 32 | "An error indicating a failure involving a character set converter." |
paul@380 | 33 | |
paul@380 | 34 | pass |
paul@380 | 35 | |
paul@380 | 36 | class Converter: |
paul@380 | 37 | |
paul@380 | 38 | "A character set converter." |
paul@380 | 39 | |
paul@380 | 40 | def __init__(self, from_encoding, to_encoding): |
paul@380 | 41 | |
paul@380 | 42 | "Initialise conversion between 'from_encoding' and 'to_encoding'." |
paul@380 | 43 | |
paul@380 | 44 | check_string(from_encoding) |
paul@380 | 45 | check_string(to_encoding) |
paul@380 | 46 | self.__data__ = iconv_open(to_encoding, from_encoding) |
paul@386 | 47 | self.reset() |
paul@386 | 48 | |
paul@386 | 49 | def reset(self): |
paul@386 | 50 | |
paul@386 | 51 | "Reset the state of the converter." |
paul@386 | 52 | |
paul@386 | 53 | self.state = ["", 0, 0] |
paul@386 | 54 | self.result = [] |
paul@386 | 55 | iconv_reset(self.__data__) |
paul@380 | 56 | |
paul@380 | 57 | def close(self): |
paul@380 | 58 | |
paul@380 | 59 | "Close this converter." |
paul@380 | 60 | |
paul@380 | 61 | iconv_close(self.__data__) |
paul@380 | 62 | self.__data__ = None |
paul@380 | 63 | |
paul@386 | 64 | def feed(self, s): |
paul@380 | 65 | |
paul@386 | 66 | "Feed 's' to the converter." |
paul@380 | 67 | |
paul@380 | 68 | if self.__data__ is None: |
paul@380 | 69 | raise ConverterError |
paul@380 | 70 | |
paul@380 | 71 | check_string(s) |
paul@380 | 72 | |
paul@386 | 73 | _s, start, remaining = self.state |
paul@386 | 74 | |
paul@386 | 75 | if _s: |
paul@403 | 76 | self.state = [_s + s, start, remaining + s.bytelength()] |
paul@386 | 77 | else: |
paul@403 | 78 | self.state = [s, 0, s.bytelength()] |
paul@380 | 79 | |
paul@380 | 80 | while True: |
paul@380 | 81 | |
paul@380 | 82 | # Obtain converted text and update the state. |
paul@380 | 83 | |
paul@386 | 84 | try: |
paul@386 | 85 | out = iconv(self.__data__, self.state) |
paul@386 | 86 | |
paul@386 | 87 | # Incomplete input does not cause an exception. |
paul@386 | 88 | |
paul@386 | 89 | except OSError, exc: |
paul@386 | 90 | if exc.value == EINVAL: |
paul@386 | 91 | self.result.append(exc.arg) |
paul@386 | 92 | return |
paul@386 | 93 | else: |
paul@386 | 94 | raise |
paul@386 | 95 | |
paul@386 | 96 | # Add any returned text to the result. |
paul@386 | 97 | |
paul@386 | 98 | self.result.append(out) |
paul@380 | 99 | |
paul@380 | 100 | # Test for the end of the conversion. |
paul@380 | 101 | |
paul@386 | 102 | _s, start, remaining = self.state |
paul@386 | 103 | |
paul@380 | 104 | if not remaining: |
paul@386 | 105 | return |
paul@386 | 106 | |
paul@386 | 107 | def __str__(self): |
paul@386 | 108 | |
paul@386 | 109 | "Return the value of the converted string." |
paul@386 | 110 | |
paul@386 | 111 | return "".join(self.result) |
paul@380 | 112 | |
paul@380 | 113 | # vim: tabstop=4 expandtab shiftwidth=4 |