Skip to content

Commit 60a11fa

Browse files
committed
added some basic python utilities (a bit messy) that aid the mashing of xml files.
1 parent b811b21 commit 60a11fa

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

java_generate/utils.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
import sys
5+
import os
6+
import re
7+
8+
def main():
9+
directory = 'api_examples'
10+
for root, dirs, files in os.walk(directory):
11+
for name in files:
12+
# convertTags(os.path.join(root,name), 'c', 'kbd')
13+
# moveFile(root,name)
14+
# addCDATA(os.path.join(root, name))
15+
16+
def moveFile(root, name):
17+
data = open(os.path.join(root,name),'r').read()
18+
include = re.compile(r'<subcategory>((Primitive)|(Composite)|(Relational Operators)|(Iteration)|(Conditionals)|(Logical Operators))').match
19+
if(include(data)):
20+
print "Moving " + os.path.join(root,name) + "to root" + "include/" + name
21+
os.rename(os.path.join(root,name), root + "include/" + name)
22+
23+
24+
def addCDATA(f):
25+
if(f[-3:] == 'xml'):
26+
xml = open(f, 'r')
27+
txt = xml.read()
28+
29+
# pattern = re.compile(r'(<description>(?!<\!\[CDATA))([\s\S]+?)(</description>)', re.MULTILINE)
30+
# pattern = re.compile(r'(<syntax>(?!<\!\[CDATA))([\s\S]+?)(</syntax>)', re.MULTILINE)
31+
pattern = re.compile(r'(<code>(?!<\!\[CDATA))([\s\S]+?)(</code>)', re.MULTILINE)
32+
txt = re.sub( pattern, r'\1<![CDATA[\2]]>\3', txt)
33+
34+
# pattern = re.compile(r'(<code>(?!<\!\[CDATA))([\s\S]+?)(</code>)', re.MULTILINE)
35+
# txt = re.sub( pattern, r'\1<![CDATA[\2]]>\3', txt)
36+
37+
xml.close()
38+
xml = open(f, 'w')
39+
xml.write(txt)
40+
xml.close()
41+
42+
def convertTags(f, pName, newName):
43+
xml = open(f, 'r')
44+
txt = xml.read()
45+
46+
reString = '(<'+ pName + '>)([\s\S]+?)(</' + pName + r'>)'
47+
pattern = re.compile( reString, re.MULTILINE)
48+
txt = re.sub(pattern, r'<' + newName + r'>\2</'+ newName +'>', txt)
49+
50+
xml.close()
51+
xml = open(f, 'w')
52+
xml.write(txt)
53+
xml.close()
54+
55+
def removeCDATA(f):
56+
xml = open(f, 'r')
57+
txt = xml.read()
58+
59+
pattern = re.compile(r'<\!\[CDATA\[', re.MULTILINE)
60+
txt = re.sub( pattern, r'', txt)
61+
62+
pattern = re.compile(r'\]\]>')
63+
txt = re.sub(pattern, r'', txt)
64+
65+
xml.close()
66+
xml = open(f, 'w')
67+
xml.write(txt)
68+
xml.close()
69+
print "wrote: " + f
70+
71+
72+
if __name__ == '__main__':
73+
main()
74+

0 commit comments

Comments
 (0)