Look for config file in stabdard location on startup

This commit is contained in:
Kovid Goyal 2016-10-14 18:30:09 +05:30
parent 789e75afac
commit 3069faff5c
3 changed files with 38 additions and 3 deletions

View File

@ -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))

View File

@ -2,6 +2,28 @@
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
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

View File

@ -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)))