Debian Codegraph @

scripts/ohm/help_parsers/helpparsers.py

raw
1 import logging
2 import os
3 import os.path
4 import shutil
5 import tempfile
6
7 try:
8 from html.parser import HTMLParser
9 except ImportError:
10 from HTMLParser import HTMLParser
11
12 from ohm.dataclasses import Tag
13 from ohm.utilfunctions import update_attribute, change_filename
14
15
16 class OutputParser(HTMLParser):
17 """Provide common functionality for the Parsers.
18
19 Currently class passes the input file through to the passed in output"""
20
21 def __init__(self, file):
22 HTMLParser.__init__(self)
23
24 if hasattr(file, 'write'):
25 self.output_file = file
26 else:
27 raise ValueError("file is not a file-like")
28
29 self.tag_stack = list()
30
31 def handle_starttag(self, tag, attrs):
32 logging.debug(" Handle starttag: %s", tag)
33 logging.debug(" %s", attrs)
34 self.tag_stack.append(Tag(tag, attrs))
35
36 def handle_startendtag(self, tag, attrs):
37 logging.debug(" Handle start and end tag: %s", tag)
38 logging.debug(" %s", attrs)
39 self.write_tag(Tag(tag, attrs))
40
41 def handle_endtag(self, tagname):
42 """Finds the matching tag recursing until finding a match"""
43 logging.debug(" End tag: %s", tagname)
44 tag = self.find_match(tagname)
45 self.write_tag(tag)
46
47 def handle_data(self, data):
48 logging.debug(" Data: %s", data)
49 if len(self.tag_stack) > 0:
50 tag = self.tag_stack.pop()
51 if tag.data is not None:
52 tag.data += data
53 else:
54 tag.data = data
55 self.tag_stack.append(tag)
56 else:
57 self.output_file.write(data)
58
59 def find_match(self, tagtofind, indent=0):
60 """Recursive function to match the tag"""
61 tag = self.tag_stack.pop()
62 if not tag.name == tagtofind:
63 logging.warning(" %smismatched tag found (expected %s, found %s)",
64 " " * indent, tagtofind, tag.name)
65 self.write_tag(tag)
66 tag = self.find_match(tagtofind)
67
68 return tag
69
70 def write_tag(self, tag):
71 """Write a tag out if it is top level.
72
73 When tag stack is empty, writes tag to file passed in the constructor.
74 When tag stack is not empty formats the tag and sets (or if the next
75 tag.data is not None, appends) the formatted string."""
76 if tag.data is None:
77 s = "<%s%s />" % (tag.name, self.format_attributes(tag))
78 else:
79 s = "<%s%s>%s</%s>" % (
80 tag.name, self.format_attributes(tag), tag.data, tag.name)
81
82 if len(self.tag_stack) > 0:
83 tag = self.tag_stack.pop()
84 if tag.data is not None:
85 tag.data += s
86 else:
87 tag.data = s
88 self.tag_stack.append(tag)
89 else:
90 self.output_file.write(s)
91
92 @staticmethod
93 def format_attributes(tag):
94 """Returns the attributes formatted to be placed in a tag."""
95 ret = ""
96 if len(tag.attributes) > 0:
97 for name, value in tag.attributes:
98 ret = "%s %s=\"%s\"" % (ret, name, value)
99 return ret
100
101
102 class Stage2Parser(OutputParser):
103 def __init__(self, *args, **kwargs):
104 OutputParser.__init__(self, *args, **kwargs)
105 self.control = []
106
107 def handle_starttag(self, tag, attrs):
108 if tag == "meta":
109 if (attrs[0][0] == "name"
110 and attrs[0][1] == "control"
111 and attrs[1][0] == "content"):
112 self.control.append(attrs[1][1])
113 else:
114 OutputParser.handle_starttag(self, tag, attrs)
115
116 def handle_startendtag(self, tag, attrs):
117 if tag == "meta":
118 if (attrs[0][0] == "name"
119 and attrs[0][1] == "control"
120 and attrs[1][0] == "content"):
121 self.control.append(attrs[1][1])
122 else:
123 OutputParser.handle_startendtag(self, tag, attrs)
124
125
126 class Stage3Parser(OutputParser):
127 def __init__(self, options, files, extrafiles, subdir, *args, **kwargs):
128 OutputParser.__init__(self, *args, **kwargs)
129 self.options = options
130 self.files = files
131 self.extrafiles = extrafiles
132 self.subdir = subdir
133 logging.debug(" Subdirectory is '%s'", subdir)
134
135 def handle_startendtag(self, tag, attrs):
136 """Find the image and copy it to the stage3 folder where it should
137 be in the f output."""
138 alt_index = None
139 srv_index = None
140 if tag == "img":
141 # figure out which attribute is src
142 for x in range(0, len(attrs)):
143 if attrs[x][0] == "src":
144 srv_index = x
145 elif attrs[x][0] == "alt":
146 alt_index = x
147
148 if attrs[srv_index][1].startswith("/"):
149 # manual wants an absolute path, the help manual does not
150 # support absolute path, so make sure that the image exists
151 # where the absolute path indicates, then make the path into a
152 # relative path with the appropriate number of updirs
153 test = os.path.join(self.options.indir, attrs[srv_index][1][1:])
154 if not os.path.exists(test):
155 raise IOError(
156 "Cannot find %s in base path" % (attrs[srv_index][1]))
157
158 # try find a valid relative path
159 subdirdepth = len(self.subdir.split(os.path.sep))
160 prefix = "../" * subdirdepth
161 relpath = os.path.join(prefix, attrs[srv_index][1][1:])
162 if not os.path.exists(
163 os.path.join(self.options.indir, self.subdir, relpath)):
164 raise Exception(
165 "Cannot relativize path: %s" % (attrs[srv_index][1]))
166 else:
167 attrs = update_attribute(attrs, 'src', relpath)
168
169 location1 = os.path.join(self.options.indir, self.subdir,
170 attrs[srv_index][1])
171 location = os.path.normpath(location1)
172
173 # check to make sure that the image I am including was in the
174 # onlinehelp folder, if not change the dst name so that it will be
175 # located correctly in the stage3 directory
176 logging.debug("%s - %s", location, self.options.indir)
177 if location.startswith(self.options.indir):
178 dst1 = os.path.join(self.files['stage3'], self.subdir,
179 attrs[srv_index][1])
180 dst = os.path.normpath(dst1)
181 else:
182 # get extension
183 basename = os.path.basename(attrs[srv_index][1])
184 (name, ext) = os.path.splitext(basename)
185 (f, outname) = tempfile.mkstemp(ext, name, self.files['stage3'])
186 # make into a relative path
187 dst1 = outname.replace(os.getcwd(), ".")
188
189 # fix up attrs
190 dst = os.path.normpath(dst1)
191 filename = change_filename(dst, os.path.splitext(dst)[1],
192 self.files['stage3'], ".",
193 makedirs=False)
194 attrs = update_attribute(attrs, 'src', filename)
195
196 if alt_index is None:
197 alt_index = srv_index
198 logging.debug(" Image (%s) should be in %s and copying to %s",
199 attrs[alt_index][1], location, dst)
200 try:
201 if not os.path.exists(os.path.dirname(dst)):
202 os.mkdir(os.path.dirname(dst))
203 shutil.copy2(location, dst)
204 except:
205 logging.exception(" '%s' does not exist", location)
206 raise
207
208 self.extrafiles.append(dst)
209 OutputParser.handle_startendtag(self, tag, attrs)
210
211
212 class Stage4Parser(OutputParser):
213 def __init__(self, files, options, subdir, *args, **kwargs):
214 OutputParser.__init__(self, *args, **kwargs)
215 self.files = files
216 self.options = options
217 self.subdir = subdir
218
219 def handle_starttag(self, tag, attrs):
220 if tag == "a":
221 for name, value in attrs:
222 attrs = self._handle_start_tag(attrs, name, value)
223 OutputParser.handle_starttag(self, tag, attrs)
224
225 def _handle_start_tag(self, attrs, name, value):
226 if name == "href" and value.startswith("/"):
227 if value.startswith("/"):
228 # manual wants an absolute path, the help manual does not
229 # support absolute path, so make sure that the image exists
230 # where the absolute path indicates, then make the path into a
231 # relative path with the appropriate number of up dirs
232
233 # value is absolute but we need it relative, so drop the slash
234 if not self._path_exists(value[1:]):
235 raise ValueError("Cannot find %s in base path", value)
236
237 # try find a valid relative path
238 subdirdepth = len(self.subdir.split(os.path.sep))
239 prefix = "../" * subdirdepth
240 relpath = os.path.join(prefix, value[1:])
241 if not self._path_exists(self.subdir, relpath):
242 raise ValueError("Cannot relativize path: %s", value)
243 else:
244 attrs = update_attribute(attrs, 'src', relpath)
245 value = relpath
246 if name == "href" and not value.startswith("http://"):
247 # make sure the file being referred to exists in stage3
248 check_ref = change_filename(value, ".stage3", ".",
249 self.files['stage3'],
250 makedirs=False)
251 if not os.path.exists(check_ref):
252 logging.warning(
253 " File (%s) does not exist to be bundled into archive!",
254 check_ref)
255
256 fixed_ref = change_filename(value, ".htm", ".", ".",
257 makedirs=False)
258 attrs = update_attribute(attrs, "href", fixed_ref)
259 return attrs
260
261 def _path_exists(self, *paths):
262 return os.path.exists(os.path.join(self.options.indir, *paths))
263
264
265 class Stage5Parser(OutputParser):
266 def __init__(self, *args, **kwargs):
267 OutputParser.__init__(self, *args, **kwargs)
268 self.title = "Untitled"
269
270 def handle_endtag(self, tag):
271 if tag == "title":
272 t = self.tag_stack.pop()
273 self.title = t.data
274 self.tag_stack.append(t)
275 elif tag == "h1" and self.title == "Untitled":
276 # make first h1 tag found into the title will be overwritten by
277 # a title tag if it exists.
278 t = self.tag_stack.pop()
279 self.title = t.data
280 self.tag_stack.append(t)
281 OutputParser.handle_endtag(self, tag)