-
-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathGenerateDescriptions.py
More file actions
158 lines (129 loc) · 4.77 KB
/
GenerateDescriptions.py
File metadata and controls
158 lines (129 loc) · 4.77 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
# encoding: utf-8
import sys
import os
import re
from xml.dom import minidom
reload(sys)
sys.setdefaultencoding('utf8')
linePrefix = " *"
startString = linePrefix + " ( begin auto-generated from %s )"
endString = "%s ( end auto-generated )" % linePrefix
# the tag we're looking for to generate descriptions
shortString = "@generate"
xmlDirectory = "somwhere/"
# auto-generated reference will get hard returns after this many characters
maxCharsPerLine = 72
codeDir = "/Users/REAS/Documents/30-Code/processing/processing-docs/java_generate/ReferenceGenerator/test/core/"
xmlDir = "/Users/REAS/Documents/30-Code/processing/processing-docs/content/api_en/"
netCodeDir = "/Users/REAS/Documents/reas@processing.org/trunk/processing/java/libraries/net/src/processing/net/"
netXmlDir = "/Users/REAS/Documents/reas@processing.org/trunk/web/content/api_en/LIB_net/"
videoCodeDir = "/Users/REAS/Documents/reas@processing.org/trunk/processing/java/libraries/video/src/processing/video/"
videoXmlDir = "/Users/REAS/Documents/reas@processing.org/trunk/web/content/api_en/LIB_video/"
thisFile = ""
def main():
#maker = DescriptionIntegrator( codeDirectory=sys.argv[1], xmlDirectory=sys.argv[2] )
maker = DescriptionIntegrator( codeDirectory=codeDir, xmlDirectory=xmlDir )
maker.run()
#makerVideo = DescriptionIntegrator( codeDirectory=videoCodeDir, xmlDirectory=videoXmlDir )
#makerVideo.run()
#makerNet = DescriptionIntegrator( codeDirectory=netCodeDir, xmlDirectory=netXmlDir )
#makerNet.run()
def prefixedString( input ):
return "%s %s" % (linePrefix, input)
class DescriptionIntegrator:
def __init__(self, xmlDirectory="", codeDirectory=""):
self.xmlDirectory = xmlDirectory
self.codeDirectory = codeDirectory
def run(self):
for root, dirs, files in os.walk( self.codeDirectory ):
for name in files:
if name[-4:] == "java":
self.addDescriptionsToFile( os.path.join(root,name) )
def addDescriptionsToFile(self, f):
input = open(f,'r+')
text = input.read()
input.close()
didEdit = False
# split apart the source code by line
portions = text.split("\n")
for line in portions:
#line = line.rstrip()
#print "line is '%s'" % (line)
if( line.find( shortString ) != -1):
print line + " - full line"
parts = line.split(" ")
[xml] = [ p for p in parts if p[-3:] == "xml" ]
description = self.getDescription(xml)
index = portions.index(line)
print xml + " - gen insert"
portions.insert( index, startString % xml )
endIndex = self.insertDescription(description, portions, index+1 )
portions.insert( endIndex, endString )
portions.remove( line )
print " "
didEdit = True
elif( line.find("begin auto-generated") != -1 ):
parts = line.split(" ")
[xml] = [ p for p in parts if p[-3:] == "xml" ]
description = self.getDescription(xml)
index = portions.index(line) + 1
print xml + " - auto remove"
self.removeOldDescription(portions, index)
print xml + " - auto insert"
self.insertDescription( description, portions, index )
print " "
didEdit = True
if( didEdit == True ):
output = open(f, 'w')
output.write( '\n'.join(portions) )
def insertDescription(self, description, list, startIndex):
parts = self.generateLines( description, maxCharsPerLine )
for p in parts:
# insert each comment line into the source code
list.insert( startIndex, prefixedString(p).rstrip() )
startIndex += 1
return startIndex
def generateLines( self, text, maxLength ):
words = text.split(" ")
lines = []
txt = ""
for word in words:
# if we've hit a newline, break the line
if( word.find("\n") != -1 ):
pieces = word.split("\n")
txt = txt + pieces[0]
lines.append( txt )
txt = ""
if( pieces[1] != "" ):
word = pieces[1]
else:
continue
# if we're out of room, write the text
if( len(txt) + len(word) > maxLength ):
lines.append( txt )
txt = word + " "
else:
# keep adding words
txt = txt + word + " "
# if there's text left over, append it now
if( txt != "" ):
lines.append( txt )
lines.append( "" )
return lines
def removeOldDescription(self, list, startIndex):
lastIndex = list.index( endString, startIndex )
numIndices = lastIndex - startIndex
for i in range(0, numIndices):
list.pop( startIndex )
def getDescription(self, xml):
print xml
thisFile = xml
doc = minidom.parse( self.xmlDirectory + xml )
elements = doc.getElementsByTagName("description")
#return elements[0].firstChild.nodeValue
#return " ".join( t.nodeValue for t in elements[0].childNodes )
#return " ".join( t.nodeValue for t in elements[0].childNodes if t.nodeType == t.TEXT_NODE )
return " ".join( t.nodeValue for t in elements[0].childNodes if t.nodeType == t.CDATA_SECTION_NODE)
if __name__ == '__main__':
main()