/click/_unicodefun.py

https://gitlab.com/bigger-picture-develop-group/batch-maya-processing · Python · 114 lines · 96 code · 13 blank · 5 comment · 24 complexity · 544499dc6e8a061ef3c8d9b8a9b3c479 MD5 · raw file

  1. import os
  2. import sys
  3. import codecs
  4. from ._compat import PY2
  5. # If someone wants to vendor click, we want to ensure the
  6. # correct package is discovered. Ideally we could use a
  7. # relative import here but unfortunately Python does not
  8. # support that.
  9. click = sys.modules[__name__.rsplit('.', 1)[0]]
  10. def _find_unicode_literals_frame():
  11. import __future__
  12. frm = sys._getframe(1)
  13. idx = 1
  14. while frm is not None:
  15. if frm.f_globals.get('__name__', '').startswith('click.'):
  16. frm = frm.f_back
  17. idx += 1
  18. elif frm.f_code.co_flags & __future__.unicode_literals.compiler_flag:
  19. return idx
  20. else:
  21. break
  22. return 0
  23. def _check_for_unicode_literals():
  24. if not __debug__:
  25. return
  26. if not PY2 or click.disable_unicode_literals_warning:
  27. return
  28. bad_frame = _find_unicode_literals_frame()
  29. if bad_frame <= 0:
  30. return
  31. from warnings import warn
  32. warn(Warning('Click detected the use of the unicode_literals '
  33. '__future__ import. This is heavily discouraged '
  34. 'because it can introduce subtle bugs in your '
  35. 'code. You should instead use explicit u"" literals '
  36. 'for your unicode strings. For more information see '
  37. 'http://click.pocoo.org/python3/'),
  38. stacklevel=bad_frame)
  39. def _verify_python3_env():
  40. """Ensures that the environment is good for unicode on Python 3."""
  41. if PY2:
  42. return
  43. try:
  44. import locale
  45. fs_enc = codecs.lookup(locale.getpreferredencoding()).name
  46. except Exception:
  47. fs_enc = 'ascii'
  48. if fs_enc != 'ascii':
  49. return
  50. extra = ''
  51. if os.name == 'posix':
  52. import subprocess
  53. rv = subprocess.Popen(['locale', '-a'], stdout=subprocess.PIPE,
  54. stderr=subprocess.PIPE).communicate()[0]
  55. good_locales = set()
  56. has_c_utf8 = False
  57. for line in rv.splitlines():
  58. locale = line.strip()
  59. if locale.lower().endswith(('.utf-8', '.utf8')):
  60. good_locales.add(locale)
  61. if locale.lower() in ('c.utf8', 'c.utf-8'):
  62. has_c_utf8 = True
  63. extra += '\n\n'
  64. if not good_locales:
  65. extra += (
  66. 'Additional information: on this system no suitable UTF-8\n'
  67. 'locales were discovered. This most likely requires resolving\n'
  68. 'by reconfiguring the locale system.'
  69. )
  70. elif has_c_utf8:
  71. extra += (
  72. 'This system supports the C.UTF-8 locale which is recommended.\n'
  73. 'You might be able to resolve your issue by exporting the\n'
  74. 'following environment variables:\n\n'
  75. ' export LC_ALL=C.UTF-8\n'
  76. ' export LANG=C.UTF-8'
  77. )
  78. else:
  79. extra += (
  80. 'This system lists a couple of UTF-8 supporting locales that\n'
  81. 'you can pick from. The following suitable locales where\n'
  82. 'discovered: %s'
  83. ) % ', '.join(sorted(good_locales))
  84. bad_locale = None
  85. for locale in os.environ.get('LC_ALL'), os.environ.get('LANG'):
  86. if locale and locale.lower().endswith(('.utf-8', '.utf8')):
  87. bad_locale = locale
  88. if locale is not None:
  89. break
  90. if bad_locale is not None:
  91. extra += (
  92. '\n\nClick discovered that you exported a UTF-8 locale\n'
  93. 'but the locale system could not pick up from it because\n'
  94. 'it does not exist. The exported locale is "%s" but it\n'
  95. 'is not supported'
  96. ) % bad_locale
  97. raise RuntimeError('Click will abort further execution because Python 3 '
  98. 'was configured to use ASCII as encoding for the '
  99. 'environment. Either run this under Python 2 or '
  100. 'consult http://click.pocoo.org/python3/ for '
  101. 'mitigation steps.' + extra)