split the loader classes into their own file, split the recursive filename grabbing...
authorAlec Warner <antarus@gentoo.org>
Sun, 25 Mar 2007 06:57:06 +0000 (06:57 -0000)
committerAlec Warner <antarus@gentoo.org>
Sun, 25 Mar 2007 06:57:06 +0000 (06:57 -0000)
svn path=/main/trunk/; revision=6281

pym/portage/env/config.py
pym/portage/env/loaders.py [new file with mode: 0644]

index 9f81d633fc852cab7e6045d4c48e7e5541415cd8..4af7ac47b233e13c2a295733d992a27a4e94e0a1 100644 (file)
@@ -3,173 +3,8 @@
 # Distributed under the terms of the GNU General Public License v2
 # $Id$
 
-import os
 from UserDict import UserDict
-
-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
-                file_list = None
-                if self._recursive and os.path.isdir(self.fname):
-                        for root, dirs, files in os.walk(self.fname):
-                                if 'CVS' in dirs:
-                                        dirs.remove('CVS')
-                                files = filter(files,startswith('.'))
-                                file_list.append([f.join(root,f) for f in files])
-                else:
-                        file_list = [self.fname]
-               
-               for file in file_list:
-                       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
-               file_list = None
-               if self._recursive and os.path.isdir(self.fname):
-                       for root, dirs, files in os.walk(self.fname):
-                               if 'CVS' in dirs:
-                                       dirs.remove('CVS')
-                               files = filter(files,startswith('.'))
-                               file_list.append([f.join(root,f) for f in files])
-               else:
-                       file_list = [self.fname]
-
-               for file in file_list:
-                       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
-                file_list = None
-                if self._recursive and os.path.isdir(self.fname):
-                        for root, dirs, files in os.walk(self.fname):
-                                if 'CVS' in dirs:
-                                        dirs.remove('CVS')
-                                files = filter(files,startswith('.'))
-                                file_list.append([f.join(root,f) for f in files])
-                else:
-                        file_list = [self.fname]
-
-                for file in file_list:
-                       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)
+from portage.env.loaders import KeyListFileLoader, KeyValuePairFileLoader, AtomFileLoader
 
 class PackageKeywords(UserDict):
        """
@@ -287,7 +122,7 @@ class PackageMaskFile(PackageMask):
        to revert a previous mask; this only works when masking files are stacked
        """
        
-       default_loader = KeyValuePairFileLoader
+       default_loader = AtomFileLoader
 
        def __init__(self, filename):
                self.loader = self.default_loader(filename)
diff --git a/pym/portage/env/loaders.py b/pym/portage/env/loaders.py
new file mode 100644 (file)
index 0000000..c19c400
--- /dev/null
@@ -0,0 +1,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)