cengal.file_system.file_patch.generic.versions.v_0.generic
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.text_processing import DEFAULT_ENCODING 38from typing import List, Tuple, Optional, Callable, Any 39 40 41 42def patch_text_file( 43 file_path: str, 44 patch: Any, 45 patch_text: Callable, 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: Any, 79 patch_text: Callable, 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: Any, patch_text: Callable, count: int = 1, encoding: Union[str, NoneType] = 'utf-8', normalizer: Union[Callable, NoneType] = None, fallback_to_utf_8: bool = True, replace_errors: bool = True):
43def patch_text_file( 44 file_path: str, 45 patch: Any, 46 patch_text: Callable, 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: Any, patch_text: Callable, count: int = 1, encoding: Union[str, NoneType] = 'utf-8', normalizer: Union[Callable, NoneType] = None):
77def patch_bin_file( 78 file_path: str, 79 patch: Any, 80 patch_text: Callable, 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)