forked from calimarkus/XcodeAutocompleteSnippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.renameSnippets.py
59 lines (43 loc) · 1.51 KB
/
.renameSnippets.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from xml.dom import minidom
import os
import glob
import re
# define first character lowercase function
first_lower = lambda s: s[:1].lower() + s[1:] if s else ''
print "Updating codesnippet filenames with their actual titles"
count = 0
listing = os.listdir(".")
for fileName in listing:
# ignore other files than snippets
if not fileName.endswith(".codesnippet"):
continue
# parse snippet file
xmldoc = minidom.parse(fileName)
keyslist = xmldoc.getElementsByTagName('key')
allChilds = xmldoc.getElementsByTagName('dict')[0].childNodes
for x in allChilds:
if not x.firstChild:
allChilds.remove(x)
snippetTitle = None
# find title
for key in keyslist:
value = key.firstChild.nodeValue
if value == "IDECodeSnippetTitle":
snippetTitle = allChilds[allChilds.index(key)+1].firstChild.nodeValue
# if snippet has a title
if not snippetTitle == None:
# build filename (only a-zA-Z)
snippetTitle = snippetTitle.replace("&","and")
allWords = re.findall("[a-zA-Z]+", snippetTitle)
uppercaseWords = map(lambda x: x.title(), allWords)
newName = "".join(uppercaseWords) + ".codesnippet"
newName = first_lower(newName)
# if name changed, rename file
if not newName == fileName:
os.rename(fileName, newName)
count = count + 1
# log renamed files
print " renamed: " + fileName + " -> "
print " " + newName
# show total count
print str(count) + " snippets renamed."