def set_body(self, events): body = self.get_body() events = self.events[:body.start+1] + events + self.events[body.end:] if stream_to_str(events) == stream_to_str(self.events): return False self.set_changed() self.events = events return True
def set_body(self, events): body = self.get_body() events = self.events[:body.start + 1] + events + self.events[body.end:] if stream_to_str(events) == stream_to_str(self.events): return False self.set_changed() self.events = events return True
def stl(document=None, namespace=freeze({}), prefix=None, events=None, mode='events', skip=(DOCUMENT_TYPE,)): # Input encoding = 'utf-8' if events is None: events = document.events # Prefix if prefix is not None: stream = set_prefix(events, prefix) events = list(stream) elif type(events) in (GeneratorType, XMLParser): events = list(events) # Initialize the namespace stacks stack = NamespaceStack() stack.append(namespace) repeat = NamespaceStack() # Process stream = process(events, 0, len(events), stack, repeat, encoding, skip) # Return if mode == 'events': return stream elif mode == 'xml': return stream_to_str(stream, encoding) elif mode == 'xhtml': return stream_to_str_as_xhtml(stream, encoding) elif mode == 'html': return stream_to_str_as_html(stream, encoding) raise ValueError, 'unexpected mode "%s"' % mode
def stl(document=None, namespace=freeze({}), prefix=None, events=None, mode='events', skip=(DOCUMENT_TYPE,)): # Input encoding = 'utf-8' if events is None: events = document.events # Prefix if prefix is not None: stream = set_prefix(events, prefix) events = list(stream) elif isinstance(events, (GeneratorType, XMLParser)): events = list(events) # Initialize the namespace stacks stack = NamespaceStack() stack.append(namespace) repeat = NamespaceStack() # Process stream = process(events, 0, len(events), stack, repeat, encoding, skip) # Return if mode == 'events': return stream elif mode == 'xml': return stream_to_str(stream, encoding) elif mode == 'xhtml': return stream_to_str_as_xhtml(stream, encoding) elif mode == 'html': return stream_to_str_as_html(stream, encoding) raise ValueError, 'unexpected mode "%s"' % mode
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 to_str(text, format, encoding='utf-8'): if format == 'xml': events = block_stream(text) return stream_to_str(events, encoding) elif format == 'xhtml': events = to_html_events(text) return stream_to_str_as_xhtml(events, encoding) elif format == 'html': events = to_html_events(text) return stream_to_str_as_html(events, encoding) elif format == 'latex': events = block_stream(text) return stream_to_str_as_latex(events, encoding) raise ValueError, "unexpected format '%s'" % format
def stl(document=None, namespace=freeze({}), prefix=None, events=None, mode='events', skip=(DOCUMENT_TYPE, )): # Input encoding = 'utf-8' if events is None: events = document.events # Prefix if prefix is not None: stream = set_prefix(events, prefix) events = list(stream) elif type(events) in (GeneratorType, XMLParser): events = list(events) # Initialize the namespace stacks stack = NamespaceStack() stack.append(namespace) repeat = NamespaceStack() # Process stream = process(events, 0, len(events), stack, repeat, encoding, skip) # Return try: if mode == 'events': return stream elif mode == 'xml': return stream_to_str(stream, encoding) elif mode == 'xhtml': return stream_to_str_as_xhtml(stream, encoding) elif mode == 'html': return stream_to_str_as_html(stream, encoding) except STLError, e: error = 'Error in generation of {0}\n'.format(mode) if document: error += 'Template {0}\n'.format(document.key) raise STLError(error + e.message)
def stl(document=None, namespace=freeze({}), prefix=None, events=None, mode='events', skip=(DOCUMENT_TYPE,)): # Input encoding = 'utf-8' if events is None: events = document.events # Prefix if prefix is not None: stream = set_prefix(events, prefix) events = list(stream) elif type(events) in (GeneratorType, XMLParser): events = list(events) # Initialize the namespace stacks stack = NamespaceStack() stack.append(namespace) repeat = NamespaceStack() # Process stream = process(events, 0, len(events), stack, repeat, encoding, skip) # Return try: if mode == 'events': return stream elif mode == 'xml': return stream_to_str(stream, encoding) elif mode == 'xhtml': return stream_to_str_as_xhtml(stream, encoding) elif mode == 'html': return stream_to_str_as_html(stream, encoding) except STLError, e: error = 'Error in generation of {0}\n'.format(mode) if document: error += 'Template {0}\n'.format(document.key) raise STLError(error + e.message)
def GET(self, resource, context): # Content-Type context.set_content_type('text/opml') context.set_content_disposition('attachment', 'rss-agregator.opml') stream = resource.to_opml_stream(context) return stream_to_str(stream)
def translate(self, catalog, srx_handler=None): stream = translate(self.events, catalog, srx_handler) return stream_to_str(stream)
def to_str(self, encoding='UTF-8'): return stream_to_str(self.events, encoding)
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 test_simple(self): data = 'Hello ${name}' stream = substitute(data, self.stack, self.repeat) # Assert out = stream_to_str(stream) self.assertEqual(out, 'Hello Toto')
def test_doctype(self): data = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">' stream = HTMLParser(data) self.assertEqual(stream_to_str(stream), data)
def stream_to_html(stream, encoding='UTF-8', map=stream_to_html_map): return stream_to_str(stream, encoding=encoding, map=map)
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 stream_to_str_as_xhtml(stream, encoding='UTF-8'): content_type = 'application/xhtml+xml; charset=%s' % encoding stream = set_content_type(stream, content_type) return stream_to_str(stream, encoding)
def _get_translatable_blocks(events): # Default value encoding = 'utf-8' # To identify the begin/end format id = 0 id_stack = [] context_stack = [None] stream = None message = Message() skip_level = 0 for event in events: type, value, line = event # Set the good encoding if type == XML_DECL: encoding = value[1] # And now, we catch only the good events elif type == START_ELEMENT: if skip_level > 0: skip_level += 1 if stream: stream.append(event) continue else: tag_uri, tag_name, attributes = value schema = get_element_schema(tag_uri, tag_name) # Context management if schema.context is not None: context_stack.append(schema.context) # Skip content ? if schema.skip_content: skip_level = 1 if id_stack: stream = [event] continue # Is inline ? elif schema.is_inline: id += 1 id_stack.append(id) start_format = _make_start_format(tag_uri, tag_name, attributes, encoding) message.append_start_format(start_format, id, line) continue elif id_stack: skip_level = 1 stream = [event] continue elif type == END_ELEMENT: if skip_level > 0: skip_level -= 1 if stream: stream.append(event) if skip_level == 0: id += 1 aux = stream_to_str(stream, encoding) aux = unicode(aux, encoding) aux = [(aux, False, context_stack[-1])] message.append_start_format(aux, id, line) message.append_end_format([], id, line) stream = None continue else: tag_uri, tag_name = value[:2] schema = get_element_schema(tag_uri, tag_name) # Context management if schema.context is not None: context_stack.pop() # Is inline ? if schema.is_inline: message.append_end_format([(get_end_tag(value), False, None)], id_stack.pop(), line) continue elif type == TEXT: # Not empty ? if stream: stream.append(event) continue elif skip_level == 0 and (value.strip() != '' or message): value = XMLContent.encode(value) value = unicode(value, encoding) message.append_text(value, line, context_stack[-1]) continue elif type == COMMENT: if stream: stream.append(event) continue elif message: id += 1 if isinstance(value, str): value = unicode(value, encoding) value = u'<!--%s-->' % value message.append_start_format([(value, False, None)], id, line) message.append_end_format([], id, line) continue # Not a good event => break + send the event if message: yield MESSAGE, message, message.get_line() message = Message() yield event # Send the last message! if message: yield MESSAGE, message, message.get_line()