escprober.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. from . import constants
  28. from .escsm import (HZSMModel, ISO2022CNSMModel, ISO2022JPSMModel,
  29. ISO2022KRSMModel)
  30. from .charsetprober import CharSetProber
  31. from .codingstatemachine import CodingStateMachine
  32. from .compat import wrap_ord
  33. class EscCharSetProber(CharSetProber):
  34. def __init__(self):
  35. CharSetProber.__init__(self)
  36. self._mCodingSM = [
  37. CodingStateMachine(HZSMModel),
  38. CodingStateMachine(ISO2022CNSMModel),
  39. CodingStateMachine(ISO2022JPSMModel),
  40. CodingStateMachine(ISO2022KRSMModel)
  41. ]
  42. self.reset()
  43. def reset(self):
  44. CharSetProber.reset(self)
  45. for codingSM in self._mCodingSM:
  46. if not codingSM:
  47. continue
  48. codingSM.active = True
  49. codingSM.reset()
  50. self._mActiveSM = len(self._mCodingSM)
  51. self._mDetectedCharset = None
  52. def get_charset_name(self):
  53. return self._mDetectedCharset
  54. def get_confidence(self):
  55. if self._mDetectedCharset:
  56. return 0.99
  57. else:
  58. return 0.00
  59. def feed(self, aBuf):
  60. for c in aBuf:
  61. # PY3K: aBuf is a byte array, so c is an int, not a byte
  62. for codingSM in self._mCodingSM:
  63. if not codingSM:
  64. continue
  65. if not codingSM.active:
  66. continue
  67. codingState = codingSM.next_state(wrap_ord(c))
  68. if codingState == constants.eError:
  69. codingSM.active = False
  70. self._mActiveSM -= 1
  71. if self._mActiveSM <= 0:
  72. self._mState = constants.eNotMe
  73. return self.get_state()
  74. elif codingState == constants.eItsMe:
  75. self._mState = constants.eFoundIt
  76. self._mDetectedCharset = codingSM.get_coding_state_machine() # nopep8
  77. return self.get_state()
  78. return self.get_state()