-
Notifications
You must be signed in to change notification settings - Fork 5
/
cf-beautify
executable file
·104 lines (95 loc) · 4.27 KB
/
cf-beautify
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
from __future__ import absolute_import
from __future__ import print_function
from cfbeautifier.color import Color
from cfbeautifier.version_abstraction import string_from_file, string_from_stream, write_stream
from cfbeautifier import beautifier
import codecs
import os
import sys
def write_output(path, output):
if path:
with open(path, 'w') as file:
write_stream(file, output)
else:
write_stream(sys.stdout, output)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-v', action = 'count', default = 0, dest = 'verbose',
help = "Verbose mode, repeat for more detail")
parser.add_argument("-o", "--out", dest = "output_path",
help = "Output file. Cannot be used with multiple input files",
metavar = "FILE")
parser.add_argument("--stdout", action = "store_true", dest = "use_stdout",
help = "Write to stdout instead of output file or overwriting original")
parser.add_argument("-c", "--keep-empty", action = "store_true",
dest = "keeps_empty_promise_types", help = "Keep empty promise types")
parser.add_argument("-n", "--keep-order", action = "store_true",
dest = "keeps_promise_type_order",
help = "Does not sort promise types to evaluation order")
parser.add_argument("-p", "--page-width",
type = int,
dest = "page_width",
help = "Page width, default %d" % beautifier.Options().page_width)
parser.add_argument("-l", "--line-endings",
dest = "line_endings",
help = "Line endings: 'windows', 'unix', 'detect'. Default 'detect'")
parser.add_argument("input_paths", nargs = "*",
help = """
Source .cf file paths. If input paths are not specified, reads
from stdin instead. If --out is not specified, use of stdin assumes
--stdout.")
""")
args = parser.parse_args()
paths = []
for input_path in args.input_paths:
if os.path.isdir(input_path):
for root, sub_folders, files in os.walk(input_path):
paths.extend(map(lambda name: os.path.join(root, name),
filter(lambda name: name.endswith(".cf"), files)))
elif not os.path.isfile(input_path):
print("Cannot find file '%s'" % input_path)
exit(-1)
else:
paths.append(input_path)
if args.output_path and 1 < len(paths):
print("output_path with multiple input paths is not supported.")
exit(-1)
def print_verbose(string, **kwargs):
if args.verbose != 0:
print(string, **kwargs)
sys.stdout.flush()
# beautifier will detect line endings
options = beautifier.Options()
if args.keeps_empty_promise_types:
options.removes_empty_promise_types = False
if args.keeps_promise_type_order:
options.sorts_promise_types_to_evaluation_order = False
if args.page_width:
options.page_width = args.page_width
if args.line_endings:
if args.line_endings == "windows":
options.line_endings = "\r\n"
elif args.line_endings == "unix":
options.line_endings = "\n"
elif not args.line_endings == "detect":
print("Invalid line endings: '%s'" % args.line_endings)
exit(-1)
# stdin?
if not paths:
input = string_from_stream(sys.stdin)
output = beautifier.beautified_string(input, options)
write_output(args.output_path, output)
# beautify given file names
for path in paths:
print_verbose("Processing... " + path, end = "")
input = string_from_file(path)
output = beautifier.beautified_string(input, options)
if args.output_path or args.use_stdout:
write_output(args.output_path, output)
elif input != output:
print_verbose(Color.green(" -> Beautified"))
write_output(path, output)
else:
print_verbose("") # add line feed