xspec.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """
  2. (c) 2008-2013, holger krekel
  3. """
  4. class XSpec:
  5. """ Execution Specification: key1=value1//key2=value2 ...
  6. * keys need to be unique within the specification scope
  7. * neither key nor value are allowed to contain "//"
  8. * keys are not allowed to contain "="
  9. * keys are not allowed to start with underscore
  10. * if no "=value" is given, assume a boolean True value
  11. """
  12. # XXX allow customization, for only allow specific key names
  13. popen = ssh = socket = python = chdir = nice = \
  14. dont_write_bytecode = execmodel = None
  15. def __init__(self, string):
  16. self._spec = string
  17. self.env = {}
  18. for keyvalue in string.split("//"):
  19. i = keyvalue.find("=")
  20. if i == -1:
  21. key, value = keyvalue, True
  22. else:
  23. key, value = keyvalue[:i], keyvalue[i + 1:]
  24. if key[0] == "_":
  25. raise AttributeError("%r not a valid XSpec key" % key)
  26. if key in self.__dict__:
  27. raise ValueError("duplicate key: {!r} in {!r}".format(key, string))
  28. if key.startswith("env:"):
  29. self.env[key[4:]] = value
  30. else:
  31. setattr(self, key, value)
  32. def __getattr__(self, name):
  33. if name[0] == "_":
  34. raise AttributeError(name)
  35. return None
  36. def __repr__(self):
  37. return "<XSpec {!r}>".format(self._spec)
  38. def __str__(self):
  39. return self._spec
  40. def __hash__(self):
  41. return hash(self._spec)
  42. def __eq__(self, other):
  43. return self._spec == getattr(other, '_spec', None)
  44. def __ne__(self, other):
  45. return self._spec != getattr(other, '_spec', None)
  46. def _samefilesystem(self):
  47. return self.popen is not None and self.chdir is None