move more startup routines after lang initialization

This commit is contained in:
Damien Elmes 2012-04-23 01:55:47 +09:00
parent bf13b5d619
commit 74a092d9b1
2 changed files with 34 additions and 26 deletions

View file

@ -130,6 +130,11 @@ def run():
# i18n # i18n
setupLang(pm, app, opts.lang) setupLang(pm, app, opts.lang)
# remaining pm init
pm.checkPid()
pm.ensureProfile()
# load the main window
import aqt.main import aqt.main
mw = aqt.main.AnkiQt(app, pm) mw = aqt.main.AnkiQt(app, pm)
app.exec_() app.exec_()

View file

@ -62,37 +62,36 @@ class ProfileManager(object):
def __init__(self, base=None, profile=None): def __init__(self, base=None, profile=None):
self.name = None self.name = None
# instantiate base folder # instantiate base folder
if not base: self.base = base or self._defaultBase()
base = self._defaultBase() self.ensureBaseExists()
self.ensureBaseExists(base) # load metadata
self.checkPid(base) self.firstRun = self._loadMeta()
self.base = base # did the user request a profile to start up with?
# load database and cmdline-provided profile
self._load()
if profile: if profile:
try: try:
self.load(profile) self.load(profile)
except TypeError: except TypeError:
raise Exception("Provided profile does not exist.") raise Exception("Provided profile does not exist.")
# Startup checks # Base creation
###################################################################### ######################################################################
# These routines run before the language code is initialized, so they
# can't be translated
def ensureBaseExists(self, base): def ensureBaseExists(self):
if not os.path.exists(base): try:
try: self._ensureExists(self.base)
os.makedirs(base) except:
except: # can't translate, as lang not initialized
QMessageBox.critical( QMessageBox.critical(
None, "Error", """\ None, "Error", """\
Anki can't write to the harddisk. Please see the \ Anki can't write to the harddisk. Please see the \
documentation for information on using a flash drive.""") documentation for information on using a flash drive.""")
raise raise
def checkPid(self, base): # Pid checking
p = os.path.join(base, "pid") ######################################################################
def checkPid(self):
p = os.path.join(self.base, "pid")
# check if an existing instance is running # check if an existing instance is running
if os.path.exists(p): if os.path.exists(p):
pid = int(open(p).read()) pid = int(open(p).read())
@ -104,9 +103,9 @@ documentation for information on using a flash drive.""")
pass pass
if exists: if exists:
QMessageBox.warning( QMessageBox.warning(
None, "Error", """\ None, "Error", _("""\
Anki is already running. Please close the existing copy or restart your \ Anki is already running. Please close the existing copy or restart your \
computer.""") computer."""))
raise Exception("Already running") raise Exception("Already running")
# write out pid to the file # write out pid to the file
open(p, "w").write(str(os.getpid())) open(p, "w").write(str(os.getpid()))
@ -198,7 +197,7 @@ computer.""")
else: else:
return os.path.expanduser("~/Anki") return os.path.expanduser("~/Anki")
def _load(self): def _loadMeta(self):
path = os.path.join(self.base, "prefs.db") path = os.path.join(self.base, "prefs.db")
new = not os.path.exists(path) new = not os.path.exists(path)
self.db = DB(path, text=str) self.db = DB(path, text=str)
@ -211,14 +210,18 @@ create table if not exists profiles
self.db.execute("insert into profiles values ('_global', ?)", self.db.execute("insert into profiles values ('_global', ?)",
cPickle.dumps(metaConf)) cPickle.dumps(metaConf))
self._setDefaultLang() self._setDefaultLang()
# and save a default user profile for later (commits) return True
self.create("User 1")
else: else:
# load previously created # load previously created
self.meta = cPickle.loads( self.meta = cPickle.loads(
self.db.scalar( self.db.scalar(
"select data from profiles where name = '_global'")) "select data from profiles where name = '_global'"))
def ensureProfile(self):
"Create a new profile if none exists."
if self.firstRun:
self.create(_("User 1"))
def _pwhash(self, passwd): def _pwhash(self, passwd):
return checksum(unicode(self.meta['id'])+unicode(passwd)) return checksum(unicode(self.meta['id'])+unicode(passwd))
@ -228,7 +231,7 @@ create table if not exists profiles
def _setDefaultLang(self): def _setDefaultLang(self):
# the dialog expects _ to be defined, but we're running before # the dialog expects _ to be defined, but we're running before
# setLang() has been called. so we create a dummy op for now # setupLang() has been called. so we create a dummy op for now
import __builtin__ import __builtin__
__builtin__.__dict__['_'] = lambda x: x __builtin__.__dict__['_'] = lambda x: x
# create dialog # create dialog