blob: 5cab6f84e7e22bad92ceda5131844ec675f153be (
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
|
# 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 os
import ConfigParser
class ConfigFile(object):
def __init__(self):
self._files = []
self._parser = None
self.files_loaded = []
self._dir = None
def set_directory(self, path):
""" Sets the direcory where files will be looked for
raises: InvalidDirectoryException or DirectoryDoesNotExistException
"""
if os.path.isdir(path):
self._dir = path
else:
if os.path.isfile(path):
raise InvalidDirectoryException(path)
else:
raise DirectoryDoesNotExistException(path)
def get_directory(self):
""" Returns the directory where files will be looked for
"""
return self._dir
def add_file(self, path):
""" Adds a full file path with the given path (list or string)
raises: FileDoesNotExistException
"""
if isinstance(path, list):
for file_ in path:
self._add_file(file_)
else:
if path not in self._files:
self._add_file(path)
def _add_file(self, path):
""" Adds the given file path file to the object if the filepath
doesn't already exist
"""
if os.path.isfile(path):
if path not in self._files:
self._files.append(path)
else:
raise FileDoesNotExistException(path)
def get_files(self):
""" Returns a list of files that were added to this object
"""
return self._files
def parser(self):
""" Returns a ConfigParser.ConfigParser object with files loaded
raises: NoFilesToLoadException
"""
self._reset_parser()
if not self._parser:
if not self._files:
raise NoFilesToLoadException()
self._parser = ConfigParser.ConfigParser()
self.files_loaded = self._parser.read(self._files)
return self._parser
def load(self, name):
""" Takes the given name and merges it with the object's directory
then adds the path to the object
"""
if not self._dir:
raise DirectoryNotSetException()
else:
path = os.path.join(self._dir, name)
self.add_file(path)
def _reset_parser(self):
""" Resets the _parser property if the files_loaded does not equal
the files assigned to this object
"""
if self._parser:
if self.files_loaded != self._files:
self._parser = None
class NoFilesToLoadException(Exception):
pass
class DirectoryNotSetException(Exception):
pass
class InvalidDirectoryException(Exception):
pass
class DirectoryDoesNotExistException(Exception):
pass
class FileDoesNotExistException(Exception):
pass
|