source: orange/Orange/OrangeCanvas/preview/scanner.py @ 11207:8145a5984767

Revision 11207:8145a5984767, 3.2 KB checked in by Ales Erjavec <ales.erjavec@…>, 17 months ago (diff)

Fixed anchor point layout order reset while dragging (creating) a new link.

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