cengal.file_system.file_patch.simple.versions.v_0.simple

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