|
| 1 | +import io |
| 2 | +import os |
| 3 | +import re |
| 4 | + |
| 5 | +from coverage import env |
| 6 | +from coverage.backward import configparser, path_types, string_class, toml |
| 7 | +from coverage.misc import CoverageException, substitute_variables |
| 8 | + |
| 9 | + |
| 10 | +class TomlDecodeError(Exception): |
| 11 | + """An exception class that exists even when toml isn't installed.""" |
| 12 | + |
| 13 | + |
| 14 | +class TomlConfigParser: |
| 15 | + def __init__(self, our_file): |
| 16 | + self.getters = [lambda obj: obj['tool']['coverage']] |
| 17 | + if our_file: |
| 18 | + self.getters.append(lambda obj: obj) |
| 19 | + |
| 20 | + self._data = [] |
| 21 | + |
| 22 | + def read(self, filenames): |
| 23 | + if toml is None: |
| 24 | + raise RuntimeError('toml module is not installed.') |
| 25 | + |
| 26 | + if isinstance(filenames, path_types): |
| 27 | + filenames = [filenames] |
| 28 | + read_ok = [] |
| 29 | + for filename in filenames: |
| 30 | + try: |
| 31 | + with io.open(filename, encoding='utf-8') as fp: |
| 32 | + self._data.append(toml.load(fp)) |
| 33 | + except IOError: |
| 34 | + continue |
| 35 | + except toml.TomlDecodeError as err: |
| 36 | + raise TomlDecodeError(*err.args) |
| 37 | + if env.PYVERSION >= (3, 6): |
| 38 | + filename = os.fspath(filename) |
| 39 | + read_ok.append(filename) |
| 40 | + return read_ok |
| 41 | + |
| 42 | + def has_option(self, section, option): |
| 43 | + for data in self._data: |
| 44 | + for getter in self.getters: |
| 45 | + try: |
| 46 | + getter(data)[section][option] |
| 47 | + except KeyError: |
| 48 | + continue |
| 49 | + return True |
| 50 | + return False |
| 51 | + |
| 52 | + def has_section(self, section): |
| 53 | + for data in self._data: |
| 54 | + for getter in self.getters: |
| 55 | + try: |
| 56 | + getter(data)[section] |
| 57 | + except KeyError: |
| 58 | + continue |
| 59 | + return section |
| 60 | + return False |
| 61 | + |
| 62 | + def options(self, section): |
| 63 | + for data in self._data: |
| 64 | + for getter in self.getters: |
| 65 | + try: |
| 66 | + section = getter(data)[section] |
| 67 | + except KeyError: |
| 68 | + continue |
| 69 | + return list(section.keys()) |
| 70 | + raise configparser.NoSectionError(section) |
| 71 | + |
| 72 | + def get_section(self, section): |
| 73 | + d = {} |
| 74 | + for opt in self.options(section): |
| 75 | + d[opt] = self.get(section, opt) |
| 76 | + return d |
| 77 | + |
| 78 | + def get(self, section, option): |
| 79 | + found_section = False |
| 80 | + for data in self._data: |
| 81 | + for getter in self.getters: |
| 82 | + try: |
| 83 | + section = getter(data)[section] |
| 84 | + except KeyError: |
| 85 | + continue |
| 86 | + |
| 87 | + found_section = True |
| 88 | + try: |
| 89 | + value = section[option] |
| 90 | + except KeyError: |
| 91 | + continue |
| 92 | + if isinstance(value, string_class): |
| 93 | + value = substitute_variables(value, os.environ) |
| 94 | + return value |
| 95 | + if not found_section: |
| 96 | + raise configparser.NoSectionError(section) |
| 97 | + raise configparser.NoOptionError(option, section) |
| 98 | + |
| 99 | + def getboolean(self, section, option): |
| 100 | + value = self.get(section, option) |
| 101 | + if not isinstance(value, bool): |
| 102 | + raise ValueError( |
| 103 | + 'Option {!r} in section {!r} is not a boolean: {!r}' |
| 104 | + .format(option, section, value)) |
| 105 | + return value |
| 106 | + |
| 107 | + def getlist(self, section, option): |
| 108 | + values = self.get(section, option) |
| 109 | + if not isinstance(values, list): |
| 110 | + raise ValueError( |
| 111 | + 'Option {!r} in section {!r} is not a list: {!r}' |
| 112 | + .format(option, section, values)) |
| 113 | + for i, value in enumerate(values): |
| 114 | + if isinstance(value, string_class): |
| 115 | + values[i] = substitute_variables(value, os.environ) |
| 116 | + return values |
| 117 | + |
| 118 | + def getregexlist(self, section, option): |
| 119 | + values = self.getlist(section, option) |
| 120 | + for value in values: |
| 121 | + value = value.strip() |
| 122 | + try: |
| 123 | + re.compile(value) |
| 124 | + except re.error as e: |
| 125 | + raise CoverageException( |
| 126 | + "Invalid [%s].%s value %r: %s" % (section, option, value, e) |
| 127 | + ) |
| 128 | + return values |
| 129 | + |
| 130 | + def getint(self, section, option): |
| 131 | + value = self.get(section, option) |
| 132 | + if not isinstance(value, int): |
| 133 | + raise ValueError( |
| 134 | + 'Option {!r} in section {!r} is not an integer: {!r}' |
| 135 | + .format(option, section, value)) |
| 136 | + return value |
| 137 | + |
| 138 | + def getfloat(self, section, option): |
| 139 | + value = self.get(section, option) |
| 140 | + if isinstance(value, int): |
| 141 | + value = float(value) |
| 142 | + if not isinstance(value, float): |
| 143 | + raise ValueError( |
| 144 | + 'Option {!r} in section {!r} is not a float: {!r}' |
| 145 | + .format(option, section, value)) |
| 146 | + return value |
0 commit comments