refactored the `default_path` function in `sheets`.

And added two functions to `utils`:

* `get_userdata_dir`

  returns the default user data directory

* `get_default_data_dir`

  returns the default directory used for user data

Now which directory is used to store the user sheets,
is determined the following way:

1. If the `DEFAULT_CHEAT_DIR` environment variable is set, use it.

2. If a `.cheat` directory exists in the home directory, use it.

3. Use a `cheat` directory in the systems default directory for user data.
This commit is contained in:
Brutus 2014-08-19 14:47:18 +02:00
parent 3b1f8ccecc
commit c212b12f4b
3 changed files with 38 additions and 3 deletions

View File

@ -10,16 +10,16 @@ cheats = {}
def default_path(): def default_path():
""" Returns the default cheatsheet path """ """ Returns the full path to the default cheatsheet directory """
# the default path becomes confused when cheat is run as root, so fail # the default path becomes confused when cheat is run as root, so fail
# under those circumstances. (There is no good reason to need to run cheat # under those circumstances. (There is no good reason to need to run cheat
# as root.) # as root.)
if os.name != 'nt' and os.geteuid() == 0: if os.name != 'nt' and os.geteuid() == 0:
die('Please do not run this application as root.'); die('Please do not run this application as root.')
# determine the default cheatsheet dir # determine the default cheatsheet dir
default_sheets_dir = os.environ.get('DEFAULT_CHEAT_DIR') or os.path.join(os.path.expanduser('~'), '.cheat') default_sheets_dir = get_default_data_dir()
# create the DEFAULT_CHEAT_DIR if it does not exist # create the DEFAULT_CHEAT_DIR if it does not exist
if not os.path.isdir(default_sheets_dir): if not os.path.isdir(default_sheets_dir):

View File

@ -1,6 +1,8 @@
import os import os
import sys import sys
from xdg.BaseDirectory import save_data_path
def colorize(sheet_content): def colorize(sheet_content):
""" Colorizes cheatsheet content if so configured """ """ Colorizes cheatsheet content if so configured """
@ -54,3 +56,35 @@ def prompt_yes_or_no(question):
def warn(message): def warn(message):
""" Prints a message to stderr """ """ Prints a message to stderr """
print >> sys.stderr, (message) print >> sys.stderr, (message)
def get_default_data_dir():
"""
Returns the full path to the directory containing the users data.
Which directory is used, is determined the following way:
1. If the `DEFAULT_CHEAT_DIR` environment variable is set, use it.
2. If a `.cheat` directory exists in the home directory, use it.
3. Use a `cheat` directory in the systems default directory for user data.
"""
user_dir = os.environ.get("DEFAULT_CHEAT_DIR")
if not user_dir:
user_dir = os.path.expanduser(os.path.join("~", ".cheat"))
if not os.path.exists(user_dir):
user_dir = get_userdata_dir()
return user_dir
def get_userdata_dir():
"""
Returns the full path to a `cheat` directory in the platform specific
default data directory for the current user.
.. note:: The directory is created, if it's not already present.
"""
return save_data_path('cheat')

View File

@ -24,5 +24,6 @@ setup(
install_requires = [ install_requires = [
'docopt >= 0.6.1', 'docopt >= 0.6.1',
'pygments >= 1.6.0', 'pygments >= 1.6.0',
'pyxdg >= 0.25'
] ]
) )