mirror of
https://github.com/cheat/cheat.git
synced 2025-09-01 09:38:29 +02:00
Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
d14c759a48 | |||
c70dc002fa | |||
ff8ba4e717 | |||
718ec4f685 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
*.pyc
|
||||
MANIFEST
|
||||
build
|
||||
cheat.egg-info
|
||||
dist
|
||||
|
@ -38,7 +38,7 @@ from docopt import docopt
|
||||
|
||||
if __name__ == '__main__':
|
||||
# parse the command-line options
|
||||
options = docopt(__doc__, version='cheat 2.1.4')
|
||||
options = docopt(__doc__, version='cheat 2.1.6')
|
||||
|
||||
# list directories
|
||||
if options['--directories']:
|
||||
|
@ -25,29 +25,13 @@ def create_or_edit(sheet):
|
||||
# if the cheatsheet does not exist
|
||||
if not exists(sheet):
|
||||
create(sheet)
|
||||
|
||||
# if the cheatsheet exists and is writeable...
|
||||
elif exists(sheet) and is_writable(sheet):
|
||||
|
||||
# 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):
|
||||
copy(path(sheet), os.path.join(sheets.default_path(), sheet))
|
||||
edit(sheet)
|
||||
|
||||
# if the cheatsheet exists but is not writable...
|
||||
elif exists(sheet) and not is_writable(sheet):
|
||||
# ... ask the user if we should copy the cheatsheet to her home directory for editing
|
||||
yes = prompt_yes_or_no(
|
||||
'The ' + sheet + ' sheet is not editable. Do you want to copy it to '
|
||||
'your user cheatsheets directory before editing? Keep in mind that '
|
||||
'your sheet will always be used before system-wide one.'
|
||||
)
|
||||
|
||||
# if yes, copy the cheatsheet to the home directory before editing
|
||||
if yes:
|
||||
copy(path(sheet), os.path.join(sheets.default_path(), sheet))
|
||||
edit(sheet)
|
||||
|
||||
# if no, just abort
|
||||
else:
|
||||
die('Aborting.')
|
||||
|
||||
|
||||
def create(sheet):
|
||||
""" Creates a cheatsheet """
|
||||
@ -75,6 +59,12 @@ def exists(sheet):
|
||||
return sheet in sheets.get() and os.access(path(sheet), os.R_OK)
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
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)
|
||||
|
@ -2,13 +2,6 @@ from cheat import cheatsheets
|
||||
from cheat.utils import *
|
||||
import os
|
||||
|
||||
# @kludge: it breaks the functional paradigm to a degree, but declaring this
|
||||
# var here (versus within get()) gives us a "poor man's" memoization on the
|
||||
# call to get(). This, in turn, spares us from having to call out to the
|
||||
# filesystem more than once.
|
||||
cheats = {}
|
||||
|
||||
|
||||
def default_path():
|
||||
""" Returns the default cheatsheet path """
|
||||
|
||||
@ -37,11 +30,7 @@ def default_path():
|
||||
|
||||
def get():
|
||||
""" Assembles a dictionary of cheatsheets as name => file-path """
|
||||
|
||||
# if we've already reached out to the filesystem, just return the result
|
||||
# from memory
|
||||
if cheats:
|
||||
return cheats
|
||||
cheats = {}
|
||||
|
||||
# otherwise, scan the filesystem
|
||||
for cheat_dir in reversed(paths()):
|
||||
|
Reference in New Issue
Block a user