config.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. # Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
  2. #
  3. # This module is part of GitPython and is released under the
  4. # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
  5. """Parser for reading and writing configuration files."""
  6. __all__ = ["GitConfigParser", "SectionConstraint"]
  7. import abc
  8. import configparser as cp
  9. import fnmatch
  10. from functools import wraps
  11. import inspect
  12. from io import BufferedReader, IOBase
  13. import logging
  14. import os
  15. import os.path as osp
  16. import re
  17. import sys
  18. from git.compat import defenc, force_text
  19. from git.util import LockFile
  20. # typing-------------------------------------------------------
  21. from typing import (
  22. Any,
  23. Callable,
  24. Generic,
  25. IO,
  26. List,
  27. Dict,
  28. Sequence,
  29. TYPE_CHECKING,
  30. Tuple,
  31. TypeVar,
  32. Union,
  33. cast,
  34. )
  35. from git.types import Lit_config_levels, ConfigLevels_Tup, PathLike, assert_never, _T
  36. if TYPE_CHECKING:
  37. from io import BytesIO
  38. from git.repo.base import Repo
  39. T_ConfigParser = TypeVar("T_ConfigParser", bound="GitConfigParser")
  40. T_OMD_value = TypeVar("T_OMD_value", str, bytes, int, float, bool)
  41. if sys.version_info[:3] < (3, 7, 2):
  42. # typing.Ordereddict not added until Python 3.7.2.
  43. from collections import OrderedDict
  44. OrderedDict_OMD = OrderedDict
  45. else:
  46. from typing import OrderedDict
  47. OrderedDict_OMD = OrderedDict[str, List[T_OMD_value]] # type: ignore[assignment, misc]
  48. # -------------------------------------------------------------
  49. _logger = logging.getLogger(__name__)
  50. CONFIG_LEVELS: ConfigLevels_Tup = ("system", "user", "global", "repository")
  51. """The configuration level of a configuration file."""
  52. CONDITIONAL_INCLUDE_REGEXP = re.compile(r"(?<=includeIf )\"(gitdir|gitdir/i|onbranch|hasconfig:remote\.\*\.url):(.+)\"")
  53. """Section pattern to detect conditional includes.
  54. See: https://git-scm.com/docs/git-config#_conditional_includes
  55. """
  56. class MetaParserBuilder(abc.ABCMeta): # noqa: B024
  57. """Utility class wrapping base-class methods into decorators that assure read-only
  58. properties."""
  59. def __new__(cls, name: str, bases: Tuple, clsdict: Dict[str, Any]) -> "MetaParserBuilder":
  60. """Equip all base-class methods with a needs_values decorator, and all non-const
  61. methods with a :func:`set_dirty_and_flush_changes` decorator in addition to
  62. that.
  63. """
  64. kmm = "_mutating_methods_"
  65. if kmm in clsdict:
  66. mutating_methods = clsdict[kmm]
  67. for base in bases:
  68. methods = (t for t in inspect.getmembers(base, inspect.isroutine) if not t[0].startswith("_"))
  69. for method_name, method in methods:
  70. if method_name in clsdict:
  71. continue
  72. method_with_values = needs_values(method)
  73. if method_name in mutating_methods:
  74. method_with_values = set_dirty_and_flush_changes(method_with_values)
  75. # END mutating methods handling
  76. clsdict[method_name] = method_with_values
  77. # END for each name/method pair
  78. # END for each base
  79. # END if mutating methods configuration is set
  80. new_type = super().__new__(cls, name, bases, clsdict)
  81. return new_type
  82. def needs_values(func: Callable[..., _T]) -> Callable[..., _T]:
  83. """Return a method for ensuring we read values (on demand) before we try to access
  84. them."""
  85. @wraps(func)
  86. def assure_data_present(self: "GitConfigParser", *args: Any, **kwargs: Any) -> _T:
  87. self.read()
  88. return func(self, *args, **kwargs)
  89. # END wrapper method
  90. return assure_data_present
  91. def set_dirty_and_flush_changes(non_const_func: Callable[..., _T]) -> Callable[..., _T]:
  92. """Return a method that checks whether given non constant function may be called.
  93. If so, the instance will be set dirty. Additionally, we flush the changes right to
  94. disk.
  95. """
  96. def flush_changes(self: "GitConfigParser", *args: Any, **kwargs: Any) -> _T:
  97. rval = non_const_func(self, *args, **kwargs)
  98. self._dirty = True
  99. self.write()
  100. return rval
  101. # END wrapper method
  102. flush_changes.__name__ = non_const_func.__name__
  103. return flush_changes
  104. class SectionConstraint(Generic[T_ConfigParser]):
  105. """Constrains a ConfigParser to only option commands which are constrained to
  106. always use the section we have been initialized with.
  107. It supports all ConfigParser methods that operate on an option.
  108. :note:
  109. If used as a context manager, will release the wrapped ConfigParser.
  110. """
  111. __slots__ = ("_config", "_section_name")
  112. _valid_attrs_ = (
  113. "get_value",
  114. "set_value",
  115. "get",
  116. "set",
  117. "getint",
  118. "getfloat",
  119. "getboolean",
  120. "has_option",
  121. "remove_section",
  122. "remove_option",
  123. "options",
  124. )
  125. def __init__(self, config: T_ConfigParser, section: str) -> None:
  126. self._config = config
  127. self._section_name = section
  128. def __del__(self) -> None:
  129. # Yes, for some reason, we have to call it explicitly for it to work in PY3 !
  130. # Apparently __del__ doesn't get call anymore if refcount becomes 0
  131. # Ridiculous ... .
  132. self._config.release()
  133. def __getattr__(self, attr: str) -> Any:
  134. if attr in self._valid_attrs_:
  135. return lambda *args, **kwargs: self._call_config(attr, *args, **kwargs)
  136. return super().__getattribute__(attr)
  137. def _call_config(self, method: str, *args: Any, **kwargs: Any) -> Any:
  138. """Call the configuration at the given method which must take a section name as
  139. first argument."""
  140. return getattr(self._config, method)(self._section_name, *args, **kwargs)
  141. @property
  142. def config(self) -> T_ConfigParser:
  143. """return: ConfigParser instance we constrain"""
  144. return self._config
  145. def release(self) -> None:
  146. """Equivalent to :meth:`GitConfigParser.release`, which is called on our
  147. underlying parser instance."""
  148. return self._config.release()
  149. def __enter__(self) -> "SectionConstraint[T_ConfigParser]":
  150. self._config.__enter__()
  151. return self
  152. def __exit__(self, exception_type: str, exception_value: str, traceback: str) -> None:
  153. self._config.__exit__(exception_type, exception_value, traceback)
  154. class _OMD(OrderedDict_OMD):
  155. """Ordered multi-dict."""
  156. def __setitem__(self, key: str, value: _T) -> None:
  157. super().__setitem__(key, [value])
  158. def add(self, key: str, value: Any) -> None:
  159. if key not in self:
  160. super().__setitem__(key, [value])
  161. return
  162. super().__getitem__(key).append(value)
  163. def setall(self, key: str, values: List[_T]) -> None:
  164. super().__setitem__(key, values)
  165. def __getitem__(self, key: str) -> Any:
  166. return super().__getitem__(key)[-1]
  167. def getlast(self, key: str) -> Any:
  168. return super().__getitem__(key)[-1]
  169. def setlast(self, key: str, value: Any) -> None:
  170. if key not in self:
  171. super().__setitem__(key, [value])
  172. return
  173. prior = super().__getitem__(key)
  174. prior[-1] = value
  175. def get(self, key: str, default: Union[_T, None] = None) -> Union[_T, None]:
  176. return super().get(key, [default])[-1]
  177. def getall(self, key: str) -> List[_T]:
  178. return super().__getitem__(key)
  179. def items(self) -> List[Tuple[str, _T]]: # type: ignore[override]
  180. """List of (key, last value for key)."""
  181. return [(k, self[k]) for k in self]
  182. def items_all(self) -> List[Tuple[str, List[_T]]]:
  183. """List of (key, list of values for key)."""
  184. return [(k, self.getall(k)) for k in self]
  185. def get_config_path(config_level: Lit_config_levels) -> str:
  186. # We do not support an absolute path of the gitconfig on Windows.
  187. # Use the global config instead.
  188. if sys.platform == "win32" and config_level == "system":
  189. config_level = "global"
  190. if config_level == "system":
  191. return "/etc/gitconfig"
  192. elif config_level == "user":
  193. config_home = os.environ.get("XDG_CONFIG_HOME") or osp.join(os.environ.get("HOME", "~"), ".config")
  194. return osp.normpath(osp.expanduser(osp.join(config_home, "git", "config")))
  195. elif config_level == "global":
  196. return osp.normpath(osp.expanduser("~/.gitconfig"))
  197. elif config_level == "repository":
  198. raise ValueError("No repo to get repository configuration from. Use Repo._get_config_path")
  199. else:
  200. # Should not reach here. Will raise ValueError if does. Static typing will warn
  201. # about missing elifs.
  202. assert_never( # type: ignore[unreachable]
  203. config_level,
  204. ValueError(f"Invalid configuration level: {config_level!r}"),
  205. )
  206. class GitConfigParser(cp.RawConfigParser, metaclass=MetaParserBuilder):
  207. """Implements specifics required to read git style configuration files.
  208. This variation behaves much like the :manpage:`git-config(1)` command, such that the
  209. configuration will be read on demand based on the filepath given during
  210. initialization.
  211. The changes will automatically be written once the instance goes out of scope, but
  212. can be triggered manually as well.
  213. The configuration file will be locked if you intend to change values preventing
  214. other instances to write concurrently.
  215. :note:
  216. The config is case-sensitive even when queried, hence section and option names
  217. must match perfectly.
  218. :note:
  219. If used as a context manager, this will release the locked file.
  220. """
  221. # { Configuration
  222. t_lock = LockFile
  223. """The lock type determines the type of lock to use in new configuration readers.
  224. They must be compatible to the :class:`~git.util.LockFile` interface.
  225. A suitable alternative would be the :class:`~git.util.BlockingLockFile`.
  226. """
  227. re_comment = re.compile(r"^\s*[#;]")
  228. # } END configuration
  229. optvalueonly_source = r"\s*(?P<option>[^:=\s][^:=]*)"
  230. OPTVALUEONLY = re.compile(optvalueonly_source)
  231. OPTCRE = re.compile(optvalueonly_source + r"\s*(?P<vi>[:=])\s*" + r"(?P<value>.*)$")
  232. del optvalueonly_source
  233. _mutating_methods_ = ("add_section", "remove_section", "remove_option", "set")
  234. """Names of :class:`~configparser.RawConfigParser` methods able to change the
  235. instance."""
  236. def __init__(
  237. self,
  238. file_or_files: Union[None, PathLike, "BytesIO", Sequence[Union[PathLike, "BytesIO"]]] = None,
  239. read_only: bool = True,
  240. merge_includes: bool = True,
  241. config_level: Union[Lit_config_levels, None] = None,
  242. repo: Union["Repo", None] = None,
  243. ) -> None:
  244. """Initialize a configuration reader to read the given `file_or_files` and to
  245. possibly allow changes to it by setting `read_only` False.
  246. :param file_or_files:
  247. A file path or file object, or a sequence of possibly more than one of them.
  248. :param read_only:
  249. If ``True``, the ConfigParser may only read the data, but not change it.
  250. If ``False``, only a single file path or file object may be given. We will
  251. write back the changes when they happen, or when the ConfigParser is
  252. released. This will not happen if other configuration files have been
  253. included.
  254. :param merge_includes:
  255. If ``True``, we will read files mentioned in ``[include]`` sections and
  256. merge their contents into ours. This makes it impossible to write back an
  257. individual configuration file. Thus, if you want to modify a single
  258. configuration file, turn this off to leave the original dataset unaltered
  259. when reading it.
  260. :param repo:
  261. Reference to repository to use if ``[includeIf]`` sections are found in
  262. configuration files.
  263. """
  264. cp.RawConfigParser.__init__(self, dict_type=_OMD)
  265. self._dict: Callable[..., _OMD]
  266. self._defaults: _OMD
  267. self._sections: _OMD
  268. # Used in Python 3. Needs to stay in sync with sections for underlying
  269. # implementation to work.
  270. if not hasattr(self, "_proxies"):
  271. self._proxies = self._dict()
  272. if file_or_files is not None:
  273. self._file_or_files: Union[PathLike, "BytesIO", Sequence[Union[PathLike, "BytesIO"]]] = file_or_files
  274. else:
  275. if config_level is None:
  276. if read_only:
  277. self._file_or_files = [
  278. get_config_path(cast(Lit_config_levels, f)) for f in CONFIG_LEVELS if f != "repository"
  279. ]
  280. else:
  281. raise ValueError("No configuration level or configuration files specified")
  282. else:
  283. self._file_or_files = [get_config_path(config_level)]
  284. self._read_only = read_only
  285. self._dirty = False
  286. self._is_initialized = False
  287. self._merge_includes = merge_includes
  288. self._repo = repo
  289. self._lock: Union["LockFile", None] = None
  290. self._acquire_lock()
  291. def _acquire_lock(self) -> None:
  292. if not self._read_only:
  293. if not self._lock:
  294. if isinstance(self._file_or_files, (str, os.PathLike)):
  295. file_or_files = self._file_or_files
  296. elif isinstance(self._file_or_files, (tuple, list, Sequence)):
  297. raise ValueError(
  298. "Write-ConfigParsers can operate on a single file only, multiple files have been passed"
  299. )
  300. else:
  301. file_or_files = self._file_or_files.name
  302. # END get filename from handle/stream
  303. # Initialize lock base - we want to write.
  304. self._lock = self.t_lock(file_or_files)
  305. # END lock check
  306. self._lock._obtain_lock()
  307. # END read-only check
  308. def __del__(self) -> None:
  309. """Write pending changes if required and release locks."""
  310. # NOTE: Only consistent in Python 2.
  311. self.release()
  312. def __enter__(self) -> "GitConfigParser":
  313. self._acquire_lock()
  314. return self
  315. def __exit__(self, *args: Any) -> None:
  316. self.release()
  317. def release(self) -> None:
  318. """Flush changes and release the configuration write lock. This instance must
  319. not be used anymore afterwards.
  320. In Python 3, it's required to explicitly release locks and flush changes, as
  321. ``__del__`` is not called deterministically anymore.
  322. """
  323. # Checking for the lock here makes sure we do not raise during write()
  324. # in case an invalid parser was created who could not get a lock.
  325. if self.read_only or (self._lock and not self._lock._has_lock()):
  326. return
  327. try:
  328. self.write()
  329. except IOError:
  330. _logger.error("Exception during destruction of GitConfigParser", exc_info=True)
  331. except ReferenceError:
  332. # This happens in Python 3... and usually means that some state cannot be
  333. # written as the sections dict cannot be iterated. This usually happens when
  334. # the interpreter is shutting down. Can it be fixed?
  335. pass
  336. finally:
  337. if self._lock is not None:
  338. self._lock._release_lock()
  339. def optionxform(self, optionstr: str) -> str:
  340. """Do not transform options in any way when writing."""
  341. return optionstr
  342. def _read(self, fp: Union[BufferedReader, IO[bytes]], fpname: str) -> None:
  343. """Originally a direct copy of the Python 2.4 version of
  344. :meth:`RawConfigParser._read <configparser.RawConfigParser._read>`, to ensure it
  345. uses ordered dicts.
  346. The ordering bug was fixed in Python 2.4, and dict itself keeps ordering since
  347. Python 3.7. This has some other changes, especially that it ignores initial
  348. whitespace, since git uses tabs. (Big comments are removed to be more compact.)
  349. """
  350. cursect = None # None, or a dictionary.
  351. optname = None
  352. lineno = 0
  353. is_multi_line = False
  354. e = None # None, or an exception.
  355. def string_decode(v: str) -> str:
  356. if v and v.endswith("\\"):
  357. v = v[:-1]
  358. # END cut trailing escapes to prevent decode error
  359. return v.encode(defenc).decode("unicode_escape")
  360. # END string_decode
  361. while True:
  362. # We assume to read binary!
  363. line = fp.readline().decode(defenc)
  364. if not line:
  365. break
  366. lineno = lineno + 1
  367. # Comment or blank line?
  368. if line.strip() == "" or self.re_comment.match(line):
  369. continue
  370. if line.split(None, 1)[0].lower() == "rem" and line[0] in "rR":
  371. # No leading whitespace.
  372. continue
  373. # Is it a section header?
  374. mo = self.SECTCRE.match(line.strip())
  375. if not is_multi_line and mo:
  376. sectname: str = mo.group("header").strip()
  377. if sectname in self._sections:
  378. cursect = self._sections[sectname]
  379. elif sectname == cp.DEFAULTSECT:
  380. cursect = self._defaults
  381. else:
  382. cursect = self._dict((("__name__", sectname),))
  383. self._sections[sectname] = cursect
  384. self._proxies[sectname] = None
  385. # So sections can't start with a continuation line.
  386. optname = None
  387. # No section header in the file?
  388. elif cursect is None:
  389. raise cp.MissingSectionHeaderError(fpname, lineno, line)
  390. # An option line?
  391. elif not is_multi_line:
  392. mo = self.OPTCRE.match(line)
  393. if mo:
  394. # We might just have handled the last line, which could contain a quotation we want to remove.
  395. optname, vi, optval = mo.group("option", "vi", "value")
  396. optname = self.optionxform(optname.rstrip())
  397. if vi in ("=", ":") and ";" in optval and not optval.strip().startswith('"'):
  398. pos = optval.find(";")
  399. if pos != -1 and optval[pos - 1].isspace():
  400. optval = optval[:pos]
  401. optval = optval.strip()
  402. if len(optval) < 2 or optval[0] != '"':
  403. # Does not open quoting.
  404. pass
  405. elif optval[-1] != '"':
  406. # Opens quoting and does not close: appears to start multi-line quoting.
  407. is_multi_line = True
  408. optval = string_decode(optval[1:])
  409. elif optval.find("\\", 1, -1) == -1 and optval.find('"', 1, -1) == -1:
  410. # Opens and closes quoting. Single line, and all we need is quote removal.
  411. optval = optval[1:-1]
  412. # TODO: Handle other quoted content, especially well-formed backslash escapes.
  413. # Preserves multiple values for duplicate optnames.
  414. cursect.add(optname, optval)
  415. else:
  416. # Check if it's an option with no value - it's just ignored by git.
  417. if not self.OPTVALUEONLY.match(line):
  418. if not e:
  419. e = cp.ParsingError(fpname)
  420. e.append(lineno, repr(line))
  421. continue
  422. else:
  423. line = line.rstrip()
  424. if line.endswith('"'):
  425. is_multi_line = False
  426. line = line[:-1]
  427. # END handle quotations
  428. optval = cursect.getlast(optname)
  429. cursect.setlast(optname, optval + string_decode(line))
  430. # END parse section or option
  431. # END while reading
  432. # If any parsing errors occurred, raise an exception.
  433. if e:
  434. raise e
  435. def _has_includes(self) -> Union[bool, int]:
  436. return self._merge_includes and len(self._included_paths())
  437. def _included_paths(self) -> List[Tuple[str, str]]:
  438. """List all paths that must be included to configuration.
  439. :return:
  440. The list of paths, where each path is a tuple of (option, value).
  441. """
  442. paths = []
  443. for section in self.sections():
  444. if section == "include":
  445. paths += self.items(section)
  446. match = CONDITIONAL_INCLUDE_REGEXP.search(section)
  447. if match is None or self._repo is None:
  448. continue
  449. keyword = match.group(1)
  450. value = match.group(2).strip()
  451. if keyword in ["gitdir", "gitdir/i"]:
  452. value = osp.expanduser(value)
  453. if not any(value.startswith(s) for s in ["./", "/"]):
  454. value = "**/" + value
  455. if value.endswith("/"):
  456. value += "**"
  457. # Ensure that glob is always case insensitive if required.
  458. if keyword.endswith("/i"):
  459. value = re.sub(
  460. r"[a-zA-Z]",
  461. lambda m: f"[{m.group().lower()!r}{m.group().upper()!r}]",
  462. value,
  463. )
  464. if self._repo.git_dir:
  465. if fnmatch.fnmatchcase(os.fspath(self._repo.git_dir), value):
  466. paths += self.items(section)
  467. elif keyword == "onbranch":
  468. try:
  469. branch_name = self._repo.active_branch.name
  470. except TypeError:
  471. # Ignore section if active branch cannot be retrieved.
  472. continue
  473. if fnmatch.fnmatchcase(branch_name, value):
  474. paths += self.items(section)
  475. elif keyword == "hasconfig:remote.*.url":
  476. for remote in self._repo.remotes:
  477. if fnmatch.fnmatchcase(remote.url, value):
  478. paths += self.items(section)
  479. break
  480. return paths
  481. def read(self) -> None: # type: ignore[override]
  482. """Read the data stored in the files we have been initialized with.
  483. This will ignore files that cannot be read, possibly leaving an empty
  484. configuration.
  485. :raise IOError:
  486. If a file cannot be handled.
  487. """
  488. if self._is_initialized:
  489. return
  490. self._is_initialized = True
  491. files_to_read: List[Union[PathLike, IO]] = [""]
  492. if isinstance(self._file_or_files, (str, os.PathLike)):
  493. # For str or Path, as str is a type of Sequence.
  494. files_to_read = [self._file_or_files]
  495. elif not isinstance(self._file_or_files, (tuple, list, Sequence)):
  496. # Could merge with above isinstance once runtime type known.
  497. files_to_read = [self._file_or_files]
  498. else: # For lists or tuples.
  499. files_to_read = list(self._file_or_files)
  500. # END ensure we have a copy of the paths to handle
  501. seen = set(files_to_read)
  502. num_read_include_files = 0
  503. while files_to_read:
  504. file_path = files_to_read.pop(0)
  505. file_ok = False
  506. if hasattr(file_path, "seek"):
  507. # Must be a file-object.
  508. # TODO: Replace cast with assert to narrow type, once sure.
  509. file_path = cast(IO[bytes], file_path)
  510. self._read(file_path, file_path.name)
  511. else:
  512. try:
  513. with open(file_path, "rb") as fp:
  514. file_ok = True
  515. self._read(fp, fp.name)
  516. except IOError:
  517. continue
  518. # Read includes and append those that we didn't handle yet. We expect all
  519. # paths to be normalized and absolute (and will ensure that is the case).
  520. if self._has_includes():
  521. for _, include_path in self._included_paths():
  522. if include_path.startswith("~"):
  523. include_path = osp.expanduser(include_path)
  524. if not osp.isabs(include_path):
  525. if not file_ok:
  526. continue
  527. # END ignore relative paths if we don't know the configuration file path
  528. file_path = cast(PathLike, file_path)
  529. assert osp.isabs(file_path), "Need absolute paths to be sure our cycle checks will work"
  530. include_path = osp.join(osp.dirname(file_path), include_path)
  531. # END make include path absolute
  532. include_path = osp.normpath(include_path)
  533. if include_path in seen or not os.access(include_path, os.R_OK):
  534. continue
  535. seen.add(include_path)
  536. # Insert included file to the top to be considered first.
  537. files_to_read.insert(0, include_path)
  538. num_read_include_files += 1
  539. # END each include path in configuration file
  540. # END handle includes
  541. # END for each file object to read
  542. # If there was no file included, we can safely write back (potentially) the
  543. # configuration file without altering its meaning.
  544. if num_read_include_files == 0:
  545. self._merge_includes = False
  546. def _write(self, fp: IO) -> None:
  547. """Write an .ini-format representation of the configuration state in
  548. git compatible format."""
  549. def write_section(name: str, section_dict: _OMD) -> None:
  550. fp.write(("[%s]\n" % name).encode(defenc))
  551. values: Sequence[str] # Runtime only gets str in tests, but should be whatever _OMD stores.
  552. v: str
  553. for key, values in section_dict.items_all():
  554. if key == "__name__":
  555. continue
  556. for v in values:
  557. fp.write(("\t%s = %s\n" % (key, self._value_to_string(v).replace("\n", "\n\t"))).encode(defenc))
  558. # END if key is not __name__
  559. # END section writing
  560. if self._defaults:
  561. write_section(cp.DEFAULTSECT, self._defaults)
  562. value: _OMD
  563. for name, value in self._sections.items():
  564. write_section(name, value)
  565. def items(self, section_name: str) -> List[Tuple[str, str]]: # type: ignore[override]
  566. """:return: list((option, value), ...) pairs of all items in the given section"""
  567. return [(k, v) for k, v in super().items(section_name) if k != "__name__"]
  568. def items_all(self, section_name: str) -> List[Tuple[str, List[str]]]:
  569. """:return: list((option, [values...]), ...) pairs of all items in the given section"""
  570. rv = _OMD(self._defaults)
  571. for k, vs in self._sections[section_name].items_all():
  572. if k == "__name__":
  573. continue
  574. if k in rv and rv.getall(k) == vs:
  575. continue
  576. for v in vs:
  577. rv.add(k, v)
  578. return rv.items_all()
  579. @needs_values
  580. def write(self) -> None:
  581. """Write changes to our file, if there are changes at all.
  582. :raise IOError:
  583. If this is a read-only writer instance or if we could not obtain a file
  584. lock.
  585. """
  586. self._assure_writable("write")
  587. if not self._dirty:
  588. return
  589. if isinstance(self._file_or_files, (list, tuple)):
  590. raise AssertionError(
  591. "Cannot write back if there is not exactly a single file to write to, have %i files"
  592. % len(self._file_or_files)
  593. )
  594. # END assert multiple files
  595. if self._has_includes():
  596. _logger.debug(
  597. "Skipping write-back of configuration file as include files were merged in."
  598. + "Set merge_includes=False to prevent this."
  599. )
  600. return
  601. # END stop if we have include files
  602. fp = self._file_or_files
  603. # We have a physical file on disk, so get a lock.
  604. is_file_lock = isinstance(fp, (str, os.PathLike, IOBase)) # TODO: Use PathLike (having dropped 3.5).
  605. if is_file_lock and self._lock is not None: # Else raise error?
  606. self._lock._obtain_lock()
  607. if not hasattr(fp, "seek"):
  608. fp = cast(PathLike, fp)
  609. with open(fp, "wb") as fp_open:
  610. self._write(fp_open)
  611. else:
  612. fp = cast("BytesIO", fp)
  613. fp.seek(0)
  614. # Make sure we do not overwrite into an existing file.
  615. if hasattr(fp, "truncate"):
  616. fp.truncate()
  617. self._write(fp)
  618. def _assure_writable(self, method_name: str) -> None:
  619. if self.read_only:
  620. raise IOError("Cannot execute non-constant method %s.%s" % (self, method_name))
  621. def add_section(self, section: "cp._SectionName") -> None:
  622. """Assures added options will stay in order."""
  623. return super().add_section(section)
  624. @property
  625. def read_only(self) -> bool:
  626. """:return: ``True`` if this instance may change the configuration file"""
  627. return self._read_only
  628. # FIXME: Figure out if default or return type can really include bool.
  629. def get_value(
  630. self,
  631. section: str,
  632. option: str,
  633. default: Union[int, float, str, bool, None] = None,
  634. ) -> Union[int, float, str, bool]:
  635. """Get an option's value.
  636. If multiple values are specified for this option in the section, the last one
  637. specified is returned.
  638. :param default:
  639. If not ``None``, the given default value will be returned in case the option
  640. did not exist.
  641. :return:
  642. A properly typed value, either int, float or string
  643. :raise TypeError:
  644. In case the value could not be understood.
  645. Otherwise the exceptions known to the ConfigParser will be raised.
  646. """
  647. try:
  648. valuestr = self.get(section, option)
  649. except Exception:
  650. if default is not None:
  651. return default
  652. raise
  653. return self._string_to_value(valuestr)
  654. def get_values(
  655. self,
  656. section: str,
  657. option: str,
  658. default: Union[int, float, str, bool, None] = None,
  659. ) -> List[Union[int, float, str, bool]]:
  660. """Get an option's values.
  661. If multiple values are specified for this option in the section, all are
  662. returned.
  663. :param default:
  664. If not ``None``, a list containing the given default value will be returned
  665. in case the option did not exist.
  666. :return:
  667. A list of properly typed values, either int, float or string
  668. :raise TypeError:
  669. In case the value could not be understood.
  670. Otherwise the exceptions known to the ConfigParser will be raised.
  671. """
  672. try:
  673. self.sections()
  674. lst = self._sections[section].getall(option)
  675. except Exception:
  676. if default is not None:
  677. return [default]
  678. raise
  679. return [self._string_to_value(valuestr) for valuestr in lst]
  680. def _string_to_value(self, valuestr: str) -> Union[int, float, str, bool]:
  681. types = (int, float)
  682. for numtype in types:
  683. try:
  684. val = numtype(valuestr)
  685. # truncated value ?
  686. if val != float(valuestr):
  687. continue
  688. return val
  689. except (ValueError, TypeError):
  690. continue
  691. # END for each numeric type
  692. # Try boolean values as git uses them.
  693. vl = valuestr.lower()
  694. if vl == "false":
  695. return False
  696. if vl == "true":
  697. return True
  698. if not isinstance(valuestr, str):
  699. raise TypeError(
  700. "Invalid value type: only int, long, float and str are allowed",
  701. valuestr,
  702. )
  703. return valuestr
  704. def _value_to_string(self, value: Union[str, bytes, int, float, bool]) -> str:
  705. if isinstance(value, (int, float, bool)):
  706. return str(value)
  707. return force_text(value)
  708. @needs_values
  709. @set_dirty_and_flush_changes
  710. def set_value(self, section: str, option: str, value: Union[str, bytes, int, float, bool]) -> "GitConfigParser":
  711. """Set the given option in section to the given value.
  712. This will create the section if required, and will not throw as opposed to the
  713. default ConfigParser ``set`` method.
  714. :param section:
  715. Name of the section in which the option resides or should reside.
  716. :param option:
  717. Name of the options whose value to set.
  718. :param value:
  719. Value to set the option to. It must be a string or convertible to a string.
  720. :return:
  721. This instance
  722. """
  723. if not self.has_section(section):
  724. self.add_section(section)
  725. self.set(section, option, self._value_to_string(value))
  726. return self
  727. @needs_values
  728. @set_dirty_and_flush_changes
  729. def add_value(self, section: str, option: str, value: Union[str, bytes, int, float, bool]) -> "GitConfigParser":
  730. """Add a value for the given option in section.
  731. This will create the section if required, and will not throw as opposed to the
  732. default ConfigParser ``set`` method. The value becomes the new value of the
  733. option as returned by :meth:`get_value`, and appends to the list of values
  734. returned by :meth:`get_values`.
  735. :param section:
  736. Name of the section in which the option resides or should reside.
  737. :param option:
  738. Name of the option.
  739. :param value:
  740. Value to add to option. It must be a string or convertible to a string.
  741. :return:
  742. This instance
  743. """
  744. if not self.has_section(section):
  745. self.add_section(section)
  746. self._sections[section].add(option, self._value_to_string(value))
  747. return self
  748. def rename_section(self, section: str, new_name: str) -> "GitConfigParser":
  749. """Rename the given section to `new_name`.
  750. :raise ValueError:
  751. If:
  752. * `section` doesn't exist.
  753. * A section with `new_name` does already exist.
  754. :return:
  755. This instance
  756. """
  757. if not self.has_section(section):
  758. raise ValueError("Source section '%s' doesn't exist" % section)
  759. if self.has_section(new_name):
  760. raise ValueError("Destination section '%s' already exists" % new_name)
  761. super().add_section(new_name)
  762. new_section = self._sections[new_name]
  763. for k, vs in self.items_all(section):
  764. new_section.setall(k, vs)
  765. # END for each value to copy
  766. # This call writes back the changes, which is why we don't have the respective
  767. # decorator.
  768. self.remove_section(section)
  769. return self