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

python模块:configparser的更多相关文章

  1. 保留注释换行的python模块configparser

    python语言用来解析配置文件的模块是ConfigParser,python3中是configparser模块,我在使用中发现write方法在将配置项重新写入文 件时,配置文件中的空行和注释行都会被 ...

  2. Python模块configparser(操作配置文件ini)

    configparser模块提供对ini文件的增删改查方法. ini文件的数据格式: [name1] attribute1=value1 attribute2=value2 [name2] attri ...

  3. Python模块 - configparser

    configparser模块用来对配置文件进行操作 1.获取所有块 import configparser config = configparser.ConfigParser() config.re ...

  4. Python模块——configparser

    configparser模块 该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值) 创建文件 import configp ...

  5. python 模块之-configparser

    python 模块configparser   配置文件模块 import configparser    config = configparser.ConfigParser() config[&q ...

  6. python模块(shelve,xml,configparser,hashlib,logging)

    1.1shelve模块 shelve 模块比pickle模块简单,只有一个open函数,返回类似字典对象,可读可写:key必须为字符串, 而值可以是python所支持的数据类型. shelve模块主要 ...

  7. python模块基础之json,requeste,xml,configparser,logging,subprocess,shutil。

    1.json模块 json     用于[字符串]和 [python基本数据类型] 间进行转换(可用于不同语言之前转换),json.loads,将字符串转成python的基本数据类型,json.dum ...

  8. Python之配置文件模块 ConfigParser

    写项目肯定用的到配置文件,这次学习一下python中的配置文件模块 ConfigParser 安装就不说了,pip一下即可,直接来个实例 配置文件 project.conf [db] host = ' ...

  9. python封装configparser模块获取conf.ini值(优化版)

    昨天晚上封装了configparser模块,是根据keyname获取的value.python封装configparser模块获取conf.ini值 我原本是想通过config.ini文件中的sect ...

  10. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

随机推荐

  1. 学习MeteoInfo二次开发教程(七)

    1.站点文件 12010615.syn在D:\Program Files (x86)\MeteoInfo\SampleSYNOP_Stations.csv在D:\Program Files (x86) ...

  2. python实战博客

    2018-10-31 更新Logging日志记录以及异常捕获 感谢廖大教程.Python实战 直接在闲置的服务器上开发.阿里云Centos 6.8 64位. 1 搭建开发环境 Python 环境是Py ...

  3. git 修改客户端用户名和密码

    1.修改某个具体项目中的用户名密码 vim xx/{yourProject dir }/.git/.git-credentials 在.git-credentials文件中配置用户名密码 https: ...

  4. [phvia/firman] PHP多进程服务器模型中的惊群

    [ 典型场景 ] 典型的多进程服务器模型是这样的,主进程绑定ip,监听port,fork几个子进程,子进程安装信号处理器,随后轮询资源描述符检查是否可读可写: 子进程的轮询又涉及到 IO复用,acce ...

  5. Linux-vim文本编辑器

    [Vim文本编辑器] vim的三种模式:一般模式.编辑模式.命令模式. * 一般模式: 当你vim filename 编辑一个文件时,一进入该文件就是一般模式了.在这个模式下,你可以做的操作有,上下移 ...

  6. JAVA设计模式一策略模式(Strategy Pattern)

    什么是设计模式? 就是一些经验.让程序代码更具弹性.好维护.代码复用的经验.而且设计模式都遵从一些OO设计原则. 题外话:以下罗列出常用的OO设计原则:链接 本文章介绍策略模式(Strategy Pa ...

  7. vue浏览器滚动加载更多

    created () { var that = this; window.addEventListener('scroll',this.scroll,true) console.log(this.$r ...

  8. Windows Server 2008中使用计划任务定时执行BAT bat进行PHP脚本的执行

    Windows Server 2008中使用计划任务定时执行BAT bat进行PHP脚本的执行 2016年01月03日 17:36:00 持之以恒 阅读数:5520 标签: windows定时任务.b ...

  9. JS获取URL中文参数乱码的解决方法

    浏览器URL参数值中带有汉字字符,在接收时直接获取会出现乱码,下面是解决方法(传递前不需要encodeURI): function getUrlVars() { var vars = [], hash ...

  10. hdoj2089(入门数位dp)

    题目链接:https://vjudge.net/problem/HDU-2089 题意:给定一段区间求出该区间中不含4且不含连续的62的数的个数. 思路:这周开始做数位dp专题,给自己加油^_^,一直 ...