A config loader for pure python files. This is responsible for locating a Python config file by filename and path, then executing it to construct a Config object.
| 606 | |
| 607 | |
| 608 | class PyFileConfigLoader(FileConfigLoader): |
| 609 | """A config loader for pure python files. |
| 610 | |
| 611 | This is responsible for locating a Python config file by filename and |
| 612 | path, then executing it to construct a Config object. |
| 613 | """ |
| 614 | |
| 615 | def load_config(self) -> Config: |
| 616 | """Load the config from a file and return it as a Config object.""" |
| 617 | self.clear() |
| 618 | try: |
| 619 | self._find_file() |
| 620 | except OSError as e: |
| 621 | raise ConfigFileNotFound(str(e)) from e |
| 622 | self._read_file_as_dict() |
| 623 | return self.config |
| 624 | |
| 625 | def load_subconfig(self, fname: str, path: str | None = None) -> None: |
| 626 | """Injected into config file namespace as load_subconfig""" |
| 627 | if path is None: |
| 628 | path = self.path |
| 629 | |
| 630 | loader = self.__class__(fname, path) |
| 631 | try: |
| 632 | sub_config = loader.load_config() |
| 633 | except ConfigFileNotFound: |
| 634 | # Pass silently if the sub config is not there, |
| 635 | # treat it as an empty config file. |
| 636 | pass |
| 637 | else: |
| 638 | self.config.merge(sub_config) |
| 639 | |
| 640 | def _read_file_as_dict(self) -> None: |
| 641 | """Load the config file into self.config, with recursive loading.""" |
| 642 | |
| 643 | def get_config() -> Config: |
| 644 | """Unnecessary now, but a deprecation warning is more trouble than it's worth.""" |
| 645 | return self.config |
| 646 | |
| 647 | namespace = dict( # noqa: C408 |
| 648 | c=self.config, |
| 649 | load_subconfig=self.load_subconfig, |
| 650 | get_config=get_config, |
| 651 | __file__=self.full_filename, |
| 652 | ) |
| 653 | conf_filename = self.full_filename |
| 654 | with open(conf_filename, "rb") as f: |
| 655 | exec(compile(f.read(), conf_filename, "exec"), namespace, namespace) # noqa: S102 |
| 656 | |
| 657 | |
| 658 | class CommandLineConfigLoader(ConfigLoader): |
no outgoing calls
searching dependent graphs…