Completed
Pull Request — master (#1311)
by Sudheesh
03:55
created

coalib.main()   B

Complexity

Conditions 5

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
dl 0
loc 31
rs 8.0894
1
# This program is free software: you can redistribute it and/or modify it
2
# under the terms of the GNU Affero General Public License as published by the
3
# Free Software Foundation, either version 3 of the License, or (at your
4
# option) any later version.
5
#
6
# This program is distributed in the hope that it will be useful, but WITHOUT
7
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License
9
# for more details.
10
#
11
# You should have received a copy of the GNU Affero General Public License
12
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
13
14
import json
15
import sys
16
17
from coalib.output.printers.ListLogPrinter import ListLogPrinter
18
from coalib.output.JSONEncoder import JSONEncoder
19
from coalib.coala_main import run_coala
20
21
22
def main():
23
    log_printer = ListLogPrinter()
24
25
    results, exitcode = run_coala(log_printer, autoapply=False)
26
27
    retval = {"logs": log_printer.logs, "results": results}
28
    if len(sys.argv) > 2:
29
        try:
30
            flag = sys.argv[-2]
31
            filename = sys.argv[-1]
32
            if flag == '-o':
33
                with open(filename, "w") as fi:
34
                    fi.write(json.dumps(retval,
35
                                        cls=JSONEncoder,
36
                                        sort_keys=True,
37
                                        indent=2,
38
                                        separators=(',', ': ')))
39
                print('Output has been written to ' + filename)
40
            else:
41
                print('There has been an error in the syntax.\n\
42
Please try coala-json -o <filename.json>')
43
        except IndexError:
44
            pass
45
    else:
46
        print(json.dumps(retval,
47
                         cls=JSONEncoder,
48
                         sort_keys=True,
49
                         indent=2,
50
                         separators=(',', ': ')))
51
52
    return exitcode
53