summaryrefslogtreecommitdiffstats
path: root/pym/portage/env/loaders.py
blob: a789b3fa86c127c254e738e6fb57e40ab34aee24 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# config.py -- Portage Config
# Copyright 2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

import os

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 load(self):
		"""
		Function to do the actual work of a Loader
		"""
		pass

class AtomFileLoader(DataLoader):
	"""
	Class to load data from a file full of atoms one per line
	
	>>> atom1
	>>> atom2
	>>> atom3
	
	becomes ['atom1', 'atom2', 'atom3']
	"""
	
	_recursive = False

	def __init__(self, filename):
		DataLoader.__init__(self)
		self.fname = filename
	
	def load(self):
		data = {}
		errors = {}
		line_count = 0
		for file in RecursiveFileLoader(self.fname):
			f = open(file, 'rb')
			for line in f:
				line_count = line_count + 1
				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_count, split))
				key = split[0]
				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 = {}
		line_count = 0
		for file in RecursiveFileLoader(self.fname):
			f = open(file, 'rb')
			for line in f:
				line_count = line_count + 1
				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_count, split))
				key = split[0]
				value = split[1:]
				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 = {}
		line_count = 0
                for file in RecursiveFileLoader(self.fname):
			f = open(file, 'rb')
			for line in f:
				line_count = line_count + 1 # Increment line count
				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_count, split))
				key = split[0]
				value = split[1:]
				if key in data:
					data[key].append(value)
				else:
					data[key] = value
		return (data,errors)