cengal.file_system.file_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
35import codecs
36from cengal.text_processing.encoding_detection import detect_and_decode
37from cengal.text_processing.brackets_processing import BracketPair
38from cengal.text_processing.text_patch.brackets import patch_text
39from cengal.text_processing.text_processing import DEFAULT_ENCODING
40from typing import List, Tuple, Optional, Callable
41
42
43
44def patch_text_file(
45        file_path: str, 
46        patch: List[Tuple[BracketPair, str]], 
47        count: int = 1, 
48        encoding: Optional[str] = DEFAULT_ENCODING, 
49        normalizer: Optional[Callable] = None,
50        fallback_to_utf_8: bool = True,
51        replace_errors: bool = True,
52        ):
53    with open(file_path, 'r+b') as file:
54        text, text_encoding, bom_bytes = detect_and_decode(file.read())
55        text = patch_text(text, patch, count, encoding, normalizer)
56        file.seek(0, 0)
57        file.truncate(0)
58        try:
59            try:
60                data = bom_bytes + text.encode(text_encoding)
61            except UnicodeEncodeError:
62                if fallback_to_utf_8:
63                    bom_bytes = codecs.BOM_UTF8
64                    text_encoding = 'utf-8'
65                    data = bom_bytes + text.encode(text_encoding)
66                else:
67                    raise
68        except UnicodeEncodeError:
69            if replace_errors:
70                data = bom_bytes + text.encode(text_encoding, errors='replace')
71            else:
72                raise
73        
74        file.write(data)
75
76
77def patch_bin_file(
78        file_path: str, 
79        patch: List[Tuple[BracketPair, bytes]], 
80        count: int = 1, 
81        encoding: Optional[str] = DEFAULT_ENCODING, 
82        normalizer: Optional[Callable] = None
83        ):
84    with open(file_path, 'r+b') as file:
85        data: bytes = file.read()
86        data = patch_text(data, patch, count, encoding, normalizer)
87        file.seek(0, 0)
88        file.truncate(0)
89        file.write(data)
def patch_text_file( file_path: str, patch: List[Tuple[cengal.text_processing.brackets_processing.versions.v_0.brackets.BracketPair, str]], count: int = 1, encoding: Union[str, NoneType] = 'utf-8', normalizer: Union[Callable, NoneType] = None, fallback_to_utf_8: bool = True, replace_errors: bool = True):
45def patch_text_file(
46        file_path: str, 
47        patch: List[Tuple[BracketPair, str]], 
48        count: int = 1, 
49        encoding: Optional[str] = DEFAULT_ENCODING, 
50        normalizer: Optional[Callable] = None,
51        fallback_to_utf_8: bool = True,
52        replace_errors: bool = True,
53        ):
54    with open(file_path, 'r+b') as file:
55        text, text_encoding, bom_bytes = detect_and_decode(file.read())
56        text = patch_text(text, patch, count, encoding, normalizer)
57        file.seek(0, 0)
58        file.truncate(0)
59        try:
60            try:
61                data = bom_bytes + text.encode(text_encoding)
62            except UnicodeEncodeError:
63                if fallback_to_utf_8:
64                    bom_bytes = codecs.BOM_UTF8
65                    text_encoding = 'utf-8'
66                    data = bom_bytes + text.encode(text_encoding)
67                else:
68                    raise
69        except UnicodeEncodeError:
70            if replace_errors:
71                data = bom_bytes + text.encode(text_encoding, errors='replace')
72            else:
73                raise
74        
75        file.write(data)
def patch_bin_file( file_path: str, patch: List[Tuple[cengal.text_processing.brackets_processing.versions.v_0.brackets.BracketPair, bytes]], count: int = 1, encoding: Union[str, NoneType] = 'utf-8', normalizer: Union[Callable, NoneType] = None):
78def patch_bin_file(
79        file_path: str, 
80        patch: List[Tuple[BracketPair, bytes]], 
81        count: int = 1, 
82        encoding: Optional[str] = DEFAULT_ENCODING, 
83        normalizer: Optional[Callable] = None
84        ):
85    with open(file_path, 'r+b') as file:
86        data: bytes = file.read()
87        data = patch_text(data, patch, count, encoding, normalizer)
88        file.seek(0, 0)
89        file.truncate(0)
90        file.write(data)