def include_additional_menu_items(): parser = XMLParser(remove_blank_text=True) doc = xml_from_string(gen_menu_items(), parser=parser) html_fragments_str = '' for elem in doc.xpath('/child::node()/*'): # because Jinja2 produces HTML5 instead of XHTML, we need to re-parse the fragments: html_fragments_str += html_to_string( html_fragment_from_string(xml_to_string(elem))).decode('utf-8') return Markup(html_fragments_str)
def create_and_import(self, xml): name = inspect.stack()[1][3] path = os.path.join(self.package_tests_path, name) if os.access(path, os.F_OK): shutil.rmtree(path) xml = xml.format(root = name) structure = CTypesStructure.from_xml(xml_from_string(xml)) structure.packages[name].add_library(Library("mock", self.mock_library_path, True)) export_structure(structure, self.package_tests_path) mod = import_module(name) return mod
def import_from_ods(filename_or_fobj, index=0, *args, **kwargs): # TODO: import spreadsheet by name # TODO: unescape values filename, _ = get_filename_and_fobj(filename_or_fobj) ods_file = zipfile.ZipFile(filename) content_fobj = ods_file.open("content.xml") xml = content_fobj.read() # will return bytes content_fobj.close() document = xml_from_string(xml) namespaces = document.nsmap spreadsheet = document.xpath("//office:spreadsheet", namespaces=namespaces)[0] tables = xpath(spreadsheet, "//table:table", namespaces) table = tables[index] table_rows_obj = xpath(table, "//table:table-row", namespaces) table_rows = [] for row_obj in table_rows_obj: row = [] for cell in xpath(row_obj, "//table:table-cell", namespaces): children = cell.getchildren() if not children: continue # TODO: evalute 'boolean' and 'time' types value_type = attrib(cell, namespaces["office"], "value-type") if value_type == "date": cell_value = attrib(cell, namespaces["office"], "date-value") elif value_type == "float": cell_value = attrib(cell, namespaces["office"], "value") elif value_type == "percentage": cell_value = attrib(cell, namespaces["office"], "value") cell_value = Decimal(cell_value) cell_value = "{:%}".format(cell_value) elif value_type == "string": try: # get computed string (from formula, for example) cell_value = attrib(cell, namespaces["office"], "string-value") except KeyError: # computed string not present => get from <p>...</p> cell_value = children[0].text else: # value_type == some type we don't know cell_value = children[0].text try: repeat = attrib(cell, namespaces["table"], "number-columns-repeated") except KeyError: row.append(cell_value) else: for _ in range(int(repeat)): row.append(cell_value) if row: table_rows.append(row) max_length = max(len(row) for row in table_rows) full_rows = complete_with_None(table_rows, max_length) meta = {"imported_from": "ods", "filename": filename} return create_table(full_rows, meta=meta, *args, **kwargs)
def xpath(element, xpath, namespaces): return xml_from_string(xml_to_string(element)).xpath(xpath, namespaces=namespaces)
def import_from_ods(filename_or_fobj, index=0, *args, **kwargs): # TODO: import spreadsheet by name # TODO: unescape values filename, _ = get_filename_and_fobj(filename_or_fobj) ods_file = zipfile.ZipFile(filename) content_fobj = ods_file.open('content.xml') xml = content_fobj.read() # will return bytes content_fobj.close() document = xml_from_string(xml) namespaces = document.nsmap spreadsheet = document.xpath('//office:spreadsheet', namespaces=namespaces)[0] tables = xpath(spreadsheet, '//table:table', namespaces) table = tables[index] table_rows_obj = xpath(table, '//table:table-row', namespaces) table_rows = [] for row_obj in table_rows_obj: row = [] for cell in xpath(row_obj, '//table:table-cell', namespaces): children = cell.getchildren() if not children: continue # TODO: evalute 'boolean' and 'time' types value_type = attrib(cell, namespaces['office'], 'value-type') if value_type == 'date': cell_value = attrib(cell, namespaces['office'], 'date-value') elif value_type == 'float': cell_value = attrib(cell, namespaces['office'], 'value') elif value_type == 'percentage': cell_value = attrib(cell, namespaces['office'], 'value') cell_value = Decimal(str(Decimal(cell_value) * 100)[:-2]) cell_value = '{}%'.format(cell_value) elif value_type == 'string': try: # get computed string (from formula, for example) cell_value = attrib(cell, namespaces['office'], 'string-value') except KeyError: # computed string not present => get from <p>...</p> cell_value = children[0].text else: # value_type == some type we don't know cell_value = children[0].text try: repeat = attrib(cell, namespaces['table'], 'number-columns-repeated') except KeyError: row.append(cell_value) else: for _ in range(int(repeat)): row.append(cell_value) if row: table_rows.append(row) max_length = max(len(row) for row in table_rows) full_rows = complete_with_None(table_rows, max_length) meta = {'imported_from': 'ods', 'filename': filename,} return create_table(full_rows, meta=meta, *args, **kwargs)
def import_from_ods(filename_or_fobj, index=0, *args, **kwargs): # TODO: import spreadsheet by name # TODO: unescape values filename, _ = get_filename_and_fobj(filename_or_fobj) ods_file = zipfile.ZipFile(filename) content_fobj = ods_file.open('content.xml') xml = content_fobj.read() # will return bytes content_fobj.close() document = xml_from_string(xml) namespaces = document.nsmap spreadsheet = document.xpath('//office:spreadsheet', namespaces=namespaces)[0] tables = xpath(spreadsheet, '//table:table', namespaces) table = tables[index] table_rows_obj = xpath(table, '//table:table-row', namespaces) table_rows = [] for row_obj in table_rows_obj: row = [] for cell in xpath(row_obj, '//table:table-cell', namespaces): children = cell.getchildren() if not children: continue # TODO: evalute 'boolean' and 'time' types value_type = attrib(cell, namespaces['office'], 'value-type') if value_type == 'date': cell_value = attrib(cell, namespaces['office'], 'date-value') elif value_type == 'float': cell_value = attrib(cell, namespaces['office'], 'value') elif value_type == 'percentage': cell_value = attrib(cell, namespaces['office'], 'value') cell_value = Decimal(str(Decimal(cell_value) * 100)[:-2]) cell_value = '{}%'.format(cell_value) elif value_type == 'string': try: # get computed string (from formula, for example) cell_value = attrib(cell, namespaces['office'], 'string-value') except KeyError: # computed string not present => get from <p>...</p> cell_value = children[0].text else: # value_type == some type we don't know cell_value = children[0].text try: repeat = attrib(cell, namespaces['table'], 'number-columns-repeated') except KeyError: row.append(cell_value) else: for _ in range(int(repeat)): row.append(cell_value) if row: table_rows.append(row) max_length = max(len(row) for row in table_rows) full_rows = complete_with_None(table_rows, max_length) meta = { 'imported_from': 'ods', 'filename': filename, } return create_table(full_rows, meta=meta, *args, **kwargs)