-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter.py
More file actions
48 lines (34 loc) · 1.27 KB
/
formatter.py
File metadata and controls
48 lines (34 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import subprocess
import sys
__all__ = ['Formatter']
class Formatter():
''' Launches clang-format to ensure the input and output is decently formatted'''
def __init__(self, style_override='Chromium'):
self.style_override = style_override
def _open(self, file_path):
with open(file_path) as f:
while True:
line = f.readline()
if not line:
break
yield line
def launch_batch(self, lines_list):
return self.launch(''.join([s + '\n' for s in lines_list]))
def launch(self, lines):
command = ['clang-format']
if self.style_override:
command = ['clang-format', '-style=' + self.style_override]
p = subprocess.Popen(
command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
shell=False)
_out = p.communicate(input=lines.encode())[0]
return (_out.decode().split('\n'))
def open_and_launch(self, file_path):
''' launches the clang_format with specified clang_format style if provided.
It outputs a list of strings properly formatted'''
lines = ""
for l in self._open(file_path):
lines += l
return self.launch(lines)