mirror of https://github.com/cheat/cheat.git
Add reading settings from configuration file
This commit is contained in:
parent
c4c935a6a5
commit
a651426075
|
@ -1,3 +1,4 @@
|
||||||
from . import sheet
|
from . import sheet
|
||||||
from . import sheets
|
from . import sheets
|
||||||
from . import utils
|
from . import utils
|
||||||
|
from . import configuration
|
|
@ -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')
|
|
@ -1,14 +1,14 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from cheat import cheatsheets
|
|
||||||
from cheat.utils import die
|
from cheat.utils import die
|
||||||
from cheat.utils import highlight
|
from cheat.utils import highlight
|
||||||
|
from cheat.configuration import Configuration
|
||||||
|
|
||||||
def default_path():
|
def default_path():
|
||||||
""" Returns the default cheatsheet path """
|
""" Returns the default cheatsheet path """
|
||||||
|
|
||||||
# determine the default cheatsheet dir
|
# 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))
|
default_sheets_dir = os.path.expanduser(os.path.expandvars(default_sheets_dir))
|
||||||
|
|
||||||
# create the DEFAULT_CHEAT_DIR if it does not exist
|
# create the DEFAULT_CHEAT_DIR if it does not exist
|
||||||
|
@ -57,8 +57,8 @@ def paths():
|
||||||
]
|
]
|
||||||
|
|
||||||
# merge the CHEATPATH paths into the sheet_paths
|
# merge the CHEATPATH paths into the sheet_paths
|
||||||
if 'CHEATPATH' in os.environ and os.environ['CHEATPATH']:
|
if Configuration().get_cheatpath():
|
||||||
for path in os.environ['CHEATPATH'].split(os.pathsep):
|
for path in Configuration().get_cheatpath().split(os.pathsep):
|
||||||
if os.path.isdir(path):
|
if os.path.isdir(path):
|
||||||
sheet_paths.append(path)
|
sheet_paths.append(path)
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@ import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from cheat.configuration import Configuration
|
||||||
|
|
||||||
def highlight(needle, haystack):
|
def highlight(needle, haystack):
|
||||||
""" Highlights a search term matched within a line """
|
""" Highlights a search term matched within a line """
|
||||||
|
|
||||||
|
@ -26,7 +28,7 @@ def colorize(sheet_content):
|
||||||
""" Colorizes cheatsheet content if so configured """
|
""" Colorizes cheatsheet content if so configured """
|
||||||
|
|
||||||
# only colorize if configured to do so, and if stdout is a tty
|
# 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
|
return sheet_content
|
||||||
|
|
||||||
# don't attempt to colorize an empty cheatsheet
|
# don't attempt to colorize an empty cheatsheet
|
||||||
|
@ -68,16 +70,13 @@ def editor():
|
||||||
""" Determines the user's preferred editor """
|
""" Determines the user's preferred editor """
|
||||||
|
|
||||||
# determine which editor to use
|
# determine which editor to use
|
||||||
editor = os.environ.get('CHEAT_EDITOR') \
|
editor = Configuration().get_editor()
|
||||||
or os.environ.get('VISUAL') \
|
|
||||||
or os.environ.get('EDITOR') \
|
|
||||||
or False
|
|
||||||
|
|
||||||
# assert that the editor is set
|
# assert that the editor is set
|
||||||
if editor == False:
|
if (not editor):
|
||||||
die(
|
die(
|
||||||
'You must set a CHEAT_EDITOR, VISUAL, or EDITOR environment '
|
'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
|
return editor
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
{"CHEATCOLORS":true,"EDITOR":"vi"}
|
2
setup.py
2
setup.py
|
@ -1,5 +1,4 @@
|
||||||
from distutils.core import setup
|
from distutils.core import setup
|
||||||
import glob
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
cheat_files = []
|
cheat_files = []
|
||||||
|
@ -28,5 +27,6 @@ setup(
|
||||||
],
|
],
|
||||||
data_files = [
|
data_files = [
|
||||||
('/usr/share/cheat', cheat_files),
|
('/usr/share/cheat', cheat_files),
|
||||||
|
('/etc', ['config/cheat']),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue