1.1 --- a/moinformat/input/common.py Sun Jul 07 23:46:21 2019 +0200
1.2 +++ b/moinformat/input/common.py Fri Jul 12 21:54:20 2019 +0200
1.3 @@ -19,6 +19,7 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 +from moinformat.utils.file import readfile
1.8 from os.path import split
1.9 import codecs
1.10
1.11 @@ -99,11 +100,7 @@
1.12 optional 'encoding' is specified, override the general encoding.
1.13 """
1.14
1.15 - f = codecs.open(filename, encoding=encoding or self.encoding)
1.16 - try:
1.17 - return f.read()
1.18 - finally:
1.19 - f.close()
1.20 + return readfile(filename, encoding or self.encoding)
1.21
1.22 # Name translation methods.
1.23
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/moinformat/utils/file.py Fri Jul 12 21:54:20 2019 +0200
2.3 @@ -0,0 +1,37 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +"""
2.7 +File utilities.
2.8 +
2.9 +Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk>
2.10 +
2.11 +This program is free software; you can redistribute it and/or modify it under
2.12 +the terms of the GNU General Public License as published by the Free Software
2.13 +Foundation; either version 3 of the License, or (at your option) any later
2.14 +version.
2.15 +
2.16 +This program is distributed in the hope that it will be useful, but WITHOUT
2.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
2.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
2.19 +details.
2.20 +
2.21 +You should have received a copy of the GNU General Public License along with
2.22 +this program. If not, see <http://www.gnu.org/licenses/>.
2.23 +"""
2.24 +
2.25 +import codecs
2.26 +
2.27 +def readfile(filename, encoding):
2.28 +
2.29 + """
2.30 + Return the contents of the file having the given 'filename' and employing
2.31 + the given 'encoding'.
2.32 + """
2.33 +
2.34 + f = codecs.open(filename, encoding=encoding)
2.35 + try:
2.36 + return f.read()
2.37 + finally:
2.38 + f.close()
2.39 +
2.40 +# vim: tabstop=4 expandtab shiftwidth=4