constraints.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from pathlib import Path
  2. from datalad.support.constraints import (
  3. Constraint,
  4. EnsureStr,
  5. )
  6. class EnsureDatasetSiblingName(EnsureStr):
  7. # we cannot really test this, because we have no access to a repo
  8. # TODO think about expanding the __call__ API to take a positional
  9. # a the key value (status quo), but also take a range of kwargs
  10. # such that a constraint could be validated more than once
  11. # (i.e. not just by argparse at the CLI, but also inside an
  12. # implementation, maybe once a dataset context is known).
  13. # Could also be implemented by a dedicated `valid_for(value, dataset)`
  14. def __init__(self):
  15. # basic protection against an empty label
  16. super().__init__(min_len=1)
  17. def long_description(self):
  18. return 'value must be the name of a dataset sibling'
  19. def short_description(self):
  20. return 'sibling name'
  21. class EnsureExistingDirectory(Constraint):
  22. def __call__(self, value):
  23. if not Path(value).is_dir():
  24. raise ValueError(
  25. f"{value} is not an existing directory")
  26. return value
  27. def short_description(self):
  28. return 'existing directory'