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.

66 lines
2.1 KiB

4 years ago
  1. # -*- coding: utf-8 -*-
  2. """
  3. werkzeug.filesystem
  4. ~~~~~~~~~~~~~~~~~~~
  5. Various utilities for the local filesystem.
  6. :copyright: (c) 2015 by the Werkzeug Team, see AUTHORS for more details.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. import codecs
  10. import sys
  11. import warnings
  12. # We do not trust traditional unixes.
  13. has_likely_buggy_unicode_filesystem = \
  14. sys.platform.startswith('linux') or 'bsd' in sys.platform
  15. def _is_ascii_encoding(encoding):
  16. """
  17. Given an encoding this figures out if the encoding is actually ASCII (which
  18. is something we don't actually want in most cases). This is necessary
  19. because ASCII comes under many names such as ANSI_X3.4-1968.
  20. """
  21. if encoding is None:
  22. return False
  23. try:
  24. return codecs.lookup(encoding).name == 'ascii'
  25. except LookupError:
  26. return False
  27. class BrokenFilesystemWarning(RuntimeWarning, UnicodeWarning):
  28. '''The warning used by Werkzeug to signal a broken filesystem. Will only be
  29. used once per runtime.'''
  30. _warned_about_filesystem_encoding = False
  31. def get_filesystem_encoding():
  32. """
  33. Returns the filesystem encoding that should be used. Note that this is
  34. different from the Python understanding of the filesystem encoding which
  35. might be deeply flawed. Do not use this value against Python's unicode APIs
  36. because it might be different. See :ref:`filesystem-encoding` for the exact
  37. behavior.
  38. The concept of a filesystem encoding in generally is not something you
  39. should rely on. As such if you ever need to use this function except for
  40. writing wrapper code reconsider.
  41. """
  42. global _warned_about_filesystem_encoding
  43. rv = sys.getfilesystemencoding()
  44. if has_likely_buggy_unicode_filesystem and not rv \
  45. or _is_ascii_encoding(rv):
  46. if not _warned_about_filesystem_encoding:
  47. warnings.warn(
  48. 'Detected a misconfigured UNIX filesystem: Will use UTF-8 as '
  49. 'filesystem encoding instead of {0!r}'.format(rv),
  50. BrokenFilesystemWarning)
  51. _warned_about_filesystem_encoding = True
  52. return 'utf-8'
  53. return rv