def translate(self, catalog, srx_handler=None): """Translate the document and reconstruct an odt document. """ # Translate modified_files = {} for filename in ['content.xml', 'meta.xml', 'styles.xml']: events = self.get_events(filename) translation = translate(events, catalog, srx_handler) modified_files[filename] = stream_to_str(translation) # Zip return zip_data(self.data, modified_files)
def test_translation_paragraph(self): """Test translation of an element content""" po = POFile(string='msgctxt "paragraph"\n' 'msgid "hello world"\n' 'msgstr "hola mundo"\n') content = ('<office:text>' '<text:p text:style-name="Standard">' 'hello world' '</text:p>' '</office:text>') content = odt_template % content messages = XMLParser(content) messages = translate(messages, po) messages = [unit[0] for unit in get_units(messages)] self.assertEqual(messages, [((TEXT, u'hola mundo'), )])
def test_translation_paragraph(self): """Test translation of an element content""" po = POFile(string= 'msgctxt "paragraph"\n' 'msgid "hello world"\n' 'msgstr "hola mundo"\n') content = ('<office:text>' '<text:p text:style-name="Standard">' 'hello world' '</text:p>' '</office:text>') content = odt_template % content messages = XMLParser(content) messages = translate(messages, po) messages = [unit[0] for unit in get_units(messages)] self.assertEqual(messages, [((TEXT, u'hola mundo'),)])
def translate(self, catalog, srx_handler=None): stream = translate(self.events, catalog, srx_handler) return stream_to_str_as_html(stream)
def greek(self): """Anonymize the ODF file. """ # Verify PIL is installed if PILImage is None: err = 'The greeking feature requires the Python Imaging Library' raise ImportError, err folder = lfs.open(get_abspath('.')) err = 'Unexpected "%s" file will be omitted from the greeked document' modified_files = {} for filename in self.get_contents(): extension = splitext(filename)[1] startswith = filename.startswith # Files to keep as they are # TODO the manifest.xml file should be properly updated keep = ['mimetype', 'settings.xml', 'META-INF/manifest.xml'] if filename in keep: pass # Content, metadata and style elif filename in ['content.xml', 'meta.xml', 'styles.xml']: events = self.get_events(filename) translation = translate(events, GreekCatalog) modified_files[filename] = stream_to_str(translation) # Thumbnails elif startswith('Thumbnails'): if extension == '.pdf': modified_files[filename] = folder.open('thumb.pdf').read() elif extension == '.png': modified_files[filename] = folder.open('thumb.png').read() else: # Unexpected (TODO use the logging system) modified_files[filename] = None print err % filename # SVM files (usually they are in the Pictures folder) elif extension == '.svm': modified_files[filename] = folder.open('square.svm').read() # Pictures elif startswith('Pictures') or startswith('media'): # Try with PIL file = self.get_file(filename) file = StringIO(file) image = PILImage.open(file) format = image.format image = image.convert('RGB') image.filename = filename draw = PILImageDraw.Draw(image) # Make a cross h, l = image.size draw.rectangle((0, 0, h-1, l-1), fill="grey", outline="black") draw.line((0, 0, h-1, l-1), fill="black") draw.line((0, l-1, h-1, 0), fill="black") # Save data = StringIO() image.save(data, format) modified_files[filename] = data.getvalue() # Unexpected (TODO use the logging system) else: modified_files[filename] = None print err % filename return zip_data(self.data, modified_files)
def greek(self): """Anonymize the ODF file. """ # Verify PIL is installed if PILImage is None: err = 'The greeking feature requires the Python Imaging Library' raise ImportError, err folder = lfs.open(get_abspath('.')) err = 'Unexpected "%s" file will be omitted from the greeked document' modified_files = {} for filename in self.get_contents(): extension = splitext(filename)[1] startswith = filename.startswith # Files to keep as they are # TODO the manifest.xml file should be properly updated keep = ['mimetype', 'settings.xml', 'META-INF/manifest.xml'] if filename in keep: pass # Content, metadata and style elif filename in ['content.xml', 'meta.xml', 'styles.xml']: events = self.get_events(filename) translation = translate(events, GreekCatalog) modified_files[filename] = stream_to_str(translation) # Thumbnails elif startswith('Thumbnails'): if extension == '.pdf': modified_files[filename] = folder.open('thumb.pdf').read() elif extension == '.png': modified_files[filename] = folder.open('thumb.png').read() else: # Unexpected (TODO use the logging system) modified_files[filename] = None print err % filename # SVM files (usually they are in the Pictures folder) elif extension == '.svm': modified_files[filename] = folder.open('square.svm').read() # Pictures elif startswith('Pictures') or startswith('media'): # Try with PIL file = self.get_file(filename) file = StringIO(file) image = PILImage.open(file) format = image.format image = image.convert('RGB') image.filename = filename draw = PILImageDraw.Draw(image) # Make a cross h, l = image.size draw.rectangle((0, 0, h - 1, l - 1), fill="grey", outline="black") draw.line((0, 0, h - 1, l - 1), fill="black") draw.line((0, l - 1, h - 1, 0), fill="black") # Save data = StringIO() image.save(data, format) modified_files[filename] = data.getvalue() # Unexpected (TODO use the logging system) else: modified_files[filename] = None print err % filename return zip_data(self.data, modified_files)