Skip to content

Instantly share code, notes, and snippets.

@Alyetama
Last active May 31, 2022 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alyetama/63e9c7400fceb8f191b29fd8ee69e8b7 to your computer and use it in GitHub Desktop.
Save Alyetama/63e9c7400fceb8f191b29fd8ee69e8b7 to your computer and use it in GitHub Desktop.
#!/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