def figformat(*filenames): """ Check that figures are properly formatted. """ for f in filenames: doc = read_xml(f) figures = doc.findall(".//figure") for fig in figures: fig_id, caption_text = _get_fig_info(f, fig) all_images = fig.findall(".//img") assert len(all_images) <= 1, \ "Two or more images in %s for %s" % (f, fig_id) if len(all_images) == 1: # some figures contain tables or code img = all_images[0] img_alt = img.attrib.get('alt') assert img_alt == caption_text, \ "Inconsistent alt and caption for Figure %s in %s: '%s' vs '%s'" % \ (fig_id, f, img_alt, caption_text) img_path = img.attrib.get('src') try: img_basename = os.path.basename(os.path.splitext(img_path)[0]) assert fig_id.endswith(img_basename), \ "Figure id %s in %s inconsistent with image path %s" % \ (fig_id, f, img_path) except AttributeError, e: assert False, \ "Failed to check path and id consistency of Figure %s in %s" % \ (fig_id, f)
def echo(*filenames): """ Parse and then print. """ for f in filenames: doc = read_xml(f) print ET.tostring(doc.getroot())
def valid(*filenames): """ Check that all files are HTML compliant. """ for f in filenames: doc = read_xml(f) if doc is None: print 'failed to parse "%s"' % f
def glossformat(*filenames): """ Look for improperly formatted glossary references. """ for f in filenames: doc = read_xml(f) refs = doc.findall(".//a[@href]") refs = [r for r in refs if r.attrib.get('href', '').startswith('glossary.html')] refs = [r for r in refs if r.attrib.get('class', '') != 'dfn'] for r in refs: print '%s %s: %s' % (FMT_BAD, f, r.attrib['href'])
def classes(*filenames): """ List all the HTML classes used in a set of files. """ result = set() for f in filenames: for n in read_xml(f).findall(".//*[@class]"): for a in n.attrib['class'].split(): result.add(a) for r in sorted(result): print r
def structure(*filenames): """ Check overall structure of files. """ for f in filenames: doc = read_xml(f) _find_one_node(f, doc, ".//div[@class='mainmenu']") _find_one_node(f, doc, ".//div[@class='footer']") doctype = _find_one_node(f, doc, ".//meta[@name='type']") if doctype.attrib["content"] != "chapter": continue _find_one_node(f, doc, ".//ol[@class='toc']")
def _get_defs_refs(filenames, ident_key): """ Get definitions and cross-references from files. """ defs = set() refs = set() for f in filenames: doc = read_xml(f) defs.update(_get_defs(doc, ident_key)) refs.update(_get_refs(doc, ident_key)) return defs, refs
def _find_refs(filenames, element, attribute, strip=PARENT_PREFIX): """ Find all references to files of a particular type. """ xpath = ".//%s[@%s]" % (element, attribute) result = set() for f in filenames: doc = read_xml(f) nodes = doc.findall(xpath) vals = {n.attrib[attribute] for n in nodes} result.update(vals) result = {x.lstrip(strip) for x in result} return result
def get_links(root_dir, filenames): """ Extract links from files, return a set of (filename, normalized, raw) links. """ links = set() for f in filenames: doc = read_xml(f) try: tags = doc.findall('.//a[@href]') except SyntaxError: # ElementTree 1.2 lacks attribute XPath support tags = [t for t in doc.findall('.//a') if 'href' in t.attrib] links.update( set(normalize(root_dir, f, r.attrib['href']) for r in tags)) return set(lnk for lnk in links if lnk) # filter out None's
def get_links(root_dir, filenames): """ Extract links from files, return a set of (filename, normalized, raw) links. """ links = set() for f in filenames: doc = read_xml(f) try: tags = doc.findall('.//a[@href]') except SyntaxError: # ElementTree 1.2 lacks attribute XPath support tags = [t for t in doc.findall('.//a') if 'href' in t.attrib] links.update(set(normalize(root_dir, f, r.attrib['href']) for r in tags)) return set(lnk for lnk in links if lnk) # filter out None's
def summaries(*filenames): """ Extract goals and keypoints from files. """ def _lecture(filename, doc): title = doc.findall(".//div[@class='title']")[0] title.text = title.text.strip() title.tag = 'h2' title.tail = '' print '<a href="%s">%s</a>' % (filename, ET.tostring(title)) def _get_section_title(node): title = node.findall(".//h2") if not title: return None title = title[0] title.tag = 'h3' title.tail = '' return title def _sections(filename, doc): for s in doc.findall(".//section"): title = _get_section_title(s) if title is None: continue title = ET.tostring(title) understand = _get_points(filename, s, 'understand') keypoints = _get_points(filename, s, 'keypoints') if (not understand) and (not keypoints): continue assert understand and keypoints, \ 'Section %s in %s has understanding/keypoints mis-match' % (title, filename) print ' <a href="%s#%s">%s</a>' % \ (filename, s.attrib.get('id'), title) print '<p><strong>Understand:</strong></p>%s\n<p><strong>Summary:</strong></p>%s' % \ (understand, keypoints) print '<html>' print '<body>' for f in filenames: doc = read_xml(f) _lecture(f, doc) _sections(f, doc) print '</body>' print '</html>'
def words(*filenames): """ Count words in files (excluding code blocks). """ format = _make_format(filenames) single = '%6d' total = 0 for f in filenames: doc = read_xml(f) nodes = doc.findall(".//*") count = 0 for n in nodes: if n.tag == 'pre': continue for x in (n.text, n.tail): if x: count += len(x.split()) print format % _remove_dir(f), single % count total += count print format % 'Total', single % total
def fix(*filenames): """ Count 'fixme' markers in files. """ flags = 'fixme'.split() counts = {f:0 for f in flags} format = _make_format(filenames) print format % 'File', for flag in flags: print '%6s' % flag.lstrip('fix'), print for f in filenames: doc = read_xml(f) print format % f, for flag in flags: num = len(doc.findall(".//*[@class='%s']" % flag)) counts[flag] += num print '%6d' % num, print print format % 'Total', for flag in flags: print '%6d' % counts[flag], print
def ideas(*filenames): """ Extract ideas from files and display in groups. """ all_ideas = {} for f in filenames: doc = read_xml(f) for section in doc.findall(".//div[@class='keypoints']"): for example in section.findall(".//li[@idea]"): ideas = example.attrib['idea'].split(';') del example.attrib['idea'] example.tag = 'a' example.attrib['href'] = '%s#%s' % (f, section.attrib.get('id')) example.tail = '' for i in ideas: if i not in all_ideas: all_ideas[i] = [] all_ideas[i].append(ET.tostring(example)) for idea in all_ideas: print '<h2>%s</h2>' % idea print '<ul>' for item in all_ideas[idea]: print ' <li>%s</li>' % item print '</ul>'
""" Third example: single-fluid, single asymmetric pulse """ import os import numpy as np from plot import make_figure from util import run_ebtel, read_xml top_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) if __name__ == '__main__': # Configure and run ebtel++ base = read_xml((os.path.join(top_dir, 'config', 'ebtel.example.cfg.xml'))) base['calculate_dem'] = True base['heating']['partition'] = 0.5 base['force_single_fluid'] = True base['heating']['events'] = [{ 'event': { 'rise_start': 0.0, 'rise_end': 250.0, 'decay_start': 1000.0, 'decay_end': 2000.0, 'magnitude': 0.005 } }] results = run_ebtel(base, top_dir) # Time average DEM results['dem_total'] = np.average(results['dem_tr'] +