From 1d55a17623bf19401ccc6987a06885334bf7f8b5 Mon Sep 17 00:00:00 2001 From: Lowell Alleman Date: Fri, 22 Dec 2023 22:02:41 -0500 Subject: [PATCH] Layer: remove _path_join() helper - Remove special path join helper function by using PurePath(), or '.' rather than None to represent empty path components. --- ksconf/layer.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/ksconf/layer.py b/ksconf/layer.py index 8129509..9624bb9 100644 --- a/ksconf/layer.py +++ b/ksconf/layer.py @@ -55,12 +55,7 @@ """ - -def _path_join(*parts) -> Path: - """ A slightly smarter / more flexible path appender. - Drop any None - """ - return Path(*filter(None, parts)) +no_path = PurePath() # Exceptions @@ -207,11 +202,11 @@ def match(path: PurePath): @cached_property def physical_path(self) -> Path: - return _path_join(self.layer.root, self.layer.physical_path, self.relative_path) + return Path(self.layer.root, self.layer.physical_path, self.relative_path) @cached_property def logical_path(self) -> Path: - return _path_join(self.layer.logical_path, self.relative_path) + return Path(self.layer.logical_path, self.relative_path) @property def resource_path(self): @@ -287,12 +282,12 @@ def transform_name(path: PurePath) -> PurePath: @cached_property def logical_path(self) -> Path: - return _path_join(self.layer.logical_path, - self.transform_name(self.relative_path)) + return Path(self.layer.logical_path, + self.transform_name(self.relative_path)) @cached_property def physical_path(self) -> Path: - return _path_join(self.layer.root, self.layer.physical_path, self.relative_path) + return Path(self.layer.root, self.layer.physical_path, self.relative_path) @property def resource_path(self) -> Path: @@ -459,7 +454,7 @@ def walk(self) -> R_walk: and given directories. Paths are relative. """ # In the simple case, this is good enough. Some subclasses will need to override - for (root, dirs, files) in relwalk(_path_join(self.root, self.physical_path), + for (root, dirs, files) in relwalk(Path(self.root, self.physical_path), followlinks=self.context.follow_symlink): root = Path(root) files = [f for f in files if not self.context.block_files.search(f)] @@ -680,7 +675,7 @@ def add_layer(self, path: Path): if not path.is_dir(): raise LayerUsageException("Layers must be directories. " f"Given path '{path}' is not a directory.") - layer = Layer(layer_name, path, None, None, context=self.context, + layer = Layer(layer_name, path, no_path, no_path, context=self.context, file_factory=layer_file_factory) super().add_layer(layer) @@ -783,7 +778,7 @@ def set_root(self, root: Path, follow_symlinks=None): """ Set a root path, and auto discover all '.d' directories. Note: We currently only support ``.d/`` directories, a file like - ``default.d/10-props.conf`` won't be handled here. + ``props.conf.d/10-upstream`` won't be handled here. A valid name would be ``default.d/10-name/props.conf``. """ root = Path(root) @@ -826,7 +821,7 @@ def set_root(self, root: Path, follow_symlinks=None): prune_points = [mount / layer for mount, layers in self._mount_points.items() for layer in layers] - layer = DotdLayer("", root, None, None, context=self.context, + layer = DotdLayer("", root, no_path, no_path, context=self.context, type=LayerType.IMPLICIT, file_factory=layer_file_factory, prune_points=prune_points)