MCPcopy Create free account
hub / github.com/ActiveState/code / getNewName

Function getNewName

recipes/Python/576443_Renamer/recipe-576443.py:59–146  ·  view source on GitHub ↗
(oldname,path,first)

Source from the content-addressed store, hash-verified

57 raise SystemExit
58
59def getNewName(oldname,path,first):
60
61 global prefix,suffix,replace,count,replaceWith,pattern
62
63 if choice == "SPACE to UNDERSCORE":
64 newname = oldname.replace(' ', '_')
65
66 elif choice == "UNDERSCORE to SPACE":
67 newname = oldname.replace('_',' ')
68
69 elif choice == "DASH to SPACE":
70 newname = oldname.replace('-',' ')
71
72 elif choice == "SPACE to DASH":
73 newname = oldname.replace(' ','-')
74
75 elif choice == "Replace Manually":
76 newname = oldname.replace(replace,replaceWith)
77
78 elif choice == "ALL CAPITAL":
79 newname = oldname.upper()
80
81 elif choice == "all lower":
82 newname = oldname.lower()
83
84 elif choice == "First letter uppercase":
85 newname = oldname.capitalize()
86
87 elif choice == "First Letter Uppercase In Each Word":
88 newname = oldname.title()
89
90 elif choice == "Add a Prefix":
91 newname = prefix + oldname
92
93 elif choice == "Add a Suffix":
94 #Place suffix before the extension if it got ...
95 if oldname.find('.') != -1:
96 newname = oldname.split('.')[0] + suffix + '.' + oldname.split('.')[1]
97 else:
98 newname = oldname + suffix
99
100 elif choice == "Patternize":
101
102 newname = pattern
103
104 #for number substiution
105 c = re.compile(r'(\{num\d*\}|(\{num\d*\+\d*\}))')
106 if c.search(newname):
107 tmp = c.search(newname).group()
108
109 #if it is a directory just set count 0 and pass
110 if os.path.isdir(path) and first == 1:
111 newname = c.sub("",newname)
112 count = 1
113 #if {num3}
114 elif len(tmp)== 6:
115 substitute = str(count).zfill(int(tmp[4]))
116 newname = c.sub(substitute, newname)

Callers 1

renameFunction · 0.85

Calls 13

strFunction · 0.85
capitalizeMethod · 0.80
isdirMethod · 0.80
localtimeMethod · 0.80
replaceMethod · 0.45
lowerMethod · 0.45
titleMethod · 0.45
findMethod · 0.45
splitMethod · 0.45
compileMethod · 0.45
searchMethod · 0.45
groupMethod · 0.45

Tested by

no test coverage detected