2014-04-27 05:31:13 +02:00
|
|
|
import os
|
|
|
|
|
2016-07-19 13:37:18 +02:00
|
|
|
from cheat import cheatsheets
|
|
|
|
from cheat.utils import die
|
|
|
|
|
2014-04-27 05:31:13 +02:00
|
|
|
def default_path():
|
|
|
|
""" Returns the default cheatsheet path """
|
|
|
|
|
2014-04-27 06:28:28 +02:00
|
|
|
# determine the default cheatsheet dir
|
2016-11-24 16:14:37 +01:00
|
|
|
default_sheets_dir = os.environ.get('DEFAULT_CHEAT_DIR') or os.path.join('~', '.cheat')
|
|
|
|
default_sheets_dir = os.path.expanduser(os.path.expandvars(default_sheets_dir))
|
2014-04-27 06:28:28 +02:00
|
|
|
|
|
|
|
# create the DEFAULT_CHEAT_DIR if it does not exist
|
|
|
|
if not os.path.isdir(default_sheets_dir):
|
|
|
|
try:
|
|
|
|
# @kludge: unclear on why this is necessary
|
|
|
|
os.umask(0000)
|
|
|
|
os.mkdir(default_sheets_dir)
|
|
|
|
|
|
|
|
except OSError:
|
|
|
|
die('Could not create DEFAULT_CHEAT_DIR')
|
|
|
|
|
|
|
|
# assert that the DEFAULT_CHEAT_DIR is readable and writable
|
|
|
|
if not os.access(default_sheets_dir, os.R_OK):
|
|
|
|
die('The DEFAULT_CHEAT_DIR (' + default_sheets_dir +') is not readable.')
|
|
|
|
if not os.access(default_sheets_dir, os.W_OK):
|
2016-07-19 13:37:18 +02:00
|
|
|
die('The DEFAULT_CHEAT_DIR (' + default_sheets_dir +') is not writable.')
|
2014-04-27 06:28:28 +02:00
|
|
|
|
|
|
|
# return the default dir
|
|
|
|
return default_sheets_dir
|
2014-04-27 05:31:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get():
|
|
|
|
""" Assembles a dictionary of cheatsheets as name => file-path """
|
2015-02-11 01:35:14 +01:00
|
|
|
cheats = {}
|
2014-05-26 03:55:25 +02:00
|
|
|
|
|
|
|
# otherwise, scan the filesystem
|
2014-04-27 05:31:13 +02:00
|
|
|
for cheat_dir in reversed(paths()):
|
2018-02-24 17:44:32 +01:00
|
|
|
subdirs = []
|
|
|
|
for root, dirs, files in os.walk(cheat_dir, topdown=True):
|
|
|
|
dirs[:] = [d for d in dirs if not d.startswith('.')]
|
|
|
|
subdirs.append(root)
|
|
|
|
|
|
|
|
for subdir in subdirs:
|
|
|
|
cheats.update(
|
|
|
|
dict([
|
|
|
|
(cheat, os.path.join(subdir, cheat))
|
|
|
|
for cheat in os.listdir(subdir)
|
|
|
|
if not cheat.startswith('.')
|
|
|
|
and not cheat.startswith('__')
|
|
|
|
and not os.path.isdir(os.path.join(subdir, cheat))
|
|
|
|
])
|
|
|
|
)
|
|
|
|
|
2014-04-27 05:31:13 +02:00
|
|
|
|
|
|
|
return cheats
|
|
|
|
|
|
|
|
|
|
|
|
def paths():
|
|
|
|
""" Assembles a list of directories containing cheatsheets """
|
|
|
|
sheet_paths = [
|
|
|
|
default_path(),
|
|
|
|
cheatsheets.sheets_dir()[0],
|
|
|
|
]
|
|
|
|
|
|
|
|
# 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 os.path.isdir(path):
|
|
|
|
sheet_paths.append(path)
|
|
|
|
|
|
|
|
if not sheet_paths:
|
|
|
|
die('The DEFAULT_CHEAT_DIR dir does not exist or the CHEATPATH is not set.')
|
|
|
|
|
|
|
|
return sheet_paths
|
|
|
|
|
|
|
|
|
|
|
|
def list():
|
|
|
|
""" Lists the available cheatsheets """
|
|
|
|
sheet_list = ''
|
|
|
|
pad_length = max([len(x) for x in get().keys()]) + 4
|
|
|
|
for sheet in sorted(get().items()):
|
|
|
|
sheet_list += sheet[0].ljust(pad_length) + sheet[1] + "\n"
|
|
|
|
return sheet_list
|
|
|
|
|
|
|
|
|
2018-02-24 17:44:32 +01:00
|
|
|
def search(term, target_sheet):
|
|
|
|
""" Searches all cheatsheets for the specified term.
|
|
|
|
Restrict search to target_sheet if given """
|
2014-04-27 05:31:13 +02:00
|
|
|
result = ''
|
|
|
|
for cheatsheet in sorted(get().items()):
|
2018-02-24 17:44:32 +01:00
|
|
|
if target_sheet and target_sheet != cheatsheet[0]:
|
|
|
|
continue
|
2014-04-27 05:31:13 +02:00
|
|
|
match = ''
|
2018-02-24 17:44:32 +01:00
|
|
|
# for line in open(cheatsheet[1]):
|
|
|
|
for index, line in enumerate(open(cheatsheet[1])):
|
2016-07-19 13:37:18 +02:00
|
|
|
if term in line:
|
2018-02-24 17:44:32 +01:00
|
|
|
match += '[%d]\t\t' % index + line.strip() + '\n'
|
|
|
|
# if 'CHEATCOLORS' in os.environ:
|
|
|
|
# line = line.replace(term, '\033[1;31m'+ term +'\033[0m');
|
|
|
|
# match += ' ' + line
|
2014-04-27 05:31:13 +02:00
|
|
|
|
2016-07-19 13:37:18 +02:00
|
|
|
if match != '':
|
2014-04-27 05:31:13 +02:00
|
|
|
result += cheatsheet[0] + ":\n" + match + "\n"
|
|
|
|
|
|
|
|
return result
|