source: orange/Orange/OrangeCanvas/config.py @ 11388:aa3090917151

Revision 11388:aa3090917151, 6.3 KB checked in by Ales Erjavec <ales.erjavec@…>, 14 months ago (diff)

Fixed 'config.cache_dir' location.

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