Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re, argparse, sys, os
- from colorama import just_fix_windows_console
- from termcolor import cprint
- from termcolor._types import Color
- # thanks https://github.com/strohel/works/blob/
- # 4abb93baab1d89e2275af6ec04e0b2e4351a7e76/dipl-DVD-ROM/Ceygen/support/visualize_stats.py#L#18
- tempfile = "temp__batch_remove_invalid.txt"
- # ----- ----- ----- ----- ----- -----
- # Arguments Handling
- # ----- ----- ----- ----- ----- -----
- just_fix_windows_console()
- parser = argparse.ArgumentParser(
- formatter_class=argparse.RawTextHelpFormatter,
- description="""
- description:
- Transforms strings into valid filenames for Windows' Batch.
- Accepts inputs:
- -------
- list[str]
- any form
- Returns
- -------
- list[str]
- string, valid batch filename
- """, usage="")
- parser.add_argument("-d", "-dbg", "--debug",
- action="store_true",
- help="Show debug log.")
- parser.add_argument("-f",
- dest="file",
- help="Consume inputs from given file.")
- parser.add_argument("--file",
- dest="file",
- help=argparse.SUPPRESS)
- parser.add_argument("input",
- nargs=argparse.REMAINDER, # allows accepting hyphens
- help="Input, in supported form.")
- # custom Usage
- nl = "\n"
- all_inputs = " ".join(["[-" + action.option_strings[0][-1:] + "]"
- for action in parser._actions[:-1]])
- last_input = parser._actions
- parser.usage = f'\
- {nl} \
- {parser.prog} \
- {all_inputs} \
- {last_input[-1].dest.upper()}'
- # flags handling
- parsed = parser.parse_args(args=None if sys.argv[1:] else ['--help'])
- whether_debug = parsed.debug
- whether_from_file = parsed.file
- if whether_from_file is not None:
- user_list = []
- with open(whether_from_file, "r") as file:
- for line in file:
- user_list.append(line.strip())
- else:
- user_list = parsed.input
- # ----- ----- ----- ----- ----- -----
- # Helpers
- # ----- ----- ----- ----- ----- -----
- def d(
- str: None | str = None,
- level: None | str = None,
- color: None | Color = None
- ) -> None:
- '''Print debug message.'''
- if color is None:
- color = "yellow"
- if level is None:
- level = "debug"
- if whether_debug is True and level == "debug":
- cprint(f'[DEBUG] {str}', "yellow")
- if level == "none":
- cprint(f'{str}', color)
- def validate(text: str):
- '''Transforms strings into valid filenames for Windows' Batch.
- Valid chars: %!@#$%'¨&()_+-=.,;
- '''
- invalids = ["\\", "/", ":",
- "*" , "?", "\"",
- "<" , ">", "|",
- "^", "#"
- ]
- for invalid in invalids:
- if invalid in [":", "|", "*", "?"]:
- repl = "-"
- elif invalid in "#":
- repl = ""
- else:
- repl = "_"
- text = text.replace(invalid, repl)
- # elif ".com" in invalids:
- # repl = ""
- return text
- # ----- ----- ----- ----- ----- -----
- # Main
- # ----- ----- ----- ----- ----- -----
- i = 0
- if whether_debug is True:
- d(f"")
- d(f" in:", "none", "yellow")
- d(f" {user_list[0]}", "none", "yellow")
- valid_filename = validate(user_list[0])
- d(f" out:", "none", "green")
- d(f" {valid_filename}", "none", "green")
- with open(tempfile, 'w') as file:
- print(valid_filename, file=file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement