def set_current_univ(univ): global current global current_modname if univ == 'backends' or (not is_valid_modulename(univ)): raise ValueError('invalid module name') current = dynload('.'.join([__name__, univ, ])) # keep module name for possible future use current_modname = univ loginfo('Current university set to %s (%s)', univ, current.UNIVERSITY_NAME)
def init_threadproc(self): if not autostart: showfirst_lock.acquire() showfirst_lock.release() seq_lock.acquire() else: self.Hide() # moved from module level to this place to (significantly) # improve user experience # NOTE: univlib's init MUST come before all shrimp's init, # because some of the shrimp (like academic) depend on # univlib.current at module level, thus univlib must be available # when they're being loaded. if not autostart: wx.CallAfter(self.lblIndicator.SetLabel, _(u'加载大学支持库')) # init univlib ahead of time from gingerprawn.api import univlib # FIXED: not hardcoded anymore, can be changed via cmdline # default value moved there univlib.set_current_univ(_APP_OPTIONS.univ_sel) logdebug('Attempt to load all shrimp') # explicit discovery of shrimp, as close to the loading part as # possible cooker.do_discovery() if not autostart: cooker.load_shrimp(self.ProgressCallback) else: # auto-start, be silent cooker.load_shrimp(self.LightProgressCallback) if not autostart: wx.CallAfter(self.pbr.SetValue, 100) wx.CallAfter(self.lblIndicator.SetLabel, _(u'主界面初始化')) if autostart: # auto start alongside OS bootup, no main interface. # Instead ask all shrimp to display their startup interface # such as a reminder num_shrimp = len(cooker.SHRIMP_MODULES) queue = Queue.Queue(num_shrimp) # it's quite OK to directly execute this init code in # the same thread cooker.do_autostart(queue) # wait for all shrimp to finish, this is cooperative for i in range(num_shrimp): # simply get one signal indicating who has finished exit_shrimp = queue.get(block=True) logdebug('shrimp `%s\' announced exit', exit_shrimp) logdebug('all shrimp exited, bailing out') wx.CallAfter(self.Destroy) return else: # name of main module is now present at self.options.mainmod MAIN_MODULE = _APP_OPTIONS.mainmod logdebug('Initializing metashrimp %s', MAIN_MODULE) # this name needs checking for sake of security if not is_valid_modulename(MAIN_MODULE): logcritical('Metashrimp name invalid') wx.CallAfter(wx.MessageBox, 'Invalid module name!', 'gingerprawn', wx.ICON_ERROR) wx.CallAfter(self.Destroy) seq_lock.release() return main_stat = cooker.SHRIMP_LOADSTATUS.get(MAIN_MODULE, 'fail') if main_stat == 'fail': logcritical('Metashrimp loading failed') wx.CallAfter(wx.MessageBox, 'Main module load failed!', 'gingerprawn', wx.ICON_ERROR) wx.CallAfter(self.Destroy) seq_lock.release() return try: logdebug('Attempt to bring up metashrimp') cooker.bring_up_shrimp(MAIN_MODULE) # (self.app, )) except (ValueError, KeyError, ): logcritical('Metashrimp loading success but bringup failed') wx.CallAfter(wx.MessageBox, 'Failed to bring up main UI!', 'gingerprawn', wx.ICON_ERROR) wx.CallAfter(self.Destroy) finally: seq_lock.release() logdebug('initthread normal exit') return