aboutsummaryrefslogtreecommitdiff
path: root/dodai/config/file.py
diff options
context:
space:
mode:
Diffstat (limited to 'dodai/config/file.py')
-rw-r--r--dodai/config/file.py116
1 files changed, 116 insertions, 0 deletions
diff --git a/dodai/config/file.py b/dodai/config/file.py
new file mode 100644
index 0000000..5cab6f8
--- /dev/null
+++ b/dodai/config/file.py
@@ -0,0 +1,116 @@
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
19import os
20import ConfigParser
21
22class ConfigFile(object):
23
24 def __init__(self):
25 self._files = []
26 self._parser = None
27 self.files_loaded = []
28 self._dir = None
29
30 def set_directory(self, path):
31 """ Sets the direcory where files will be looked for
32 raises: InvalidDirectoryException or DirectoryDoesNotExistException
33 """
34 if os.path.isdir(path):
35 self._dir = path
36 else:
37 if os.path.isfile(path):
38 raise InvalidDirectoryException(path)
39 else:
40 raise DirectoryDoesNotExistException(path)
41
42 def get_directory(self):
43 """ Returns the directory where files will be looked for
44 """
45 return self._dir
46
47 def add_file(self, path):
48 """ Adds a full file path with the given path (list or string)
49 raises: FileDoesNotExistException
50 """
51 if isinstance(path, list):
52 for file_ in path:
53 self._add_file(file_)
54 else:
55 if path not in self._files:
56 self._add_file(path)
57
58 def _add_file(self, path):
59 """ Adds the given file path file to the object if the filepath
60 doesn't already exist
61 """
62 if os.path.isfile(path):
63 if path not in self._files:
64 self._files.append(path)
65 else:
66 raise FileDoesNotExistException(path)
67
68 def get_files(self):
69 """ Returns a list of files that were added to this object
70 """
71 return self._files
72
73 def parser(self):
74 """ Returns a ConfigParser.ConfigParser object with files loaded
75 raises: NoFilesToLoadException
76 """
77 self._reset_parser()
78 if not self._parser:
79 if not self._files:
80 raise NoFilesToLoadException()
81 self._parser = ConfigParser.ConfigParser()
82 self.files_loaded = self._parser.read(self._files)
83 return self._parser
84
85 def load(self, name):
86 """ Takes the given name and merges it with the object's directory
87 then adds the path to the object
88 """
89 if not self._dir:
90 raise DirectoryNotSetException()
91 else:
92 path = os.path.join(self._dir, name)
93 self.add_file(path)
94
95 def _reset_parser(self):
96 """ Resets the _parser property if the files_loaded does not equal
97 the files assigned to this object
98 """
99 if self._parser:
100 if self.files_loaded != self._files:
101 self._parser = None
102
103class NoFilesToLoadException(Exception):
104 pass
105
106class DirectoryNotSetException(Exception):
107 pass
108
109class InvalidDirectoryException(Exception):
110 pass
111
112class DirectoryDoesNotExistException(Exception):
113 pass
114
115class FileDoesNotExistException(Exception):
116 pass