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

        with io.open(self._path(sheet), encoding='utf-8') as cheatfile:
            return cheatfile.read()
Exemple #2
0
 def open(self, filepath):
     """ Open `filepath` using the EDITOR specified by the env variables """
     editor_cmd = self.editor().split()
     try:
         subprocess.call(editor_cmd + [filepath])
     except OSError:
         Utils.die('Could not launch ' + self.editor())
Exemple #3
0
    def edit(self, sheet):
        """ Creates or edits a cheatsheet """

        # if the cheatsheet does not exist
        if not self._exists(sheet):
            new_path = os.path.join(self._config.cheat_user_dir, sheet)
            self._editor.open(new_path)

        # if the cheatsheet exists but not in the default_path, copy it to the
        # default path before editing
        elif self._exists(sheet) and not self._exists_in_default_path(sheet):
            try:
                shutil.copy(self._path(sheet),
                            os.path.join(self._config.cheat_user_dir, sheet))

            # fail gracefully if the cheatsheet cannot be copied. This can
            # happen if CHEAT_USER_DIR does not exist
            except IOError:
                Utils.die('Could not copy cheatsheet for editing.')

            self._editor.open(self._path(sheet))

        # if it exists and is in the default path, then just open it
        else:
            self._editor.open(self._path(sheet))
Exemple #4
0
    def __init__(self, config):
        self._config = config
        self._colorize = Colorize(config)

        # Assembles a dictionary of cheatsheets as name => file-path
        self._sheets = {}
        sheet_paths = [
            config.cheat_user_dir
        ]

        # merge the CHEAT_PATH paths into the sheet_paths
        if config.cheat_path:
            for path in config.cheat_path.split(os.pathsep):
                if os.path.isdir(path):
                    sheet_paths.append(path)

        if not sheet_paths:
            Utils.die('The CHEAT_USER_DIR dir does not exist '
                      + 'or the CHEAT_PATH is not set.')

        # otherwise, scan the filesystem
        for cheat_dir in reversed(sheet_paths):
            self._sheets.update(
                dict([
                    (cheat, os.path.join(cheat_dir, cheat))
                    for cheat in os.listdir(cheat_dir)
                    if not cheat.startswith('.')
                    and not cheat.startswith('__')
                ])
            )
Exemple #5
0
    def __init__(self, config):
        self._config = config
        self._colorize = Colorize(config)

        # Assembles a dictionary of cheatsheets as name => file-path
        self._sheets = {}
        sheet_paths = [config.cheat_user_dir]

        # merge the CHEAT_PATH paths into the sheet_paths
        if config.cheat_path:
            for path in config.cheat_path.split(os.pathsep):
                if os.path.isdir(path):
                    sheet_paths.append(path)

        if not sheet_paths:
            Utils.die('The CHEAT_USER_DIR dir does not exist ' +
                      'or the CHEAT_PATH is not set.')

        # otherwise, scan the filesystem
        for cheat_dir in reversed(sheet_paths):
            self._sheets.update(
                dict([
                    (cheat, os.path.join(cheat_dir, cheat))
                    for cheat in os.listdir(cheat_dir)
                    if not cheat.startswith('.') and not cheat.startswith('__')
                ]))
Exemple #6
0
    def read(self, sheet):
        """ Returns the contents of the cheatsheet as a String """
        if not self._exists(sheet):
            Utils.die('No cheatsheet found for ' + sheet)

        with io.open(self._path(sheet), encoding='utf-8') as cheatfile:
            return cheatfile.read()
Exemple #7
0
    def edit(self, sheet):
        """ Creates or edits a cheatsheet """

        # if the cheatsheet does not exist
        if not self._exists(sheet):
            new_path = os.path.join(self._config.cheat_user_dir, sheet)
            self._editor.open(new_path)

        # if the cheatsheet exists but not in the default_path, copy it to the
        # default path before editing
        elif self._exists(sheet) and not self._exists_in_default_path(sheet):
            try:
                shutil.copy(
                            self._path(sheet),
                            os.path.join(self._config.cheat_user_dir, sheet)
                           )

            # fail gracefully if the cheatsheet cannot be copied. This can
            # happen if CHEAT_USER_DIR does not exist
            except IOError:
                Utils.die('Could not copy cheatsheet for editing.')

            self._editor.open(self._path(sheet))

        # if it exists and is in the default path, then just open it
        else:
            self._editor.open(self._path(sheet))
