# HG changeset patch # User Paul Boddie # Date 1562961260 -7200 # Node ID 2ddf8fb7e6f7f6bff4191b05648f584df368750f # Parent 22bc7d3d51bb611f0ffe8e9bd33de931974b4e6b Introduced a common file-reading utility function. diff -r 22bc7d3d51bb -r 2ddf8fb7e6f7 moinformat/input/common.py --- a/moinformat/input/common.py Sun Jul 07 23:46:21 2019 +0200 +++ b/moinformat/input/common.py Fri Jul 12 21:54:20 2019 +0200 @@ -19,6 +19,7 @@ this program. If not, see . """ +from moinformat.utils.file import readfile from os.path import split import codecs @@ -99,11 +100,7 @@ optional 'encoding' is specified, override the general encoding. """ - f = codecs.open(filename, encoding=encoding or self.encoding) - try: - return f.read() - finally: - f.close() + return readfile(filename, encoding or self.encoding) # Name translation methods. diff -r 22bc7d3d51bb -r 2ddf8fb7e6f7 moinformat/utils/file.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/moinformat/utils/file.py Fri Jul 12 21:54:20 2019 +0200 @@ -0,0 +1,37 @@ +#!/usr/bin/env python + +""" +File utilities. + +Copyright (C) 2019 Paul Boddie + +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 . +""" + +import codecs + +def readfile(filename, encoding): + + """ + Return the contents of the file having the given 'filename' and employing + the given 'encoding'. + """ + + f = codecs.open(filename, encoding=encoding) + try: + return f.read() + finally: + f.close() + +# vim: tabstop=4 expandtab shiftwidth=4