MCPcopy Create free account
hub / github.com/OSGeo/PROJ / from_yaml

Method from_yaml

test/cli/run_cli_test.py:730–807  ·  view source on GitHub ↗
(cls, filename, update=False, preferred=None, verbose=1)

Source from the content-addressed store, hash-verified

728
729 @classmethod
730 def from_yaml(cls, filename, update=False, preferred=None, verbose=1):
731 supported_libs = {None, "ruamel.yaml", "pyyaml"}
732 if preferred not in supported_libs:
733 raise NotImplementedError(f"{preferred} not in {supported_libs}")
734 yaml_lib = None
735 if preferred == "ruamel.yaml" or preferred is None:
736 try:
737 from ruamel.yaml import YAML
738
739 yaml_lib = "ruamel.yaml"
740 except ImportError:
741 pass
742 if yaml_lib is None:
743 try:
744 import yaml
745 from yaml.resolver import Resolver
746
747 # https://stackoverflow.com/a/36470466/
748 # remove resolver entries for On/Off/Yes/No
749 for ch in "OoYyNn":
750 if ch not in Resolver.yaml_implicit_resolvers:
751 continue
752 if len(Resolver.yaml_implicit_resolvers[ch]) == 1:
753 del Resolver.yaml_implicit_resolvers[ch]
754 else:
755 Resolver.yaml_implicit_resolvers[ch] = [
756 x
757 for x in Resolver.yaml_implicit_resolvers[ch]
758 if x[0] != "tag:yaml.org,2002:bool"
759 ]
760 yaml_lib = "pyyaml"
761 except ImportError:
762 pass
763 if preferred is None:
764 preferred = "either ruamel.yaml or pyyaml"
765 if yaml_lib is None:
766 raise ImportError(
767 f"this script needs {preferred} "
768 "which can be installed using pip, conda, apt, yum, ..."
769 )
770 if update:
771 if yaml_lib != "ruamel.yaml":
772 raise ImportError("ruamel.yaml required to update YAML file")
773 if yaml_lib == "pyyaml":
774 with open(filename, encoding="utf-8") as fp:
775 data = yaml.safe_load(fp)
776 elif yaml_lib == "ruamel.yaml":
777 typ = "rt" if update else "safe"
778 yaml = YAML(typ=typ)
779 yaml.width = 200
780 yaml.preserve_quotes = True
781 with open(filename, encoding="utf-8") as fp:
782 data = yaml.load(fp)
783 else:
784 raise NotImplementedError(yaml_lib)
785
786 if not isinstance(data, Mapping):
787 raise ValueError(f"expected dict-like structure to {filename}")

Callers 3

mainFunction · 0.80
test_tester_from_yamlFunction · 0.80

Calls 1

printFunction · 0.85

Tested by

no test coverage detected