Add package inclusion dir. at the API level
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 29 Jul 2020 14:31:30 +0000 (10:31 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Wed, 29 Jul 2020 14:31:30 +0000 (10:31 -0400)
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 <eeppeliteloop@gmail.com>
barectf/cli.py
barectf/config.py
barectf/config_parse.py
barectf/config_parse_common.py
barectf/config_parse_v2.py
barectf/config_parse_v3.py

index effe4270048be9d668e28c5ed40d3b99ebd3538b..33ebeaf735c75b1943ed5648abe86541fbbd6d49 100644 (file)
@@ -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)
index a33dea4e063264ac336222f0e9965fcfa39fb0d1..44b85fa104c3c8f752dd483a9831dc32adf6e37d 100644 (file)
@@ -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):
index 0899a349d9789290ef2d06e454f5e0330bfbb828..4b26baa9b192124e5bd2776bd475d7ac7d1705fd 100644 (file)
@@ -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)
index 8a468c0b1241aff0feb4b98a11c3d8f62e3fb0da..06e0c4242a9cdf5a5a91be4774cc8bdf919d9774 100644 (file)
@@ -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()
index 454cba5a721ad25c7f2bb70297780387da901e0e..4faea4c0956e2739e7463fc32ed7bde0bb5c79ea 100644 (file)
@@ -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,
index 18378b9099d7a1ca5b939da2bbbf13a25a461ffb..fe5a1d0faecfbadd2ed0d584f431d48efd12bd29 100644 (file)
@@ -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
 
This page took 0.026739 seconds and 4 git commands to generate.