Last active
May 31, 2022 06:15
-
-
Save Alyetama/63e9c7400fceb8f191b29fd8ee69e8b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
import re | |
import string | |
import sys | |
from pathlib import Path | |
class MissingNameArgument(Exception): | |
pass | |
def main(): | |
illegal = [ | |
x for x in string.punctuation + string.whitespace if x not in '._-' | |
] | |
if len(sys.argv) == 1: | |
raise MissingNameArgument | |
file = sys.argv[1] | |
fname = Path(file).stem | |
matches = re.search(r'\s\[(?<=\[).+?(?=\])\]', fname) | |
if matches: | |
fname = fname.replace(matches[0], '') | |
clean_fname = ''.join([x if x not in illegal else '-' for x in fname]) | |
clean_fname = clean_fname.replace('--', '-') | |
if clean_fname.endswith('-'): | |
clean_fname = clean_fname[:-1] | |
clean_fname = f'{clean_fname}{Path(file).suffix}' | |
print(clean_fname) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment