From b3abd4c356e71de850deee8680e20997c91a929d Mon Sep 17 00:00:00 2001 From: x10an14 Date: Sat, 27 May 2017 02:46:43 +0200 Subject: [PATCH] Feature: Let load() handle non-existing files in list. (#99) * Feature: Let load() handle non-existing files in list. * Feature/Enhancement: Return FileNotFoundError if no valid files are given (in list) to toml.load(). Ignore any files in list which do not exist on system. * Enhancement: Add Python 2 support for new feature's Exception raise. As [discussed here](https://github.com/uiri/toml/pull/99#issuecomment-302256123). --- toml.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/toml.py b/toml.py index 19bdf3d..fda070a 100644 --- a/toml.py +++ b/toml.py @@ -1,8 +1,10 @@ # This software is released under the MIT license import re -import datetime import io +import datetime +from os import linesep + __version__ = "0.9.2" __spec__ = "0.4.0" @@ -62,18 +64,27 @@ def load(f, _dict=dict): Parsed toml file represented as a dictionary Raises: - TypeError -- When array of non-strings is passed TypeError -- When f is invalid type TomlDecodeError: Error while decoding toml + IOError -- When array with zero valid (existing) file paths is passed (Python 2) + FileNotFoundError -- When array with zero valid (existing) file paths is passed (Python 3) """ if isinstance(f, basestring): with io.open(f, encoding='utf-8') as ffile: return loads(ffile.read(), _dict) elif isinstance(f, list): - for l in f: - if not isinstance(l, basestring): - raise TypeError("Load expects a list to contain filenames only") + from os import path as op + f = [path for path in f if op.exists(path)] + if not f: + error_msg = "Load expects a list to contain filenames only." + error_msg += linesep + error_msg += "The list needs to contain the path of at least one existing file." + try: + raise FileNotFoundError(error_msg) + except NameError: + # Program executed with Python 2, not Python 3: + raise IOError(error_msg) d = _dict() for l in f: d.update(load(l))