2014-04-27 05:31:13 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
2016-07-19 13:37:18 +02:00
|
|
|
from cheat import sheets
|
2016-10-12 22:16:26 +02:00
|
|
|
from cheat.utils import die, open_with_editor
|
2014-04-27 05:31:13 +02:00
|
|
|
|
|
|
|
def copy(current_sheet_path, new_sheet_path):
|
|
|
|
""" Copies a sheet to a new path """
|
|
|
|
|
|
|
|
# attempt to copy the sheet to DEFAULT_CHEAT_DIR
|
|
|
|
try:
|
|
|
|
shutil.copy(current_sheet_path, new_sheet_path)
|
|
|
|
|
|
|
|
# fail gracefully if the cheatsheet cannot be copied. This can happen if
|
|
|
|
# DEFAULT_CHEAT_DIR does not exist
|
|
|
|
except IOError:
|
2016-07-19 13:37:18 +02:00
|
|
|
die('Could not copy cheatsheet for editing.')
|
2014-04-27 05:31:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
def create_or_edit(sheet):
|
|
|
|
""" Creates or edits a cheatsheet """
|
|
|
|
|
|
|
|
# if the cheatsheet does not exist
|
|
|
|
if not exists(sheet):
|
|
|
|
create(sheet)
|
|
|
|
|
2015-02-12 03:02:45 +01:00
|
|
|
# if the cheatsheet exists but not in the default_path, copy it to the
|
|
|
|
# default path before editing
|
|
|
|
elif exists(sheet) and not exists_in_default_path(sheet):
|
2015-02-11 01:35:14 +01:00
|
|
|
copy(path(sheet), os.path.join(sheets.default_path(), sheet))
|
|
|
|
edit(sheet)
|
2014-04-27 05:31:13 +02:00
|
|
|
|
2015-02-12 03:45:32 +01:00
|
|
|
# if it exists and is in the default path, then just open it
|
|
|
|
else:
|
|
|
|
edit(sheet)
|
|
|
|
|
2014-04-27 05:31:13 +02:00
|
|
|
|
|
|
|
def create(sheet):
|
|
|
|
""" Creates a cheatsheet """
|
|
|
|
new_sheet_path = os.path.join(sheets.default_path(), sheet)
|
2016-10-12 22:16:26 +02:00
|
|
|
open_with_editor(new_sheet_path)
|
2014-04-27 05:31:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
def edit(sheet):
|
|
|
|
""" Opens a cheatsheet for editing """
|
2016-10-12 22:16:26 +02:00
|
|
|
open_with_editor(path(sheet))
|
2014-04-27 05:31:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
def exists(sheet):
|
|
|
|
""" Predicate that returns true if the sheet exists """
|
|
|
|
return sheet in sheets.get() and os.access(path(sheet), os.R_OK)
|
|
|
|
|
|
|
|
|
2015-02-12 03:02:45 +01:00
|
|
|
def exists_in_default_path(sheet):
|
|
|
|
""" Predicate that returns true if the sheet exists in default_path"""
|
|
|
|
default_path_sheet = os.path.join(sheets.default_path(), sheet)
|
|
|
|
return sheet in sheets.get() and os.access(default_path_sheet, os.R_OK)
|
|
|
|
|
|
|
|
|
2014-04-27 05:31:13 +02:00
|
|
|
def is_writable(sheet):
|
|
|
|
""" Predicate that returns true if the sheet is writeable """
|
|
|
|
return sheet in sheets.get() and os.access(path(sheet), os.W_OK)
|
|
|
|
|
|
|
|
|
|
|
|
def path(sheet):
|
|
|
|
""" Returns a sheet's filesystem path """
|
|
|
|
return sheets.get()[sheet]
|
|
|
|
|
|
|
|
|
|
|
|
def read(sheet):
|
|
|
|
""" Returns the contents of the cheatsheet as a String """
|
|
|
|
if not exists(sheet):
|
|
|
|
die('No cheatsheet found for ' + sheet)
|
|
|
|
|
2016-07-19 13:37:18 +02:00
|
|
|
with open(path(sheet)) as cheatfile:
|
|
|
|
return cheatfile.read()
|