forked from bamos/python-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoNeat.py
More file actions
executable file
·33 lines (28 loc) · 983 Bytes
/
Copy pathtoNeat.py
File metadata and controls
executable file
·33 lines (28 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python2.7
import re
import sys
# Maps a string such as 'The Beatles' to 'the-beatles'.
def toNeat(s, args):
if args.capital:
s = s.title().replace("&", "and")
else:
s = s.lower().replace("&", "and")
# Put spaces between and remove blank characters.
blankCharsPad = r"()\[\],.\\\?\#/\!\$\:\;"
blankCharsNoPad = r"'\""
s = re.sub(r"([" + blankCharsPad + r"])([^ ])", "\\1 \\2", s)
s = re.sub("[" + blankCharsPad + blankCharsNoPad + "]", "", s)
# Replace spaces with a single dash.
s = re.sub(r"[ \*\_]+", "-", s)
s = re.sub("-+", "-", s)
s = re.sub("^-*", "", s)
s = re.sub("-*$", "", s)
# Ensure the string is only alphanumeric with '-', '+', and '='.
if args.capital:
search = re.search("[^0-9a-zA-Z\-\+\=]", s)
else:
search = re.search("[^0-9a-z\-\+\=]", s)
if search:
print("Error: Unrecognized character in '" + s + "'")
sys.exit(-42)
return s