← Back to team overview

a4-dev team mailing list archive

[Merge] lp:~andrea-bs/a4/parser into lp:a4

 

Andrea Corbellini has proposed merging lp:~andrea-bs/a4/parser into lp:a4.

Requested reviews:
  A4 development (a4-dev)


This code looks for a <metadata> tag with the id "a4-presentation-information" into the SVG file. This tag will contain all the information about the presentation (e.g. the path and the sequence of animations).

Here's how you can test this change:

1. with your favorite editor, create an SVG document;
2. open it with a text editor and put into the root element (<svg>) the following code:

  <metadata id="a4-presentation-information">{"key": "value"}</metadata>

3. save and open the file with A4.

Now, if you introspect the a4lib.presentation.Presentation object you should see that its `information` attribute contains exactly this value:

  {u'key': u'value'}

See also my mail to the mailing list.
-- 
https://code.launchpad.net/~andrea-bs/a4/parser/+merge/26311
Your team A4 development is requested to review the proposed merge of lp:~andrea-bs/a4/parser into lp:a4.
=== modified file 'a4lib/app.py'
--- a4lib/app.py	2010-05-27 14:23:58 +0000
+++ a4lib/app.py	2010-05-28 14:36:37 +0000
@@ -6,7 +6,7 @@
 import optparse
 import os
 import gtk
-from a4lib.svgimage import SVGImage, SVGImageError
+from a4lib.presentation import Presentation, PresentationError
 
 class WindowMain(object):
     """The main window of the application."""
@@ -32,8 +32,8 @@
         self.set_status('Loading {0}...'.format(file_name))
         try:
             # Try to open a file.
-            image = SVGImage(file_name)
-        except SVGImageError as error:
+            image = Presentation(file_name)
+        except PresentationError as error:
             # The file doesn't exist, or is not an SVG file. Show an error
             # dialog and don't do anything else.
             dialog = gtk.MessageDialog(

=== renamed file 'a4lib/svgimage.py' => 'a4lib/presentation.py'
--- a4lib/svgimage.py	2010-05-27 13:58:29 +0000
+++ a4lib/presentation.py	2010-05-28 14:36:37 +0000
@@ -3,16 +3,18 @@
 
 """Code to deal with SVG images."""
 
+import json
+from xml.dom import minidom
 import rsvg
 from glib import GError
 
 
-class SVGImageError(StandardError):
+class PresentationError(StandardError):
     """Error raised when an operation with an image fails."""
 
 
-class SVGImage(object):
-    """An object that represents an image contained in a SVG file."""
+class Presentation(object):
+    """An object that represents a presentation."""
 
     def __init__(self, file_name):
         self.file_name = file_name
@@ -20,9 +22,24 @@
             svg_data = open(file_name).read()
             self._svg_handler = rsvg.Handle(data=svg_data)
         except IOError as error:
-            raise SVGImageError(error.args[1])
+            raise PresentationError(error.args[1])
         except GError as error:
-            raise SVGImageError('Unknown file type')
-        self.width = self._svg_handler.get_property('width')
-        self.height = self._svg_handler.get_property('height')
+            raise PresentationError('Unknown file type')
+
+        dom = minidom.parseString(svg_data)
+        for node in dom.getElementsByTagName('metadata'):
+            if node.getAttribute('id') == 'a4-presentation-information':
+                data = u''.join(
+                    child.data for child in node.childNodes
+                    if child.nodeType == node.TEXT_NODE)
+                try:
+                    self.information = json.loads(data)
+                except ValueError:
+                    raise PresentationError('File corrupted')
+                break
+        else:
+            raise PresentationError('Not a presentation')
+
+        self.width = self._svg_handler.props.width
+        self.height = self._svg_handler.props.height
         self.render_cairo = self._svg_handler.render_cairo