sublime-rana/packages/wakatime/configs.py

65 lines
1.5 KiB
Python
Raw Normal View History

2017-02-21 00:18:38 +00:00
# -*- coding: utf-8 -*-
"""
wakatime.configs
~~~~~~~~~~~~~~~~
Config file parser.
:copyright: (c) 2016 Alan Hamlett.
:license: BSD, see LICENSE for more details.
"""
from __future__ import print_function
import os
import traceback
2017-03-16 15:26:24 +00:00
from .compat import open
2017-02-21 00:18:38 +00:00
from .constants import CONFIG_FILE_PARSE_ERROR
try:
import configparser
2017-11-23 20:41:59 +00:00
except ImportError: # pragma: nocover
2017-03-16 15:26:24 +00:00
from .packages import configparser
2017-02-21 00:18:38 +00:00
2017-05-25 06:53:28 +00:00
def getConfigFile():
"""Returns the config file location.
If $WAKATIME_HOME env varialbe is defined, returns
$WAKATIME_HOME/.wakatime.cfg, otherwise ~/.wakatime.cfg.
"""
fileName = '.wakatime.cfg'
home = os.environ.get('WAKATIME_HOME')
if home:
return os.path.join(os.path.expanduser(home), fileName)
return os.path.join(os.path.expanduser('~'), fileName)
2017-02-21 00:18:38 +00:00
def parseConfigFile(configFile=None):
"""Returns a configparser.SafeConfigParser instance with configs
read from the config file. Default location of the config file is
at ~/.wakatime.cfg.
"""
# get config file location from ENV
if not configFile:
2017-05-25 06:53:28 +00:00
configFile = getConfigFile()
2017-02-21 00:18:38 +00:00
2017-03-16 15:26:24 +00:00
configs = configparser.ConfigParser(delimiters=('='), strict=False)
2017-02-21 00:18:38 +00:00
try:
with open(configFile, 'r', encoding='utf-8') as fh:
try:
2017-03-16 15:26:24 +00:00
configs.read_file(fh)
2017-02-21 00:18:38 +00:00
except configparser.Error:
print(traceback.format_exc())
2017-03-16 15:26:24 +00:00
raise SystemExit(CONFIG_FILE_PARSE_ERROR)
2017-02-21 00:18:38 +00:00
except IOError:
2017-03-16 15:26:24 +00:00
pass
2017-02-21 00:18:38 +00:00
return configs