-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripting.py
353 lines (317 loc) · 9.58 KB
/
scripting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# -*- coding: iso-8859-1 -*-
"""
Unit: scripting
Project: BioImageXD
Description:
A module containing various shortcuts for ease of scripting the app
Copyright (C) 2006 BioImageXD Project
See CREDITS.txt for details
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
__author__ = "BioImageXD Project <http://www.bioimagexd.org/>"
__version__ = "$Revision: 1.21 $"
__date__ = "$Date: 2005/01/13 13:42:03 $"
from lib.DataUnit.DataUnitSetting import DataUnitSettings
from Modules.Task.Merging.MergingSettings import MergingSettings
import sys
import os
import os.path
import imp
import platform
import getpass
import Logging
import ConfigParser
import codecs
class MyConfigParser(ConfigParser.RawConfigParser):
"""
a config parser that doesn't ignore the case of the options and sections
"""
def optionxform(self, optionstr):
"""
A method used to transform the option names. Does not transform the names in any way
"""
return optionstr
settingsCache = {}
record = 0
conf = None
renderingEnabled = True
TFLag = 1
combinedDataUnit = None
unhandledException=0
app = None
mainWindow = None
animator=None
currentVisualizationMode=""
visualizer = None
processingManager = None
resamplingDisabled = 0
resampleToFit = 0
processingTimepoint = -1
wantAlphaChannel = 1
preferRGB = 1
wantWholeDataset = 0
inIO = 0
uncleanLog = None
logFile = None
recorder = None
WHOLE_DATASET = -1
WHOLE_DATASET_NO_ALPHA = -2
#COLOR_BEGINNER = (200, 200, 200)
#COLOR_INTERMEDIATE = (202, 202, 226)
#COLOR_EXPERIENCED = (224, 188, 232)
COLOR_EXPERIENCED = (193, 0, 0)
COLOR_INTERMEDIATE = None
COLOR_BEGINNER = None
dialogs = {}
def getCacheKey(paths, names, taskname):
"""
Return a key for caching of settings data based on the task name and the filepaths
"""
keyList = paths[:]
keyList.append(taskname)
keyList.extend(names)
return tuple(keyList)
def getSettingsFromCache(key):
"""
Return the settings stored under a given key in the cache
"""
global settingsCache
data = settingsCache.get(tuple(key), None)
value = None
parser = None
if data:
value = []
for (n, configParser) in data:
if not configParser.sections():
continue
print "n=",n,"configParser=",configParser
print configParser.sections()
settings = DataUnitSettings(n)
settings.set("Type", None)
settings = settings.readFrom(configParser)
value.append(settings)
parser = configParser
return value, parser
def storeSettingsToCache(key, settingsList):
"""
Store the given settings to cache
"""
global settingsCache
key = tuple(key)
if key in settingsCache:
del settingsCache[key]
value = []
for setting in settingsList:
configParser = MyConfigParser()
setting.writeTo(configParser)
value.append((setting.getDatasetNumber(), configParser))
settingsCache[key] = value
def removeSettingsFromCache(key, settingsList):
"""
"""
global settingsCache
key = tuple(key)
if key in settingsCache:
del settingsCache[key]
def registerDialog(dialogName, dialog):
"""
Register a dialog object with a given name. This can be used when creating
a script requires that a dialog be accesible from various different parts
of the software.
"""
global dialogs
dialogs[dialogName] = dialog
def unregisterDialog(dialogName):
"""
unregister a dialog with a given name. this just removes the reference
"""
del dialogs[dialogName]
def main_is_frozen():
"""
Checks if the application is "frozen", ie. packaged with py2exe for windows, py2app for mac or freeze for linux.
"""
return (hasattr(sys, "frozen") or # new py2exe
hasattr(sys, "importers") # old py2exe
or imp.is_frozen("__main__")) # tools/freeze
def get_main_dir():
"""
Gets the main directory. Varies depending on if the app is packaged or run from source
"""
if "checker.py" in sys.argv[0]:
return "."
if main_is_frozen():
return os.path.dirname(sys.executable)
return os.path.dirname(sys.argv[0])
def get_windows_appdir():
"""
return the base directory where application settings, logs etc. should be stored on windows
"""
if "AppData" in os.environ:
appbase = os.environ["AppData"].decode(sys.getfilesystemencoding())
if not os.path.exists(appbase) and "UserProfile" in os.environ:
appbase = os.environ["UserProfile"].decode(sys.getfilesystemencoding())
return appbase
return os.path.join("C:\\", "Documents and Settings", getpass.getuser(), "Application Data")
def get_log_dir():
"""
Tries to create and return a path to a directory for logging
"""
parser = ConfigParser.RawConfigParser()
configFile = os.path.join(get_config_dir(), u"BioImageXD.ini")
fp = codecs.open(configFile,"r","utf-8")
parser.readfp(fp)
if parser.has_section("Paths"):
try:
value = unicode(parser.get("Paths","LogPath"))
except:
value = None
if value:
print u"Log directory is ",type(value),value
return value
if platform.system()=="Darwin":
return os.path.expanduser("~/Library/Logs/BioImageXD")
elif platform.system() == "Windows":
appbase = get_windows_appdir()
appdir = os.path.join(appbase, "BioImageXD")
if not os.access(appdir, os.F_OK):
try:
os.mkdir(appdir)
# Probably want to except an OSError when making a directory
except OSError:
#except:
pass
if not os.access(appdir, os.F_OK):
Logging.info("Cannot write to log application data, using current directory", kw = "io")
appdir = "."
if not os.path.exists(appdir):
os.mkdir(appdir)
return os.path.join(appdir, "Logs")
else:
appdir = os.path.expanduser("~/.BioImageXD")
if not os.path.exists(appdir):
os.mkdir(appdir)
appdir = os.path.join(appdir, "Logs")
if not os.path.exists(appdir):
os.mkdir(appdir)
return appdir
def get_preview_dir():
"""
Return a directory where preview images can be stored
"""
parser = ConfigParser.RawConfigParser()
fp = codecs.open(os.path.join(get_config_dir(), "BioImageXD.ini"),"r","utf-8")
parser.readfp(fp)
if parser.has_section("Paths"):
try:
value = unicode(parser.get("Paths","PreviewPath"))
except:
value = None
if value:
print "Preview directory is ",value
return value
if platform.system() == "Darwin":
dirpath = os.path.expanduser("~/Library/Caches/BioImageXD")
if not os.path.exists(dirpath):
os.mkdir(dirpath)
return dirpath
elif platform.system() == "Windows":
appbase = get_windows_appdir()
appdir = os.path.join(appbase, "BioImageXD")
if not os.access(appdir, os.F_OK):
try:
os.mkdir(appdir)
except OSError:
#except:
pass
if not os.access(appdir, os.F_OK):
Logging.info("Cannot write preview to application data, using current directory", kw = "io")
appdir = "."
if not os.path.exists(appdir):
os.mkdir(appdir)
appdir = os.path.join(appdir,"Previews")
if not os.path.exists(appdir):
os.mkdir(appdir)
return appdir
else:
appdir = os.path.expanduser("~/.BioImageXD")
if not os.path.exists(appdir):
os.mkdir(appdir)
appdir = os.path.join(appdir,"Previews")
if not os.path.exists(appdir):
os.mkdir(appdir)
return appdir
def get_config_dir():
"""
Returns the path to the config file
"""
if platform.system() == "Darwin":
return os.path.expanduser("~/Library/Preferences")
elif platform.system() == "Windows":
appbase = get_windows_appdir()
appdir = os.path.join(appbase, "BioImageXD")
# Check if appdir exists
if not os.access(appdir, os.F_OK):
try:
# Else try to create it
os.mkdir(appdir)
except OSError:
# except:
# Probably except an OSError if we can't make the directory
pass
if not os.access(appdir, os.F_OK):
appdir = "."
if not os.path.exists(appdir):
os.mkdir(appdir)
return appdir
else:
confdir = os.path.expanduser("~/.BioImageXD")
if not os.path.exists(confdir):
os.mkdir(confdir)
return confdir
def get_help_dir():
"""
Returns the path where help files are located
"""
if platform.system() == "Darwin" and main_is_frozen():
resourcePath = os.environ['RESOURCEPATH']
helpPath = os.path.join(resourcePath, "Help")
return helpPath
else:
return os.path.join(".", "Help")
def get_icon_dir():
"""
Returns the path where icons are located
"""
if platform.system() == "Darwin" and main_is_frozen():
resourcePath = os.environ['RESOURCEPATH']
iconPath = os.path.join(resourcePath, "Icons")
return iconPath
else:
return os.path.join(".", "Icons")
def get_module_dir():
"""
Returns the path where modules are located
"""
if platform.system() == "Darwin" and main_is_frozen():
resourcePath = os.environ['RESOURCEPATH']
modulePath = os.path.join(resourcePath, "Modules")
return modulePath
else:
return "Modules"
def get_preset_dir():
"""
Returns the path where presets are located
"""
configDir = get_config_dir()
presetDir = os.path.join(configDir,"Presets")
return presetDir