Refactor app.py

This commit is contained in:
Florian Bruhin 2014-01-19 18:20:57 +01:00
parent 0dd204b182
commit 9f9f5c41f7
2 changed files with 58 additions and 53 deletions

View File

@ -1,4 +1,6 @@
from qutebrowser import app from qutebrowser.app import QuteBrowser
import sys
if __name__ == '__main__': if __name__ == '__main__':
app.main() app = QuteBrowser()
sys.exit(app.exec_())

View File

@ -6,60 +6,63 @@ from qutebrowser.widgets.mainwindow import MainWindow
from qutebrowser.commands.keys import KeyParser from qutebrowser.commands.keys import KeyParser
import qutebrowser.commands.utils as cmdutils import qutebrowser.commands.utils as cmdutils
def parseopts(): class QuteBrowser(QApplication):
parser = argparse.ArgumentParser("usage: %(prog)s [options]") def __init__(self):
parser.add_argument('-l', '--log', dest='loglevel', help='Set loglevel', super().__init__(sys.argv)
default=0) args = self.parseopts()
args = parser.parse_args() self.initlog()
return args
def initlog(args): self.mainwindow = MainWindow()
""" Initialisation of the log """ self.commandparser = cmdutils.CommandParser()
if (args.loglevel): self.keyparser = KeyParser(self.mainwindow)
loglevel = args.loglevel
else:
loglevel = 'info'
numeric_level = getattr(logging, loglevel.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError('Invalid log level: %s' % loglevel)
logging.basicConfig(
level=numeric_level,
format='%(asctime)s [%(levelname)s] [%(module)s:%(funcName)s:%(lineno)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
def main(): self.keyparser.set_cmd_text.connect(self.mainwindow.status.cmd.set_cmd)
args = parseopts() self.mainwindow.status.cmd.got_cmd.connect(self.commandparser.parse)
initlog(args) self.mainwindow.status.cmd.got_cmd.connect(self.mainwindow.setFocus)
self.commandparser.error.connect(self.mainwindow.status.disp_error)
app = QApplication(sys.argv) self.init_cmds()
self.mainwindow.show()
mw = MainWindow() def parseopts(self):
cp = cmdutils.CommandParser() parser = argparse.ArgumentParser("usage: %(prog)s [options]")
kp = KeyParser(mw) parser.add_argument('-l', '--log', dest='loglevel',
kp.set_cmd_text.connect(mw.status.cmd.set_cmd) help='Set loglevel', default=0)
mw.status.cmd.got_cmd.connect(cp.parse) self.args = parser.parse_args()
mw.status.cmd.got_cmd.connect(mw.setFocus)
cp.error.connect(mw.status.disp_error)
cmdutils.register_all() def initlog(self):
cmds = cmdutils.cmd_dict """ Initialisation of the log """
cmds['open'].signal.connect(mw.tabs.openurl) if self.args.loglevel:
cmds['tabopen'].signal.connect(mw.tabs.tabopen) loglevel = self.args.loglevel
cmds['quit'].signal.connect(QApplication.closeAllWindows) # FIXME else:
cmds['tabclose'].signal.connect(mw.tabs.close_act) loglevel = 'info'
cmds['tabprev'].signal.connect(mw.tabs.switch_prev) numeric_level = getattr(logging, loglevel.upper(), None)
cmds['tabnext'].signal.connect(mw.tabs.switch_next) if not isinstance(numeric_level, int):
cmds['reload'].signal.connect(mw.tabs.reload_act) raise ValueError('Invalid log level: %s' % loglevel)
cmds['stop'].signal.connect(mw.tabs.stop_act) logging.basicConfig(
cmds['back'].signal.connect(mw.tabs.back_act) level=numeric_level,
cmds['forward'].signal.connect(mw.tabs.forward_act) format='%(asctime)s [%(levelname)s] '
cmds['print'].signal.connect(mw.tabs.print_act) '[%(module)s:%(funcName)s:%(lineno)s] %(message)s',
cmds['scrolldown'].signal.connect(mw.tabs.scroll_down_act) datefmt='%Y-%m-%d %H:%M:%S')
cmds['scrollup'].signal.connect(mw.tabs.scroll_up_act)
cmds['scrollleft'].signal.connect(mw.tabs.scroll_left_act)
cmds['scrollright'].signal.connect(mw.tabs.scroll_right_act)
cmds['undo'].signal.connect(mw.tabs.undo_close)
kp.from_cmd_dict(cmds)
mw.show() def init_cmds(self):
sys.exit(app.exec_()) cmdutils.register_all()
cmds = cmdutils.cmd_dict
cmds['open'].signal.connect(self.mainwindow.tabs.openurl)
cmds['tabopen'].signal.connect(self.mainwindow.tabs.tabopen)
cmds['quit'].signal.connect(QApplication.closeAllWindows) # FIXME
cmds['tabclose'].signal.connect(self.mainwindow.tabs.close_act)
cmds['tabprev'].signal.connect(self.mainwindow.tabs.switch_prev)
cmds['tabnext'].signal.connect(self.mainwindow.tabs.switch_next)
cmds['reload'].signal.connect(self.mainwindow.tabs.reload_act)
cmds['stop'].signal.connect(self.mainwindow.tabs.stop_act)
cmds['back'].signal.connect(self.mainwindow.tabs.back_act)
cmds['forward'].signal.connect(self.mainwindow.tabs.forward_act)
cmds['print'].signal.connect(self.mainwindow.tabs.print_act)
cmds['scrolldown'].signal.connect(self.mainwindow.tabs.scroll_down_act)
cmds['scrollup'].signal.connect(self.mainwindow.tabs.scroll_up_act)
cmds['scrollleft'].signal.connect(self.mainwindow.tabs.scroll_left_act)
cmds['scrollright'].signal.connect(
self.mainwindow.tabs.scroll_right_act)
cmds['undo'].signal.connect(self.mainwindow.tabs.undo_close)
self.keyparser.from_cmd_dict(cmdutils.cmd_dict)