drop line_count in favor of izip and count(), itertools FOR THE WIN baby.
authorAlec Warner <antarus@gentoo.org>
Sat, 31 Mar 2007 20:13:21 +0000 (20:13 -0000)
committerAlec Warner <antarus@gentoo.org>
Sat, 31 Mar 2007 20:13:21 +0000 (20:13 -0000)
svn path=/main/trunk/; revision=6329

pym/portage/env/config.py
pym/portage/env/loaders.py

index ca533286fbf29c3b9f6836d10ac4206d2e7e7b69..62d3d8177b41b2a042fd46be70df60f25c05ef0c 100644 (file)
@@ -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))
index 18acd457c581f059f71f667e65df679e1dd52e3f..b38670816d1cf288bbb103b826a4e918f70bb98c 100644 (file)
@@ -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)
-