source: orange/Orange/OrangeCanvas/preview/scanner.py @ 11114:16262014a6ea

Revision 11114:16262014a6ea, 3.1 KB checked in by Ales Erjavec <ales.erjavec@…>, 18 months ago (diff)

Added preview browser widget and dialog.

Line 
1"""
2Scheme file preview parser.
3
4"""
5import os
6import logging
7
8from xml.sax import make_parser, handler, saxutils, SAXParseException
9
10log = logging.getLogger(__name__)
11
12
13class PreviewHandler(handler.ContentHandler):
14    def __init__(self):
15        self._in_name = False
16        self._in_description = False
17        self._in_thumbnail = False
18        self.name_data = []
19        self.title = None
20        self.description = None
21        self.description_data = []
22        self.thumbnail_data = []
23
24    def startElement(self, name, attrs):
25        if name == "scheme":
26            if attrs.get("version", "1.0") >= "2.0":
27                self.title = attrs.get("title", None)
28                self.description = attrs.get("description", None)
29
30        elif name == "thumbnail":
31            self._in_thumbnail = True
32
33    def endElement(self, name):
34        if name == "name":
35            self._in_name = False
36        elif name == "description":
37            self._in_description = False
38        elif name == "thumbnail":
39            self._in_thumbnail = False
40
41    def characters(self, content):
42        if self._in_name:
43            self.name_data.append(content)
44        elif self._in_description:
45            self.description_data.append(content)
46        elif self._in_thumbnail:
47            self._thumbnail_data.append(content)
48
49
50def preview_parse(scheme_file):
51    """Return the title, description, and thumbnail svg image data from a
52    `scheme_file` (can be a file path or a file-like object).
53
54    """
55    parser = make_parser()
56    handler = PreviewHandler()
57    parser.setContentHandler(handler)
58    parser.parse(scheme_file)
59
60    name_data = handler.title or ""
61    description_data = handler.description or ""
62    svg_data = "".join(handler.thumbnail_data)
63
64    return (saxutils.unescape(name_data),
65            saxutils.unescape(description_data),
66            saxutils.unescape(svg_data))
67
68
69def scheme_svg_thumbnail(scheme_file):
70    """Load the scheme scheme from a file and return it's svg image
71    representation.
72
73    """
74    from .. import scheme
75    from ..canvas import scene
76    scheme = scheme.Scheme()
77    scheme.load_from(scheme_file)
78    tmp_scene = scene.CanvasScene()
79    tmp_scene.set_scheme(scheme)
80    svg = scene.grab_svg(tmp_scene)
81    tmp_scene.clear()
82    tmp_scene.deleteLater()
83    return svg
84
85
86def scan_update(item):
87    """Given a preview item, scan the scheme file ('item.path') and update the
88    items contents.
89
90    """
91    path = unicode(item.path())
92    try:
93        title, desc, svg = preview_parse(path)
94    except SAXParseException, ex:
95        log.error("%r is malformed (%r)", path, ex)
96        item.setEnabled(False)
97        item.setSelectable(False)
98        return
99
100    if not title:
101        title = os.path.splitext(os.path.basename(path))[0]
102
103    if not svg:
104        try:
105            svg = scheme_svg_thumbnail(path)
106        except Exception:
107            log.error("Could not render scheme preview for %r", title,
108                      exc_info=True)
109
110    if item.name() != title:
111        item.setName(title)
112
113    if item.description() != desc:
114        item.setDescription(desc)
115
116    if svg:
117        item.setThumbnail(svg)
Note: See TracBrowser for help on using the repository browser.