#!/usr/bin/env python import pyaccuwage import argparse import os, os.path import sys """ Command line tool for converting IRS e-file fixed field records to/from JSON or a simple text format. Attempts to load record types from a python module in the current working directory named record_types.py The module must export a RECORD_TYPES list with the names of the classes to import as valid record types. """ def get_record_types(): try: sys.path.append(os.getcwd()) import record_types r = {} for record_type in record_types.RECORD_TYPES: r[record_type] = getattr(record_types, record_type) return r except ImportError: print('warning: using default record types (failed to import record_types.py)') return pyaccuwage.get_record_types() def read_file(fd, filename, record_types): filename, extension = os.path.splitext(filename) if extension == '.json': return pyaccuwage.json_load(fd, record_types) elif extension == '.txt': return pyaccuwage.text_load(fd, record_types) else: return pyaccuwage.load(fd, record_types) def write_file(outfile, filename, records): filename, extension = os.path.splitext(filename) if extension == '.json': pyaccuwage.json_dump(outfile, records) elif extension == '.txt': pyaccuwage.text_dump(outfile, records) else: pyaccuwage.dump(outfile, records) if __name__ == '__main__': parser = argparse.ArgumentParser( description="Convert accuwage efile data between different formats." ) parser.add_argument("-i", '--input', nargs=1, required=True, metavar="file", type=argparse.FileType('r'), help="Source file to convert") parser.add_argument("-o", "--output", nargs=1, required=True, metavar="file", type=argparse.FileType('w'), help="Destination file to output") args = parser.parse_args() in_file = args.input[0] out_file = args.output[0] records = list(read_file(in_file, in_file.name, get_record_types())) write_file(out_file, out_file.name, records)