()
| 33 | |
| 34 | |
| 35 | def main(): |
| 36 | opt = get_args() |
| 37 | |
| 38 | # check args |
| 39 | if opt.json: |
| 40 | if not opt.output_file: |
| 41 | raise ValueError("Missing --output_file") |
| 42 | if opt.language: |
| 43 | if opt.language not in PL_MATCHING.keys(): |
| 44 | raise ValueError( |
| 45 | "{language} not supported. Currently support {sp_language}" |
| 46 | .format(language=opt.language, |
| 47 | sp_language=list(PL_MATCHING.keys()))) |
| 48 | |
| 49 | # check path |
| 50 | for path in opt.paths: |
| 51 | assert os.path.exists(path) == True, "paths is not valid" |
| 52 | |
| 53 | if os.path.isdir(path): |
| 54 | files = [os.path.join(path, f) for f in os.listdir(path) \ |
| 55 | if os.path.isfile(os.path.join(path, f))] |
| 56 | elif os.path.isfile(path): |
| 57 | files = [path] |
| 58 | |
| 59 | if opt.language: |
| 60 | for file in files[:]: |
| 61 | filename, file_extension = os.path.splitext(file) |
| 62 | if file_extension not in PL_MATCHING[opt.language]: |
| 63 | files.remove(file) |
| 64 | |
| 65 | output_metadata = {} |
| 66 | for file in files: |
| 67 | filename, file_extension = os.path.splitext(file) |
| 68 | |
| 69 | if opt.language == None: |
| 70 | for lang, ext_list in PL_MATCHING.items(): |
| 71 | if file_extension in ext_list: |
| 72 | language = lang |
| 73 | break |
| 74 | else: |
| 75 | language = opt.language |
| 76 | |
| 77 | output = parse_file(file, language=language) |
| 78 | print_result( |
| 79 | output, |
| 80 | file_name=str(filename).split(os.sep)[-1]+file_extension |
| 81 | ) |
| 82 | output_metadata[file] = output |
| 83 | |
| 84 | if opt.json: |
| 85 | save_path = opt.output_file |
| 86 | with open(save_path, 'w') as output_file: |
| 87 | json.dump(output_metadata, output_file, sort_keys=True, indent=4) |
| 88 | print(50*'=') |
| 89 | print("Save report to {path}".format(path=save_path)) |
| 90 | |
| 91 | |
| 92 | if __name__ == '__main__': |
no test coverage detected