cli_utils.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from collections.abc import Sequence
  2. from distutils.util import strtobool as dist_strtobool
  3. import sys
  4. import numpy
  5. def strtobool(x):
  6. # distutils.util.strtobool returns integer, but it's confusing,
  7. return bool(dist_strtobool(x))
  8. def get_commandline_args():
  9. extra_chars = [
  10. " ",
  11. ";",
  12. "&",
  13. "(",
  14. ")",
  15. "|",
  16. "^",
  17. "<",
  18. ">",
  19. "?",
  20. "*",
  21. "[",
  22. "]",
  23. "$",
  24. "`",
  25. '"',
  26. "\\",
  27. "!",
  28. "{",
  29. "}",
  30. ]
  31. # Escape the extra characters for shell
  32. argv = [
  33. arg.replace("'", "'\\''")
  34. if all(char not in arg for char in extra_chars)
  35. else "'" + arg.replace("'", "'\\''") + "'"
  36. for arg in sys.argv
  37. ]
  38. return sys.executable + " " + " ".join(argv)
  39. def is_scipy_wav_style(value):
  40. # If Tuple[int, numpy.ndarray] or not
  41. return (
  42. isinstance(value, Sequence)
  43. and len(value) == 2
  44. and isinstance(value[0], int)
  45. and isinstance(value[1], numpy.ndarray)
  46. )
  47. def assert_scipy_wav_style(value):
  48. assert is_scipy_wav_style(
  49. value
  50. ), "Must be Tuple[int, numpy.ndarray], but got {}".format(
  51. type(value)
  52. if not isinstance(value, Sequence)
  53. else "{}[{}]".format(type(value), ", ".join(str(type(v)) for v in value))
  54. )