forked from Komodo/KomodoEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdtdgen.py
398 lines (338 loc) · 13.1 KB
/
dtdgen.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env python
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is Komodo code.
#
# The Initial Developer of the Original Code is ActiveState Software Inc.
# Portions created by ActiveState Software Inc are Copyright (C) 2000-2007
# ActiveState Software Inc. All Rights Reserved.
#
# Contributor(s):
# ActiveState Software Inc
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
"""
Generate DTD references from an XML file.
Command Line Usage:
dtdgen [<options>...] <xmlfile> <dtdfile>
Options:
-h, --help Print this help and exit.
-V, --version Print the version info and exit.
-v, --verbose Give verbose output for errors.
-f, --force Allow overwriting of existing files.
-i Do in-place replacement of <xmlfile>. This
implies "--force".
-o <outfile> Write output to the given file. By default
output if dumped to stdout.
sample usage:
Parse a xul file, generate the dtd, and overwrite the xul file with
a new xul file that has entity references added.
dtdgen.py --force -i /path/to/file.xul /path/to/file.dtd
"""
import os
import sys
import getopt
import types
import re
import pprint
import logging
log = logging.getLogger("dtdgen")
try:
import cElementTree as ET # effbot's C module
except ImportError:
logging.basicConfig()
log.setLevel(logging.INFO)
log.error("using element tree and not cElementTree, performace will suffer")
if sys.version_info[:2] < (2, 5):
import elementtree.ElementTree as ET # effbot's pure Python module
else:
import xml.etree.ElementTree as ET
from xml.sax.saxutils import escape
# additional fixes
entitydefs = {'%': '%',
'"': '"'}
nowrite = False
#---- exceptions
class DTDGenError(Exception):
pass
#---- global data
_version_ = (0, 1, 0)
#---- internal logging facility
chromeURI = "chrome://komodo/locale/"
# a list of attrs that we will convert to use the dtd
attrs = [
"label", "accesskey", "tooltiptext", "title",
# custom attributes for komodo
"locktip", "desc"
]
#---- internal support stuff
specialNames = {
'.': 'period',
'...': 'ellipsis',
'+': 'plus',
'-': 'minus',
'//': 'doubleSlash',
'#': 'hash',
}
#---- module API
def dtdgen(xmlfile, dtdfile=sys.stderr, outfile=sys.stdout, force=0):
"""Generate a DTD from the given XML file.
"xmlfile" is the XML file to parse for attributes.
"dtdfile" is the DTD file of references to generate.
"outfile" is a filename or stream to which to write the new XML
output (defaults to sys.stdout).
"force" is boolean to allow overwriting an existing file (default is
false).
A <!DOCTYPE...> reference to the <dtdfile> is added, if possible.
"""
log.info("dtdgen(xmlfile=%r, dtdfile=%r)", xmlfile, dtdfile)
entities, xmlContent = dtdcollect(xmlfile, dtdfile)
dtdwrite(dtdfile, entities, force)
xmlwrite(outfile, xmlContent, force)
def dtdcollect(xmlfile, dtdfile):
global nowrite
log.info("dtdcollect(xmlfile=%r, dtdfile=%r)", xmlfile, dtdfile)
entities = {}
# read the xml text so we can do replacements
fin = open(xmlfile, 'r')
xmlContent = fin.read()
fin.close()
# first, parse the xml file
tree = ET.parse(xmlfile)
# iterate through the nodes, looking for attributes we care about
for node in tree.getiterator():
id = node.attrib.get("id", None)
if not id:
id = node.attrib.get("label", None)
if id in specialNames:
id = specialNames[id]
for attr in attrs:
entityVal = node.attrib.get(attr, None)
if not entityVal:
continue
if attr == "accesskey" and id:
try:
words = [w for w in re.split("[\W_]+", id) if w]
if len(words[0]) > 1 and not words[0][1].isupper():
# lowercase the first letter
words[0] = words[0][0].lower()+words[0][1:]
entityName = words[0] + "".join([w[0].upper()+w[1:] for w in words[1:]])
except IndexError, e:
print "failure on name %s" % entityVal
raise
elif entityVal in specialNames:
entityName = specialNames[entityVal]
else:
try:
words = [w for w in re.split("[\W_]+", entityVal) if w]
if len(words[0]) > 1 and not words[0][1].isupper():
# lowercase the first letter
words[0] = words[0][0].lower()+words[0][1:]
entityName = words[0] + "".join([w[0].upper()+w[1:] for w in words[1:]])
except IndexError, e:
print "failure on name %s" % entityVal
raise
if entityName[0].isdigit():
# cannot have digit as first char
entityId = "%s.%s" % (attr, entityName)
else:
if entityName[0] == "1":
raise Exception()
entityId = "%s.%s" % (entityName, attr)
node.set(attr, "&%s;" % entityId)
#print "%s=%s" % (entityId, entityVal)
entities[entityId]=entityVal
entityAttr = "%s=(\"|')%s\\1" % (attr, re.escape(entityVal))
entityRef = "%s=\"&%s;\"" % (attr, entityId)
# XXX SLOWWWWWW
# XXX need our patched element tree so we can simply replace in
# the correct location of the document, otherwise errors ARE
# possible (and happen in komodo.xul) due to duplicate attrib
# values in some elements
xmlContent = re.subn(entityAttr, entityRef, xmlContent, 1)[0]
# add the doctype data to the content
if type(dtdfile) in types.StringTypes:
# XXX make big assumptions here....
chrome = "%s%s" % (chromeURI, os.path.basename(dtdfile))
basename = os.path.splitext(os.path.basename(chrome))[0]
m = re.search("(<!DOCTYPE.*?>)", xmlContent)
if m:
start, end = m.span(1)
doctype = m.group(1)[:-1]
doctype = "%s [\n <!ENTITY %% %sDTD SYSTEM \"%s\">\n %%%sDTD;\n]>" % \
(doctype, basename, chrome, basename)
else:
m = re.search("(<?xml.*?>)", xmlContent)
if m:
start, end = m.span(1)
start = end
m = re.search("(?:{(.*?)})?(.*)", tree.getroot().tag)
rootTagName = m.group(2)
rootNS = m.group(1)
if not rootNS:
rootNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
doctype = """
<!DOCTYPE %s SYSTEM "%s" [
<!ENTITY %% %sDTD SYSTEM "%s">
%%%sDTD;
]>
""" % (rootTagName, rootNS, basename, chrome, basename)
else:
# probably HTML, we don't handle it yet
raise Exception("Cannot handle non-xml files without a doctype declaration")
xmlContent = xmlContent[:start] + doctype + xmlContent[end:]
return entities, xmlContent
def dtdwrite(dtdfile, entities, force=False):
if not entities:
return
dtdEntities = ['<!ENTITY %s "%s">' % (id, escape(val, entitydefs)) for id, val in entities.items()]
dtdEntities.sort()
dtdFileData = "\n".join(dtdEntities)+"\n"
if type(dtdfile) in types.StringTypes:
if os.path.exists(dtdfile):
if force:
os.remove(dtdfile)
else:
raise DTDGenError("dtd '%s' already exists, use '--force' to "\
"allow overwrite" % dtdfile)
dtdf = open(dtdfile, 'w')
else:
dtdf = dtdfile
dtdf.write(dtdFileData)
if dtdf != dtdfile:
dtdf.close()
def xmlwrite(outfile, xmlContent, force=False):
if nowrite:
return
if type(outfile) in types.StringTypes:
if os.path.exists(outfile):
if force:
os.remove(outfile)
else:
raise DTDGenError("outfile '%s' already exists, use '--force' to "\
"allow overwrite" % outfile)
fout = open(outfile, 'w')
else:
fout = outfile
# Write out the new content.
fout.write(xmlContent)
if fout != outfile:
fout.close()
#---- mainline
def main(argv):
global nowrite
global chromeURI
logging.basicConfig()
log.setLevel(logging.INFO)
# Process options.
try:
optlist, args = getopt.getopt(argv[1:], "hVvo:ifdc:",
["help", "version", "verbose", "force", "dry-run", "chrome"])
except getopt.GetoptError, msg:
log.error("%s. Your invocation was: %s\n"\
% (msg, argv))
log.error("See 'dtdgen --help'.\n")
return 1
opts = [opt for opt, optarg in optlist]
if "-o" in opts and "-i" in opts:
log.error("cannot specify both '-i' and "\
"'-o' options\n")
return 1
force = 0
inplace = 0
outfile = sys.stdout
for opt, optarg in optlist:
if opt in ("-h", "--help"):
sys.stdout.write(__doc__)
return 0
elif opt in ("-V", "--version"):
ver = '.'.join([str(i) for i in _version_])
sys.stderr.write("dtdgen %s\n" % ver)
return 0
elif opt in ("-v", "--verbose"):
log.setLevel(logging.DEBUG)
verbose = 1
elif opt in ("-f", "--force"):
force = 1
elif opt == "-o":
outfile = optarg
elif opt in ("-c", "--chrome"):
chromeURI = optarg
elif opt == "-i":
inplace = 1
force = 1
elif opt in ("-d", "--dry-run"):
nowrite = True
# Process arguments.
if len(args) < 1:
log.error("incorrect number of arguments: argv=%r\n" % argv)
return 1
xmlfile, dtdfile = args
if os.path.isdir(xmlfile):
if not os.path.isdir(dtdfile):
print "second argument must be the path to the locale directory"
sys.exit(-1)
for root, dirs, files in os.walk(xmlfile):
print 20*'-'
print root
for dname in dirs:
if dname == "test":
del dirs[dirs.index(dname)]
pathparts = os.path.split(root)
if pathparts[1]:
dtdbase = pathparts[1]
else:
pathparts = pathparts[0].split(os.sep)
if pathparts[-1] == 'content':
dtdbase = pathparts[-2]
else:
dtdbase = pathparts[-1]
dtdname = os.path.join(dtdfile, "%s.dtd" % dtdbase)
dtdEntities= {}
for name in files:
basename = os.path.basename(name)
name, ext = os.path.splitext(basename)
if ext not in [".xul", ".xml"]:
continue
if basename.startswith("test"):
continue
name, tagext = os.path.splitext(name)
infile = outfile = os.path.join(root, basename)
entities, content = dtdcollect(infile, dtdname)
if entities:
dtdEntities.update(entities)
xmlwrite(outfile, content, force=True)
dtdwrite(dtdname, dtdEntities, force=True)
else:
if inplace:
outfile = xmlfile
try:
dtdgen(xmlfile, dtdfile, outfile, force)
except DTDGenError, ex:
log.exception(ex)
if __name__ == "__main__":
__file__ = sys.argv[0]
sys.exit( main(sys.argv) )