From 7cd4634e4194ec331268a2495b91a43e6ee8ef78 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Wed, 29 Jul 2020 10:31:30 -0400 Subject: [PATCH] Add package inclusion dir. at the API level This patch adds a `with_package_inclusion_directory` boolean parameter (true by default) to the barectf.effective_configuration_file() and barectf.configuration_from_file() functions. If this new parameter is true, then the package inclusion directory (`barectf/include/2` or `barectf/include/3`, depending on the configuration file's version) is appended to the provided inclusion directories. This patch moves this logic from the CLI module to the configuration modules. It is my understanding that users would want to be able to include package YAML files using only the API. In `cli.py`, _parse_args() now simply appends the current working directory, so that the order stays the same: 1. `--include-dir` options, in order. 2. Current working directory. 3. Package inclusion directory. Signed-off-by: Philippe Proulx --- barectf/cli.py | 21 ++++----------------- barectf/config.py | 18 ++++++++++++++---- barectf/config_parse.py | 23 +++++++++++++---------- barectf/config_parse_common.py | 15 +++++++++++---- barectf/config_parse_v2.py | 4 ++-- barectf/config_parse_v3.py | 11 ++++------- 6 files changed, 48 insertions(+), 44 deletions(-) diff --git a/barectf/cli.py b/barectf/cli.py index effe427..33ebeaf 100644 --- a/barectf/cli.py +++ b/barectf/cli.py @@ -99,21 +99,8 @@ def _parse_args(): if not os.path.isfile(args.config): _print_error(f'`{args.config}` is not an existing, regular file') - # Load configuration file to get its major version in order to - # append the correct implicit inclusion directory. - try: - with open(args.config) as f: - config_major_version = barectf.configuration_file_major_version(f) - except barectf._ConfigurationParseError as exc: - _print_config_error(exc) - except Exception as exc: - _print_unknown_exc(exc) - - # append current working directory and implicit inclusion directory - args.include_dir += [ - os.getcwd(), - pkg_resources.resource_filename(__name__, f'include/{config_major_version}') - ] + # append current working directory + args.include_dir.append(os.getcwd()) return args @@ -127,14 +114,14 @@ def run(): with open(args.config) as f: if args.dump_config: # print effective configuration file - print(barectf.effective_configuration_file(f, args.include_dir, + print(barectf.effective_configuration_file(f, True, args.include_dir, args.ignore_include_not_found)) # barectf.configuration_from_file() reads the file again # below: rewind. f.seek(0) - config = barectf.configuration_from_file(f, args.include_dir, + config = barectf.configuration_from_file(f, True, args.include_dir, args.ignore_include_not_found) except barectf._ConfigurationParseError as exc: _print_config_error(exc) diff --git a/barectf/config.py b/barectf/config.py index a33dea4..44b85fa 100644 --- a/barectf/config.py +++ b/barectf/config.py @@ -846,15 +846,25 @@ class Configuration: return self._options -def effective_configuration_file(file, inclusion_dirs, ignore_inclusion_not_found, +def effective_configuration_file(file, with_package_inclusion_directory=True, + inclusion_directories=None, ignore_inclusion_not_found=False, indent_space_count=2): - return barectf_config_parse._effective_config_file(file, inclusion_dirs, + if inclusion_directories is None: + inclusion_directories = [] + + return barectf_config_parse._effective_config_file(file, with_package_inclusion_directory, + inclusion_directories, ignore_inclusion_not_found, indent_space_count) -def configuration_from_file(file, inclusion_dirs, ignore_inclusion_not_found): - return barectf_config_parse._from_file(file, inclusion_dirs, ignore_inclusion_not_found) +def configuration_from_file(file, with_package_inclusion_directory=True, inclusion_directories=None, + ignore_inclusion_not_found=False): + if inclusion_directories is None: + inclusion_directories = [] + + return barectf_config_parse._from_file(file, with_package_inclusion_directory, + inclusion_directories, ignore_inclusion_not_found) def configuration_file_major_version(file): diff --git a/barectf/config_parse.py b/barectf/config_parse.py index 0899a34..4b26baa 100644 --- a/barectf/config_parse.py +++ b/barectf/config_parse.py @@ -32,19 +32,20 @@ import collections # parse the file-like object `file`. # # `file` can be a barectf 2 or 3 configuration file. -def _create_v3_parser(file, include_dirs, ignore_include_not_found): +def _create_v3_parser(file, with_pkg_include_dir, include_dirs, ignore_include_not_found): try: root_node = barectf_config_parse_common._yaml_load(file) if type(root_node) is barectf_config_parse_common._ConfigNodeV3: # barectf 3 configuration file - return barectf_config_parse_v3._Parser(file, root_node, include_dirs, - ignore_include_not_found) + return barectf_config_parse_v3._Parser(file, root_node, with_pkg_include_dir, + include_dirs, ignore_include_not_found) elif type(root_node) is collections.OrderedDict: # barectf 2 configuration file - v2_parser = barectf_config_parse_v2._Parser(file, root_node, include_dirs, - ignore_include_not_found) - return barectf_config_parse_v3._Parser(file, v2_parser.config_node, include_dirs, + v2_parser = barectf_config_parse_v2._Parser(file, root_node, with_pkg_include_dir, + include_dirs, ignore_include_not_found) + return barectf_config_parse_v3._Parser(file, v2_parser.config_node, + with_pkg_include_dir, include_dirs, ignore_include_not_found) else: raise _ConfigurationParseError('Configuration', @@ -54,12 +55,14 @@ def _create_v3_parser(file, include_dirs, ignore_include_not_found): 'Cannot create configuration from YAML file') -def _from_file(file, include_dirs, ignore_include_not_found): - return _create_v3_parser(file, include_dirs, ignore_include_not_found).config +def _from_file(file, with_pkg_include_dir, include_dirs, ignore_include_not_found): + return _create_v3_parser(file, with_pkg_include_dir, include_dirs, ignore_include_not_found).config -def _effective_config_file(file, include_dirs, ignore_include_not_found, indent_space_count=2): - config_node = _create_v3_parser(file, include_dirs, ignore_include_not_found).config_node +def _effective_config_file(file, with_pkg_include_dir, include_dirs, ignore_include_not_found, + indent_space_count): + config_node = _create_v3_parser(file, with_pkg_include_dir, include_dirs, + ignore_include_not_found).config_node return barectf_config_parse_common._yaml_dump(config_node, indent=indent_space_count, default_flow_style=False, explicit_start=True, explicit_end=True) diff --git a/barectf/config_parse_common.py b/barectf/config_parse_common.py index 8a468c0..06e0c42 100644 --- a/barectf/config_parse_common.py +++ b/barectf/config_parse_common.py @@ -315,9 +315,11 @@ class _Parser: # path `path`). # # For its _process_node_include() method, the parser considers the - # inclusion directories `include_dirs` and ignores nonexistent - # inclusion files if `ignore_include_not_found` is `True`. - def __init__(self, path, node, include_dirs, ignore_include_not_found, major_version): + # package inclusion directory as well as `include_dirs`, and ignores + # nonexistent inclusion files if `ignore_include_not_found` is + # `True`. + def __init__(self, path, node, with_pkg_include_dir, include_dirs, ignore_include_not_found, + major_version): self._root_path = path self._root_node = node self._ft_prop_names = [ @@ -332,7 +334,12 @@ class _Parser: # barectf 3 'element-field-type', ] - self._include_dirs = include_dirs + + self._include_dirs = copy.copy(include_dirs) + + if with_pkg_include_dir: + self._include_dirs.append(pkg_resources.resource_filename(__name__, f'include/{major_version}')) + self._ignore_include_not_found = ignore_include_not_found self._include_stack = [] self._resolved_ft_aliases = set() diff --git a/barectf/config_parse_v2.py b/barectf/config_parse_v2.py index 454cba5..4faea4c 100644 --- a/barectf/config_parse_v2.py +++ b/barectf/config_parse_v2.py @@ -60,8 +60,8 @@ def _copy_prop_if_exists(dst_node, src_node, src_prop_name, dst_prop_name=None): class _Parser(config_parse_common._Parser): # Builds a barectf 2 YAML configuration parser and parses the root # configuration node `node` (already loaded from `path`). - def __init__(self, path, node, include_dirs, ignore_include_not_found): - super().__init__(path, node, include_dirs, ignore_include_not_found, 2) + def __init__(self, path, node, with_pkg_include_dir, include_dirs, ignore_include_not_found): + super().__init__(path, node, with_pkg_include_dir, include_dirs, ignore_include_not_found, 2) self._ft_cls_name_to_conv_method = { 'int': self._conv_int_ft_node, 'integer': self._conv_int_ft_node, diff --git a/barectf/config_parse_v3.py b/barectf/config_parse_v3.py index 18378b9..fe5a1d0 100644 --- a/barectf/config_parse_v3.py +++ b/barectf/config_parse_v3.py @@ -41,8 +41,9 @@ import uuid class _Parser(barectf_config_parse_common._Parser): # Builds a barectf 3 YAML configuration parser and parses the root # configuration node `node` (already loaded from `path`). - def __init__(self, path, node, inclusion_dirs, ignore_include_not_found): - super().__init__(path, node, inclusion_dirs, ignore_include_not_found, 3) + def __init__(self, path, node, with_pkg_include_dir, inclusion_dirs, ignore_include_not_found): + super().__init__(path, node, with_pkg_include_dir, inclusion_dirs, + ignore_include_not_found, 3) self._ft_cls_name_to_create_method = { 'unsigned-integer': self._create_int_ft, 'signed-integer': self._create_int_ft, @@ -1124,11 +1125,7 @@ class _Parser(barectf_config_parse_common._Parser): except _ConfigurationParseError as exc: _append_error_ctx(exc, f'Structure field type member `{member_name}`') - default_byte_order_prop_name = '$default-byte-order' - default_byte_order_node = self._trace_type_node.get(default_byte_order_prop_name) - - if default_byte_order_prop_name in self._trace_type_node: - del self._trace_type_node[default_byte_order_prop_name] + default_byte_order_node = self._trace_type_node.get('$default-byte-order') self._default_byte_order = None -- 2.34.1