From babc86194ede8ade831848fa6347e73022986f99 Mon Sep 17 00:00:00 2001 From: Alec Warner Date: Sun, 25 Mar 2007 06:57:06 +0000 Subject: [PATCH] split the loader classes into their own file, split the recursive filename grabbing out into it's own function svn path=/main/trunk/; revision=6281 --- pym/portage/env/config.py | 169 +------------------------------------ pym/portage/env/loaders.py | 166 ++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 167 deletions(-) create mode 100644 pym/portage/env/loaders.py diff --git a/pym/portage/env/config.py b/pym/portage/env/config.py index 9f81d633f..4af7ac47b 100644 --- a/pym/portage/env/config.py +++ b/pym/portage/env/config.py @@ -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 index 000000000..c19c4001e --- /dev/null +++ b/pym/portage/env/loaders.py @@ -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) -- 2.26.2