]>
Commit | Line | Data |
---|---|---|
1 | """Glue for the "autopep8" library. | |
2 | ||
3 | """ | |
4 | ||
5 | from elpy.rpc import Fault | |
6 | import os | |
7 | ||
8 | ||
9 | try: | |
10 | import autopep8 | |
11 | except ImportError: # pragma: no cover | |
12 | autopep8 = None | |
13 | ||
14 | ||
15 | def fix_code(code, directory): | |
16 | """Formats Python code to conform to the PEP 8 style guide. | |
17 | ||
18 | """ | |
19 | if not autopep8: | |
20 | raise Fault('autopep8 not installed, cannot fix code.', | |
21 | code=400) | |
22 | old_dir = os.getcwd() | |
23 | try: | |
24 | os.chdir(directory) | |
25 | return autopep8.fix_code(code, apply_config=True) | |
26 | finally: | |
27 | os.chdir(old_dir) |