From 71fee9897b3fccf8ec7a3dc9b0f175d3da8bf734 Mon Sep 17 00:00:00 2001 From: Jeremy Carbaugh Date: Thu, 23 Jul 2009 17:44:18 -0400 Subject: [PATCH] add Files to handle opening and iteration over multiple files --- saucebrush/utils.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/saucebrush/utils.py b/saucebrush/utils.py index d75b624..2085e7e 100644 --- a/saucebrush/utils.py +++ b/saucebrush/utils.py @@ -159,4 +159,31 @@ def dotted_key_set(dict_or_list, dotted_key, value, separator='.'): elif isinstance(dict_or_list, (tuple, list)): newkey = separator.join(keys[i:]) for item in dict_or_list: - dotted_key_set(item, newkey, value, separator) \ No newline at end of file + dotted_key_set(item, newkey, value, separator) + +# +# utility classes +# + +class Files(object): + + def __init__(self, file_open_callback=None, *args): + self.paths = [] + for arg in args: + self.add(arg) + self.file_open_callback = file_open_callback + + def add(self, path): + self.paths.append(path) + + def __iter__(self): + return self.linereader() + + def linereader(self): + for path in iter(self.paths): + if os.path.exists(path): + self.file_open_callback(path) + f = open(path) + for line in f: + yield line + f.close() \ No newline at end of file