~jstys-z/helioviewer.org/client5

« back to all changes in this revision

Viewing changes to install/helioviewer/installer/gui.py

  • Committer: Keith Hughitt
  • Date: 2011-05-26 19:27:36 UTC
  • mto: This revision was merged to the branch mainline in revision 567.
  • Revision ID: keith.hughitt@nasa.gov-20110526192736-qyr0ltaiak33loik
Updated release notes and sample configuration file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
import sys
3
 
import gc
4
 
import math
5
 
import getpass
6
 
import sunpy
7
 
from PyQt4 import QtCore, QtGui
8
 
from helioviewer.installer.installwizard import Ui_InstallWizard
9
 
from helioviewer.jp2 import *
10
 
from helioviewer.db  import *
11
 
 
12
 
__INTRO_PAGE__ = 0
13
 
__DBADMIN_PAGE__ = 1
14
 
__HVDB_PAGE__ = 2
15
 
__JP2DIR_PAGE__ = 3
16
 
__INSTALL_PAGE__ = 4
17
 
__FINISH_PAGE__ = 5
18
 
 
19
 
__STEP_FXN_THROTTLE__ = 50
20
 
 
21
 
#
22
 
# Main Application Window
23
 
#
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
26
 
#
27
 
class HelioviewerInstallWizard(QtGui.QWizard):
28
 
 
29
 
    def __init__(self, parent=None):
30
 
        QtGui.QWidget.__init__(self, parent)
31
 
        self.ui = Ui_InstallWizard()
32
 
        self.ui.setupUi(self)        
33
 
        
34
 
        self.setPixmap(QtGui.QWizard.LogoPixmap, QtGui.QPixmap(":/Logos/color.png"))
35
 
        
36
 
        self.logfile = open("failed.log", "a")
37
 
        self.install_finished = False
38
 
        
39
 
        self.setup_validators()
40
 
        self.init_events()
41
 
 
42
 
    def setup_validators(self):
43
 
        # Mandatory fields
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)
49
 
 
50
 
        alphanum = QtGui.QRegExpValidator(QtCore.QRegExp("[\w$]*"), self)
51
 
        passwd = QtGui.QRegExpValidator(QtCore.QRegExp("[\w!@#\$%\^&\*\(\)_\+\.,\?'\"]*"), self)
52
 
 
53
 
        # DB Admin Info
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)
59
 
 
60
 
 
61
 
    def initializePage(self, page):
62
 
        if page is __INSTALL_PAGE__:
63
 
            jp2dir = str(self.ui.jp2RootDirInput.text())
64
 
            
65
 
            self.ui.statusMsg.setText("Searching for JPEG 2000 Images...")
66
 
            
67
 
            # Locate jp2 images in specified filepath
68
 
            self.filepaths = find_images(jp2dir)
69
 
 
70
 
            n = len(self.filepaths)
71
 
 
72
 
            if n == 0:
73
 
                print("No JPEG 2000 images found. Exiting installation.")
74
 
                sys.exit(2)
75
 
            else:
76
 
                self.ui.installProgress.setMaximum(n // __STEP_FXN_THROTTLE__)
77
 
                self.ui.statusMsg.setText("""\
78
 
Found %d JPEG2000 images.
79
 
 
80
 
If this is correct, please press "Start" to begin processing.
81
 
                """ % n)
82
 
            #self.process_images()
83
 
 
84
 
    def validateCurrentPage(self):
85
 
        ''' Validates information for a given page '''
86
 
        page = self.currentId()
87
 
 
88
 
        #print "Validating page %s" % str(page)
89
 
 
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())
93
 
            if not canConnect:
94
 
                self.ui.dbAdminStatus.setText("<span style='color: red;'>Unable to connect to the database. Please check your login information and try again.</span>")
95
 
            else:
96
 
                self.ui.dbAdminStatus.clear()
97
 
            return canConnect
98
 
 
99
 
        # JP2 Archive location
100
 
        elif page is __JP2DIR_PAGE__:
101
 
            pathExists = os.path.isdir(self.ui.jp2RootDirInput.text())
102
 
            if not pathExists:
103
 
                self.ui.jp2ArchiveStatus.setText("<span style='color: red;'>Not a valid location. Please check the filepath and permissions and try again.</span>")
104
 
            else:
105
 
                self.ui.jp2ArchiveStatus.clear()
106
 
            return pathExists
107
 
        
108
 
        # Install page
109
 
        elif page is __INSTALL_PAGE__:
110
 
            return self.install_finished
111
 
 
112
 
        # No validation required
113
 
        else:
114
 
            return True
115
 
 
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()
119
 
 
120
 
        self.ui.startProcessingBtn.setEnabled(False)
121
 
 
122
 
        self.ui.statusMsg.setText("Creating database schema")
123
 
 
124
 
        cursor = setup_database_schema(admin, adminpass, hvdb, hvuser, hvpass, mysql)
125
 
        
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:]
130
 
 
131
 
            images = []
132
 
            
133
 
            for filepath in subset:
134
 
                try:
135
 
                    image = sunpy.read_header(filepath)
136
 
                    image['filepath'] = filepath
137
 
                    images.append(image)
138
 
                except:
139
 
                    #raise BadImage("HEADER")
140
 
                    print("Skipping corrupt image: %s" %
141
 
                          os.path.basename(filepath))
142
 
                    continue
143
 
            
144
 
            # Insert image information into database
145
 
            if len(images) > 0:
146
 
                process_jp2_images(images, jp2dir, cursor, mysql, self.update_progress)
147
 
                
148
 
            # clean up afterwards
149
 
            images = []
150
 
            gc.collect()
151
 
    
152
 
        cursor.close()
153
 
        #self.ui.installProgress.setValue(len(images))
154
 
    
155
 
        self.ui.statusMsg.setText("Finished!")
156
 
        self.install_finished = True
157
 
    
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)
162
 
 
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())
172
 
 
173
 
        return admin, adminpass, hvdb, hvuser, hvpass, jp2dir, mysql
174
 
 
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)
178
 
 
179
 
    def open_browse_dialog(self):
180
 
        fd = QtGui.QFileDialog(self)
181
 
        directory = fd.getExistingDirectory()
182
 
        self.ui.jp2RootDirInput.setText(directory)
183
 
 
184
 
def install():
185
 
    ''' Load graphical installer '''
186
 
    app = QtGui.QApplication(sys.argv)
187
 
    win = HelioviewerInstallWizard()
188
 
    win.show()
189
 
    sys.exit(app.exec_())