diff options
Diffstat (limited to 'lib/dodai/tools/__init__.py')
-rw-r--r-- | lib/dodai/tools/__init__.py | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/lib/dodai/tools/__init__.py b/lib/dodai/tools/__init__.py new file mode 100644 index 0000000..bcef547 --- /dev/null +++ b/lib/dodai/tools/__init__.py | |||
@@ -0,0 +1,143 @@ | |||
1 | # Copyright (C) 2010 Leonard Thomas | ||
2 | # | ||
3 | # This file is part of Dodai. | ||
4 | # | ||
5 | # Dodai is free software: you can redistribute it and/or modify | ||
6 | # it under the terms of the GNU General Public License as published by | ||
7 | # the Free Software Foundation, either version 3 of the License, or | ||
8 | # (at your option) any later version. | ||
9 | # | ||
10 | # Dodai is distributed in the hope that it will be useful, | ||
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | # GNU General Public License for more details. | ||
14 | # | ||
15 | # You should have received a copy of the GNU General Public License | ||
16 | # along with Dodai. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | import sys | ||
19 | import os | ||
20 | import platform | ||
21 | from dodai.tools.odict import OrderedDict | ||
22 | import unicodedata | ||
23 | |||
24 | def home_directory(): | ||
25 | """Returns the full real path to the home directory of the user who | ||
26 | is executing this script. | ||
27 | |||
28 | """ | ||
29 | out = None | ||
30 | try: | ||
31 | from win32com.shell import shellcon, shell | ||
32 | out = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0) | ||
33 | except ImportError: | ||
34 | out = os.path.expanduser("~") | ||
35 | return os.path.realpath(out) | ||
36 | |||
37 | def config_directory_system(project_name=None): | ||
38 | """Returns the system config directory with the passed in project | ||
39 | appended to the path. Returns None for windows and java systems. | ||
40 | |||
41 | """ | ||
42 | path = None | ||
43 | if platform.system and platform.system() not in ['Windows', 'Java']: | ||
44 | if project_name: | ||
45 | path = os.path.join('/etc', project_name.strip()) | ||
46 | else: | ||
47 | path = '/etc' | ||
48 | return path | ||
49 | |||
50 | def config_directory_user(project_name): | ||
51 | """Returns the config direcotry of the project which is located in | ||
52 | the user's home directory. Returns None for Java and unknown systems | ||
53 | |||
54 | """ | ||
55 | path = None | ||
56 | project = '.{0}'.format(project_name.strip()) | ||
57 | if platform.system and platform.system() not in ['Java']: | ||
58 | home_dir = home_directory() | ||
59 | path = os.path.join(home_dir, project) | ||
60 | return path | ||
61 | |||
62 | def config_directory_project(): | ||
63 | """Returns the config directory that is located in the same directory | ||
64 | of the executable that ran this script | ||
65 | |||
66 | """ | ||
67 | path = os.path.dirname(os.path.abspath(sys.argv[0])) | ||
68 | return os.path.join(path, 'config') | ||
69 | |||
70 | def config_directories(project_name): | ||
71 | """Returns a list of possible project directories | ||
72 | |||
73 | """ | ||
74 | dirs = [] | ||
75 | dir = config_directory_system(project_name) | ||
76 | if dir: | ||
77 | dirs.append(dir) | ||
78 | dir = config_directory_user(project_name) | ||
79 | if dir: | ||
80 | dirs.append(dir) | ||
81 | dir = config_directory_project() | ||
82 | if dir: | ||
83 | dirs.append(dir) | ||
84 | return dirs | ||
85 | |||
86 | def list_to_english(data): | ||
87 | """Takes the input list and creates a string with each option | ||
88 | encased in single quotes and seperated by a comma with exception | ||
89 | of the last option which is prefixed with the word 'and' | ||
90 | |||
91 | """ | ||
92 | if data: | ||
93 | if len(data) > 1: | ||
94 | out = [] | ||
95 | last = "{0}".format(data.pop()) | ||
96 | for row in data: | ||
97 | out.append("{0}".format(row)) | ||
98 | out = ', '.join(out) | ||
99 | return "{0} and {1}".format(out, last) | ||
100 | else: | ||
101 | return "{0}".format(data.pop()) | ||
102 | |||
103 | def quote_list(data, double_quotes=False): | ||
104 | """Takes the give list (data) and adds quotes around each item. Returns | ||
105 | a list | ||
106 | |||
107 | """ | ||
108 | out = [] | ||
109 | if double_quotes: | ||
110 | quote = '"' | ||
111 | else: | ||
112 | quote = "'" | ||
113 | for item in data: | ||
114 | item = "{quote}{item}{quote}".format(quote=quote, item=item) | ||
115 | out.append(item) | ||
116 | return out | ||
117 | |||
118 | def normalize_unicode(data): | ||
119 | """Normalizes the unicode data so that compare functions will work | ||
120 | correctly. | ||
121 | |||
122 | """ | ||
123 | data = unicode(data) | ||
124 | data = unicodedata.normalize('NFC', data) | ||
125 | return data | ||
126 | |||
127 | class Section(OrderedDict): | ||
128 | """An ordered dictionary object that has the added benfit of holding | ||
129 | a name or title of this grouping. | ||
130 | |||
131 | """ | ||
132 | def __init__(self, title): | ||
133 | self.set_section_title(title) | ||
134 | super(Section,self).__init__() | ||
135 | |||
136 | def get_section_title(self): | ||
137 | return self.___title___ | ||
138 | |||
139 | def set_section_title(self, val): | ||
140 | self.___title___ = val | ||
141 | |||
142 | def __repr__(self): | ||
143 | return "<Section('{0}')>".format(self.get_section_title()) | ||