# $Id$
from UserDict import UserDict
-from portage.env.loaders import KeyListFileLoader, KeyValuePairFileLoader, AtomFileLoader
+from portage.env.loaders import KeyListFileLoader, KeyValuePairFileLoader, ItemFileLoader
class UserConfigKlass(UserDict,object):
"""
to revert a previous mask; this only works when masking files are stacked
"""
- default_loader = AtomFileLoader
+ default_loader = ItemFileLoader
def __init__(self, filename):
super(PackageMaskFile,self).__init__(self.default_loader(filename))
# $Id$
import os
+from itertools import izip, count
class LoaderError(Exception):
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():
+ 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 AtomFileLoader(DataLoader):
+class ItemFileLoader(DataLoader):
"""
- Class to load data from a file full of atoms one per line
+ Class to load data from a file full of items one per line
- >>> atom1
- >>> atom2
- >>> atom3
+ >>> item1
+ >>> item2
+ >>> item3
+ >>> item1
- becomes ['atom1', 'atom2', 'atom3']
+ becomes { 'item1':None, 'item2':None, 'item3':None }
+ Note that due to the data store being a dict, duplicates
+ are removed.
"""
_recursive = False
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
+ for line_num, line in izip(count(1),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_count, split))
+ % (line_num, split))
key = split[0]
- data[key] = None
+ if not self._validator.validate(key):
+ errors.setdefault(self.fname,[]).append(
+ "Validation failed at line: %s, data %s"
+ % (line_num, split))
+ continue
+ data[key] = None
return (data,errors)
class KeyListFileLoader(DataLoader):
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
+ for line_num, line in izip(count(1),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_count, split))
+ % (line_num, 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, split))
+ continue
if key in data:
data[key].append(value)
else:
DataLoader.load(self)
data = {}
errors = {}
- line_count = 0
for file in RecursiveFileLoader(self.fname):
f = open(file, 'rb')
- for line in f:
+ for line_num, line in izip(count(1),f):
line_count = line_count + 1 # Increment line count
if line.startswith('#'):
continue
% (line_count, 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_count, split))
+ continue
if key in data:
data[key].append(value)
else:
data[key] = value
return (data,errors)
-