Klimi's new dotfiles with stow.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

59 linhas
1.8 KiB

há 5 anos
  1. """Glue for the "black" library.
  2. """
  3. import sys
  4. from pkg_resources import parse_version
  5. import os
  6. try:
  7. import toml
  8. except ImportError:
  9. toml = None
  10. from elpy.rpc import Fault
  11. BLACK_NOT_SUPPORTED = sys.version_info < (3, 6)
  12. try:
  13. if BLACK_NOT_SUPPORTED:
  14. black = None
  15. else:
  16. import black
  17. except ImportError: # pragma: no cover
  18. black = None
  19. def fix_code(code, directory):
  20. """Formats Python code to conform to the PEP 8 style guide.
  21. """
  22. if not black:
  23. raise Fault("black not installed", code=400)
  24. # Get black config from pyproject.toml
  25. line_length = black.DEFAULT_LINE_LENGTH
  26. string_normalization = True
  27. pyproject_path = os.path.join(directory, "pyproject.toml")
  28. if toml is not None and os.path.exists(pyproject_path):
  29. pyproject_config = toml.load(pyproject_path)
  30. black_config = pyproject_config.get("tool", {}).get("black", {})
  31. if "line-length" in black_config:
  32. line_length = black_config["line-length"]
  33. if "skip-string-normalization" in black_config:
  34. string_normalization = not black_config["skip-string-normalization"]
  35. try:
  36. if parse_version(black.__version__) < parse_version("19.0"):
  37. reformatted_source = black.format_file_contents(
  38. src_contents=code, line_length=line_length, fast=False)
  39. else:
  40. fm = black.FileMode(
  41. line_length=line_length,
  42. string_normalization=string_normalization)
  43. reformatted_source = black.format_file_contents(
  44. src_contents=code, fast=False, mode=fm)
  45. return reformatted_source
  46. except black.NothingChanged:
  47. return code
  48. except Exception as e:
  49. raise Fault("Error during formatting: {}".format(e), code=400)