Exemple #8
0
    def read(self, sheet):
        """ Returns the contents of the cheatsheet as a String """
        if not self.exists(sheet):
            Utils.die('No cheatsheet found for ' + sheet)

        with open(self.path(sheet)) as cheatfile:
            return cheatfile.read()
Exemple #9
0
 def open(self, filepath):
     """ Open `filepath` using the EDITOR specified by the env variables """
     editor_cmd = self.editor().split()
     try:
         subprocess.call(editor_cmd + [filepath])
     except OSError:
         Utils.die('Could not launch ' + self.editor())
Exemple #10
0
    def editor(self):
        """ Determines the user's preferred editor """

        # assert that the editor is set
        if not self._config.cheat_editor:
            Utils.die(
                'You must set a CHEAT_EDITOR, VISUAL, or EDITOR environment '
                'variable or setting in order to create/edit a cheatsheet.')

        return self._config.cheat_editor
Exemple #11
0
    def _check_configuration(self, config):
        """ Check values in config and warn user or die """

        # validate CHEAT_HIGHLIGHT values if set
        colors = [
            'grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan',
            'white'
        ]
        if (config.get('CHEAT_HIGHLIGHT')
                and config.get('CHEAT_HIGHLIGHT') not in colors):
            Utils.die("%s %s" % ('CHEAT_HIGHLIGHT must be one of:', colors))
Exemple #12
0
    def copy(self, 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:
            Utils.die('Could not copy cheatsheet for editing.')
Exemple #13
0
    def editor(self):
        """ Determines the user's preferred editor """

        # assert that the editor is set
        if not self._config.cheat_editor:
            Utils.die(
                'You must set a CHEAT_EDITOR, VISUAL, or EDITOR environment '
                'variable or setting in order to create/edit a cheatsheet.'
            )

        return self._config.cheat_editor
Exemple #14
0
    def paths(self):
        """ Assembles a list of directories containing cheatsheets """
        sheet_paths = [
            self.default_path(),
        ]

        # merge the CHEATPATH paths into the sheet_paths
        if self._cheatpath:
            for path in self._cheatpath.split(os.pathsep):
                if os.path.isdir(path):
                    sheet_paths.append(path)

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

        return sheet_paths
Exemple #15
0
    def validate(self):
        """ Validates configuration parameters """

        # assert that cheat_highlight contains a valid value
        highlights = [
            'grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan',
            'white', False
        ]
        if self.cheat_highlight not in highlights:
            Utils.die("%s %s" %
                      ('CHEAT_HIGHLIGHT must be one of:', highlights))

        # assert that the color scheme is valid
        colorschemes = ['light', 'dark']
        if self.cheat_colorscheme not in colorschemes:
            Utils.die("%s %s" %
                      ('CHEAT_COLORSCHEME must be one of:', colorschemes))

        return True
Exemple #16
0
    def validate(self):
        """ Validates configuration parameters """

        # assert that cheat_highlight contains a valid value
        highlights = [
            'grey', 'red', 'green', 'yellow',
            'blue', 'magenta', 'cyan', 'white',
            False
        ]
        if self.cheat_highlight not in highlights:
            Utils.die("%s %s" %
                      ('CHEAT_HIGHLIGHT must be one of:', highlights))

        # assert that the color scheme is valid
        colorschemes = ['light', 'dark']
        if self.cheat_colorscheme not in colorschemes:
            Utils.die("%s %s" %
                      ('CHEAT_COLORSCHEME must be one of:', colorschemes))

        return True
Exemple #17
0
    def default_path(self):
        """ Returns the default cheatsheet path """

        # determine the default cheatsheet dir
        default_sheets_dir = (self._default_cheat_dir
                              or os.path.join('~', '.cheat'))
        default_sheets_dir = os.path.expanduser(
            os.path.expandvars(default_sheets_dir))

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

        # return the default dir
        return default_sheets_dir