1
# -*- coding: utf-8 -*-
7
from PyQt4 import QtCore, QtGui
8
from helioviewer.installer.installwizard import Ui_InstallWizard
9
from helioviewer.jp2 import *
10
from helioviewer.db import *
19
__STEP_FXN_THROTTLE__ = 50
22
# Main Application Window
24
# TODO (2009/08/21): Generate setup/Config.php and copy/move API files into proper location.
25
# TODO (2009/08/21): Add checks: 1. db exists, 2. no images found
27
class HelioviewerInstallWizard(QtGui.QWizard):
29
def __init__(self, parent=None):
30
QtGui.QWidget.__init__(self, parent)
31
self.ui = Ui_InstallWizard()
34
self.setPixmap(QtGui.QWizard.LogoPixmap, QtGui.QPixmap(":/Logos/color.png"))
36
self.logfile = open("failed.log", "a")
37
self.install_finished = False
39
self.setup_validators()
42
def setup_validators(self):
44
self.ui.dbAdminPage.registerField("dbAdminUserName*", self.ui.dbAdminUserName)
45
self.ui.dbAdminPage.registerField("dbAdminPassword*", self.ui.dbAdminPassword)
46
self.ui.hvDatabaseSetupPage.registerField("hvDatabaseName*", self.ui.hvDatabaseName)
47
self.ui.hvDatabaseSetupPage.registerField("hvUserName*", self.ui.hvUserName)
48
self.ui.hvDatabaseSetupPage.registerField("hvPassword*", self.ui.hvPassword)
50
alphanum = QtGui.QRegExpValidator(QtCore.QRegExp("[\w$]*"), self)
51
passwd = QtGui.QRegExpValidator(QtCore.QRegExp("[\w!@#\$%\^&\*\(\)_\+\.,\?'\"]*"), self)
54
self.ui.dbAdminUserName.setValidator(alphanum)
55
self.ui.dbAdminPassword.setValidator(passwd)
56
self.ui.hvDatabaseName.setValidator(alphanum)
57
self.ui.hvUserName.setValidator(alphanum)
58
self.ui.hvPassword.setValidator(passwd)
61
def initializePage(self, page):
62
if page is __INSTALL_PAGE__:
63
jp2dir = str(self.ui.jp2RootDirInput.text())
65
self.ui.statusMsg.setText("Searching for JPEG 2000 Images...")
67
# Locate jp2 images in specified filepath
68
self.filepaths = find_images(jp2dir)
70
n = len(self.filepaths)
73
print("No JPEG 2000 images found. Exiting installation.")
76
self.ui.installProgress.setMaximum(n // __STEP_FXN_THROTTLE__)
77
self.ui.statusMsg.setText("""\
78
Found %d JPEG2000 images.
80
If this is correct, please press "Start" to begin processing.
82
#self.process_images()
84
def validateCurrentPage(self):
85
''' Validates information for a given page '''
86
page = self.currentId()
88
#print "Validating page %s" % str(page)
90
# Database type & administrator information
91
if page is __DBADMIN_PAGE__:
92
canConnect = check_db_info(str(self.ui.dbAdminUserName.text()), str(self.ui.dbAdminPassword.text()), self.ui.mysqlRadioBtn.isChecked())
94
self.ui.dbAdminStatus.setText("<span style='color: red;'>Unable to connect to the database. Please check your login information and try again.</span>")
96
self.ui.dbAdminStatus.clear()
99
# JP2 Archive location
100
elif page is __JP2DIR_PAGE__:
101
pathExists = os.path.isdir(self.ui.jp2RootDirInput.text())
103
self.ui.jp2ArchiveStatus.setText("<span style='color: red;'>Not a valid location. Please check the filepath and permissions and try again.</span>")
105
self.ui.jp2ArchiveStatus.clear()
109
elif page is __INSTALL_PAGE__:
110
return self.install_finished
112
# No validation required
116
def process_images(self):
117
''' Process JPEG 2000 archive and enter information into the database '''
118
admin, adminpass, hvdb, hvuser, hvpass, jp2dir, mysql = self.get_form_fields()
120
self.ui.startProcessingBtn.setEnabled(False)
122
self.ui.statusMsg.setText("Creating database schema")
124
cursor = setup_database_schema(admin, adminpass, hvdb, hvuser, hvpass, mysql)
126
# Extract image parameters, 10,000 at a time
127
while len(self.filepaths) > 0:
128
subset = self.filepaths[:10000]
129
self.filepaths = self.filepaths[10000:]
133
for filepath in subset:
135
image = sunpy.read_header(filepath)
136
image['filepath'] = filepath
139
#raise BadImage("HEADER")
140
print("Skipping corrupt image: %s" %
141
os.path.basename(filepath))
144
# Insert image information into database
146
process_jp2_images(images, jp2dir, cursor, mysql, self.update_progress)
148
# clean up afterwards
153
#self.ui.installProgress.setValue(len(images))
155
self.ui.statusMsg.setText("Finished!")
156
self.install_finished = True
158
def update_progress(self, img):
159
value = self.ui.installProgress.value() + 1
160
self.ui.installProgress.setValue(value)
161
self.ui.statusMsg.setText("Processing image:\n %s" % img)
163
def get_form_fields(self):
164
''' Grab form information '''
165
mysql = self.ui.mysqlRadioBtn.isChecked()
166
admin = str(self.ui.dbAdminUserName.text())
167
adminpass = str(self.ui.dbAdminPassword.text())
168
hvdb = str(self.ui.hvDatabaseName.text())
169
hvuser = str(self.ui.hvUserName.text())
170
hvpass = str(self.ui.hvPassword.text())
171
jp2dir = str(self.ui.jp2RootDirInput.text())
173
return admin, adminpass, hvdb, hvuser, hvpass, jp2dir, mysql
175
def init_events(self):
176
QtCore.QObject.connect(self.ui.jp2BrowseBtn, QtCore.SIGNAL("clicked()"), self.open_browse_dialog)
177
QtCore.QObject.connect(self.ui.startProcessingBtn, QtCore.SIGNAL("clicked()"), self.process_images)
179
def open_browse_dialog(self):
180
fd = QtGui.QFileDialog(self)
181
directory = fd.getExistingDirectory()
182
self.ui.jp2RootDirInput.setText(directory)
185
''' Load graphical installer '''
186
app = QtGui.QApplication(sys.argv)
187
win = HelioviewerInstallWizard()
189
sys.exit(app.exec_())