From bef6ad9438144401a0635ea6bf4dbe7e3cdae7c7 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sat, 26 Apr 2025 15:10:55 +0300 Subject: [PATCH] Move conversion code into separate module --- .../convert_file_encoding.cpython-312.pyc | Bin 0 -> 2035 bytes convert_file_encoding.py | 33 ++++++++++++++ music-tools.py | 43 +++++------------- 3 files changed, 44 insertions(+), 32 deletions(-) create mode 100644 __pycache__/convert_file_encoding.cpython-312.pyc create mode 100644 convert_file_encoding.py diff --git a/__pycache__/convert_file_encoding.cpython-312.pyc b/__pycache__/convert_file_encoding.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4684f5694a65118fb8126ac321fdd706cadb934f GIT binary patch literal 2035 zcmah~%}*Og6rb4-dlzH#O#nfxc^BqJF|(d5~TC$Y?YjV2kH1&F3I*ITDY;^oBSl3}`mUh#CDFenY+%`vD5xRjgIE^FJ|D~~ATxS@_1fK0; ze)u9DFFP*5<6i=U<(JHMF%N9vT|ji&{<1TTzDM`Y`6kgDs0SH1!C*9jFgzJJ+_?91WF>)`YtpIB04hcD-ji@+r~_nVj)xRJdTVq z>=$ej&PV42PN1`l{H}3=b>axed)~a!LmY;wt6|g65J8EBHCc_#5Qn1cnr;xBNb&RL zba!7$yfYsTD~8c8rJT3vCMXS3w-nP%=?qA+q7^yCg4ypRoEBGNgwvIfO!$PZnhN3L zx*9V{?ME>cUZjjDR9a|6yuBD!;-;#_2nz}MIU-D}Q6(NSX9=Dr&af6U6^O-(L5oF# zaXJ{B)#j97XdxKawTKcn4N%mDkf{Xc<_$I6ZE9N72tw$BqMK6`Z^}MmAf6=E_Kt1T z8v&YDi?IvhE;?i?`C!3allA3>w|o!#_S_?f2#c@%jt?5ItWU2^e>b}{dcuO`xdVA> zEjRB&-=2Gzy0!iG`9Y<7b#!?&JGxidO8wfvPxNHPoG;gs_jK$F*G}9JZGoPiIKaUQ zeDS;jRbD+pn18n*xUfZcI5g1Xbr4|O_syiTeI_@Q3s&Rdq= z-!zQa-Fkt_)|U5gvb#5V&@T|%0niq)$1P%;usj*fl-*l=d(`li~v5c?D!toAFl=Ml%R!5r zkoE`~eU_l^1v}j{z5#I;60h-eeZ^M)AzWg^9XT#*Rp4cTMA_x4(gka5G!^qjT}_ft!9=p`L^!;sz9Oj z>iyA;(feZ?V_WrG=Hu44vtprI+HBsh?tJD1|7R?K977`LLM78GwkkOQ%KC*rl*?+l z{Keo);c-7>9g>KpL;Eq|Li&th(63Xx8mF?Tgy&5~@1i#N86FUDAzVXNUXQKC_PJ}i z4@`buZ-z;4rGmC23*8F)8Cf3IU7rfK<}`UestoEhYx*Xm1qARO<0nXbf+~(2ZX7(4 NMBH`Ui1BqR_CJKoxbOe~ literal 0 HcmV?d00001 diff --git a/convert_file_encoding.py b/convert_file_encoding.py new file mode 100644 index 0000000..2ee45e1 --- /dev/null +++ b/convert_file_encoding.py @@ -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) \ No newline at end of file diff --git a/music-tools.py b/music-tools.py index bbd25ec..7c075af 100755 --- a/music-tools.py +++ b/music-tools.py @@ -1,33 +1,14 @@ #!/usr/bin/env python3 -import os -import sys import argparse - -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) +import convert_file_encoding 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', epilog='Example: python convert_encoding.py /path/to/directory' ) @@ -36,11 +17,9 @@ if __name__ == "__main__": type=str, help='Path to target directory for conversion' ) + parser.set_defaults(func=convert_file_encoding.execute) - args = parser.parse_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) \ No newline at end of file + args = top_parser.parse_args() + print(args) + args.func(args) + \ No newline at end of file