def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hf:o:ct", ["help", "file=", "output=","csv","tsv"])
    except getopt.GetoptError as err:
        print (str(err))
        usage()
    outfile,format = None,None
    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
        elif o in ("-f", "--file"):
            try:
                f = open(a, "rb")
            except:
                print ("Unable to read file:", a)
                usage()
            args = [line for line in f.read().splitlines() if line.strip()]
            f.close()
        elif o in ("-o", "--output"):
            outfile = a
        elif o in ("-c", "--csv"):
            format = "csv"
        elif o in ("-t", "--tsv"):
            format = "tsv"
    # Whatever args remain are considered serials
    serials = args
    warranty_dicts = []

    print(offline_estimated_manufacture("C02PQB5VG8WP"))

    for serial in (serials or my_serial()):
        results = warranty(serial)
        # print(results)
        if type(results) == dict:
             results = [results]
        warranty_dicts.extend(results)
    csv.register_dialect('exceltab', delimiter='\t')
    csv.register_dialect('excel', delimiter=',')
    # writer = UnicodeWriter(outfile, dialect='exceltab')
    if (not format):
        plain_format = ""
        for result in warranty_dicts:
            plain_format += "%s: %s\n" % (u'SERIAL_ID', result[u'SERIAL_ID'])
            plain_format += "%s: %s\n" % (u'PROD_DESCR', result[u'PROD_DESCR'])
            for key,val in sorted(result.items(), key=lambda x: x[0]):
                if (key not in (u'SERIAL_ID', u'PROD_DESCR', u'ERROR_CODE')):
                    plain_format += "%s: %s\n" % (key, val)
            plain_format += "%s: %s\n\n" % (u'ERROR_CODE', result[u'ERROR_CODE'])
        if (not outfile):
            sys.stdout.write(plain_format)
        else:
            open(outfile,"wb").write(plain_format)
    elif (format in ['csv','tsv']):
        if (not outfile):
            outfile = sys.stdout
        else:
            outfile = open(outfile, "wb")
        dialect = {'csv': 'excel', 'tsv': 'exceltab'}[format]
        writer = UnicodeWriter(outfile, dialect=dialect)
        # write out headers
        sample_machine = blank_machine_dict()
        header_row = [u'SERIAL_ID', u'PROD_DESCR']
        for key,val in sorted(sample_machine.items(), key=lambda x: x[0]):
            if (key not in (u'SERIAL_ID', u'PROD_DESCR', u'ERROR_CODE')):
                header_row.append(key)
        header_row.append(u'ERROR_CODE')
        writer.writerow(header_row)
        for result in warranty_dicts:
            row_data = [result[u'SERIAL_ID']]
            row_data.append(result[u'PROD_DESCR'])
            for key,val in sorted(result.items(), key=lambda x: x[0]):
                if (key not in (u'SERIAL_ID', u'PROD_DESCR', u'ERROR_CODE')):
                    row_data.append(val)
            row_data.append(result[u'ERROR_CODE'])
            writer.writerow(row_data)
dataset/ETHPy150Open cwolfram/inge/lib/getwarranty.py/main/original