def convertodf(inputfile, outputfile, templates, engine='toolkit'): """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout """ def translate_toolkit_implementation(store): import cStringIO import zipfile from translate.storage.xml_extract import extract from translate.storage import odf_shared contents = odf_io.open_odf(inputfile) for data in contents.values(): parse_state = extract.ParseState(odf_shared.no_translate_content_elements, odf_shared.inline_elements) extract.build_store(cStringIO.StringIO(data), store, parse_state) def itools_implementation(store): from itools.handlers import get_handler from itools.gettext.po import encode_source import itools.odf filename = getattr(inputfile, 'name', 'unkown') handler = get_handler(filename) try: get_units = handler.get_units except AttributeError: raise AttributeError('error: the file "%s" could not be processed' % filename) # Make the XLIFF file for source, context, line in get_units(): source = encode_source(source) unit = store.UnitClass(source) store.addunit(unit) @contextmanager def store_context(): store = factory.getobject(outputfile) try: store.setfilename(store.getfilenode('NoName'), inputfile.name) except: print "couldn't set origin filename" yield store store.save() def with_block(store): if engine == "toolkit": translate_toolkit_implementation(store) else: itools_implementation(store) # Since the convertoptionsparser will give us an open file, we risk that # it could have been opened in non-binary mode on Windows, and then we'll # have problems, so let's make sure we have what we want. inputfile.close() inputfile = file(inputfile.name, mode='rb') with_(store_context(), with_block) return True
def find_translatable_dom_nodes(dom_node, state): # For now, we only want to deal with XML elements. # And we want to avoid processing instructions, which # are XML elements (in the inheritance hierarchy). if not isinstance(dom_node, etree._Element) or \ isinstance(dom_node, etree._ProcessingInstruction): return [] namespace, tag = misc.parse_tag(dom_node.tag) @contextmanager def xpath_set(): state.xpath_breadcrumb.start_tag( compact_tag(state.nsmap, namespace, tag)) yield state.xpath_breadcrumb state.xpath_breadcrumb.end_tag() @contextmanager def placeable_set(): old_placeable_name = state.placeable_name state.placeable_name = tag yield state.placeable_name state.placeable_name = old_placeable_name @contextmanager def inline_set(): old_inline = state.is_inline if (namespace, tag) in state.inline_elements: state.is_inline = True else: state.is_inline = False yield state.is_inline state.is_inline = old_inline def with_block(xpath_breadcrumb, placeable_name, is_inline): if (namespace, tag) not in state.no_translate_content_elements: return _process_translatable(dom_node, state) else: return _process_children(dom_node, state) return with_(nested(xpath_set(), placeable_set(), inline_set()), with_block)
def find_translatable_dom_nodes(dom_node, state): # For now, we only want to deal with XML elements. # And we want to avoid processing instructions, which # are XML elements (in the inheritance hierarchy). if not isinstance(dom_node, etree._Element) or \ isinstance(dom_node, etree._ProcessingInstruction): return [] namespace, tag = misc.parse_tag(dom_node.tag) @contextmanager def xpath_set(): state.xpath_breadcrumb.start_tag(compact_tag(state.nsmap, namespace, tag)) yield state.xpath_breadcrumb state.xpath_breadcrumb.end_tag() @contextmanager def placeable_set(): old_placeable_name = state.placeable_name state.placeable_name = tag yield state.placeable_name state.placeable_name = old_placeable_name @contextmanager def inline_set(): old_inline = state.is_inline if (namespace, tag) in state.inline_elements: state.is_inline = True else: state.is_inline = False yield state.is_inline state.is_inline = old_inline def with_block(xpath_breadcrumb, placeable_name, is_inline): if (namespace, tag) not in state.no_translate_content_elements: return _process_translatable(dom_node, state) else: return _process_children(dom_node, state) return with_(nested(xpath_set(), placeable_set(), inline_set()), with_block)