Skip to content

Commit

Permalink
Treat empty path in config file as None
Browse files Browse the repository at this point in the history
  • Loading branch information
tttapa committed May 22, 2024
1 parent b142418 commit eaf1ba3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/py_build_cmake/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ def get_cmaker(
def cvt_path(x):
if x is None:
return None
if isinstance(x, str) and not x:
return None
assert isinstance(x, (Path, str))
return Path(x)

Expand Down
24 changes: 21 additions & 3 deletions src/py_build_cmake/config/options/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from pathlib import Path, PurePosixPath

from ...common import ConfigError
from .config_option import ConfigOption
from .config_path import ConfPath
from .default import DefaultValue
from .string import StringConfigOption
from .value_reference import ValueReference


Expand All @@ -22,7 +22,7 @@ class RelativeToProject:
description: str = "project directory"


class PathConfigOption(StringConfigOption):
class PathConfigOption(ConfigOption):
def __init__(
self,
name: str,
Expand Down Expand Up @@ -58,6 +58,22 @@ def __init__(
def get_typename(self, md: bool = False):
return "path" if self.is_folder else "filepath"

def override(self, old_value, new_value):
if new_value.values is None:
return old_value.values
return new_value.values

def _verify_string(self, values: ValueReference):
if self.sub_options:
msg = f"Type of {values.value_path} should be {str}, "
msg += f"not {dict}"
raise ConfigError(msg)
elif not isinstance(values.values, str):
msg = f"Type of {values.value_path} should be {str}, "
msg += f"not {type(values.values)}"
raise ConfigError(msg)
return values.values

def check_path(self, values: ValueReference):
assert isinstance(values.values, str)
path: Path | PurePosixPath = Path(values.values)
Expand Down Expand Up @@ -101,5 +117,7 @@ def check_path(self, values: ValueReference):
return path.resolve() if isinstance(path, Path) else path

def verify(self, values: ValueReference):
values.values = super().verify(values)
values.values = self._verify_string(values)
if not values.values:
return values.values
return self.check_path(values)

0 comments on commit eaf1ba3

Please sign in to comment.