source: orange/Orange/OrangeCanvas/config.py @ 11285:c0fc66213bfd

Revision 11285:c0fc66213bfd, 6.2 KB checked in by Ales Erjavec <ales.erjavec@…>, 15 months ago (diff)

Changed discovery to run from arbitrary entry points iterator/group.

Line 
1"""
2Orange Canvas Configuration
3
4"""
5
6import os
7import logging
8import cPickle as pickle
9import itertools
10
11import pkg_resources
12
13from PyQt4.QtGui import QDesktopServices
14from PyQt4.QtCore import QCoreApplication, QSettings
15
16from .utils.settings import Settings, config_slot
17
18log = logging.getLogger(__name__)
19
20
21def init():
22    """Initialize the QCoreApplication.organizationDomain, applicationName,
23    applicationVersion and the default settings format. Will only run once.
24
25    .. note:: This should not be run before QApplication has been initialized.
26              Otherwise it can break Qt's plugin search paths.
27
28    """
29    # Set application name, version and org. domain
30    QCoreApplication.setOrganizationDomain("biolab.si")
31    QCoreApplication.setApplicationName("Orange Canvas")
32    QCoreApplication.setApplicationVersion("2.6")
33    QSettings.setDefaultFormat(QSettings.IniFormat)
34
35    # Make it a null op.
36    global init
37    init = lambda: None
38
39rc = {}
40
41
42spec = \
43    [("startup/show-splash-screen", bool, True,
44      "Show splash screen at startup"),
45
46     ("startup/show-welcome-screen", bool, True,
47      "Show Welcome screen at startup"),
48
49     ("stylesheet", unicode, "orange",
50      "QSS stylesheet to use"),
51
52     ("schemeinfo/show-at-new-scheme", bool, True,
53      "Show Scheme Properties when creating a new Scheme"),
54
55     ("mainwindow/scheme-margins-enabled", bool, True,
56      "Show margins around the scheme view"),
57
58     ("mainwindow/show-scheme-shadow", bool, True,
59      "Show shadow around the scheme view"),
60
61     ("mainwindow/toolbox-dock-exclusive", bool, False,
62      "Should the toolbox show only one expanded category at the time"),
63
64     ("mainwindow/toolbox-dock-floatable", bool, False,
65      "Is the canvas toolbox floatable (detachable from the main window)"),
66
67     ("mainwindow/toolbox-dock-movable", bool, True,
68      "Is the canvas toolbox movable (between left and right edge)"),
69
70     ("mainwindow/number-of-recent-schemes", int, 7,
71      "Number of recent schemes to keep in history"),
72
73     ("schemeedit/show-channel-names", bool, True,
74      "Show channel names"),
75
76     ("schemeedit/show-link-state", bool, True,
77      "Show link state hints."),
78
79     ("schemeedit/freeze-on-load", bool, False,
80      "Freeze signal propagation when loading a scheme."),
81
82     ("quickmenu/trigger-on-double-click", bool, True,
83      "Show quick menu on double click."),
84
85     ("quickmenu/trigger-on-left-click", bool, False,
86      "Show quick menu on left click."),
87
88     ("quickmenu/trigger-on-space-key", bool, True,
89      "Show quick menu on space key press."),
90
91     ("quickmenu/trigger-on-any-key", bool, False,
92      "Show quick menu on double click."),
93
94     ("logging/level", int, 1, "Logging level"),
95
96     ("logging/show-on-error", bool, True, "Show log window on error"),
97
98     ("logging/dockable", bool, True, "Allow log window to be docked"),
99
100     ("output/redirect-stderr", bool, True,
101      "Redirect and display standard error output"),
102
103     ("output/redirect-stdout", bool, True,
104      "Redirect and display standard output"),
105
106     ("output/stay-on-top", bool, True, ""),
107
108     ("output/show-on-error", bool, True, "Show output window on error"),
109
110     ("output/dockable", bool, True, "Allow output window to be docked"),
111
112     ("help/stay-on-top", bool, True, ""),
113
114     ("help/dockable", bool, True, "Allow help window to be docked"),
115
116     ("help/open-in-external-browser", bool, False,
117      "Open help in an external browser")
118     ]
119
120spec = [config_slot(*t) for t in spec]
121
122
123def settings():
124    init()
125    store = QSettings()
126    settings = Settings(defaults=spec, store=store)
127    return settings
128
129
130def data_dir():
131    """Return the application data directory. If the directory path
132    does not yet exists then create it.
133
134    """
135    init()
136    datadir = QDesktopServices.storageLocation(QDesktopServices.DataLocation)
137    datadir = unicode(datadir)
138    if not os.path.exists(datadir):
139        os.makedirs(datadir)
140    return datadir
141
142
143def cache_dir():
144    """Return the application cache directory. If the directory path
145    does not yet exists then create it.
146
147    """
148    init()
149    datadir = QDesktopServices.storageLocation(QDesktopServices.DataLocation)
150    datadir = unicode(datadir)
151    if not os.path.exists(datadir):
152        os.makedirs(datadir)
153    return datadir
154
155
156def open_config():
157    global rc
158    app_dir = data_dir()
159    filename = os.path.join(app_dir, "canvas-rc.pck")
160    if os.path.exists(filename):
161        with open(os.path.join(app_dir, "canvas-rc.pck"), "rb") as f:
162            rc.update(pickle.load(f))
163
164
165def save_config():
166    app_dir = data_dir()
167    with open(os.path.join(app_dir, "canvas-rc.pck"), "wb") as f:
168        pickle.dump(rc, f)
169
170
171def recent_schemes():
172    """Return a list of recently accessed schemes.
173    """
174    app_dir = data_dir()
175    recent_filename = os.path.join(app_dir, "recent.pck")
176    recent = []
177    if os.path.isdir(app_dir) and os.path.isfile(recent_filename):
178        with open(recent_filename, "rb") as f:
179            recent = pickle.load(f)
180
181    # Filter out files not found on the file system
182    recent = [(title, path) for title, path in recent \
183              if os.path.exists(path)]
184    return recent
185
186
187def save_recent_scheme_list(scheme_list):
188    """Save the list of recently accessed schemes
189    """
190    app_dir = data_dir()
191    recent_filename = os.path.join(app_dir, "recent.pck")
192
193    if os.path.isdir(app_dir):
194        with open(recent_filename, "wb") as f:
195            pickle.dump(scheme_list, f)
196
197
198WIDGETS_ENTRY = "orange.widgets"
199
200
201# This could also be achieved by declaring the entry point in
202# Orange's setup.py, but that would not guaranty this entry point
203# is the first in a list.
204
205def default_entry_point():
206    """
207    Return a default orange.widgets entry point for loading
208    default Orange Widgets.
209
210    """
211    dist = pkg_resources.get_distribution("Orange")
212    ep = pkg_resources.EntryPoint("Orange Widgets", "Orange.OrangeWidgets",
213                                  dist=dist)
214    return ep
215
216
217def widgets_entry_points():
218    """
219    Return an `EntryPoint` iterator for all 'orange.widget' entry
220    points plus the default Orange Widgets.
221
222    """
223    ep_iter = pkg_resources.iter_entry_points(WIDGETS_ENTRY)
224    chain = [[default_entry_point()],
225             ep_iter
226             ]
227    return itertools.chain(*chain)
Note: See TracBrowser for help on using the repository browser.