1# Based on http://docs.python.org/3.5/library/configparser.html and on
2# reading configparser.py.
3
4import sys
5from typing import (AbstractSet, MutableMapping, Mapping, Dict, Sequence, List,
6                    Union, Iterable, Iterator, Callable, Any, IO, overload,
7                    Optional, Pattern, Type, TypeVar, ClassVar)
8# Types only used in type comments only
9from typing import Optional, Tuple  # noqa
10
11if sys.version_info >= (3, 6):
12    from os import PathLike
13
14# Internal type aliases
15_section = Mapping[str, str]
16_parser = MutableMapping[str, _section]
17_converter = Callable[[str], Any]
18_converters = Dict[str, _converter]
19_T = TypeVar('_T')
20
21if sys.version_info >= (3, 7):
22    _Path = Union[str, bytes, PathLike[str]]
23elif sys.version_info >= (3, 6):
24    _Path = Union[str, PathLike[str]]
25else:
26    _Path = str
27
28DEFAULTSECT: str
29MAX_INTERPOLATION_DEPTH: int
30
31class Interpolation:
32    def before_get(self, parser: _parser,
33                   section: str,
34                   option: str,
35                   value: str,
36                   defaults: _section) -> str: ...
37
38    def before_set(self, parser: _parser,
39                   section: str,
40                   option: str,
41                   value: str) -> str: ...
42
43    def before_read(self, parser: _parser,
44                    section: str,
45                    option: str,
46                    value: str) -> str: ...
47
48    def before_write(self, parser: _parser,
49                     section: str,
50                     option: str,
51                     value: str) -> str: ...
52
53
54class BasicInterpolation(Interpolation): ...
55class ExtendedInterpolation(Interpolation): ...
56class LegacyInterpolation(Interpolation): ...
57
58
59class RawConfigParser(_parser):
60    BOOLEAN_STATES: ClassVar[Mapping[str, bool]] = ...  # Undocumented
61    def __init__(self,
62                 defaults: Optional[_section] = ...,
63                 dict_type: Type[Mapping[str, str]] = ...,
64                 allow_no_value: bool = ...,
65                 *,
66                 delimiters: Sequence[str] = ...,
67                 comment_prefixes: Sequence[str] = ...,
68                 inline_comment_prefixes: Optional[Sequence[str]] = ...,
69                 strict: bool = ...,
70                 empty_lines_in_values: bool = ...,
71                 default_section: str = ...,
72                 interpolation: Optional[Interpolation] = ...) -> None: ...
73
74    def __len__(self) -> int: ...
75
76    def __getitem__(self, section: str) -> SectionProxy: ...
77
78    def __setitem__(self, section: str, options: _section) -> None: ...
79
80    def __delitem__(self, section: str) -> None: ...
81
82    def __iter__(self) -> Iterator[str]: ...
83
84    def defaults(self) -> _section: ...
85
86    def sections(self) -> List[str]: ...
87
88    def add_section(self, section: str) -> None: ...
89
90    def has_section(self, section: str) -> bool: ...
91
92    def options(self, section: str) -> List[str]: ...
93
94    def has_option(self, section: str, option: str) -> bool: ...
95
96    def read(self, filenames: Union[_Path, Iterable[_Path]],
97             encoding: Optional[str] = ...) -> List[str]: ...
98    def read_file(self, f: Iterable[str], source: Optional[str] = ...) -> None: ...
99    def read_string(self, string: str, source: str = ...) -> None: ...
100    def read_dict(self, dictionary: Mapping[str, Mapping[str, Any]],
101                  source: str = ...) -> None: ...
102    def readfp(self, fp: Iterable[str], filename: Optional[str] = ...) -> None: ...
103
104    # These get* methods are partially applied (with the same names) in
105    # SectionProxy; the stubs should be kept updated together
106    def getint(self, section: str, option: str, *, raw: bool = ..., vars: Optional[_section] = ..., fallback: int = ...) -> int: ...
107
108    def getfloat(self, section: str, option: str, *, raw: bool = ..., vars: Optional[_section] = ..., fallback: float = ...) -> float: ...
109
110    def getboolean(self, section: str, option: str, *, raw: bool = ..., vars: Optional[_section] = ..., fallback: bool = ...) -> bool: ...
111
112    def _get_conv(self, section: str, option: str, conv: Callable[[str], _T], *, raw: bool = ..., vars: Optional[_section] = ..., fallback: _T = ...) -> _T: ...
113
114    # This is incompatible with MutableMapping so we ignore the type
115    @overload  # type: ignore
116    def get(self, section: str, option: str, *, raw: bool = ..., vars: Optional[_section] = ...) -> str: ...
117
118    @overload
119    def get(self, section: str, option: str, *, raw: bool = ..., vars: Optional[_section] = ..., fallback: _T) -> Union[str, _T]: ...
120
121    @overload
122    def items(self, *, raw: bool = ..., vars: Optional[_section] = ...) -> AbstractSet[Tuple[str, SectionProxy]]: ...
123
124    @overload
125    def items(self, section: str, raw: bool = ..., vars: Optional[_section] = ...) -> List[Tuple[str, str]]: ...
126
127    def set(self, section: str, option: str, value: str) -> None: ...
128
129    def write(self,
130              fileobject: IO[str],
131              space_around_delimiters: bool = ...) -> None: ...
132
133    def remove_option(self, section: str, option: str) -> bool: ...
134
135    def remove_section(self, section: str) -> bool: ...
136
137    def optionxform(self, option: str) -> str: ...
138
139
140class ConfigParser(RawConfigParser):
141    def __init__(self,
142                 defaults: Optional[_section] = ...,
143                 dict_type: Type[Mapping[str, str]] = ...,
144                 allow_no_value: bool = ...,
145                 delimiters: Sequence[str] = ...,
146                 comment_prefixes: Sequence[str] = ...,
147                 inline_comment_prefixes: Optional[Sequence[str]] = ...,
148                 strict: bool = ...,
149                 empty_lines_in_values: bool = ...,
150                 default_section: str = ...,
151                 interpolation: Optional[Interpolation] = ...,
152                 converters: _converters = ...) -> None: ...
153
154class SafeConfigParser(ConfigParser): ...
155
156class SectionProxy(MutableMapping[str, str]):
157    def __init__(self, parser: RawConfigParser, name: str) -> None: ...
158    def __getitem__(self, key: str) -> str: ...
159    def __setitem__(self, key: str, value: str) -> None: ...
160    def __delitem__(self, key: str) -> None: ...
161    def __contains__(self, key: object) -> bool: ...
162    def __len__(self) -> int: ...
163    def __iter__(self) -> Iterator[str]: ...
164    @property
165    def parser(self) -> RawConfigParser: ...
166    @property
167    def name(self) -> str: ...
168    def get(self, option: str, fallback: Optional[str] = ..., *, raw: bool = ..., vars: Optional[_section] = ..., **kwargs: Any) -> str: ...  # type: ignore
169
170    # These are partially-applied version of the methods with the same names in
171    # RawConfigParser; the stubs should be kept updated together
172    def getint(self, option: str, *, raw: bool = ..., vars: Optional[_section] = ..., fallback: int = ...) -> int: ...
173    def getfloat(self, option: str, *, raw: bool = ..., vars: Optional[_section] = ..., fallback: float = ...) -> float: ...
174    def getboolean(self, option: str, *, raw: bool = ..., vars: Optional[_section] = ..., fallback: bool = ...) -> bool: ...
175
176    # SectionProxy can have arbitrary attributes when custon converters are used
177    def __getattr__(self, key: str) -> Callable[..., Any]: ...
178
179class ConverterMapping(MutableMapping[str, Optional[_converter]]):
180    GETTERCRE: Pattern[Any]
181    def __init__(self, parser: RawConfigParser) -> None: ...
182    def __getitem__(self, key: str) -> _converter: ...
183    def __setitem__(self, key: str, value: Optional[_converter]) -> None: ...
184    def __delitem__(self, key: str) -> None: ...
185    def __iter__(self) -> Iterator[str]: ...
186    def __len__(self) -> int: ...
187
188
189class Error(Exception): ...
190
191
192class NoSectionError(Error): ...
193
194
195class DuplicateSectionError(Error):
196    section: str
197    source: Optional[str]
198    lineno: Optional[int]
199
200
201class DuplicateOptionError(Error):
202    section: str
203    option: str
204    source: Optional[str]
205    lineno: Optional[int]
206
207
208class NoOptionError(Error):
209    section: str
210    option: str
211
212
213class InterpolationError(Error):
214    section: str
215    option: str
216
217
218class InterpolationDepthError(InterpolationError): ...
219
220
221class InterpolationMissingOptionError(InterpolationError):
222    reference: str
223
224
225class InterpolationSyntaxError(InterpolationError): ...
226
227
228class ParsingError(Error):
229    source: str
230    errors: Sequence[Tuple[int, str]]
231
232
233class MissingSectionHeaderError(ParsingError):
234    lineno: int
235    line: str
236