Add reading settings from configuration file

This commit is contained in:
Tomas Korbar 2018-05-24 14:12:43 +02:00 committed by Tomas Korbar
parent c4c935a6a5
commit a651426075
6 changed files with 89 additions and 12 deletions

View File

@ -1,3 +1,4 @@
from . import sheet
from . import sheets
from . import utils
from . import configuration

76
cheat/configuration.py Normal file
View File

@ -0,0 +1,76 @@
import os
from cheat.utils import warn
import json
class Configuration:
def __init__(self):
self._saved_configuration = self._get_configuration()
def _get_configuration(self):
# get options from config files and environment vairables
merged_config = {}
try:
merged_config.update(self._read_configuration_file('/etc/cheat'))
except Exception:
warn('error while parsing global configuration')
try:
merged_config.update(self._read_configuration_file(os.path.expanduser(os.path.join('~','.config','cheat','cheat'))))
except Exception:
warn('error while parsing user configuration')
merged_config.update(self._read_env_vars_config())
return merged_config
def _read_configuration_file(self,path):
# Reads configuration file and returns list of set variables
read_config = {}
if (os.path.isfile(path)):
with open(path) as config_file:
read_config.update(json.load(config_file))
return read_config
def _read_env_vars_config(self):
read_config = {}
# All these variables are left here because of backwards compatibility
if (os.environ.get('CHEAT_EDITOR')):
read_config['EDITOR'] = os.environ.get('CHEAT_EDITOR')
if (os.environ.get('VISUAL')):
read_config['EDITOR'] = os.environ.get('VISUAL')
keys = ['DEFAULT_CHEAT_DIR','CHEATPATH','CHEATCOLORS','EDITOR']
for k in keys:
self._read_env_var(read_config,k)
return read_config
def _read_env_var(self,current_config,key):
if (os.environ.get(key)):
current_config[key] = os.environ.get(key)
def get_default_cheat_dir(self):
return self._saved_configuration.get('DEFAULT_CHEAT_DIR')
def get_cheatpath(self):
return self._saved_configuration.get('CHEATPATH')
def get_cheatcolors(self):
return self._saved_configuration.get('CHEATCOLORS')
def get_editor(self):
return self._saved_configuration.get('EDITOR')

View File

@ -1,14 +1,14 @@
import os
from cheat import cheatsheets
from cheat.utils import die
from cheat.utils import highlight
from cheat.configuration import Configuration
def default_path():
""" Returns the default cheatsheet path """
# determine the default cheatsheet dir
default_sheets_dir = os.environ.get('DEFAULT_CHEAT_DIR') or os.path.join('~', '.cheat')
default_sheets_dir = Configuration().get_default_cheat_dir() or os.path.join('~', '.cheat')
default_sheets_dir = os.path.expanduser(os.path.expandvars(default_sheets_dir))
# create the DEFAULT_CHEAT_DIR if it does not exist
@ -57,8 +57,8 @@ def paths():
]
# merge the CHEATPATH paths into the sheet_paths
if 'CHEATPATH' in os.environ and os.environ['CHEATPATH']:
for path in os.environ['CHEATPATH'].split(os.pathsep):
if Configuration().get_cheatpath():
for path in Configuration().get_cheatpath().split(os.pathsep):
if os.path.isdir(path):
sheet_paths.append(path)

View File

@ -3,6 +3,8 @@ import os
import subprocess
import sys
from cheat.configuration import Configuration
def highlight(needle, haystack):
""" Highlights a search term matched within a line """
@ -26,7 +28,7 @@ def colorize(sheet_content):
""" Colorizes cheatsheet content if so configured """
# only colorize if configured to do so, and if stdout is a tty
if os.environ.get('CHEATCOLORS') != 'true' or not sys.stdout.isatty():
if not Configuration().get_cheatcolors() or not sys.stdout.isatty():
return sheet_content
# don't attempt to colorize an empty cheatsheet
@ -68,16 +70,13 @@ def editor():
""" Determines the user's preferred editor """
# determine which editor to use
editor = os.environ.get('CHEAT_EDITOR') \
or os.environ.get('VISUAL') \
or os.environ.get('EDITOR') \
or False
editor = Configuration().get_editor()
# assert that the editor is set
if editor == False:
if (not editor):
die(
'You must set a CHEAT_EDITOR, VISUAL, or EDITOR environment '
'variable in order to create/edit a cheatsheet.'
'variable or setting in order to create/edit a cheatsheet.'
)
return editor

1
config/cheat Normal file
View File

@ -0,0 +1 @@
{"CHEATCOLORS":true,"EDITOR":"vi"}

View File

@ -1,5 +1,4 @@
from distutils.core import setup
import glob
import os
cheat_files = []
@ -28,5 +27,6 @@ setup(
],
data_files = [
('/usr/share/cheat', cheat_files),
('/etc', ['config/cheat']),
],
)