1 """\
2 Visualization manager for FuzzPy. This submodule allows the dispatch of any
3 supported fuzzpy datatypes to an arbitrary visualization plugin. All available
4 visualization plugins are located in the L{visplugins} submodule.
5
6 The manager provides a helper function to discover installed plugins as well
7 as a visualization backend factory.
8
9 @author: Xavier Spriet
10 @contact: linkadmin@gmail.com
11 @license: LGPL-3
12 """
13
14 import warnings
15
16 from . import visplugins
19 """\
20 Visualization plugin factory class.
21
22 Provides plugin management methods and a helper method to dispatch
23 a FuzzPy object to a plugin.
24 """
25
26 @staticmethod
28 """\
29 Returns a list of plugins supported by the current system.
30
31 If L{datatype} is specified, try to find supported plugins that can
32 be used to represent the specified datatype. Otherwise, return a list
33 of all plugins that can run on that system.
34
35 If any type of exception is raised during the C{is_supported()} call,
36 the plugin will B{not} be included in the resulting list.
37
38 @param datatype: fuzzpy datatype to look for supported plugins.
39 @type datatype: C{str}
40 @rtype: C{list}
41 @return: list of plugins that can run in the current environment
42 """
43 supported = []
44
45 for plugin in visplugins.__all__:
46
47 try:
48 plugin_mod = __import__("visplugins.%s" % plugin,
49 globals(), locals(), fromlist=[plugin])
50 except ImportError as ex:
51 warnings.warn(ex)
52 continue
53
54
55 if not getattr(plugin_mod, 'VIS_PLUGIN'):
56 raise AttributeError(("Plugin %s is missing VIS_PLUGIN "
57 "property") % plugin)
58 plugin_class = getattr(plugin_mod, plugin_mod.VIS_PLUGIN)
59
60 if (getattr(plugin_class, 'is_supported')() == True) and \
61 (datatype in [None] + getattr(plugin_mod, 'VIS_TYPES')):
62 supported.append(plugin)
63
64 return supported
65
66 @staticmethod
68 """\
69 Visualization plugin factory method.
70
71 Returns a new instance of the appropriate visualization plugin.
72 If no 'plugin' argument is specified as the preferred visualization
73 backend, the first plugin that supports visualization for 'obj's
74 class name will be used as the backend.
75
76 @param obj: Object to draw
77 @type obj: Object
78 @param plugin: Name of the plugin to use
79 @type plugin: C{str}
80 @returns: The return value of the plugin's visualize() method
81 @rtype: C{tuple} (format, payload)
82 """
83
84
85 if None == plugin:
86 try:
87 plugin = VisManager.get_supported_plugins(obj.__class__)[0]
88 except IndexError:
89 raise ImportError(("Unable to load any plugin to handle the "
90 "specified object type"))
91
92 plugin_mod = __import__("visplugins.%s" % plugin, globals(), locals(),
93 fromlist=[plugin])
94
95
96 plugin_name = getattr(plugin_mod, 'VIS_PLUGIN')
97
98 return getattr(plugin_mod, plugin_name)\
99 (obj=obj, args=args, kwargs=kwargs)
100