From 9d6b61eba9dfe169eabf8f89790f20e142f9214f Mon Sep 17 00:00:00 2001 From: Alec Warner Date: Sat, 31 Mar 2007 20:13:21 +0000 Subject: drop line_count in favor of izip and count(), itertools FOR THE WIN baby. svn path=/main/trunk/; revision=6329 --- pym/portage/env/config.py | 4 +-- pym/portage/env/loaders.py | 61 ++++++++++++++++++++++++++++++++-------------- 2 files changed, 45 insertions(+), 20 deletions(-) (limited to 'pym/portage/env') diff --git a/pym/portage/env/config.py b/pym/portage/env/config.py index ca533286f..62d3d8177 100644 --- a/pym/portage/env/config.py +++ b/pym/portage/env/config.py @@ -4,7 +4,7 @@ # $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): """ @@ -61,7 +61,7 @@ class PackageMaskFile(UserConfigKlass): 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)) diff --git a/pym/portage/env/loaders.py b/pym/portage/env/loaders.py index 18acd457c..b38670816 100644 --- a/pym/portage/env/loaders.py +++ b/pym/portage/env/loaders.py @@ -4,6 +4,7 @@ # $Id$ import os +from itertools import izip, count class LoaderError(Exception): @@ -48,21 +49,35 @@ def RecursiveFileLoader(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(): + 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 @@ -74,20 +89,23 @@ class AtomFileLoader(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 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): @@ -108,20 +126,24 @@ 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: @@ -161,10 +183,9 @@ class KeyValuePairFileLoader(DataLoader): 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 @@ -175,9 +196,13 @@ class KeyValuePairFileLoader(DataLoader): % (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) - -- cgit v1.2.3-1-g7c22