cengal.text_processing.text_patch.brackets.versions.v_0.brackets

Module Docstring Docstrings: http://www.python.org/dev/peps/pep-0257/

 1#!/usr/bin/env python
 2# coding=utf-8
 3
 4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>
 5# 
 6# Licensed under the Apache License, Version 2.0 (the "License");
 7# you may not use this file except in compliance with the License.
 8# You may obtain a copy of the License at
 9# 
10#     http://www.apache.org/licenses/LICENSE-2.0
11# 
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18"""
19Module Docstring
20Docstrings: http://www.python.org/dev/peps/pep-0257/
21"""
22
23__author__ = "ButenkoMS <gtalk@butenkoms.space>"
24__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
25__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
26__license__ = "Apache License, Version 2.0"
27__version__ = "4.4.1"
28__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
29__email__ = "gtalk@butenkoms.space"
30# __status__ = "Prototype"
31__status__ = "Development"
32# __status__ = "Production"
33
34
35from cengal.text_processing.encoding_detection import detect_and_decode
36from cengal.text_processing.brackets_processing import find_text_in_brackets, BracketPair
37from cengal.text_processing.text_processing import Text, replace_text, replace_slice, DEFAULT_ENCODING
38from typing import List, Tuple, Optional, Callable
39
40
41def patch_text(
42    text: Text, 
43    patch: List[Tuple[BracketPair, Text]], 
44    count: int = 1, 
45    encoding: Optional[str] = DEFAULT_ENCODING, 
46    normalizer: Optional[Callable] = None
47    ) -> Text:
48    iter_num: int = 0
49    sub_iter_num: int = -1
50    while (0 != sub_iter_num) and ((iter_num < count) if (0 <= count) else True):
51        iter_num += 1
52        sub_iter_num = 0
53        for brackets_pair, after in patch:
54            old_text_slice = find_text_in_brackets(text, brackets_pair, encoding=encoding, normalizer=normalizer)
55            if old_text_slice is None:
56                continue
57
58            text, _ = replace_slice(text, old_text_slice, after, encoding=encoding, normalizer=normalizer)
59            sub_iter_num += 1
60
61    return text
def patch_text( text: Union[bytes, bytearray, str], patch: List[Tuple[cengal.text_processing.brackets_processing.versions.v_0.brackets.BracketPair, Union[bytes, bytearray, str]]], count: int = 1, encoding: Union[str, NoneType] = 'utf-8', normalizer: Union[Callable, NoneType] = None) -> Union[bytes, bytearray, str]:
42def patch_text(
43    text: Text, 
44    patch: List[Tuple[BracketPair, Text]], 
45    count: int = 1, 
46    encoding: Optional[str] = DEFAULT_ENCODING, 
47    normalizer: Optional[Callable] = None
48    ) -> Text:
49    iter_num: int = 0
50    sub_iter_num: int = -1
51    while (0 != sub_iter_num) and ((iter_num < count) if (0 <= count) else True):
52        iter_num += 1
53        sub_iter_num = 0
54        for brackets_pair, after in patch:
55            old_text_slice = find_text_in_brackets(text, brackets_pair, encoding=encoding, normalizer=normalizer)
56            if old_text_slice is None:
57                continue
58
59            text, _ = replace_slice(text, old_text_slice, after, encoding=encoding, normalizer=normalizer)
60            sub_iter_num += 1
61
62    return text