aboutsummaryrefslogtreecommitdiff
path: root/dodai/tools/__init__.py
blob: b6d93a187017d6102424842527a43a530717eb02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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 <http://www.gnu.org/licenses/>.

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())