aboutsummaryrefslogtreecommitdiff
path: root/dodai/tools/__init__.py
blob: bcef547982d056b1c639ce9b2646a00b895e94ed (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# 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
from dodai.tools.odict import OrderedDict
import unicodedata

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

def quote_list(data, double_quotes=False):
    """Takes the give list (data) and adds quotes around each item.  Returns
    a list

    """
    out = []
    if double_quotes:
        quote = '"'
    else:
        quote = "'"
    for item in data:
        item = "{quote}{item}{quote}".format(quote=quote, item=item)
        out.append(item)
    return out

def normalize_unicode(data):
    """Normalizes the unicode data so that compare functions will work
    correctly.

    """
    data = unicode(data)
    data = unicodedata.normalize('NFC', data)
    return data

class Section(OrderedDict):
    """An ordered dictionary object that has the added benfit of holding
    a name or title of this grouping.

    """
    def __init__(self, title):
        self.set_section_title(title)
        super(Section,self).__init__()

    def get_section_title(self):
        return self.___title___

    def set_section_title(self, val):
        self.___title___ = val

    def __repr__(self):
        return "<Section('{0}')>".format(self.get_section_title())