nod3scripting.py
3.11 KB
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
#!/usr/bin/env python
import pyfits
import time, os
from numpy import array, float32
import re, sys, os.path as osp
from guidata.qt.compat import getsavefilename, getopenfilename, getopenfilenames
from guidata.qt.QtGui import QMessageBox
from guiqwt.config import _
#from guiqwt.io import (IMAGE_SAVE_FILTERS, IMAGE_LOAD_FILTERS)
def _add_all_supported_files(filters):
extlist = re.findall(r'\*.[a-zA-Z0-9]*', filters)
allfiles = '%s (%s)\n' % (_("All supported files"), ' '.join(extlist))
return allfiles+filters
def exec_script_open_dialog(parent, basedir='', app_name=None):
"""
Executes an image*s* open dialog box (QFileDialog.getOpenFileNames)
* parent: parent widget (None means no parent)
* basedir: base directory ('' means current directory)
* app_name (opt.): application name (used as a title for an eventual
error message box in case something goes wrong when saving image)
* mapnum (opt): chooes the map number in the fitsfile
Yields (filename, data) tuples if dialog is accepted, None otherwise
"""
IMAGE_LOAD_FILTERS = '%s (*.nod *.nod3)' % (_(u"NOD3Scripts"))
SCRIPT_LOAD_FILTERS = _add_all_supported_files(IMAGE_LOAD_FILTERS)
saved_in, saved_out, saved_err = sys.stdin, sys.stdout, sys.stderr
sys.stderr = None
sys.stdout = None
filenames, _filter = getopenfilenames(parent, _("Open"), basedir,
SCRIPT_LOAD_FILTERS)
sys.stdin, sys.stdout, sys.stderr = saved_in, saved_out, saved_err
filename = [str(fname) for fname in list(filenames)]
return filename
def exec_script_save_dialog(parent, hlines, outdir='', app_name=None):
"""
Executes an image*s* open dialog box (QFileDialog.getOpenFileNames)
* parent: parent widget (None means no parent)
* outdir: base directory ('' means current directory)
* app_name (opt.): application name (used as a title for an eventual
error message box in case something goes wrong when saving image)
* mapnum (opt): chooes the map number in the fitsfile
Yields (filename, data) tuples if dialog is accepted, None otherwise
"""
IMAGE_SAVE_FILTERS = '%s (*.nod *.nod3)' % (_(u"NOD3Scripts"))
SCRIPT_SAVE_FILTERS = _add_all_supported_files(IMAGE_SAVE_FILTERS)
saved_in, saved_out, saved_err = sys.stdin, sys.stdout, sys.stderr
sys.stdout = None
filename, _filter = getsavefilename(parent, _("Save as"), outdir,
SCRIPT_SAVE_FILTERS)
sys.stdin, sys.stdout, sys.stderr = saved_in, saved_out, saved_err
if filename:
filename = str(filename)
try:
fout = open(filename, "w")
for h in hlines:
fout.write(h+"\n")
fout.close()
return filename
except Exception, msg:
import traceback
traceback.print_exc()
QMessageBox.critical(parent,
_('Error') if app_name is None else app_name,
(_(u"%s could not be written:") % osp.basename(filename))+\
"\n"+str(msg))
return