Use ConfigParser and add [settings] header in case it's missing
This commit is contained in:
parent
81fe41409a
commit
23c586e616
2 changed files with 37 additions and 26 deletions
|
@ -27,6 +27,8 @@ import sys
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
from ConfigParser import RawConfigParser
|
||||||
|
|
||||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages'))
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'packages'))
|
||||||
from .log import setup_logging
|
from .log import setup_logging
|
||||||
|
@ -51,16 +53,29 @@ class FileAction(argparse.Action):
|
||||||
values = os.path.realpath(values)
|
values = os.path.realpath(values)
|
||||||
setattr(namespace, self.dest, values)
|
setattr(namespace, self.dest, values)
|
||||||
|
|
||||||
|
def checkUpdateConfigFile(configFile):
|
||||||
|
"""Checks if the config has a header section, if not add it for ConfigParser"""
|
||||||
|
with open(configFile) as fh:
|
||||||
|
configData = fh.read()
|
||||||
|
if not configData.strip().startswith('[settings]'):
|
||||||
|
configData = "[settings]\n" + configData.strip()
|
||||||
|
|
||||||
|
with open(configFile, 'w') as fh:
|
||||||
|
fh.write(configData)
|
||||||
|
|
||||||
def parseConfigFile(configFile):
|
def parseConfigFile(configFile):
|
||||||
if not configFile:
|
if not configFile:
|
||||||
configFile = os.path.join(os.path.expanduser('~'), '.wakatime.conf')
|
configFile = os.path.join(os.path.expanduser('~'), '.wakatime.conf')
|
||||||
|
|
||||||
|
checkUpdateConfigFile(configFile)
|
||||||
|
|
||||||
# define default config values
|
# define default config values
|
||||||
configs = {
|
defaults = {
|
||||||
'api_key': None,
|
'settings' : {
|
||||||
'ignore': [],
|
'api_key': None,
|
||||||
'verbose': False,
|
'ignore': [],
|
||||||
|
'verbose': False
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if not os.path.isfile(configFile):
|
if not os.path.isfile(configFile):
|
||||||
|
@ -68,24 +83,19 @@ def parseConfigFile(configFile):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(configFile) as fh:
|
with open(configFile) as fh:
|
||||||
for line in fh:
|
configs = RawConfigParser()
|
||||||
line = line.split('=', 1)
|
setConfigDefaults(configs, defaults)
|
||||||
if len(line) == 2 and line[0].strip() and line[1].strip():
|
configs.readfp(fh)
|
||||||
line[0] = line[0].strip()
|
|
||||||
line[1] = line[1].strip()
|
|
||||||
if line[0] in configs:
|
|
||||||
if isinstance(configs[line[0]], list):
|
|
||||||
configs[line[0]].append(line[1])
|
|
||||||
elif isinstance(configs[line[0]], bool):
|
|
||||||
configs[line[0]] = True if line[1].lower() == 'true' else False
|
|
||||||
else:
|
|
||||||
configs[line[0]] = line[1]
|
|
||||||
else:
|
|
||||||
configs[line[0]] = line[1]
|
|
||||||
except IOError:
|
except IOError:
|
||||||
print('Error: Could not read from config file ~/.wakatime.conf')
|
print('Error: Could not read from config file ~/.wakatime.conf')
|
||||||
return configs
|
return configs
|
||||||
|
|
||||||
|
def setConfigDefaults(config, defaults):
|
||||||
|
for section, values in defaults.iteritems():
|
||||||
|
if not config.has_section(section):
|
||||||
|
config.add_section(section)
|
||||||
|
for key, value in values.iteritems():
|
||||||
|
config.set(section, key, value)
|
||||||
|
|
||||||
def parseArguments(argv):
|
def parseArguments(argv):
|
||||||
try:
|
try:
|
||||||
|
@ -126,19 +136,20 @@ def parseArguments(argv):
|
||||||
# set arguments from config file
|
# set arguments from config file
|
||||||
configs = parseConfigFile(args.config)
|
configs = parseConfigFile(args.config)
|
||||||
if not args.key:
|
if not args.key:
|
||||||
default_key = configs.get('api_key')
|
default_key = configs.get('settings', 'api_key')
|
||||||
if default_key:
|
if default_key:
|
||||||
args.key = default_key
|
args.key = default_key
|
||||||
else:
|
else:
|
||||||
parser.error('Missing api key')
|
parser.error('Missing api key')
|
||||||
for pattern in configs.get('ignore', []):
|
for pattern in configs.get('settings', 'ignore'):
|
||||||
if not args.ignore:
|
if not args.ignore:
|
||||||
args.ignore = []
|
args.ignore = []
|
||||||
args.ignore.append(pattern)
|
args.ignore.append(pattern)
|
||||||
if not args.verbose and 'verbose' in configs:
|
if not args.verbose and configs.has_option('settings', 'verbose'):
|
||||||
args.verbose = configs['verbose']
|
args.verbose = configs.getboolean('settings', 'verbose')
|
||||||
if not args.logfile and 'logfile' in configs:
|
if not args.logfile and configs.has_option('settings', 'logfile'):
|
||||||
args.logfile = configs['logfile']
|
args.logfile = configs.get('settings', 'logfile')
|
||||||
|
|
||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
|
@ -243,7 +254,7 @@ def main(argv=None):
|
||||||
branch = None
|
branch = None
|
||||||
name = None
|
name = None
|
||||||
stats = get_file_stats(args.targetFile)
|
stats = get_file_stats(args.targetFile)
|
||||||
project = find_project(args.targetFile)
|
project = find_project(args.targetFile, args.config)
|
||||||
if project:
|
if project:
|
||||||
branch = project.branch()
|
branch = project.branch()
|
||||||
name = project.name()
|
name = project.name()
|
||||||
|
|
|
@ -30,7 +30,7 @@ PLUGINS = [
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def find_project(path):
|
def find_project(path, config):
|
||||||
for plugin in PLUGINS:
|
for plugin in PLUGINS:
|
||||||
project = plugin(path)
|
project = plugin(path)
|
||||||
if project.process():
|
if project.process():
|
||||||
|
|
Loading…
Reference in a new issue