From 5e4610f0a687f4b308ac1283d4c2ad0b707e42f5 Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Wed, 5 Feb 2014 14:31:10 +0000 Subject: Factor out shell library loading into yarnlib --- yarnlib/shell_libraries.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 yarnlib/shell_libraries.py (limited to 'yarnlib/shell_libraries.py') diff --git a/yarnlib/shell_libraries.py b/yarnlib/shell_libraries.py new file mode 100644 index 0000000..3607adc --- /dev/null +++ b/yarnlib/shell_libraries.py @@ -0,0 +1,45 @@ +# Copyright 2014 Lars Wirzenius and Codethink Limited +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# =*= License: GPL-3+ =*= + + +def load_shell_libraries(paths, pre_read_cb=lambda filename: None, open=open): + '''Helper for loading shell libraries from files. + + `paths`: Iterable of file paths to shell libraries. + `pre_read_cb`: Optional callback for providing progress reporting + and logging. + It is given only one parameter, of the path to the + shell library that is about to be loaded. + `open`: Optional function for opening file paths. This is useful to + override how files are opened for unit tests or virtual + file systems. + + This function is provided so that when shell libraries are loaded + by applications that use yarnlib, they can all load them with the + same format, including the comments about where the snippet came from, + and any future additions. + + ''' + + libs = [] + for filename in paths: + pre_read_cb(filename) + with open(filename) as f: + text = f.read() + libs.append('# Loaded from %s\n\n%s\n\n' % (filename, text)) + + return ''.join(libs) -- cgit v1.2.1