From 3069faff5cd3dbf37ae0d903893b2d61dfcf16bc Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 14 Oct 2016 18:30:09 +0530 Subject: [PATCH] Look for config file in stabdard location on startup --- kitty/config.py | 15 ++++++++++++++- kitty/constants.py | 22 ++++++++++++++++++++++ kitty/main.py | 4 ++-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/kitty/config.py b/kitty/config.py index 9a4e7ddb6..0b06b289b 100644 --- a/kitty/config.py +++ b/kitty/config.py @@ -5,6 +5,8 @@ import re from collections import namedtuple +from PyQt5.QtGui import QFont, QFontInfo + key_pat = re.compile(r'([a-zA-Z][a-zA-Z0-9_-]*)\s+(.+)$') defaults = {} @@ -15,6 +17,8 @@ foreground #dddddd foreground_bold #ffffff cursor #dddddd background #000000 +font_family monospace +font_size system # black color0 #000000 @@ -63,7 +67,11 @@ def load_config(path): if not path: return defaults ans = defaults._asdict() - with open(path) as f: + try: + f = open(path) + except FileNotFoundError: + return defaults + with f: for line in f: line = line.strip() if not line or line.startswith('#'): @@ -74,3 +82,8 @@ def load_config(path): if key in ans: ans[key] = val return Options(**ans) + + +def validate_font(opts): + if not QFontInfo(QFont(opts.font_family)).fixedPitch(): + raise ValueError('The font specified in the configuration "{}" is not a monospace font'.format(opts.font_family)) diff --git a/kitty/constants.py b/kitty/constants.py index 7e8945464..f68d54be9 100644 --- a/kitty/constants.py +++ b/kitty/constants.py @@ -2,6 +2,28 @@ # vim:fileencoding=utf-8 # License: GPL v3 Copyright: 2016, Kovid Goyal +import os +from PyQt5.QtCore import QStandardPaths + appname = 'kitty' version = (0, 1, 0) str_version = '.'.join(map(str, version)) + + +def _get_config_dir(): + # This must be called before calling setApplicationName + if 'KITTY_CONFIG_DIRECTORY' in os.environ: + return os.path.abspath(os.path.expanduser(os.environ['VISE_CONFIG_DIRECTORY'])) + + candidate = QStandardPaths.writableLocation(QStandardPaths.ConfigLocation) + if not candidate: + raise RuntimeError( + 'Failed to find path for application config directory') + ans = os.path.join(candidate, appname) + try: + os.makedirs(ans) + except FileExistsError: + pass + return ans +config_dir = _get_config_dir() +del _get_config_dir diff --git a/kitty/main.py b/kitty/main.py index d88c8158f..57c6a872f 100644 --- a/kitty/main.py +++ b/kitty/main.py @@ -13,7 +13,7 @@ from PyQt5.QtCore import Qt, QSocketNotifier from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox from .config import load_config, validate_font -from .constants import appname, str_version +from .constants import appname, str_version, config_dir from .term import TerminalWidget @@ -67,7 +67,7 @@ def option_parser(): a = parser.add_argument a('--name', default=appname, help=_('Set the name part of the WM_CLASS property')) a('--class', default=appname, dest='cls', help=_('Set the class part of the WM_CLASS property')) - a('--config', default=None, help=_('Specify a path to the config file to use')) + a('--config', default=os.path.join(config_dir, 'kitty.conf'), help=_('Specify a path to the config file to use')) a('--cmd', '-c', default=None, help=_('Run python code in the kitty context')) a('-d', '--directory', default='.', help=_('Change to the specified directory when launching')) a('--version', action='version', version='{} {} by Kovid Goyal'.format(appname, '.'.join(str_version)))