# Copyright (C) 2010 Leonard Thomas # # This file is part of Dodai. # # Dodai is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Dodai is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Dodai. If not, see . import sys import os import platform def home_directory(): """Returns the full real path to the home directory of the user who is executing this script. """ out = None try: from win32com.shell import shellcon, shell out = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0) except ImportError: out = os.path.expanduser("~") return os.path.realpath(out) def config_directory_system(project_name=None): """Returns the system config directory with the passed in project appended to the path. Returns None for windows and java systems. """ path = None if platform.system and platform.system() not in ['Windows', 'Java']: if project_name: path = os.path.join('/etc', project_name.strip()) else: path = '/etc' return path def config_directory_user(project_name): """Returns the config direcotry of the project which is located in the user's home directory. Returns None for Java and unknown systems """ path = None project = '.{0}'.format(project_name.strip()) if platform.system and platform.system() not in ['Java']: home_dir = home_directory() path = os.path.join(home_dir, project) return path def config_directory_project(): """Returns the config directory that is located in the same directory of the executable that ran this script """ path = os.path.dirname(os.path.abspath(sys.argv[0])) return os.path.join(path, 'config') def config_directories(project_name): """Returns a list of possible project directories """ dirs = [] dir = config_directory_system(project_name) if dir: dirs.append(dir) dir = config_directory_user(project_name) if dir: dirs.append(dir) dir = config_directory_project() if dir: dirs.append(dir) return dirs def list_to_english(data): """Takes the input list and creates a string with each option encased in single quotes and seperated by a comma with exception of the last option which is prefixed with the word 'and' """ if data: if len(data) > 1: out = [] last = "{0}".format(data.pop()) for row in data: out.append("{0}".format(row)) out = ', '.join(out) return "{0} and {1}".format(out, last) else: return "{0}".format(data.pop())