Commit | Line | Data |
---|---|---|
7f4429f2 PP |
1 | # The MIT License (MIT) |
2 | # | |
4a90140d | 3 | # Copyright (c) 2015-2020 Philippe Proulx <pproulx@efficios.com> |
7f4429f2 | 4 | # |
1378f213 PP |
5 | # Permission is hereby granted, free of charge, to any person obtaining |
6 | # a copy of this software and associated documentation files (the | |
7 | # "Software"), to deal in the Software without restriction, including | |
8 | # without limitation the rights to use, copy, modify, merge, publish, | |
9 | # distribute, sublicense, and/or sell copies of the Software, and to | |
10 | # permit persons to whom the Software is furnished to do so, subject to | |
11 | # the following conditions: | |
7f4429f2 | 12 | # |
1378f213 PP |
13 | # The above copyright notice and this permission notice shall be |
14 | # included in all copies or substantial portions of the Software. | |
7f4429f2 | 15 | # |
1378f213 PP |
16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
17 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
18 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
19 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
20 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
21 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
22 | # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
7f4429f2 | 23 | |
4810b707 PP |
24 | import barectf.config_parse_common as barectf_config_parse_common |
25 | from barectf.config_parse_common import _ConfigurationParseError | |
2d55dc7d | 26 | from barectf.config_parse_common import _MapNode |
4810b707 PP |
27 | import barectf.config_parse_v2 as barectf_config_parse_v2 |
28 | import barectf.config_parse_v3 as barectf_config_parse_v3 | |
2d55dc7d | 29 | import barectf.config as barectf_config |
7f4429f2 | 30 | import collections |
2d55dc7d PP |
31 | from barectf.typing import Count, VersionNumber |
32 | from typing import Optional, List, TextIO | |
33 | import typing | |
7f4429f2 PP |
34 | |
35 | ||
4810b707 PP |
36 | # Creates and returns a barectf 3 YAML configuration file parser to |
37 | # parse the file-like object `file`. | |
be7df3b2 | 38 | # |
4810b707 | 39 | # `file` can be a barectf 2 or 3 configuration file. |
2d55dc7d PP |
40 | def _create_v3_parser(file: TextIO, with_pkg_include_dir: bool, include_dirs: Optional[List[str]], |
41 | ignore_include_not_found: bool) -> barectf_config_parse_v3._Parser: | |
4810b707 PP |
42 | try: |
43 | root_node = barectf_config_parse_common._yaml_load(file) | |
44 | ||
45 | if type(root_node) is barectf_config_parse_common._ConfigNodeV3: | |
46 | # barectf 3 configuration file | |
2d55dc7d PP |
47 | return barectf_config_parse_v3._Parser(file, |
48 | typing.cast(barectf_config_parse_common._ConfigNodeV3, | |
49 | root_node), | |
50 | with_pkg_include_dir, include_dirs, | |
51 | ignore_include_not_found) | |
4810b707 PP |
52 | elif type(root_node) is collections.OrderedDict: |
53 | # barectf 2 configuration file | |
2d55dc7d PP |
54 | v2_parser = barectf_config_parse_v2._Parser(file, typing.cast(_MapNode, root_node), |
55 | with_pkg_include_dir, include_dirs, | |
56 | ignore_include_not_found) | |
7cd4634e PP |
57 | return barectf_config_parse_v3._Parser(file, v2_parser.config_node, |
58 | with_pkg_include_dir, include_dirs, | |
4810b707 | 59 | ignore_include_not_found) |
7f4429f2 | 60 | else: |
4810b707 PP |
61 | raise _ConfigurationParseError('Configuration', |
62 | f'Root (configuration) node is not an object (it\'s a `{type(root_node)}`)') | |
63 | except _ConfigurationParseError as exc: | |
64 | barectf_config_parse_common._append_error_ctx(exc, 'Configuration', | |
65 | 'Cannot create configuration from YAML file') | |
7f4429f2 | 66 | |
2d55dc7d PP |
67 | # satisfy static type checker (never reached) |
68 | raise | |
69 | ||
7f4429f2 | 70 | |
2d55dc7d PP |
71 | def _from_file(file: TextIO, with_pkg_include_dir: bool, include_dirs: Optional[List[str]], |
72 | ignore_include_not_found: bool) -> barectf_config.Configuration: | |
7cd4634e | 73 | return _create_v3_parser(file, with_pkg_include_dir, include_dirs, ignore_include_not_found).config |
7f4429f2 | 74 | |
c8270369 | 75 | |
2d55dc7d PP |
76 | def _effective_config_file(file: TextIO, with_pkg_include_dir: bool, |
77 | include_dirs: Optional[List[str]], ignore_include_not_found: bool, | |
78 | indent_space_count: Count) -> str: | |
7cd4634e | 79 | config_node = _create_v3_parser(file, with_pkg_include_dir, include_dirs, |
3fe7471a | 80 | ignore_include_not_found).root_node |
4810b707 PP |
81 | return barectf_config_parse_common._yaml_dump(config_node, indent=indent_space_count, |
82 | default_flow_style=False, explicit_start=True, | |
83 | explicit_end=True) | |
7f4429f2 PP |
84 | |
85 | ||
2d55dc7d | 86 | def _config_file_major_version(file: TextIO) -> VersionNumber: |
7f4429f2 | 87 | try: |
4810b707 PP |
88 | root_node = barectf_config_parse_common._yaml_load(file) |
89 | ||
90 | if type(root_node) is barectf_config_parse_common._ConfigNodeV3: | |
91 | # barectf 3 configuration file | |
2d55dc7d PP |
92 | return VersionNumber(3) |
93 | else: | |
4810b707 | 94 | # barectf 2 configuration file |
2d55dc7d PP |
95 | assert type(root_node) is collections.OrderedDict |
96 | return VersionNumber(2) | |
4810b707 PP |
97 | except _ConfigurationParseError as exc: |
98 | barectf_config_parse_common._append_error_ctx(exc, 'Configuration', 'Cannot load YAML file') | |
2d55dc7d PP |
99 | |
100 | # satisfy static type checker (never reached) | |
101 | raise |