80a844ff34c3af1fb6a247a4ec4034c64d0fc33d
[sdc/sdc-distribution-client.git] /
1 ######################## BEGIN LICENSE BLOCK ########################
2 # The Original Code is mozilla.org code.
3 #
4 # The Initial Developer of the Original Code is
5 # Netscape Communications Corporation.
6 # Portions created by the Initial Developer are Copyright (C) 1998
7 # the Initial Developer. All Rights Reserved.
8 #
9 # Contributor(s):
10 #   Mark Pilgrim - port to Python
11 #
12 # This library is free software; you can redistribute it and/or
13 # modify it under the terms of the GNU Lesser General Public
14 # License as published by the Free Software Foundation; either
15 # version 2.1 of the License, or (at your option) any later version.
16 #
17 # This library is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 # Lesser General Public License for more details.
21 #
22 # You should have received a copy of the GNU Lesser General Public
23 # License along with this library; if not, write to the Free Software
24 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 # 02110-1301  USA
26 ######################### END LICENSE BLOCK #########################
27
28 from . import constants
29 from .escsm import (HZSMModel, ISO2022CNSMModel, ISO2022JPSMModel,
30                     ISO2022KRSMModel)
31 from .charsetprober import CharSetProber
32 from .codingstatemachine import CodingStateMachine
33 from .compat import wrap_ord
34
35
36 class EscCharSetProber(CharSetProber):
37     def __init__(self):
38         CharSetProber.__init__(self)
39         self._mCodingSM = [
40             CodingStateMachine(HZSMModel),
41             CodingStateMachine(ISO2022CNSMModel),
42             CodingStateMachine(ISO2022JPSMModel),
43             CodingStateMachine(ISO2022KRSMModel)
44         ]
45         self.reset()
46
47     def reset(self):
48         CharSetProber.reset(self)
49         for codingSM in self._mCodingSM:
50             if not codingSM:
51                 continue
52             codingSM.active = True
53             codingSM.reset()
54         self._mActiveSM = len(self._mCodingSM)
55         self._mDetectedCharset = None
56
57     def get_charset_name(self):
58         return self._mDetectedCharset
59
60     def get_confidence(self):
61         if self._mDetectedCharset:
62             return 0.99
63         else:
64             return 0.00
65
66     def feed(self, aBuf):
67         for c in aBuf:
68             # PY3K: aBuf is a byte array, so c is an int, not a byte
69             for codingSM in self._mCodingSM:
70                 if not codingSM:
71                     continue
72                 if not codingSM.active:
73                     continue
74                 codingState = codingSM.next_state(wrap_ord(c))
75                 if codingState == constants.eError:
76                     codingSM.active = False
77                     self._mActiveSM -= 1
78                     if self._mActiveSM <= 0:
79                         self._mState = constants.eNotMe
80                         return self.get_state()
81                 elif codingState == constants.eItsMe:
82                     self._mState = constants.eFoundIt
83                     self._mDetectedCharset = codingSM.get_coding_state_machine()  # nopep8
84                     return self.get_state()
85
86         return self.get_state()