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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# config.py -- Portage Config
# Copyright 2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$
import os
class LoaderError(Exception):
def __init__(self, resource, error_msg):
"""
@param resource: Resource that failed to load (file/sql/etc)
@type resource: String
@param error_msg: Error from underlying Loader system
@type error_msg: String
"""
self.resource
def __str__(self):
return "Failed while loading resource: %s, error was: %s" % (
resource, error_msg)
def RecursiveFileLoader(filename):
"""
If filename is of type file, return [filename]
else if filename is of type directory, return an array
full of files in that directory to process.
Ignore files beginning with . or ending in ~.
Prune CVS directories.
@param filename: name of a file/directory to traverse
@rtype: list
@returns: List of files to process
"""
if os.path.isdir(filename):
for root, dirs, files in os.walk(self.fname):
if 'CVS' in dirs:
dirs.remove('CVS')
files = filter(files,startswith('.'))
files = filter(files,endswith('~'))
for file in files:
yield file
else:
yield filename
class DataLoader(object):
def __init__(self, validator=None):
f = validator
if f is None:
# if they pass in no validator, just make a fake one
# that always returns true
class AlwaysTrue(object):
def validate(self, key):
return True
f = AlwaysTrue()
self._validator = f
def load(self):
"""
Function to do the actual work of a Loader
"""
pass
class ItemFileLoader(DataLoader):
"""
Class to load data from a file full of items one per line
>>> item1
>>> item2
>>> item3
>>> item1
becomes { 'item1':None, 'item2':None, 'item3':None }
Note that due to the data store being a dict, duplicates
are removed.
"""
_recursive = False
def __init__(self, filename):
DataLoader.__init__(self)
self.fname = filename
def load(self):
data = {}
errors = {}
for file in RecursiveFileLoader(self.fname):
f = open(file, 'rb')
for line_num, line in enumerate(f):
if line.startswith('#'):
continue
split = line.strip().split()
if not len(split):
errors.setdefault(self.fname,[]).append(
"Malformed data at line: %s, data: %s"
% (line_num + 1, split))
key = split[0]
if not self._validator.validate(key):
errors.setdefault(self.fname,[]).append(
"Validation failed at line: %s, data %s"
% (line_num + 1, split))
continue
data[key] = None
return (data,errors)
class KeyListFileLoader(DataLoader):
"""
Class to load data from a file full of key [list] tuples
>>>>key foo1 foo2 foo3
becomes
{'key':['foo1','foo2','foo3']}
"""
_recursive = False
def __init__(self, filename):
DataLoader.__init__(self)
self.fname = filename
def load(self):
data = {}
errors = {}
for file in RecursiveFileLoader(self.fname):
f = open(file, 'rb')
for line_num, line in enumerate(f):
if line.startswith('#'):
continue
split = line.strip().split()
if len(split) < 2:
errors.setdefault(self.fname,[]).append(
"Malformed data at line: %s, data: %s"
% (line_num + 1, split))
continue
key = split[0]
value = split[1:]
if not self._validator.validate(key):
errors.setdefault(self.fname,[]).append(
"Validation failed at line: %s, data %s"
% (line_num + 1, split))
continue
if key in data:
data[key].append(value)
else:
data[key] = value
return (data,errors)
class KeyValuePairFileLoader(DataLoader):
"""
Class to load data from a file full of key=value pairs
>>>>key=value
>>>>foo=bar
becomes:
{'key':'value',
'foo':'bar'}
"""
_recursive = False
def __init__(self, filename):
DataLoader.__init__(self)
self.fname = filename
def load(self):
"""
Return the {source: {key: value}} pairs from a file
Return the {source: [list of errors] from a load
@param recursive: If set and self.fname is a directory;
load all files in self.fname
@type: Boolean
@rtype: tuple
@returns:
Returns (data,errors), both may be empty dicts or populated.
"""
DataLoader.load(self)
data = {}
errors = {}
for file in RecursiveFileLoader(self.fname):
f = open(file, 'rb')
for line_num, line in enumerate(f):
if line.startswith('#'):
continue
split = line.strip().split('=')
if len(split) < 2:
errors.setdefault(self.fname,[]).append(
"Malformed data at line: %s, data %s"
% (line_num + 1, split))
key = split[0]
value = split[1:]
if not self._validator.validate(key):
errors.setdefault(self.fname,[]).append(
"Validation failed at line: %s, data %s"
% (line_num + 1, split))
continue
if key in data:
data[key].append(value)
else:
data[key] = value
return (data,errors)
|