Add package inclusion dir. at the API level
[deliverable/barectf.git] / barectf / config_parse.py
1 # The MIT License (MIT)
2 #
3 # Copyright (c) 2015-2020 Philippe Proulx <pproulx@efficios.com>
4 #
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:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
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.
23
24 import barectf.config_parse_common as barectf_config_parse_common
25 from barectf.config_parse_common import _ConfigurationParseError
26 import barectf.config_parse_v2 as barectf_config_parse_v2
27 import barectf.config_parse_v3 as barectf_config_parse_v3
28 import collections
29
30
31 # Creates and returns a barectf 3 YAML configuration file parser to
32 # parse the file-like object `file`.
33 #
34 # `file` can be a barectf 2 or 3 configuration file.
35 def _create_v3_parser(file, with_pkg_include_dir, include_dirs, ignore_include_not_found):
36 try:
37 root_node = barectf_config_parse_common._yaml_load(file)
38
39 if type(root_node) is barectf_config_parse_common._ConfigNodeV3:
40 # barectf 3 configuration file
41 return barectf_config_parse_v3._Parser(file, root_node, with_pkg_include_dir,
42 include_dirs, ignore_include_not_found)
43 elif type(root_node) is collections.OrderedDict:
44 # barectf 2 configuration file
45 v2_parser = barectf_config_parse_v2._Parser(file, root_node, with_pkg_include_dir,
46 include_dirs, ignore_include_not_found)
47 return barectf_config_parse_v3._Parser(file, v2_parser.config_node,
48 with_pkg_include_dir, include_dirs,
49 ignore_include_not_found)
50 else:
51 raise _ConfigurationParseError('Configuration',
52 f'Root (configuration) node is not an object (it\'s a `{type(root_node)}`)')
53 except _ConfigurationParseError as exc:
54 barectf_config_parse_common._append_error_ctx(exc, 'Configuration',
55 'Cannot create configuration from YAML file')
56
57
58 def _from_file(file, with_pkg_include_dir, include_dirs, ignore_include_not_found):
59 return _create_v3_parser(file, with_pkg_include_dir, include_dirs, ignore_include_not_found).config
60
61
62 def _effective_config_file(file, with_pkg_include_dir, include_dirs, ignore_include_not_found,
63 indent_space_count):
64 config_node = _create_v3_parser(file, with_pkg_include_dir, include_dirs,
65 ignore_include_not_found).config_node
66 return barectf_config_parse_common._yaml_dump(config_node, indent=indent_space_count,
67 default_flow_style=False, explicit_start=True,
68 explicit_end=True)
69
70
71 def _config_file_major_version(file):
72 try:
73 root_node = barectf_config_parse_common._yaml_load(file)
74
75 if type(root_node) is barectf_config_parse_common._ConfigNodeV3:
76 # barectf 3 configuration file
77 return 3
78 elif type(root_node) is collections.OrderedDict:
79 # barectf 2 configuration file
80 return 2
81 except _ConfigurationParseError as exc:
82 barectf_config_parse_common._append_error_ctx(exc, 'Configuration', 'Cannot load YAML file')
This page took 0.032492 seconds and 5 git commands to generate.