aboutsummaryrefslogtreecommitdiff
path: root/dodai/tools/himo.py
blob: 5a96f9106bb6bdc184461df36aaf3dc1437ec49e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Copyright (C) 2010  Leonard Thomas
#
# This file is part of Dodai.
#
# Dodai is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Dodai is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Dodai.  If not, see <http://www.gnu.org/licenses/>.

import chardet
import re
import sys
import unicodedata
from htmlentitydefs import name2codepoint
from htmlentitydefs import codepoint2name
from decimal import Decimal as D

class Himo(object):
    """
    A unicode-string object with some added features to help with
    unicode decoding and output conversions.
    """

    MAP = {169:u'(C)', 174:u'(R)', 8471:u'(P)'}

    def __init__(self, data, encoding=None):
        """
        data:       Accepts any type of string object (int, float,
                    string, unicode)

        encoding:   Character encoding to help with converting the input
                    into unicode

        The input data will be converted into an unicode object, unless
        the input data is already an unicode object.  If the param
        'encoding' is set, the input data will be converted to unicode
        using that value.  If no 'encoding' is given this object will
        attempt to figure out the encoding.  First the encoding of the
        operating system will be used.  If there are any errors, the
        chardet module will be used.  This object makes no guarantees
        that the correct encoding will be detected.

        """

        self._encoding = encoding or self._system_encoding()
        self.data = self._decode(data)

    def ascii(self):
        """
        Returns an ascii representation of this object value.
        Throws HimoAsciiError if this method was unable to
        convert a unicode character down to it's root character.
        For example if in your string you have a character
        like the letter 'e' but it has an accent mark over it,
        this method will convert that character to it's root
        character.  Thus 'e' with an accent mark over it will
        replaced with the regular letter 'e'.

        """
        out = []
        for char in self.data:
            if ord(char) < 127:
                out.append(char)
            elif ord(char) in self.MAP:
                out.append(self.MAP[ord(char)])
            else:
                num = unicodedata.decomposition(char).split(' ')[0]
                if num:
                    out.append(unichr(int(num, 16)))
                else:
                    print char
                    raise HimoAsciiError("Unable to convert 'u{0}' "\
                                    "character to ascii".format(ord(char)))
        return str(''.join(out))

    def html(self):
        """
        Returns a unicode string containing this object's value
        html enetity encoded.
        """
        out = []
        for char in self.data:
            out.append(self._html_char_encode(char))
        return ''.join(out)

    def decimal(self):
        """
        Returns a decimal object with the value of this object

        """

        return D(self.data)

    def _decode(self, data):
        # Returns a unicode string.  If data contains any html encoded
        # characters, the characters will be converted to their unicode
        # equivalent

        data = self._as_unicode(data)
        expression = re.compile(r'&(#?)(x?)(\w+);')
        return expression.subn(self._html_decode, data)[0]

    def _as_unicode(self, data):
        # Returns string as a unicode string

        if not isinstance(data, unicode):
            if not isinstance(data, str):
                data = str(data)
            try:
                data = data.decode(self._encoding)
            except UnicodeDecodeError:
                info = chardet.detect(data)
                self.encoding = info['encoding']
                data = data.decode(info['encoding'])
        return unicodedata.normalize('NFC', data)

    def _html_char_encode(self, char):
        # Returns an html version of the char

        number = ord(char)
        try:
            char = "&{0};".format(codepoint2name[number])
        except KeyError:
            if number > 127:
                char = "&#{0};".format(number)
        return char

    def _html_decode(self, values):
        # Returns the unicode character from the re.subn

        value = values.group(3)
        if values.group(1):
            if values.group(2):
                return unichr(int('0x{0}'.format(value), 16))
            else:
                return unichr(int(value))
        else:
            try:
                char = name2codepoint[value]
            except KeyError:
                return values.group()
            else:
                return unichr(char)

    def _system_encoding(self):
        # Returns the character encoding of the system

        encoding = sys.getfilesystemencoding()
        if not encoding:
            encoding = sys.getdefaultencoding()
        return encoding

    #def __cmp__(self, other):
    #    if self.__eq__(other):
    #        return 1
    #    else:
    #        pool = [str(self.data), str(other)]
    #        pool.sort()
    #        if pool[0] == self.data:
    #            return -1
    #        else:
    #            return 1


    def _is_himo(self, other):
        if hasattr(other, '_is_himo'):
            return True
        return False

    def __len__(self):
        return len(self.data)

    def __repr__(self):
        return repr(self.data)

    def __str__(self):
        return self.data.encode(self._encoding)

    def __iter__(self):
        for char in self.data:
            yield char

    def __int__(self):
        return int(self.data)

    def __float__(self):
        return float(self.data)

    def __eq__(self, other):
        if self._is_himo(other):
            other = other.data
        return self.data.__eq__(other)

    def __ne__(self, other):
        if self._is_himo(other):
            other = other.data
        return self.data.__ne__(other)

    def __gt__(self, other):
        if self._is_himo(other):
            other = other.data
        lines = [self.data, other]
        lines.sort()
        if lines[0] == self.data:
            return True
        else:
            return False

    def __lt__(self, other):
        if self._is_himo(other):
            other = other.data
        lines = [self.data, other]
        lines.sort()
        if lines[0] != self.data:
            return True
        else:
            return False

    def __cmp__(self, other):
        if self.__eq__(other):
            return 0
        elif self.__lt__(other):
            return -1
        else:
            return 1

    def __unicode__(self):
        return self.data

    def capitalize(self, *args, **kargs):
        return self.data.capitalize(*args, **kargs)

    def center(self, *args, **kargs):
        return self.data.center(*args, **kargs)

    def count(self, *args, **kargs):
        return self.data.count(*args, **kargs)

    def decode(self, *args, **kargs):
        return self.data.decode(*args, **kargs)

    def encode(self, *args, **kargs):
        return self.data.encode(*args, **kargs)

    def encode(self, *args, **kargs):
        return self.data.encode(*args, **kargs)

    def endswith(self, *args, **kargs):
        return self.data.endswith(*args, **kargs)

    def expandtabs(self, *args, **kargs):
        return self.data.expandtabs(*args, **kargs)

    def find(self, *args, **kargs):
        return self.data.find(*args, **kargs)

    def format(self, *args, **kargs):
        return self.data.format(*args, **kargs)

    def index(self, *args, **kargs):
        return self.data.index(*args, **kargs)

    def isalnum(self, *args, **kargs):
        return self.data.isalnum(*args, **kargs)

    def isalpha(self, *args, **kargs):
        return self.data.isalpha(*args, **kargs)

    def isdecimal(self, *args, **kargs):
        return self.data.isdecimal(*args, **kargs)

    def isdigit(self, *args, **kargs):
        return self.data.isdigit(*args, **kargs)

    def islower(self, *args, **kargs):
        return self.data.islower(*args, **kargs)

    def isnumeric(self, *args, **kargs):
        return self.data.isnumeric(*args, **kargs)

    def isspace(self, *args, **kargs):
        return self.data.isspace(*args, **kargs)

    def istitle(self, *args, **kargs):
        return self.data.istitle(*args, **kargs)

    def isupper(self, *args, **kargs):
        return self.data.isupper(*args, **kargs)

    def join(self, *args, **kargs):
        return self.data.join(*args, **kargs)

    def ljust(self, *args, **kargs):
        return self.data.ljust(*args, **kargs)

    def lower(self, *args, **kargs):
        return self.data.lower(*args, **kargs)

    def lstrip(self, *args, **kargs):
        return self.data.lstrip(*args, **kargs)

    def partition(self, *args, **kargs):
        return self.data.partition(*args, **kargs)

    def replace(self, *args, **kargs):
        return self.data.replace(*args, **kargs)

    def rfind(self, *args, **kargs):
        return self.data.rfind(*args, **kargs)

    def rindex(self, *args, **kargs):
        return self.data.rindex(*args, **kargs)

    def rjust(self, *args, **kargs):
        return self.data.rjust(*args, **kargs)

    def rpartition(self, *args, **kargs):
        return self.data.rpartition(*args, **kargs)

    def rsplit(self, *args, **kargs):
        return self.data.rsplit(*args, **kargs)

    def rstrip(self, *args, **kargs):
        return self.data.rstrip(*args, **kargs)

    def split(self, *args, **kargs):
        return self.data.split(*args, **kargs)

    def splitlines(self, *args, **kargs):
        return self.data.splitlines(*args, **kargs)

    def startswith(self, *args, **kargs):
        return self.data.startswith(*args, **kargs)

    def strip(self, *args, **kargs):
        return self.data.strip(*args, **kargs)

    def swapcase(self, *args, **kargs):
        return self.data.swapcase(*args, **kargs)

    def title(self, *args, **kargs):
        return self.data.title(*args, **kargs)

    def translate(self, *args, **kargs):
        return self.data.translate(*args, **kargs)

    def upper(self, *args, **kargs):
        return self.data.upper(*args, **kargs)

    def zfill(self, *args, **kargs):
        return self.data.zfill(*args, **kargs)

class HimoAsciiError(Exception):
    pass