def make_html_element( tag, text='', children=None, tail=None, ): 'Make a HtmlElement object' el = Element(tag) if text is not None: el.text = text if children is not None: el.extend(children) if tail is not None: el.tail = text return el
def fragment_fromstring(html, create_parent=False, guess_charset=None, parser=None): """Parses a single HTML element; it is an error if there is more than one element, or if anything but whitespace precedes or follows the element. If 'create_parent' is true (or is a tag name) then a parent node will be created to encapsulate the HTML in a single element. In this case, leading or trailing text is allowed. If `guess_charset` is true, the `chardet` library will perform charset guessing on the string. """ if not isinstance(html, _strings): raise TypeError("string required") accept_leading_text = bool(create_parent) elements = fragments_fromstring( html, guess_charset=guess_charset, parser=parser, no_leading_text=not accept_leading_text, ) if create_parent: if not isinstance(create_parent, _strings): create_parent = "div" new_root = Element(create_parent) if elements: if isinstance(elements[0], _strings): new_root.text = elements[0] del elements[0] new_root.extend(elements) return new_root if not elements: raise etree.ParserError("No elements found") if len(elements) > 1: raise etree.ParserError("Multiple elements found") result = elements[0] if result.tail and result.tail.strip(): raise etree.ParserError("Element followed by text: %r" % result.tail) result.tail = None return result
def fragment_fromstring(html, create_parent=False, guess_charset=None, parser=None): """Parses a single HTML element; it is an error if there is more than one element, or if anything but whitespace precedes or follows the element. If 'create_parent' is true (or is a tag name) then a parent node will be created to encapsulate the HTML in a single element. In this case, leading or trailing text is allowed. If `guess_charset` is true, the `chardet` library will perform charset guessing on the string. """ if not isinstance(html, _strings): raise TypeError('string required') accept_leading_text = bool(create_parent) elements = fragments_fromstring( html, guess_charset=guess_charset, parser=parser, no_leading_text=not accept_leading_text) if create_parent: if not isinstance(create_parent, _strings): create_parent = 'div' new_root = Element(create_parent) if elements: if isinstance(elements[0], _strings): new_root.text = elements[0] del elements[0] new_root.extend(elements) return new_root if not elements: raise etree.ParserError('No elements found') if len(elements) > 1: raise etree.ParserError('Multiple elements found') result = elements[0] if result.tail and result.tail.strip(): raise etree.ParserError('Element followed by text: %r' % result.tail) result.tail = None return result
perc = float(output[1].split(':')[1].split('%')[0]) gcov = output[2].strip().split()[1].strip("'") # move generated gcov to coverage folder new_dir = os.path.join(target_dir, os.path.dirname(source)) try: os.makedirs(new_dir) except OSError: pass os.rename(os.path.join(obspy_dir, gcov), os.path.join(new_dir, gcov)) cov.append((filename, os.path.join(new_dir, gcov), perc)) # GENERATE HTML page = fromstring("<html><table></table></html>") table = page.xpath('.//table')[0] for name, gcov, perc in cov: td1, td2 = Element('td'), Element('td') gcov = gcov.replace(target_dir, './') a = Element('a', attrib={'href': gcov}) a.text = name td1.append(a) td2.text = "%6.2f%%" % perc tr = Element('tr') tr.extend([td1, td2]) table.append(tr) with open(os.path.join(target_dir, 'index.html'), 'wb') as fp: fp.write(tostring(page)) cleanup('*.o')
# read stdout filename = fp.readline().strip().split()[1].strip("'") perc = float(fp.readline().split(':')[1].split('%')[0]) gcov = fp.readline().strip().split()[1].strip("'") # move genereted gcov to coverage folder new_dir = join(target_dir, dirname(source)) try: makedirs(new_dir) except OSError: pass rename(join(obspy_dir, gcov), join(new_dir, gcov)) cov.append((filename, join(new_dir, gcov), perc)) # GENERATE HTML page = fromstring("<html><table></table></html>") table = page.xpath('.//table')[0] for name, gcov, perc in cov: td1, td2 = Element('td'), Element('td') gcov = gcov.replace(target_dir, './') a = Element('a', attrib={'href': gcov}) a.text = name td1.append(a) td2.text = "%6.2f%%" % perc tr = Element('tr') tr.extend([td1, td2]) table.append(tr) with open(join(target_dir, 'index.html'), 'wb') as fp: fp.write(tostring(page)) cleanup('*.o')