__init__.py 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # flake8: noqa
  4. """Configuration file parser.
  5. A configuration file consists of sections, lead by a "[section]" header,
  6. and followed by "name: value" entries, with continuations and such in
  7. the style of RFC 822.
  8. Intrinsic defaults can be specified by passing them into the
  9. ConfigParser constructor as a dictionary.
  10. class:
  11. ConfigParser -- responsible for parsing a list of
  12. configuration files, and managing the parsed database.
  13. methods:
  14. __init__(defaults=None, dict_type=_default_dict, allow_no_value=False,
  15. delimiters=('=', ':'), comment_prefixes=('#', ';'),
  16. inline_comment_prefixes=None, strict=True,
  17. empty_lines_in_values=True, default_section='DEFAULT',
  18. interpolation=<unset>, converters=<unset>):
  19. Create the parser. When `defaults' is given, it is initialized into the
  20. dictionary or intrinsic defaults. The keys must be strings, the values
  21. must be appropriate for %()s string interpolation.
  22. When `dict_type' is given, it will be used to create the dictionary
  23. objects for the list of sections, for the options within a section, and
  24. for the default values.
  25. When `delimiters' is given, it will be used as the set of substrings
  26. that divide keys from values.
  27. When `comment_prefixes' is given, it will be used as the set of
  28. substrings that prefix comments in empty lines. Comments can be
  29. indented.
  30. When `inline_comment_prefixes' is given, it will be used as the set of
  31. substrings that prefix comments in non-empty lines.
  32. When `strict` is True, the parser won't allow for any section or option
  33. duplicates while reading from a single source (file, string or
  34. dictionary). Default is True.
  35. When `empty_lines_in_values' is False (default: True), each empty line
  36. marks the end of an option. Otherwise, internal empty lines of
  37. a multiline option are kept as part of the value.
  38. When `allow_no_value' is True (default: False), options without
  39. values are accepted; the value presented for these is None.
  40. sections()
  41. Return all the configuration section names, sans DEFAULT.
  42. has_section(section)
  43. Return whether the given section exists.
  44. has_option(section, option)
  45. Return whether the given option exists in the given section.
  46. options(section)
  47. Return list of configuration options for the named section.
  48. read(filenames, encoding=None)
  49. Read and parse the iterable of named configuration files, given by
  50. name. A single filename is also allowed. Non-existing files
  51. are ignored. Return list of successfully read files.
  52. read_file(f, filename=None)
  53. Read and parse one configuration file, given as a file object.
  54. The filename defaults to f.name; it is only used in error
  55. messages (if f has no `name' attribute, the string `<???>' is used).
  56. read_string(string)
  57. Read configuration from a given string.
  58. read_dict(dictionary)
  59. Read configuration from a dictionary. Keys are section names,
  60. values are dictionaries with keys and values that should be present
  61. in the section. If the used dictionary type preserves order, sections
  62. and their keys will be added in order. Values are automatically
  63. converted to strings.
  64. get(section, option, raw=False, vars=None, fallback=_UNSET)
  65. Return a string value for the named option. All % interpolations are
  66. expanded in the return values, based on the defaults passed into the
  67. constructor and the DEFAULT section. Additional substitutions may be
  68. provided using the `vars' argument, which must be a dictionary whose
  69. contents override any pre-existing defaults. If `option' is a key in
  70. `vars', the value from `vars' is used.
  71. getint(section, options, raw=False, vars=None, fallback=_UNSET)
  72. Like get(), but convert value to an integer.
  73. getfloat(section, options, raw=False, vars=None, fallback=_UNSET)
  74. Like get(), but convert value to a float.
  75. getboolean(section, options, raw=False, vars=None, fallback=_UNSET)
  76. Like get(), but convert value to a boolean (currently case
  77. insensitively defined as 0, false, no, off for False, and 1, true,
  78. yes, on for True). Returns False or True.
  79. items(section=_UNSET, raw=False, vars=None)
  80. If section is given, return a list of tuples with (name, value) for
  81. each option in the section. Otherwise, return a list of tuples with
  82. (section_name, section_proxy) for each section, including DEFAULTSECT.
  83. remove_section(section)
  84. Remove the given file section and all its options.
  85. remove_option(section, option)
  86. Remove the given option from the given section.
  87. set(section, option, value)
  88. Set the given option.
  89. write(fp, space_around_delimiters=True)
  90. Write the configuration state in .ini format. If
  91. `space_around_delimiters' is True (the default), delimiters
  92. between keys and values are surrounded by spaces.
  93. """
  94. from __future__ import absolute_import
  95. from __future__ import division
  96. from __future__ import print_function
  97. from __future__ import unicode_literals
  98. import functools
  99. import io
  100. import itertools
  101. import os
  102. import re
  103. import sys
  104. import warnings
  105. from backports.configparser.helpers import OrderedDict as _default_dict
  106. from backports.configparser.helpers import ChainMap as _ChainMap
  107. from backports.configparser.helpers import from_none, open, str, PY2
  108. from backports.configparser.helpers import PathLike, fspath
  109. from backports.configparser.helpers import MutableMapping
  110. __all__ = ["NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
  111. "NoOptionError", "InterpolationError", "InterpolationDepthError",
  112. "InterpolationMissingOptionError", "InterpolationSyntaxError",
  113. "ParsingError", "MissingSectionHeaderError",
  114. "ConfigParser", "SafeConfigParser", "RawConfigParser",
  115. "Interpolation", "BasicInterpolation", "ExtendedInterpolation",
  116. "LegacyInterpolation", "SectionProxy", "ConverterMapping",
  117. "DEFAULTSECT", "MAX_INTERPOLATION_DEPTH"]
  118. DEFAULTSECT = "DEFAULT"
  119. MAX_INTERPOLATION_DEPTH = 10
  120. # exception classes
  121. class Error(Exception):
  122. """Base class for ConfigParser exceptions."""
  123. def __init__(self, msg=''):
  124. self.message = msg
  125. Exception.__init__(self, msg)
  126. def __repr__(self):
  127. return self.message
  128. __str__ = __repr__
  129. class NoSectionError(Error):
  130. """Raised when no section matches a requested option."""
  131. def __init__(self, section):
  132. Error.__init__(self, 'No section: %r' % (section,))
  133. self.section = section
  134. self.args = (section, )
  135. class DuplicateSectionError(Error):
  136. """Raised when a section is repeated in an input source.
  137. Possible repetitions that raise this exception are: multiple creation
  138. using the API or in strict parsers when a section is found more than once
  139. in a single input file, string or dictionary.
  140. """
  141. def __init__(self, section, source=None, lineno=None):
  142. msg = [repr(section), " already exists"]
  143. if source is not None:
  144. message = ["While reading from ", repr(source)]
  145. if lineno is not None:
  146. message.append(" [line {0:2d}]".format(lineno))
  147. message.append(": section ")
  148. message.extend(msg)
  149. msg = message
  150. else:
  151. msg.insert(0, "Section ")
  152. Error.__init__(self, "".join(msg))
  153. self.section = section
  154. self.source = source
  155. self.lineno = lineno
  156. self.args = (section, source, lineno)
  157. class DuplicateOptionError(Error):
  158. """Raised by strict parsers when an option is repeated in an input source.
  159. Current implementation raises this exception only when an option is found
  160. more than once in a single file, string or dictionary.
  161. """
  162. def __init__(self, section, option, source=None, lineno=None):
  163. msg = [repr(option), " in section ", repr(section),
  164. " already exists"]
  165. if source is not None:
  166. message = ["While reading from ", repr(source)]
  167. if lineno is not None:
  168. message.append(" [line {0:2d}]".format(lineno))
  169. message.append(": option ")
  170. message.extend(msg)
  171. msg = message
  172. else:
  173. msg.insert(0, "Option ")
  174. Error.__init__(self, "".join(msg))
  175. self.section = section
  176. self.option = option
  177. self.source = source
  178. self.lineno = lineno
  179. self.args = (section, option, source, lineno)
  180. class NoOptionError(Error):
  181. """A requested option was not found."""
  182. def __init__(self, option, section):
  183. Error.__init__(self, "No option %r in section: %r" %
  184. (option, section))
  185. self.option = option
  186. self.section = section
  187. self.args = (option, section)
  188. class InterpolationError(Error):
  189. """Base class for interpolation-related exceptions."""
  190. def __init__(self, option, section, msg):
  191. Error.__init__(self, msg)
  192. self.option = option
  193. self.section = section
  194. self.args = (option, section, msg)
  195. class InterpolationMissingOptionError(InterpolationError):
  196. """A string substitution required a setting which was not available."""
  197. def __init__(self, option, section, rawval, reference):
  198. msg = ("Bad value substitution: option {0!r} in section {1!r} contains "
  199. "an interpolation key {2!r} which is not a valid option name. "
  200. "Raw value: {3!r}".format(option, section, reference, rawval))
  201. InterpolationError.__init__(self, option, section, msg)
  202. self.reference = reference
  203. self.args = (option, section, rawval, reference)
  204. class InterpolationSyntaxError(InterpolationError):
  205. """Raised when the source text contains invalid syntax.
  206. Current implementation raises this exception when the source text into
  207. which substitutions are made does not conform to the required syntax.
  208. """
  209. class InterpolationDepthError(InterpolationError):
  210. """Raised when substitutions are nested too deeply."""
  211. def __init__(self, option, section, rawval):
  212. msg = ("Recursion limit exceeded in value substitution: option {0!r} "
  213. "in section {1!r} contains an interpolation key which "
  214. "cannot be substituted in {2} steps. Raw value: {3!r}"
  215. "".format(option, section, MAX_INTERPOLATION_DEPTH,
  216. rawval))
  217. InterpolationError.__init__(self, option, section, msg)
  218. self.args = (option, section, rawval)
  219. class ParsingError(Error):
  220. """Raised when a configuration file does not follow legal syntax."""
  221. def __init__(self, source=None, filename=None):
  222. # Exactly one of `source'/`filename' arguments has to be given.
  223. # `filename' kept for compatibility.
  224. if filename and source:
  225. raise ValueError("Cannot specify both `filename' and `source'. "
  226. "Use `source'.")
  227. elif not filename and not source:
  228. raise ValueError("Required argument `source' not given.")
  229. elif filename:
  230. source = filename
  231. Error.__init__(self, 'Source contains parsing errors: %r' % source)
  232. self.source = source
  233. self.errors = []
  234. self.args = (source, )
  235. @property
  236. def filename(self):
  237. """Deprecated, use `source'."""
  238. warnings.warn(
  239. "The 'filename' attribute will be removed in future versions. "
  240. "Use 'source' instead.",
  241. DeprecationWarning, stacklevel=2
  242. )
  243. return self.source
  244. @filename.setter
  245. def filename(self, value):
  246. """Deprecated, user `source'."""
  247. warnings.warn(
  248. "The 'filename' attribute will be removed in future versions. "
  249. "Use 'source' instead.",
  250. DeprecationWarning, stacklevel=2
  251. )
  252. self.source = value
  253. def append(self, lineno, line):
  254. self.errors.append((lineno, line))
  255. self.message += '\n\t[line %2d]: %s' % (lineno, line)
  256. class MissingSectionHeaderError(ParsingError):
  257. """Raised when a key-value pair is found before any section header."""
  258. def __init__(self, filename, lineno, line):
  259. Error.__init__(
  260. self,
  261. 'File contains no section headers.\nfile: %r, line: %d\n%r' %
  262. (filename, lineno, line))
  263. self.source = filename
  264. self.lineno = lineno
  265. self.line = line
  266. self.args = (filename, lineno, line)
  267. # Used in parser getters to indicate the default behaviour when a specific
  268. # option is not found it to raise an exception. Created to enable `None' as
  269. # a valid fallback value.
  270. _UNSET = object()
  271. class Interpolation(object):
  272. """Dummy interpolation that passes the value through with no changes."""
  273. def before_get(self, parser, section, option, value, defaults):
  274. return value
  275. def before_set(self, parser, section, option, value):
  276. return value
  277. def before_read(self, parser, section, option, value):
  278. return value
  279. def before_write(self, parser, section, option, value):
  280. return value
  281. class BasicInterpolation(Interpolation):
  282. """Interpolation as implemented in the classic ConfigParser.
  283. The option values can contain format strings which refer to other values in
  284. the same section, or values in the special default section.
  285. For example:
  286. something: %(dir)s/whatever
  287. would resolve the "%(dir)s" to the value of dir. All reference
  288. expansions are done late, on demand. If a user needs to use a bare % in
  289. a configuration file, she can escape it by writing %%. Other % usage
  290. is considered a user error and raises `InterpolationSyntaxError'."""
  291. _KEYCRE = re.compile(r"%\(([^)]+)\)s")
  292. def before_get(self, parser, section, option, value, defaults):
  293. L = []
  294. self._interpolate_some(parser, option, L, value, section, defaults, 1)
  295. return ''.join(L)
  296. def before_set(self, parser, section, option, value):
  297. tmp_value = value.replace('%%', '') # escaped percent signs
  298. tmp_value = self._KEYCRE.sub('', tmp_value) # valid syntax
  299. if '%' in tmp_value:
  300. raise ValueError("invalid interpolation syntax in %r at "
  301. "position %d" % (value, tmp_value.find('%')))
  302. return value
  303. def _interpolate_some(self, parser, option, accum, rest, section, map,
  304. depth):
  305. rawval = parser.get(section, option, raw=True, fallback=rest)
  306. if depth > MAX_INTERPOLATION_DEPTH:
  307. raise InterpolationDepthError(option, section, rawval)
  308. while rest:
  309. p = rest.find("%")
  310. if p < 0:
  311. accum.append(rest)
  312. return
  313. if p > 0:
  314. accum.append(rest[:p])
  315. rest = rest[p:]
  316. # p is no longer used
  317. c = rest[1:2]
  318. if c == "%":
  319. accum.append("%")
  320. rest = rest[2:]
  321. elif c == "(":
  322. m = self._KEYCRE.match(rest)
  323. if m is None:
  324. raise InterpolationSyntaxError(option, section,
  325. "bad interpolation variable reference %r" % rest)
  326. var = parser.optionxform(m.group(1))
  327. rest = rest[m.end():]
  328. try:
  329. v = map[var]
  330. except KeyError:
  331. raise from_none(InterpolationMissingOptionError(
  332. option, section, rawval, var))
  333. if "%" in v:
  334. self._interpolate_some(parser, option, accum, v,
  335. section, map, depth + 1)
  336. else:
  337. accum.append(v)
  338. else:
  339. raise InterpolationSyntaxError(
  340. option, section,
  341. "'%%' must be followed by '%%' or '(', "
  342. "found: %r" % (rest,))
  343. class ExtendedInterpolation(Interpolation):
  344. """Advanced variant of interpolation, supports the syntax used by
  345. `zc.buildout'. Enables interpolation between sections."""
  346. _KEYCRE = re.compile(r"\$\{([^}]+)\}")
  347. def before_get(self, parser, section, option, value, defaults):
  348. L = []
  349. self._interpolate_some(parser, option, L, value, section, defaults, 1)
  350. return ''.join(L)
  351. def before_set(self, parser, section, option, value):
  352. tmp_value = value.replace('$$', '') # escaped dollar signs
  353. tmp_value = self._KEYCRE.sub('', tmp_value) # valid syntax
  354. if '$' in tmp_value:
  355. raise ValueError("invalid interpolation syntax in %r at "
  356. "position %d" % (value, tmp_value.find('$')))
  357. return value
  358. def _interpolate_some(self, parser, option, accum, rest, section, map,
  359. depth):
  360. rawval = parser.get(section, option, raw=True, fallback=rest)
  361. if depth > MAX_INTERPOLATION_DEPTH:
  362. raise InterpolationDepthError(option, section, rawval)
  363. while rest:
  364. p = rest.find("$")
  365. if p < 0:
  366. accum.append(rest)
  367. return
  368. if p > 0:
  369. accum.append(rest[:p])
  370. rest = rest[p:]
  371. # p is no longer used
  372. c = rest[1:2]
  373. if c == "$":
  374. accum.append("$")
  375. rest = rest[2:]
  376. elif c == "{":
  377. m = self._KEYCRE.match(rest)
  378. if m is None:
  379. raise InterpolationSyntaxError(option, section,
  380. "bad interpolation variable reference %r" % rest)
  381. path = m.group(1).split(':')
  382. rest = rest[m.end():]
  383. sect = section
  384. opt = option
  385. try:
  386. if len(path) == 1:
  387. opt = parser.optionxform(path[0])
  388. v = map[opt]
  389. elif len(path) == 2:
  390. sect = path[0]
  391. opt = parser.optionxform(path[1])
  392. v = parser.get(sect, opt, raw=True)
  393. else:
  394. raise InterpolationSyntaxError(
  395. option, section,
  396. "More than one ':' found: %r" % (rest,))
  397. except (KeyError, NoSectionError, NoOptionError):
  398. raise from_none(InterpolationMissingOptionError(
  399. option, section, rawval, ":".join(path)))
  400. if "$" in v:
  401. self._interpolate_some(parser, opt, accum, v, sect,
  402. dict(parser.items(sect, raw=True)),
  403. depth + 1)
  404. else:
  405. accum.append(v)
  406. else:
  407. raise InterpolationSyntaxError(
  408. option, section,
  409. "'$' must be followed by '$' or '{', "
  410. "found: %r" % (rest,))
  411. class LegacyInterpolation(Interpolation):
  412. """Deprecated interpolation used in old versions of ConfigParser.
  413. Use BasicInterpolation or ExtendedInterpolation instead."""
  414. _KEYCRE = re.compile(r"%\(([^)]*)\)s|.")
  415. def before_get(self, parser, section, option, value, vars):
  416. rawval = value
  417. depth = MAX_INTERPOLATION_DEPTH
  418. while depth: # Loop through this until it's done
  419. depth -= 1
  420. if value and "%(" in value:
  421. replace = functools.partial(self._interpolation_replace,
  422. parser=parser)
  423. value = self._KEYCRE.sub(replace, value)
  424. try:
  425. value = value % vars
  426. except KeyError as e:
  427. raise from_none(InterpolationMissingOptionError(
  428. option, section, rawval, e.args[0]))
  429. else:
  430. break
  431. if value and "%(" in value:
  432. raise InterpolationDepthError(option, section, rawval)
  433. return value
  434. def before_set(self, parser, section, option, value):
  435. return value
  436. @staticmethod
  437. def _interpolation_replace(match, parser):
  438. s = match.group(1)
  439. if s is None:
  440. return match.group()
  441. else:
  442. return "%%(%s)s" % parser.optionxform(s)
  443. class RawConfigParser(MutableMapping):
  444. """ConfigParser that does not do interpolation."""
  445. # Regular expressions for parsing section headers and options
  446. _SECT_TMPL = r"""
  447. \[ # [
  448. (?P<header>[^]]+) # very permissive!
  449. \] # ]
  450. """
  451. _OPT_TMPL = r"""
  452. (?P<option>.*?) # very permissive!
  453. \s*(?P<vi>{delim})\s* # any number of space/tab,
  454. # followed by any of the
  455. # allowed delimiters,
  456. # followed by any space/tab
  457. (?P<value>.*)$ # everything up to eol
  458. """
  459. _OPT_NV_TMPL = r"""
  460. (?P<option>.*?) # very permissive!
  461. \s*(?: # any number of space/tab,
  462. (?P<vi>{delim})\s* # optionally followed by
  463. # any of the allowed
  464. # delimiters, followed by any
  465. # space/tab
  466. (?P<value>.*))?$ # everything up to eol
  467. """
  468. # Interpolation algorithm to be used if the user does not specify another
  469. _DEFAULT_INTERPOLATION = Interpolation()
  470. # Compiled regular expression for matching sections
  471. SECTCRE = re.compile(_SECT_TMPL, re.VERBOSE)
  472. # Compiled regular expression for matching options with typical separators
  473. OPTCRE = re.compile(_OPT_TMPL.format(delim="=|:"), re.VERBOSE)
  474. # Compiled regular expression for matching options with optional values
  475. # delimited using typical separators
  476. OPTCRE_NV = re.compile(_OPT_NV_TMPL.format(delim="=|:"), re.VERBOSE)
  477. # Compiled regular expression for matching leading whitespace in a line
  478. NONSPACECRE = re.compile(r"\S")
  479. # Possible boolean values in the configuration.
  480. BOOLEAN_STATES = {'1': True, 'yes': True, 'true': True, 'on': True,
  481. '0': False, 'no': False, 'false': False, 'off': False}
  482. def __init__(self, defaults=None, dict_type=_default_dict,
  483. allow_no_value=False, **kwargs):
  484. # keyword-only arguments
  485. delimiters = kwargs.get('delimiters', ('=', ':'))
  486. comment_prefixes = kwargs.get('comment_prefixes', ('#', ';'))
  487. inline_comment_prefixes = kwargs.get('inline_comment_prefixes', None)
  488. strict = kwargs.get('strict', True)
  489. empty_lines_in_values = kwargs.get('empty_lines_in_values', True)
  490. default_section = kwargs.get('default_section', DEFAULTSECT)
  491. interpolation = kwargs.get('interpolation', _UNSET)
  492. converters = kwargs.get('converters', _UNSET)
  493. self._dict = dict_type
  494. self._sections = self._dict()
  495. self._defaults = self._dict()
  496. self._converters = ConverterMapping(self)
  497. self._proxies = self._dict()
  498. self._proxies[default_section] = SectionProxy(self, default_section)
  499. self._delimiters = tuple(delimiters)
  500. if delimiters == ('=', ':'):
  501. self._optcre = self.OPTCRE_NV if allow_no_value else self.OPTCRE
  502. else:
  503. d = "|".join(re.escape(d) for d in delimiters)
  504. if allow_no_value:
  505. self._optcre = re.compile(self._OPT_NV_TMPL.format(delim=d),
  506. re.VERBOSE)
  507. else:
  508. self._optcre = re.compile(self._OPT_TMPL.format(delim=d),
  509. re.VERBOSE)
  510. self._comment_prefixes = tuple(comment_prefixes or ())
  511. self._inline_comment_prefixes = tuple(inline_comment_prefixes or ())
  512. self._strict = strict
  513. self._allow_no_value = allow_no_value
  514. self._empty_lines_in_values = empty_lines_in_values
  515. self.default_section=default_section
  516. self._interpolation = interpolation
  517. if self._interpolation is _UNSET:
  518. self._interpolation = self._DEFAULT_INTERPOLATION
  519. if self._interpolation is None:
  520. self._interpolation = Interpolation()
  521. if converters is not _UNSET:
  522. self._converters.update(converters)
  523. if defaults:
  524. self._read_defaults(defaults)
  525. def defaults(self):
  526. return self._defaults
  527. def sections(self):
  528. """Return a list of section names, excluding [DEFAULT]"""
  529. # self._sections will never have [DEFAULT] in it
  530. return list(self._sections.keys())
  531. def add_section(self, section):
  532. """Create a new section in the configuration.
  533. Raise DuplicateSectionError if a section by the specified name
  534. already exists. Raise ValueError if name is DEFAULT.
  535. """
  536. if section == self.default_section:
  537. raise ValueError('Invalid section name: %r' % section)
  538. if section in self._sections:
  539. raise DuplicateSectionError(section)
  540. self._sections[section] = self._dict()
  541. self._proxies[section] = SectionProxy(self, section)
  542. def has_section(self, section):
  543. """Indicate whether the named section is present in the configuration.
  544. The DEFAULT section is not acknowledged.
  545. """
  546. return section in self._sections
  547. def options(self, section):
  548. """Return a list of option names for the given section name."""
  549. try:
  550. opts = self._sections[section].copy()
  551. except KeyError:
  552. raise from_none(NoSectionError(section))
  553. opts.update(self._defaults)
  554. return list(opts.keys())
  555. def read(self, filenames, encoding=None):
  556. """Read and parse a filename or an iterable of filenames.
  557. Files that cannot be opened are silently ignored; this is
  558. designed so that you can specify an iterable of potential
  559. configuration file locations (e.g. current directory, user's
  560. home directory, systemwide directory), and all existing
  561. configuration files in the iterable will be read. A single
  562. filename may also be given.
  563. Return list of successfully read files.
  564. """
  565. if isinstance(filenames, (str, bytes, PathLike)):
  566. filenames = [filenames]
  567. read_ok = []
  568. for filename in filenames:
  569. if isinstance(filename, PathLike):
  570. filename = fspath(filename)
  571. try:
  572. with open(filename, encoding=encoding) as fp:
  573. self._read(fp, filename)
  574. except IOError:
  575. continue
  576. read_ok.append(filename)
  577. return read_ok
  578. def read_file(self, f, source=None):
  579. """Like read() but the argument must be a file-like object.
  580. The `f' argument must be iterable, returning one line at a time.
  581. Optional second argument is the `source' specifying the name of the
  582. file being read. If not given, it is taken from f.name. If `f' has no
  583. `name' attribute, `<???>' is used.
  584. """
  585. if source is None:
  586. try:
  587. source = f.name
  588. except AttributeError:
  589. source = '<???>'
  590. self._read(f, source)
  591. def read_string(self, string, source='<string>'):
  592. """Read configuration from a given string."""
  593. sfile = io.StringIO(string)
  594. self.read_file(sfile, source)
  595. def read_dict(self, dictionary, source='<dict>'):
  596. """Read configuration from a dictionary.
  597. Keys are section names, values are dictionaries with keys and values
  598. that should be present in the section. If the used dictionary type
  599. preserves order, sections and their keys will be added in order.
  600. All types held in the dictionary are converted to strings during
  601. reading, including section names, option names and keys.
  602. Optional second argument is the `source' specifying the name of the
  603. dictionary being read.
  604. """
  605. elements_added = set()
  606. for section, keys in dictionary.items():
  607. section = str(section)
  608. try:
  609. self.add_section(section)
  610. except (DuplicateSectionError, ValueError):
  611. if self._strict and section in elements_added:
  612. raise
  613. elements_added.add(section)
  614. for key, value in keys.items():
  615. key = self.optionxform(str(key))
  616. if value is not None:
  617. value = str(value)
  618. if self._strict and (section, key) in elements_added:
  619. raise DuplicateOptionError(section, key, source)
  620. elements_added.add((section, key))
  621. self.set(section, key, value)
  622. def readfp(self, fp, filename=None):
  623. """Deprecated, use read_file instead."""
  624. warnings.warn(
  625. "This method will be removed in future versions. "
  626. "Use 'parser.read_file()' instead.",
  627. DeprecationWarning, stacklevel=2
  628. )
  629. self.read_file(fp, source=filename)
  630. def get(self, section, option, **kwargs):
  631. """Get an option value for a given section.
  632. If `vars' is provided, it must be a dictionary. The option is looked up
  633. in `vars' (if provided), `section', and in `DEFAULTSECT' in that order.
  634. If the key is not found and `fallback' is provided, it is used as
  635. a fallback value. `None' can be provided as a `fallback' value.
  636. If interpolation is enabled and the optional argument `raw' is False,
  637. all interpolations are expanded in the return values.
  638. Arguments `raw', `vars', and `fallback' are keyword only.
  639. The section DEFAULT is special.
  640. """
  641. # keyword-only arguments
  642. raw = kwargs.get('raw', False)
  643. vars = kwargs.get('vars', None)
  644. fallback = kwargs.get('fallback', _UNSET)
  645. try:
  646. d = self._unify_values(section, vars)
  647. except NoSectionError:
  648. if fallback is _UNSET:
  649. raise
  650. else:
  651. return fallback
  652. option = self.optionxform(option)
  653. try:
  654. value = d[option]
  655. except KeyError:
  656. if fallback is _UNSET:
  657. raise NoOptionError(option, section)
  658. else:
  659. return fallback
  660. if raw or value is None:
  661. return value
  662. else:
  663. return self._interpolation.before_get(self, section, option, value,
  664. d)
  665. def _get(self, section, conv, option, **kwargs):
  666. return conv(self.get(section, option, **kwargs))
  667. def _get_conv(self, section, option, conv, **kwargs):
  668. # keyword-only arguments
  669. kwargs.setdefault('raw', False)
  670. kwargs.setdefault('vars', None)
  671. fallback = kwargs.pop('fallback', _UNSET)
  672. try:
  673. return self._get(section, conv, option, **kwargs)
  674. except (NoSectionError, NoOptionError):
  675. if fallback is _UNSET:
  676. raise
  677. return fallback
  678. # getint, getfloat and getboolean provided directly for backwards compat
  679. def getint(self, section, option, **kwargs):
  680. # keyword-only arguments
  681. kwargs.setdefault('raw', False)
  682. kwargs.setdefault('vars', None)
  683. kwargs.setdefault('fallback', _UNSET)
  684. return self._get_conv(section, option, int, **kwargs)
  685. def getfloat(self, section, option, **kwargs):
  686. # keyword-only arguments
  687. kwargs.setdefault('raw', False)
  688. kwargs.setdefault('vars', None)
  689. kwargs.setdefault('fallback', _UNSET)
  690. return self._get_conv(section, option, float, **kwargs)
  691. def getboolean(self, section, option, **kwargs):
  692. # keyword-only arguments
  693. kwargs.setdefault('raw', False)
  694. kwargs.setdefault('vars', None)
  695. kwargs.setdefault('fallback', _UNSET)
  696. return self._get_conv(section, option, self._convert_to_boolean,
  697. **kwargs)
  698. def items(self, section=_UNSET, raw=False, vars=None):
  699. """Return a list of (name, value) tuples for each option in a section.
  700. All % interpolations are expanded in the return values, based on the
  701. defaults passed into the constructor, unless the optional argument
  702. `raw' is true. Additional substitutions may be provided using the
  703. `vars' argument, which must be a dictionary whose contents overrides
  704. any pre-existing defaults.
  705. The section DEFAULT is special.
  706. """
  707. if section is _UNSET:
  708. return super(RawConfigParser, self).items()
  709. d = self._defaults.copy()
  710. try:
  711. d.update(self._sections[section])
  712. except KeyError:
  713. if section != self.default_section:
  714. raise NoSectionError(section)
  715. # Update with the entry specific variables
  716. if vars:
  717. for key, value in vars.items():
  718. d[self.optionxform(key)] = value
  719. value_getter = lambda option: self._interpolation.before_get(self,
  720. section, option, d[option], d)
  721. if raw:
  722. value_getter = lambda option: d[option]
  723. return [(option, value_getter(option)) for option in d.keys()]
  724. def popitem(self):
  725. """Remove a section from the parser and return it as
  726. a (section_name, section_proxy) tuple. If no section is present, raise
  727. KeyError.
  728. The section DEFAULT is never returned because it cannot be removed.
  729. """
  730. for key in self.sections():
  731. value = self[key]
  732. del self[key]
  733. return key, value
  734. raise KeyError
  735. def optionxform(self, optionstr):
  736. return optionstr.lower()
  737. def has_option(self, section, option):
  738. """Check for the existence of a given option in a given section.
  739. If the specified `section' is None or an empty string, DEFAULT is
  740. assumed. If the specified `section' does not exist, returns False."""
  741. if not section or section == self.default_section:
  742. option = self.optionxform(option)
  743. return option in self._defaults
  744. elif section not in self._sections:
  745. return False
  746. else:
  747. option = self.optionxform(option)
  748. return (option in self._sections[section]
  749. or option in self._defaults)
  750. def set(self, section, option, value=None):
  751. """Set an option."""
  752. if value:
  753. value = self._interpolation.before_set(self, section, option,
  754. value)
  755. if not section or section == self.default_section:
  756. sectdict = self._defaults
  757. else:
  758. try:
  759. sectdict = self._sections[section]
  760. except KeyError:
  761. raise from_none(NoSectionError(section))
  762. sectdict[self.optionxform(option)] = value
  763. def write(self, fp, space_around_delimiters=True):
  764. """Write an .ini-format representation of the configuration state.
  765. If `space_around_delimiters' is True (the default), delimiters
  766. between keys and values are surrounded by spaces.
  767. """
  768. if space_around_delimiters:
  769. d = " {0} ".format(self._delimiters[0])
  770. else:
  771. d = self._delimiters[0]
  772. if self._defaults:
  773. self._write_section(fp, self.default_section,
  774. self._defaults.items(), d)
  775. for section in self._sections:
  776. self._write_section(fp, section,
  777. self._sections[section].items(), d)
  778. def _write_section(self, fp, section_name, section_items, delimiter):
  779. """Write a single section to the specified `fp'."""
  780. fp.write("[{0}]\n".format(section_name))
  781. for key, value in section_items:
  782. value = self._interpolation.before_write(self, section_name, key,
  783. value)
  784. if value is not None or not self._allow_no_value:
  785. value = delimiter + str(value).replace('\n', '\n\t')
  786. else:
  787. value = ""
  788. fp.write("{0}{1}\n".format(key, value))
  789. fp.write("\n")
  790. def remove_option(self, section, option):
  791. """Remove an option."""
  792. if not section or section == self.default_section:
  793. sectdict = self._defaults
  794. else:
  795. try:
  796. sectdict = self._sections[section]
  797. except KeyError:
  798. raise from_none(NoSectionError(section))
  799. option = self.optionxform(option)
  800. existed = option in sectdict
  801. if existed:
  802. del sectdict[option]
  803. return existed
  804. def remove_section(self, section):
  805. """Remove a file section."""
  806. existed = section in self._sections
  807. if existed:
  808. del self._sections[section]
  809. del self._proxies[section]
  810. return existed
  811. def __getitem__(self, key):
  812. if key != self.default_section and not self.has_section(key):
  813. raise KeyError(key)
  814. return self._proxies[key]
  815. def __setitem__(self, key, value):
  816. # To conform with the mapping protocol, overwrites existing values in
  817. # the section.
  818. # XXX this is not atomic if read_dict fails at any point. Then again,
  819. # no update method in configparser is atomic in this implementation.
  820. if key == self.default_section:
  821. self._defaults.clear()
  822. elif key in self._sections:
  823. self._sections[key].clear()
  824. self.read_dict({key: value})
  825. def __delitem__(self, key):
  826. if key == self.default_section:
  827. raise ValueError("Cannot remove the default section.")
  828. if not self.has_section(key):
  829. raise KeyError(key)
  830. self.remove_section(key)
  831. def __contains__(self, key):
  832. return key == self.default_section or self.has_section(key)
  833. def __len__(self):
  834. return len(self._sections) + 1 # the default section
  835. def __iter__(self):
  836. # XXX does it break when underlying container state changed?
  837. return itertools.chain((self.default_section,), self._sections.keys())
  838. def _read(self, fp, fpname):
  839. """Parse a sectioned configuration file.
  840. Each section in a configuration file contains a header, indicated by
  841. a name in square brackets (`[]'), plus key/value options, indicated by
  842. `name' and `value' delimited with a specific substring (`=' or `:' by
  843. default).
  844. Values can span multiple lines, as long as they are indented deeper
  845. than the first line of the value. Depending on the parser's mode, blank
  846. lines may be treated as parts of multiline values or ignored.
  847. Configuration files may include comments, prefixed by specific
  848. characters (`#' and `;' by default). Comments may appear on their own
  849. in an otherwise empty line or may be entered in lines holding values or
  850. section names.
  851. """
  852. elements_added = set()
  853. cursect = None # None, or a dictionary
  854. sectname = None
  855. optname = None
  856. lineno = 0
  857. indent_level = 0
  858. e = None # None, or an exception
  859. for lineno, line in enumerate(fp, start=1):
  860. comment_start = sys.maxsize
  861. # strip inline comments
  862. inline_prefixes = dict(
  863. (p, -1) for p in self._inline_comment_prefixes)
  864. while comment_start == sys.maxsize and inline_prefixes:
  865. next_prefixes = {}
  866. for prefix, index in inline_prefixes.items():
  867. index = line.find(prefix, index+1)
  868. if index == -1:
  869. continue
  870. next_prefixes[prefix] = index
  871. if index == 0 or (index > 0 and line[index-1].isspace()):
  872. comment_start = min(comment_start, index)
  873. inline_prefixes = next_prefixes
  874. # strip full line comments
  875. for prefix in self._comment_prefixes:
  876. if line.strip().startswith(prefix):
  877. comment_start = 0
  878. break
  879. if comment_start == sys.maxsize:
  880. comment_start = None
  881. value = line[:comment_start].strip()
  882. if not value:
  883. if self._empty_lines_in_values:
  884. # add empty line to the value, but only if there was no
  885. # comment on the line
  886. if (comment_start is None and
  887. cursect is not None and
  888. optname and
  889. cursect[optname] is not None):
  890. cursect[optname].append('') # newlines added at join
  891. else:
  892. # empty line marks end of value
  893. indent_level = sys.maxsize
  894. continue
  895. # continuation line?
  896. first_nonspace = self.NONSPACECRE.search(line)
  897. cur_indent_level = first_nonspace.start() if first_nonspace else 0
  898. if (cursect is not None and optname and
  899. cur_indent_level > indent_level):
  900. cursect[optname].append(value)
  901. # a section header or option header?
  902. else:
  903. indent_level = cur_indent_level
  904. # is it a section header?
  905. mo = self.SECTCRE.match(value)
  906. if mo:
  907. sectname = mo.group('header')
  908. if sectname in self._sections:
  909. if self._strict and sectname in elements_added:
  910. raise DuplicateSectionError(sectname, fpname,
  911. lineno)
  912. cursect = self._sections[sectname]
  913. elements_added.add(sectname)
  914. elif sectname == self.default_section:
  915. cursect = self._defaults
  916. else:
  917. cursect = self._dict()
  918. self._sections[sectname] = cursect
  919. self._proxies[sectname] = SectionProxy(self, sectname)
  920. elements_added.add(sectname)
  921. # So sections can't start with a continuation line
  922. optname = None
  923. # no section header in the file?
  924. elif cursect is None:
  925. raise MissingSectionHeaderError(fpname, lineno, line)
  926. # an option line?
  927. else:
  928. mo = self._optcre.match(value)
  929. if mo:
  930. optname, vi, optval = mo.group('option', 'vi', 'value')
  931. if not optname:
  932. e = self._handle_error(e, fpname, lineno, line)
  933. optname = self.optionxform(optname.rstrip())
  934. if (self._strict and
  935. (sectname, optname) in elements_added):
  936. raise DuplicateOptionError(sectname, optname,
  937. fpname, lineno)
  938. elements_added.add((sectname, optname))
  939. # This check is fine because the OPTCRE cannot
  940. # match if it would set optval to None
  941. if optval is not None:
  942. optval = optval.strip()
  943. cursect[optname] = [optval]
  944. else:
  945. # valueless option handling
  946. cursect[optname] = None
  947. else:
  948. # a non-fatal parsing error occurred. set up the
  949. # exception but keep going. the exception will be
  950. # raised at the end of the file and will contain a
  951. # list of all bogus lines
  952. e = self._handle_error(e, fpname, lineno, line)
  953. self._join_multiline_values()
  954. # if any parsing errors occurred, raise an exception
  955. if e:
  956. raise e
  957. def _join_multiline_values(self):
  958. defaults = self.default_section, self._defaults
  959. all_sections = itertools.chain((defaults,),
  960. self._sections.items())
  961. for section, options in all_sections:
  962. for name, val in options.items():
  963. if isinstance(val, list):
  964. val = '\n'.join(val).rstrip()
  965. options[name] = self._interpolation.before_read(self,
  966. section,
  967. name, val)
  968. def _read_defaults(self, defaults):
  969. """Read the defaults passed in the initializer.
  970. Note: values can be non-string."""
  971. for key, value in defaults.items():
  972. self._defaults[self.optionxform(key)] = value
  973. def _handle_error(self, exc, fpname, lineno, line):
  974. if not exc:
  975. exc = ParsingError(fpname)
  976. exc.append(lineno, repr(line))
  977. return exc
  978. def _unify_values(self, section, vars):
  979. """Create a sequence of lookups with 'vars' taking priority over
  980. the 'section' which takes priority over the DEFAULTSECT.
  981. """
  982. sectiondict = {}
  983. try:
  984. sectiondict = self._sections[section]
  985. except KeyError:
  986. if section != self.default_section:
  987. raise NoSectionError(section)
  988. # Update with the entry specific variables
  989. vardict = {}
  990. if vars:
  991. for key, value in vars.items():
  992. if value is not None:
  993. value = str(value)
  994. vardict[self.optionxform(key)] = value
  995. return _ChainMap(vardict, sectiondict, self._defaults)
  996. def _convert_to_boolean(self, value):
  997. """Return a boolean value translating from other types if necessary.
  998. """
  999. if value.lower() not in self.BOOLEAN_STATES:
  1000. raise ValueError('Not a boolean: %s' % value)
  1001. return self.BOOLEAN_STATES[value.lower()]
  1002. def _validate_value_types(self, **kwargs):
  1003. """Raises a TypeError for non-string values.
  1004. The only legal non-string value if we allow valueless
  1005. options is None, so we need to check if the value is a
  1006. string if:
  1007. - we do not allow valueless options, or
  1008. - we allow valueless options but the value is not None
  1009. For compatibility reasons this method is not used in classic set()
  1010. for RawConfigParsers. It is invoked in every case for mapping protocol
  1011. access and in ConfigParser.set().
  1012. """
  1013. # keyword-only arguments
  1014. section = kwargs.get('section', "")
  1015. option = kwargs.get('option', "")
  1016. value = kwargs.get('value', "")
  1017. if PY2 and bytes in (type(section), type(option), type(value)):
  1018. # we allow for a little unholy magic for Python 2 so that
  1019. # people not using unicode_literals can still use the library
  1020. # conveniently
  1021. warnings.warn(
  1022. "You passed a bytestring. Implicitly decoding as UTF-8 string."
  1023. " This will not work on Python 3. Please switch to using"
  1024. " Unicode strings across the board.",
  1025. DeprecationWarning,
  1026. stacklevel=2,
  1027. )
  1028. if isinstance(section, bytes):
  1029. section = section.decode('utf8')
  1030. if isinstance(option, bytes):
  1031. option = option.decode('utf8')
  1032. if isinstance(value, bytes):
  1033. value = value.decode('utf8')
  1034. if not isinstance(section, str):
  1035. raise TypeError("section names must be strings")
  1036. if not isinstance(option, str):
  1037. raise TypeError("option keys must be strings")
  1038. if not self._allow_no_value or value:
  1039. if not isinstance(value, str):
  1040. raise TypeError("option values must be strings")
  1041. return section, option, value
  1042. @property
  1043. def converters(self):
  1044. return self._converters
  1045. class ConfigParser(RawConfigParser):
  1046. """ConfigParser implementing interpolation."""
  1047. _DEFAULT_INTERPOLATION = BasicInterpolation()
  1048. def set(self, section, option, value=None):
  1049. """Set an option. Extends RawConfigParser.set by validating type and
  1050. interpolation syntax on the value."""
  1051. _, option, value = self._validate_value_types(option=option, value=value)
  1052. super(ConfigParser, self).set(section, option, value)
  1053. def add_section(self, section):
  1054. """Create a new section in the configuration. Extends
  1055. RawConfigParser.add_section by validating if the section name is
  1056. a string."""
  1057. section, _, _ = self._validate_value_types(section=section)
  1058. super(ConfigParser, self).add_section(section)
  1059. def _read_defaults(self, defaults):
  1060. """Reads the defaults passed in the initializer, implicitly converting
  1061. values to strings like the rest of the API.
  1062. Does not perform interpolation for backwards compatibility.
  1063. """
  1064. try:
  1065. hold_interpolation = self._interpolation
  1066. self._interpolation = Interpolation()
  1067. self.read_dict({self.default_section: defaults})
  1068. finally:
  1069. self._interpolation = hold_interpolation
  1070. class SafeConfigParser(ConfigParser):
  1071. """ConfigParser alias for backwards compatibility purposes."""
  1072. def __init__(self, *args, **kwargs):
  1073. super(SafeConfigParser, self).__init__(*args, **kwargs)
  1074. warnings.warn(
  1075. "The SafeConfigParser class has been renamed to ConfigParser "
  1076. "in Python 3.2. This alias will be removed in future versions."
  1077. " Use ConfigParser directly instead.",
  1078. DeprecationWarning, stacklevel=2
  1079. )
  1080. class SectionProxy(MutableMapping):
  1081. """A proxy for a single section from a parser."""
  1082. def __init__(self, parser, name):
  1083. """Creates a view on a section of the specified `name` in `parser`."""
  1084. self._parser = parser
  1085. self._name = name
  1086. for conv in parser.converters:
  1087. key = 'get' + conv
  1088. getter = functools.partial(self.get, _impl=getattr(parser, key))
  1089. setattr(self, key, getter)
  1090. def __repr__(self):
  1091. return '<Section: {0}>'.format(self._name)
  1092. def __getitem__(self, key):
  1093. if not self._parser.has_option(self._name, key):
  1094. raise KeyError(key)
  1095. return self._parser.get(self._name, key)
  1096. def __setitem__(self, key, value):
  1097. _, key, value = self._parser._validate_value_types(option=key, value=value)
  1098. return self._parser.set(self._name, key, value)
  1099. def __delitem__(self, key):
  1100. if not (self._parser.has_option(self._name, key) and
  1101. self._parser.remove_option(self._name, key)):
  1102. raise KeyError(key)
  1103. def __contains__(self, key):
  1104. return self._parser.has_option(self._name, key)
  1105. def __len__(self):
  1106. return len(self._options())
  1107. def __iter__(self):
  1108. return self._options().__iter__()
  1109. def _options(self):
  1110. if self._name != self._parser.default_section:
  1111. return self._parser.options(self._name)
  1112. else:
  1113. return self._parser.defaults()
  1114. @property
  1115. def parser(self):
  1116. # The parser object of the proxy is read-only.
  1117. return self._parser
  1118. @property
  1119. def name(self):
  1120. # The name of the section on a proxy is read-only.
  1121. return self._name
  1122. def get(self, option, fallback=None, **kwargs):
  1123. """Get an option value.
  1124. Unless `fallback` is provided, `None` will be returned if the option
  1125. is not found.
  1126. """
  1127. # keyword-only arguments
  1128. kwargs.setdefault('raw', False)
  1129. kwargs.setdefault('vars', None)
  1130. _impl = kwargs.pop('_impl', None)
  1131. # If `_impl` is provided, it should be a getter method on the parser
  1132. # object that provides the desired type conversion.
  1133. if not _impl:
  1134. _impl = self._parser.get
  1135. return _impl(self._name, option, fallback=fallback, **kwargs)
  1136. class ConverterMapping(MutableMapping):
  1137. """Enables reuse of get*() methods between the parser and section proxies.
  1138. If a parser class implements a getter directly, the value for the given
  1139. key will be ``None``. The presence of the converter name here enables
  1140. section proxies to find and use the implementation on the parser class.
  1141. """
  1142. GETTERCRE = re.compile(r"^get(?P<name>.+)$")
  1143. def __init__(self, parser):
  1144. self._parser = parser
  1145. self._data = {}
  1146. for getter in dir(self._parser):
  1147. m = self.GETTERCRE.match(getter)
  1148. if not m or not callable(getattr(self._parser, getter)):
  1149. continue
  1150. self._data[m.group('name')] = None # See class docstring.
  1151. def __getitem__(self, key):
  1152. return self._data[key]
  1153. def __setitem__(self, key, value):
  1154. try:
  1155. k = 'get' + key
  1156. except TypeError:
  1157. raise ValueError('Incompatible key: {} (type: {})'
  1158. ''.format(key, type(key)))
  1159. if k == 'get':
  1160. raise ValueError('Incompatible key: cannot use "" as a name')
  1161. self._data[key] = value
  1162. func = functools.partial(self._parser._get_conv, conv=value)
  1163. func.converter = value
  1164. setattr(self._parser, k, func)
  1165. for proxy in self._parser.values():
  1166. getter = functools.partial(proxy.get, _impl=func)
  1167. setattr(proxy, k, getter)
  1168. def __delitem__(self, key):
  1169. try:
  1170. k = 'get' + (key or None)
  1171. except TypeError:
  1172. raise KeyError(key)
  1173. del self._data[key]
  1174. for inst in itertools.chain((self._parser,), self._parser.values()):
  1175. try:
  1176. delattr(inst, k)
  1177. except AttributeError:
  1178. # don't raise since the entry was present in _data, silently
  1179. # clean up
  1180. continue
  1181. def __iter__(self):
  1182. return iter(self._data)
  1183. def __len__(self):
  1184. return len(self._data)