-
-
Notifications
You must be signed in to change notification settings - Fork 448
[WIP] Initial toml support #699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0131303
Initial pyproject.toml support
RazerM aef2866
Missing getfloat
RazerM 1218f7f
TOMLConfigParser -> TomlConfigParser
RazerM f6b988b
fix getfloat for int
RazerM 9eb9d84
Move TomlConfigParser
RazerM 61faad3
Add name to contributors
RazerM 5d0fc7a
Import toml in backward.py
RazerM 64720d9
fix indentation
RazerM 330f03d
Don't ignore TomlDecodeError
RazerM 0121572
Raise if TomlConfigParser is used without toml installed
RazerM 0066322
Add tests for TOML config
RazerM bc86807
Fix test on Python 2
RazerM e54acb9
Mention toml support in documentation.
RazerM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import io | ||
import os | ||
import re | ||
|
||
from coverage import env | ||
from coverage.backward import configparser, path_types, string_class, toml | ||
from coverage.misc import CoverageException, substitute_variables | ||
|
||
|
||
class TomlDecodeError(Exception): | ||
"""An exception class that exists even when toml isn't installed.""" | ||
|
||
|
||
class TomlConfigParser: | ||
def __init__(self, our_file): | ||
self.getters = [lambda obj: obj['tool']['coverage']] | ||
if our_file: | ||
self.getters.append(lambda obj: obj) | ||
|
||
self._data = [] | ||
|
||
def read(self, filenames): | ||
if toml is None: | ||
raise RuntimeError('toml module is not installed.') | ||
|
||
if isinstance(filenames, path_types): | ||
filenames = [filenames] | ||
read_ok = [] | ||
for filename in filenames: | ||
try: | ||
with io.open(filename, encoding='utf-8') as fp: | ||
self._data.append(toml.load(fp)) | ||
except IOError: | ||
continue | ||
except toml.TomlDecodeError as err: | ||
raise TomlDecodeError(*err.args) | ||
if env.PYVERSION >= (3, 6): | ||
filename = os.fspath(filename) | ||
read_ok.append(filename) | ||
return read_ok | ||
|
||
def has_option(self, section, option): | ||
for data in self._data: | ||
for getter in self.getters: | ||
try: | ||
getter(data)[section][option] | ||
except KeyError: | ||
continue | ||
return True | ||
return False | ||
|
||
def has_section(self, section): | ||
for data in self._data: | ||
for getter in self.getters: | ||
try: | ||
getter(data)[section] | ||
except KeyError: | ||
continue | ||
return section | ||
return False | ||
|
||
def options(self, section): | ||
for data in self._data: | ||
for getter in self.getters: | ||
try: | ||
section = getter(data)[section] | ||
except KeyError: | ||
continue | ||
return list(section.keys()) | ||
raise configparser.NoSectionError(section) | ||
|
||
def get_section(self, section): | ||
d = {} | ||
for opt in self.options(section): | ||
d[opt] = self.get(section, opt) | ||
return d | ||
|
||
def get(self, section, option): | ||
found_section = False | ||
for data in self._data: | ||
for getter in self.getters: | ||
try: | ||
section = getter(data)[section] | ||
except KeyError: | ||
continue | ||
|
||
found_section = True | ||
try: | ||
value = section[option] | ||
except KeyError: | ||
continue | ||
if isinstance(value, string_class): | ||
value = substitute_variables(value, os.environ) | ||
return value | ||
if not found_section: | ||
raise configparser.NoSectionError(section) | ||
raise configparser.NoOptionError(option, section) | ||
|
||
def getboolean(self, section, option): | ||
value = self.get(section, option) | ||
if not isinstance(value, bool): | ||
raise ValueError( | ||
'Option {!r} in section {!r} is not a boolean: {!r}' | ||
.format(option, section, value)) | ||
return value | ||
|
||
def getlist(self, section, option): | ||
values = self.get(section, option) | ||
if not isinstance(values, list): | ||
raise ValueError( | ||
'Option {!r} in section {!r} is not a list: {!r}' | ||
.format(option, section, values)) | ||
for i, value in enumerate(values): | ||
if isinstance(value, string_class): | ||
values[i] = substitute_variables(value, os.environ) | ||
return values | ||
|
||
def getregexlist(self, section, option): | ||
values = self.getlist(section, option) | ||
for value in values: | ||
value = value.strip() | ||
try: | ||
re.compile(value) | ||
except re.error as e: | ||
raise CoverageException( | ||
"Invalid [%s].%s value %r: %s" % (section, option, value, e) | ||
) | ||
return values | ||
|
||
def getint(self, section, option): | ||
value = self.get(section, option) | ||
if not isinstance(value, int): | ||
raise ValueError( | ||
'Option {!r} in section {!r} is not an integer: {!r}' | ||
.format(option, section, value)) | ||
return value | ||
|
||
def getfloat(self, section, option): | ||
value = self.get(section, option) | ||
if isinstance(value, int): | ||
value = float(value) | ||
if not isinstance(value, float): | ||
raise ValueError( | ||
'Option {!r} in section {!r} is not a float: {!r}' | ||
.format(option, section, value)) | ||
return value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have a case where our_file is True, so we could remove a little bit of code here.