From b9f9932db7d6583933c4c5c28e2f945b77c9548a Mon Sep 17 00:00:00 2001 From: "Jasper St. Pierre" Date: Tue, 1 May 2012 17:19:24 -0400 Subject: util: use GLib for xdg basedir spec stuff --- Mozo/MenuEditor.py | 4 +-- Mozo/util.py | 75 ++++++++++++++++-------------------------------------- 2 files changed, 24 insertions(+), 55 deletions(-) diff --git a/Mozo/MenuEditor.py b/Mozo/MenuEditor.py index 80ef2dd..71a76eb 100644 --- a/Mozo/MenuEditor.py +++ b/Mozo/MenuEditor.py @@ -180,7 +180,7 @@ class MenuEditor: def canRevert(self, item): if item.get_type() == matemenu.TYPE_ENTRY: - if util.getItemPath(item.get_desktop_file_id()): + if util.getItemPath(item.get_desktop_file_id()) is not None: path = util.getUserItemPath() if os.path.isfile(os.path.join(path, item.get_desktop_file_id())): return True @@ -189,7 +189,7 @@ class MenuEditor: file_id = os.path.split(item.get_desktop_file_path())[1] else: file_id = item.get_menu_id() + '.directory' - if util.getDirectoryPath(file_id): + if util.getDirectoryPath(file_id) is not None: path = util.getUserDirectoryPath() if os.path.isfile(os.path.join(path, file_id)): return True diff --git a/Mozo/util.py b/Mozo/util.py index 3ea68d7..cc7db83 100644 --- a/Mozo/util.py +++ b/Mozo/util.py @@ -82,75 +82,44 @@ def getUniqueUndoFile(filepath): append += 1 return new_filepath -def getUserMenuPath(): - menu_dir = None - if os.environ.has_key('XDG_CONFIG_HOME'): - menu_dir = os.path.join(os.environ['XDG_CONFIG_HOME'], 'menus') - else: - menu_dir = os.path.join(os.environ['HOME'], '.config', 'menus') - #move .config out of the way if it's not a dir, it shouldn't be there - if os.path.isfile(os.path.split(menu_dir)[0]): - os.rename(os.path.split(menu_dir)[0], os.path.split(menu_dir)[0] + '.old') - if not os.path.isdir(menu_dir): - os.makedirs(menu_dir) - return menu_dir - def getItemPath(file_id): - if os.environ.has_key('XDG_DATA_DIRS'): - for system_path in os.environ['XDG_DATA_DIRS'].split(':'): - file_path = os.path.join(system_path, 'applications', file_id) - if os.path.isfile(file_path): - return file_path - file_path = os.path.join('/', 'usr', 'share', 'applications', file_id) + for path in GLib.get_system_data_dirs(): + file_path = os.path.join(path, 'applications', file_id) if os.path.isfile(file_path): return file_path - return False + return None def getUserItemPath(): - item_dir = None - if os.environ.has_key('XDG_DATA_HOME'): - item_dir = os.path.join(os.environ['XDG_DATA_HOME'], 'applications') - else: - item_dir = os.path.join(os.environ['HOME'], '.local', 'share', 'applications') + item_dir = os.path.join(GLib.get_user_data_dir(), 'applications') if not os.path.isdir(item_dir): os.makedirs(item_dir) return item_dir def getDirectoryPath(file_id): - home = getUserDirectoryPath() - file_path = os.path.join(home, file_id) - if os.path.isfile(file_path): - return file_path - if os.environ.has_key('XDG_DATA_DIRS'): - for system_path in os.environ['XDG_DATA_DIRS'].split(':'): - file_path = os.path.join(system_path, 'desktop-directories', file_id) - if os.path.isfile(file_path): - return file_path - file_path = os.path.join('/', 'usr', 'share', 'desktop-directories', file_id) - if os.path.isfile(file_path): - return file_path - return False + for path in GLib.get_system_data_dirs(): + file_path = os.path.join(path, 'desktop-directories', file_id) + if os.path.isfile(file_path): + return file_path + return None def getUserDirectoryPath(): - menu_dir = None - if os.environ.has_key('XDG_DATA_HOME'): - menu_dir = os.path.join(os.environ['XDG_DATA_HOME'], 'desktop-directories') - else: - menu_dir = os.path.join(os.environ['HOME'], '.local', 'share', 'desktop-directories') + menu_dir = os.path.join(GLib.get_user_data_dir(), 'desktop-directories') if not os.path.isdir(menu_dir): os.makedirs(menu_dir) return menu_dir -def getSystemMenuPath(file_name): - if os.environ.has_key('XDG_CONFIG_DIRS'): - for system_path in os.environ['XDG_CONFIG_DIRS'].split(':'): - file_path = os.path.join(system_path, 'menus', file_name) - if os.path.isfile(file_path): - return file_path - file_path = os.path.join('/', 'etc', 'xdg', 'menus', file_name) - if os.path.isfile(file_path): - return file_path - return False +def getUserMenuPath(): + menu_dir = os.path.join(GLib.get_user_config_dir(), 'menus') + if not os.path.isdir(menu_dir): + os.makedirs(menu_dir) + return menu_dir + +def getSystemMenuPath(file_id): + for path in GLib.get_system_config_dirs(): + file_path = os.path.join(path, 'menus', file_id) + if os.path.isfile(file_path): + return file_path + return None def getUserMenuXml(tree): system_file = getSystemMenuPath(tree.get_menu_file()) -- cgit v1.2.1