-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixDoctype.py
118 lines (95 loc) · 5.23 KB
/
fixDoctype.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
import os
import sys
import pyinputplus as pyip
from lxml import etree
import shutil
##REPLACE THE DOCTYPE DECLARATIONS IN A SET OF XML FILES
# Step 1: VERIFY THE INPUT (CHOSEN WHEN THE PROGRAM STARTS)
# THEN ENTER THE OUTPUT DIRECTORY AND START PROCESSING
def verifyDirs(inDir):
print('You want to modify the files in: ' + inDir + '?')
print('Type \'y\' and ENTER to confirm, any other key to abort.')
confirmInDir = input()
if confirmInDir == 'y':
print(r'Enter the absolute path to direct the output. For example:')
print(r'C:\Users\jdwin\Documents\XMLfiles\out')
outDir = input()
print('You want to direct the output to: ' + outDir + '?')
print('Type \'y\' and ENTER to confirm, any other key to abort.')
confirmOutDir = input()
if confirmOutDir == 'y':
print('OK')
copyDirs(inDir, outDir)
else:
print('Goodbye')
else:
print('Goodbye')
# Step 2: COPY THE DIRECTORY TREE FROM INPUT DIRECTORY TO OUTPUT DIRECTORY
def copyDirs(inDir, outDir):
shutil.copytree(inDir, outDir, dirs_exist_ok=True)
walkDoctype(inDir, outDir)
# Step 3: WALK THE OUTPUT DIRECTORY TO GET THE FILEPATH INFO FOR VARIABLES
# THEN CALL THE FIXDOCTYPE FUNCTION
def walkDoctype(inDir, outDir):
##/._*/ and delete veto files options in our smb.conf to prevent creation of such files. Instead we leave .DS_STORE
macRes = ('._', '.DS_Store')
xmlExt = ('ditamap', 'dita', 'xml')
for subdir, dirs, files in os.walk(outDir):
for filename in files:
filepath = subdir + os.sep + filename
if not filename.startswith(macRes):
if filepath.endswith(xmlExt):
fixDoctype(outDir, filename, filepath)
# Step 4: COPY EACH FILE IN THE DIRECTORY, REPLACING DOCTYPE
def fixDoctype(outDir, filename, filepath):
parser = etree.XMLParser(strip_cdata=False)
tree = etree.parse(filepath, parser)
docinfo = tree.docinfo
genTaskString = '''generalTask'''
classifyMapString = '''subjectScheme'''
if docinfo.root_name == 'concept':
newDoctype = '''<!DOCTYPE concept PUBLIC "urn:pubid:jdwinfodesign.com:doctypes:dita:dtd:concept" "concept.dtd">'''
print(r'Writing new doctype to ' + filepath)
elif docinfo.root_name == 'task':
if genTaskString in docinfo.doctype:
newDoctype = '''<!DOCTYPE task PUBLIC "urn:pubid:jdwinfodesign.com:doctypes:dita:dtd:generalTask" "generalTask.dtd">'''
print(r'Writing new doctype to ' + filepath)
else:
newDoctype = '''<!DOCTYPE task PUBLIC "urn:pubid:jdwinfodesign.com:doctypes:dita:dtd:task" "task.dtd">'''
print(r'Writing new doctype to ' + filepath)
elif docinfo.root_name == 'reference':
newDoctype = '''<!DOCTYPE reference PUBLIC "urn:pubid:jdwinfodesign.com:doctypes:dita:dtd:reference" "reference.dtd">'''
print(r'Writing new doctype to ' + filepath)
elif docinfo.root_name == 'map':
if classifyMapString in docinfo.doctype:
newDoctype = '''<!DOCTYPE map PUBLIC "urn:pubid:jdwinfodesign.com:doctypes:dita:dtd:classifyMap" "classifyMap.dtd">'''
print(r'Writing new doctype to ' + filepath)
else:
newDoctype = '''<!DOCTYPE map PUBLIC "urn:pubid:jdwinfodesign.com:doctypes:dita:dtd:map" "map.dtd">'''
print(r'Writing new doctype to ' + filepath)
elif docinfo.root_name == 'dita':
newDoctype = '''<!DOCTYPE dita PUBLIC "urn:pubid:jdwinfodesign.com:doctypes:dita:dtd:ditabase" "ditabase.dtd">'''
print(r'Writing new doctype to ' + filepath)
elif docinfo.root_name == 'subjectScheme':
newDoctype = '''<!DOCTYPE subjectScheme PUBLIC "urn:pubid:jdwinfodesign.com:doctypes:dita:dtd:subjectScheme" "subjectScheme.dtd">'''
print(r'Writing new doctype to ' + filepath)
elif docinfo.root_name == 'topic':
newDoctype = '''<!DOCTYPE topic PUBLIC "urn:pubid:jdwinfodesign.com:doctypes:dita:dtd:topic" "topic.dtd">'''
print(r'Writing new doctype to ' + filepath)
elif docinfo.root_name == 'glossentry':
newDoctype = '''<!DOCTYPE glossentry PUBLIC "urn:pubid:jdwinfodesign.com:doctypes:dita:dtd:glossentry" "glossary.dtd">'''
print(r'Writing new doctype to ' + filepath)
elif docinfo.root_name == 'troubleshooting':
newDoctype = '''<!DOCTYPE troubleshooting PUBLIC "urn:pubid:jdwinfodesign.com:doctypes:dita:dtd:troubleshooting" "troubleshooting.dtd">'''
print(r'Writing new doctype to ' + filepath)
else:
print(r'root_name not in list')
tree.write(filepath, xml_declaration=True, encoding='UTF-8', doctype=newDoctype)
#---------------------------------------------------------------------
# Step 0: Prompt user to choose directory containing
# files to be modified (inDir) and
# location for output (outDir)
print(r'Enter the absolute path to the directory containing files to modify. For example:')
print(r'C:\Users\jdwin\Documents\XMLfiles\src')
inDir = input()
verifyDirs(inDir)