Exemple #1
0
def read(sheet):
    """ Returns the contents of the cheatsheet as a String """
    if not exists(sheet):
        die('No cheatsheet found for ' + sheet)

    with open(path(sheet)) as cheatfile:
        return cheatfile.read()
Exemple #2
0
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)

    # GMFTBY add : add the subdir under the current_dir into the sheet_paths
    for_extend = []
    for path in sheet_paths:
        subdirs = get_subdir(path)
        if subdirs:
            for_extend.extend(subdirs)
    sheet_paths.extend(for_extend)

    if not sheet_paths:
        die('The DEFAULT_CHEAT_DIR dir does not exist or the CHEATPATH is not set.'
            )

    return sheet_paths
Exemple #3
0
def read(sheet):
    """ Returns the contents of the cheatsheet as a String """
    if not exists(sheet):
        die('\033[1;31mNo\033[0m cheatsheet found for \033[1;31m' + sheet + '\033[0m')

    with open(path(sheet)) as cheatfile:
        return cheatfile.read()
Exemple #4
0
def edit(sheet):
    """ Opens a cheatsheet for editing """

    try:
        subprocess.call([editor(), path(sheet)])

    except OSError:
        die('Could not launch ' + editor())
Exemple #5
0
def edit(sheet):
    """ Opens a cheatsheet for editing """

    try:
        subprocess.call([editor(), path(sheet)])

    except OSError:
        die('Could not launch ' + editor())
Exemple #6
0
def create(sheet):
    """ Creates a cheatsheet """
    new_sheet_path = os.path.join(sheets.default_path(), sheet)

    try:
        subprocess.call([editor(), new_sheet_path])

    except OSError:
        die('Could not launch ' + editor())
Exemple #7
0
def create(sheet):
    """ Creates a cheatsheet """
    new_sheet_path = os.path.join(sheets.default_path(), sheet)

    try:
        subprocess.call([editor(), new_sheet_path])

    except OSError:
        die('Could not launch ' + editor())
Exemple #8
0
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:
        die('Could not copy cheatsheet for editing.')
Exemple #9
0
 def __request_post(self, url, post_data):
     req = urllib2.Request(url, urllib.urlencode(post_data))
     try:
         result = urllib2.urlopen(req).read()
         result = json.loads(result)
         code = int(result['code'])
         if code % 2 == 1 and code >= 500:
             die(result['mess'])
         return result
     except Exception, e:
         warn('modify fails')
         return None
Exemple #10
0
 def __request_post(self, url, post_data):
     req = urllib2.Request(url, urllib.urlencode(post_data))
     try:
         result = urllib2.urlopen(req).read()
         result = json.loads(result)
         code = int(result['code'])
         if code % 2 == 1 and code >= 500:
             die(result['mess'])
         return result 
     except Exception, e:
         warn('modify fails')
         return None
Exemple #11
0
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
Exemple #12
0
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
Exemple #13
0
def remove(sheet, default="yes"):
    """Ask a yes/no/quit sheet via raw_input() and return their answer.

    "sheet" is a string that is presented to the user.
    "default" is the presumed answer if the user just hits <Enter>.
        It must be "yes" (the default), "no", "quit" or None (meaning
        an answer is required of the user).
    The "answer" return value is one of "yes", "no" or "quit".
    """

    if not exists(sheet):
        die('\033[1;31mNo\033[0m cheatsheet found for \033[1;31m' + sheet + '\033[0m')

    sheet = sheets.get()[sheet]

    valid = {"yes":"yes",   "y":"yes",    "ye":"yes",
             "no":"no",     "n":"no",
             "quit":"quit", "qui":"quit", "qu":"quit", "q":"quit"}
    if default == None:
        prompt = " [y/n/q] "
    elif default == "yes":
        prompt = " [Y/n/q] "
    elif default == "no":
        prompt = " [y/N/q] "
    elif default == "quit":
        prompt = " [y/n/Q] "
    else:
        raise ValueError("invalid default answer: '%s'" % default)
    while 1:
        sys.stdout.write(sheet + prompt)
        choice = input().lower()
        if default is not None and choice == '':
            print("Delete\033[1;31m %s \033[0mfile " % sheet)
            os.remove(sheet)
            return default
        elif choice in valid.keys():
            print("Delete\033[1;31m %s \033[0mfile " % sheet)
            os.remove(sheet)
            return valid[choice]
        else:
            sys.stdout.write("Please respond with 'yes', 'no' or 'quit'.\n")
Exemple #14
0
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(os.path.expanduser('~'), '.cheat')

    # 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 (%s) is not readable.' % default_sheets_dir)
    if not os.access(default_sheets_dir, os.W_OK):
        die('The DEFAULT_CHEAT_DIR (%s) is not writable.' % default_sheets_dir)

    # return the default dir
    return default_sheets_dir
Exemple #15
0
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(os.path.expanduser('~'), '.cheat')

    # 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 (%s) is not readable.' % default_sheets_dir)
    if not os.access(default_sheets_dir, os.W_OK):
        die('The DEFAULT_CHEAT_DIR (%s) is not writable.' % default_sheets_dir)

    # return the default dir
    return default_sheets_dir