Move conversion code into separate module
This commit is contained in:
parent
29e5e21825
commit
bef6ad9438
BIN
__pycache__/convert_file_encoding.cpython-312.pyc
Normal file
BIN
__pycache__/convert_file_encoding.cpython-312.pyc
Normal file
Binary file not shown.
33
convert_file_encoding.py
Normal file
33
convert_file_encoding.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def convert_encoding(filepath):
|
||||||
|
try:
|
||||||
|
with open(filepath, 'r', encoding='cp1251', errors='strict') as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
with open(filepath, 'w', encoding='utf-8', errors='strict') as f:
|
||||||
|
f.write(content)
|
||||||
|
|
||||||
|
print(f"Success: {filepath}")
|
||||||
|
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
print(f"Error: {filepath} - not cp1251 encoded")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error: {filepath} - {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
|
def process_directory(root_dir):
|
||||||
|
if not os.path.isdir(root_dir):
|
||||||
|
print(f"Error: '{root_dir}' is not a valid directory")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
for root, _, files in os.walk(root_dir):
|
||||||
|
for file in files:
|
||||||
|
if file.endswith(('.cue', '.log')):
|
||||||
|
file_path = os.path.join(root, file)
|
||||||
|
convert_encoding(file_path)
|
||||||
|
|
||||||
|
def execute(args):
|
||||||
|
process_directory(args.directory)
|
@ -1,33 +1,14 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import argparse
|
import argparse
|
||||||
|
import convert_file_encoding
|
||||||
def convert_encoding(filepath):
|
|
||||||
try:
|
|
||||||
with open(filepath, 'r', encoding='cp1251', errors='strict') as f:
|
|
||||||
content = f.read()
|
|
||||||
|
|
||||||
with open(filepath, 'w', encoding='utf-8', errors='strict') as f:
|
|
||||||
f.write(content)
|
|
||||||
|
|
||||||
print(f"Success: {filepath}")
|
|
||||||
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
print(f"Error: {filepath} - not cp1251 encoded")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error: {filepath} - {str(e)}")
|
|
||||||
|
|
||||||
def process_directory(root_dir):
|
|
||||||
for root, _, files in os.walk(root_dir):
|
|
||||||
for file in files:
|
|
||||||
if file.endswith(('.cue', '.log')):
|
|
||||||
file_path = os.path.join(root, file)
|
|
||||||
convert_encoding(file_path)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser(
|
top_parser = argparse.ArgumentParser()
|
||||||
|
subparsers = top_parser.add_subparsers()
|
||||||
|
|
||||||
|
parser = subparsers.add_parser(
|
||||||
|
'convert',
|
||||||
description='Recursively convert .cue and .log files from cp1251 to utf-8',
|
description='Recursively convert .cue and .log files from cp1251 to utf-8',
|
||||||
epilog='Example: python convert_encoding.py /path/to/directory'
|
epilog='Example: python convert_encoding.py /path/to/directory'
|
||||||
)
|
)
|
||||||
@ -36,11 +17,9 @@ if __name__ == "__main__":
|
|||||||
type=str,
|
type=str,
|
||||||
help='Path to target directory for conversion'
|
help='Path to target directory for conversion'
|
||||||
)
|
)
|
||||||
|
parser.set_defaults(func=convert_file_encoding.execute)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = top_parser.parse_args()
|
||||||
|
print(args)
|
||||||
|
args.func(args)
|
||||||
|
|
||||||
if not os.path.isdir(args.directory):
|
|
||||||
print(f"Error: '{args.directory}' is not a valid directory")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
process_directory(args.directory)
|
|
Loading…
Reference in New Issue
Block a user