source: orange/Orange/OrangeCanvas/application/tutorials/__init__.py @ 11206:49f2a7576167

Revision 11206:49f2a7576167, 3.3 KB checked in by Ales Erjavec <ales.erjavec@…>, 17 months ago (diff)

Added Tutorial dialog.

Line 
1"""
2Orange Canvas Tutorial schemes
3
4"""
5import os
6import io
7import logging
8import types
9import collections
10from itertools import chain
11
12import pkg_resources
13
14log = logging.getLogger(__name__)
15
16
17def list_schemes(package):
18    """Return a list of scheme tutorials.
19    """
20    resources = pkg_resources.resource_listdir(__name__, ".")
21    resources = filter(is_ows, resources)
22    return sorted(resources)
23
24
25def is_ows(filename):
26    return filename.endswith(".ows")
27
28
29def default_entry_point():
30    dist = pkg_resources.get_distribution("Orange")
31    ep = pkg_resources.EntryPoint("Orange Canvas", __name__, dist=dist)
32    return ep
33
34
35def tutorial_entry_points():
36    """Return an iterator over all tutorials.
37    """
38    default = default_entry_point()
39    return chain([default],
40                 pkg_resources.iter_entry_points("orange.widgets.tutorials"))
41
42
43def tutorials():
44    """Return all known tutorials.
45    """
46    all_tutorials = []
47    for ep in tutorial_entry_points():
48        tutorials = None
49        try:
50            tutorials = ep.load()
51        except pkg_resources.DistributionNotFound as ex:
52            log.warning("Could not load tutorials from %r (%r)",
53                        ep.dist, ex)
54            continue
55        except ImportError:
56            log.error("Could not load tutorials from %r",
57                      ep.dist, exc_info=True)
58            continue
59        except Exception:
60            log.error("Could not load tutorials from %r",
61                      ep.dist, exc_info=True)
62            continue
63
64        if isinstance(tutorials, types.ModuleType):
65            package = tutorials
66            tutorials = list_schemes(tutorials)
67            tutorials = [Tutorial(t, package, ep.dist) for t in tutorials]
68        elif isinstance(tutorials, (types.FunctionType, types.MethodType)):
69            try:
70                tutorials = tutorials()
71            except Exception as ex:
72                log.error("A callable entry point (%r) raised an "
73                          "unexpected error.",
74                          ex, exc_info=True)
75                continue
76            tutorials = [Tutorial(t, package=None, distribution=ep.dist)]
77
78        all_tutorials.extend(tutorials)
79
80    return all_tutorials
81
82
83class Tutorial(object):
84    def __init__(self, resource, package=None, distribution=None):
85        self.resource = resource
86        self.package = package
87        self.distribution = distribution
88
89    def abspath(self):
90        """Return absolute filename for the scheme if possible else
91        raise an ValueError.
92
93        """
94        if self.package is not None:
95            return pkg_resources.resource_filename(self.package.__name__,
96                                                   self.resource)
97        elif isinstance(self.resource, basestring):
98            if os.path.isabs(self.resource):
99                return self.resource
100
101        raise ValueError("cannot resolve resource to an absolute name")
102
103    def stream(self):
104        """Return the tutorial file as an open stream.
105        """
106        if self.package is not None:
107            return pkg_resources.resource_stream(self.package.__name__,
108                                                 self.resource)
109        elif isinstance(self.resource, basestring):
110            if os.path.isabs(self.resource) and os.path.exists(self.resource):
111                return open(self.resource, "rb")
112
113        raise ValueError
Note: See TracBrowser for help on using the repository browser.