]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/pip/_vendor/chardet/charsetgroupprober.py
1 ######################## BEGIN LICENSE BLOCK ########################
2 # The Original Code is Mozilla Communicator client code.
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.
10 # Mark Pilgrim - port to Python
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.
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.
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
26 ######################### END LICENSE BLOCK #########################
28 from typing
import List
, Optional
, Union
30 from .charsetprober
import CharSetProber
31 from .enums
import LanguageFilter
, ProbingState
34 class CharSetGroupProber(CharSetProber
):
35 def __init__(self
, lang_filter
: LanguageFilter
= LanguageFilter
.NONE
) -> None:
36 super().__init
__(lang_filter
=lang_filter
)
38 self
.probers
: List
[CharSetProber
] = []
39 self
._best
_guess
_prober
: Optional
[CharSetProber
] = None
41 def reset(self
) -> None:
44 for prober
in self
.probers
:
48 self
._best
_guess
_prober
= None
51 def charset_name(self
) -> Optional
[str]:
52 if not self
._best
_guess
_prober
:
54 if not self
._best
_guess
_prober
:
56 return self
._best
_guess
_prober
.charset_name
59 def language(self
) -> Optional
[str]:
60 if not self
._best
_guess
_prober
:
62 if not self
._best
_guess
_prober
:
64 return self
._best
_guess
_prober
.language
66 def feed(self
, byte_str
: Union
[bytes, bytearray
]) -> ProbingState
:
67 for prober
in self
.probers
:
70 state
= prober
.feed(byte_str
)
73 if state
== ProbingState
.FOUND_IT
:
74 self
._best
_guess
_prober
= prober
75 self
._state
= ProbingState
.FOUND_IT
77 if state
== ProbingState
.NOT_ME
:
80 if self
._active
_num
<= 0:
81 self
._state
= ProbingState
.NOT_ME
85 def get_confidence(self
) -> float:
87 if state
== ProbingState
.FOUND_IT
:
89 if state
== ProbingState
.NOT_ME
:
92 self
._best
_guess
_prober
= None
93 for prober
in self
.probers
:
95 self
.logger
.debug("%s not active", prober
.charset_name
)
97 conf
= prober
.get_confidence()
99 "%s %s confidence = %s", prober
.charset_name
, prober
.language
, conf
103 self
._best
_guess
_prober
= prober
104 if not self
._best
_guess
_prober
: