You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
723 B

4 years ago
  1. # coding: utf8
  2. from __future__ import unicode_literals
  3. from pathlib import Path
  4. import sys
  5. is_python2 = sys.version_info[0] == 2
  6. is_python3 = sys.version_info[0] == 3
  7. if is_python2:
  8. basestring_ = basestring # noqa: F821
  9. else:
  10. basestring_ = str
  11. def force_path(location, require_exists=True):
  12. if not isinstance(location, Path):
  13. location = Path(location)
  14. if require_exists and not location.exists():
  15. raise ValueError("Can't read file: {}".format(location))
  16. return location
  17. def force_string(location):
  18. if isinstance(location, basestring_):
  19. return location
  20. if sys.version_info[0] == 2: # Python 2
  21. return str(location).decode("utf8")
  22. return str(location)