Klimi's new dotfiles with stow.
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.

38 lines
1.1 KiB

5 years ago
  1. """Glue for the "yapf" library.
  2. """
  3. import os
  4. import sys
  5. from elpy.rpc import Fault
  6. YAPF_NOT_SUPPORTED = sys.version_info < (2, 7) or (
  7. sys.version_info >= (3, 0) and sys.version_info < (3, 4))
  8. try:
  9. if YAPF_NOT_SUPPORTED:
  10. yapf_api = None
  11. else:
  12. from yapf.yapflib import yapf_api
  13. from yapf.yapflib import file_resources
  14. except ImportError: # pragma: no cover
  15. yapf_api = None
  16. def fix_code(code, directory):
  17. """Formats Python code to conform to the PEP 8 style guide.
  18. """
  19. if not yapf_api:
  20. raise Fault('yapf not installed', code=400)
  21. style_config = file_resources.GetDefaultStyleForDir(directory or os.getcwd())
  22. try:
  23. reformatted_source, _ = yapf_api.FormatCode(code,
  24. filename='<stdin>',
  25. style_config=style_config,
  26. verify=False)
  27. return reformatted_source
  28. except Exception as e:
  29. raise Fault("Error during formatting: {}".format(e),
  30. code=400)