From c212b12f4b81d3b7d3bf6843af447c89ceefe27d Mon Sep 17 00:00:00 2001 From: Brutus Date: Tue, 19 Aug 2014 14:47:18 +0200 Subject: [PATCH] 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. --- cheat/sheets.py | 6 +++--- cheat/utils.py | 34 ++++++++++++++++++++++++++++++++++ setup.py | 1 + 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/cheat/sheets.py b/cheat/sheets.py index 24c556c..8517deb 100644 --- a/cheat/sheets.py +++ b/cheat/sheets.py @@ -10,16 +10,16 @@ cheats = {} 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 # under those circumstances. (There is no good reason to need to run cheat # as root.) 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 - 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 if not os.path.isdir(default_sheets_dir): diff --git a/cheat/utils.py b/cheat/utils.py index 4010c15..f573483 100644 --- a/cheat/utils.py +++ b/cheat/utils.py @@ -1,6 +1,8 @@ import os import sys +from xdg.BaseDirectory import save_data_path + def colorize(sheet_content): """ Colorizes cheatsheet content if so configured """ @@ -54,3 +56,35 @@ def prompt_yes_or_no(question): def warn(message): """ Prints a message to stderr """ 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') diff --git a/setup.py b/setup.py index 735ea41..037cd87 100644 --- a/setup.py +++ b/setup.py @@ -24,5 +24,6 @@ setup( install_requires = [ 'docopt >= 0.6.1', 'pygments >= 1.6.0', + 'pyxdg >= 0.25' ] )