<commit_before>"""
convert.py

Parses Santander UK account reports and produces a JSON file with the transactions.

Copyright (C) 2016 by <NAME>
Please the the file /LICENSE for the license details.
"""

import argparse
import json
import creditparser
import debitparser


def convert():
	args = parse_arguments()

	if creditparser.is_credit_report(args.input_file):
		output = creditparser.parse_file(args.input_file)
	elif debitparser.is_debit_report(args.input_file):
		output = debitparser.parse_file(args.input_file)

	with open(args.output_file, "w") as f:
		json.dump(output, f, indent = 4, separators = (",", ": "))


def parse_arguments():
	""" Parses the command-line arguments. """

	parser = argparse.ArgumentParser(
		description = "Converts Santander UK account reports to JSON.")
	parser.add_argument("input_file", help = "The input report path.")
	parser.add_argument("output_file", help = "The output report path.")

	return parser.parse_args()


if __name__ == "__main__":
	convert()<commit_msg>Added merge functionality to the reports converter.<commit_after>"""
convert.py

Parses Santander UK account reports and produces a JSON file with the transactions.

Copyright (C) 2016 by <NAME>
Please the the file /LICENSE for the license details.
"""

import argparse
import os
import json
import creditparser
import debitparser


def convert():
	"""Converts the supplied files."""

	args = parse_arguments()

	merged = []
	for report in args.reports:
		transactions = []
		if creditparser.is_credit_report(report):
			transactions = creditparser.parse_file(report)
		elif debitparser.is_debit_report(report):
			transactions = debitparser.parse_file(report)
		else:
			raise Exception(report + " isn't a known report.")

		if args.merge:
			merged.extend(transactions)
			print(report + " converted.")
		else:
			name, ext = os.path.splitext(report)
			output = name + ".json"
			save_transactions(transactions, output)
			print(report + " converted to " + output)

	if args.merge:
		save_transactions(merged, args.merge)
		print("All transactions saved to " + args.merge)


def save_transactions(transactions, output):
	"""Writes a bunch of transactions to a JSON file."""
	with open(output, "w") as f:
		json.dump(transactions, f, indent = 4, separators = (",", ": "))


def parse_arguments():
	""" Parses the command-line arguments. """

	parser = argparse.ArgumentParser(
		description = "Converts Santander UK account reports to JSON.")
	parser.add_argument("reports", nargs='+', help = "the input reports paths (supports wildcards).")
	parser.add_argument("-merge", help = "optionally merges all transactions into a single JSON file.")

	return parser.parse_args()


if __name__ == "__main__":
	convert()
{'fraction_non_alphanumeric': 0.11518708730741012, 'fraction_numerical': 0.003668378576669112, 'mean_word_length': 4.802127659574468, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6770389', 'n_tokens_mistral': 876, 'n_tokens_neox': 804, 'n_words': 257}