-
Notifications
You must be signed in to change notification settings - Fork 30
/
.renameSnippets.py
63 lines (47 loc) · 1.76 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
57
58
59
60
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 & ignore focus ones
if fileName.startswith("focus_") or 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
lang = "objc"
# prase snippet title
for key in keyslist:
value = key.firstChild.nodeValue
if value == "IDECodeSnippetTitle":
snippetTitle = allChilds[allChilds.index(key)+1].firstChild.nodeValue
elif value == "IDECodeSnippetLanguage":
if allChilds[allChilds.index(key)+1].firstChild.nodeValue.endswith("Swift"):
lang = "swift"
# if snippet has a title
if snippetTitle is not 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 = lang + "_" + 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.")