def main(): log.begin() parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter ) parser.add_argument("in_file", help="Input file path") parser.add_argument("--out_file", default=None, help="Output file path") parser.add_argument( "--raw", action="store_true", help="Decode columns into raw values without enumerations", ) args = parser.parse_args() file_in = open(args.in_file, "rb") out_path = ( args.out_file if args.out_file is not None else f"{os.path.splitext(args.in_file)[0]}_decoded.txt" ) # Extract the table upload type (byte 0) from the binary so we can # locate the table definition that we need. uptype = int.from_bytes(file_in.read(1), byteorder="big") file_in.seek(0) fswtabdict = table.getDefaultFSWTabDict() pos_defn = [map[0] for map in fswtabdict.items() if map[1].uptype == uptype] if len(pos_defn) != 1: log.error( f"Table upload type {uptype} not found in table dictionary. Stopping ..." ) sys.exit(1) tbldefn = fswtabdict[pos_defn[0]] decoded = tbldefn.decode(file_in=file_in, raw=args.raw) out_file = open(out_path, "w") # Output our header values in comments so the table can be re-encoded easily hdr_row = decoded[0] for defn, val in zip(tbldefn.fswheaderdefns, hdr_row): print(f"# {defn.name}={val}", file=out_file) for row in decoded[1:]: print(tbldefn.delimiter.join(map(str, row)), file=out_file) out_file.close() log.end()
def main(): log.begin() parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument( "table_type", choices=list(table.getDefaultDict().keys()), help= (f"The type of table being encoded. One of {list(table.getDefaultDict().keys())}" ), ) parser.add_argument("in_file", help="Input file path") parser.add_argument("--out_file", help="Output file path") args = parser.parse_args() fswtabdict = table.getDefaultFSWTabDict() tbldefn = fswtabdict[args.table_type] out_path = (args.out_file if args.out_file is not None else f"{os.path.splitext(args.in_file)[0]}.bin") with open(args.in_file, "r") as in_file: encoded = tbldefn.encode(file_in=in_file) # Verify that the encoded table is the proper size. If it's too small we need # to pad it out. If it's too big then the user needs to remove some of the # entires. enc_len = len(encoded) if enc_len < tbldefn.size: encoded += bytearray(tbldefn.size - enc_len) elif enc_len > tbldefn.size: log.error( f"Encoded {tbldefn.name} table is too large. " f"Expected size: {tbldefn.size} bytes. Encoded size: {enc_len} bytes." "Please remove some entires from the table.") sys.exit(1) with open(out_path, "wb") as out_file: out_file.write(encoded) log.end()
def main(): log.begin() parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) # Add optional command line arguments parser.add_argument('--fswtabdict', default=None, required=True) parser.add_argument('--tabfile', default=None, required=True) parser.add_argument('--binfile', default=None, required=True) parser.add_argument('--tabletype', default=None, required=True) parser.add_argument('--verbose', action='store_true', default=False) # Get command line arguments args = parser.parse_args() dictpath = args.fswtabdict tabfile = args.tabfile tabletype = args.tabletype verbose = args.verbose # Grab default command dictionary if dictpath is not None: dictCache = table.FSWTabDictCache(filename=dictpath) try: filename = dictCache.filename except IOError as e: msg = 'Could not load default table dictionary "%s": %s' log.error(msg, filename, str(e)) fswtabdict = table.FSWTabDict(filename) else: fswtabdict = table.getDefaultFSWTabDict() # Check if cmddict exists if fswtabdict is not None: # Write out the table file using the command dictionary table.writeToBinary(fswtabdict, tabletype, tabfile, verbose, outbin=args.binfile) log.end()
def testTabDefnAndWrite(): '''Test table definition''' coldefn = table.FSWColDefn( me='test_col', type='U8', bytes=4, format='%u', units='none', items=4, enum=Season, ) coldefn2 = table.FSWColDefn( name='test_col2', type='U8', bytes=1, format='%u', units='none', items=4, enum=Color, ) tabledefn = table.FSWTabDefn( name='test_table', delimiter='-', uptype=1, size=8000, rows=10, fswheaderdefns=None, coldefns=[coldefn, coldefn2], ) fileholder = TestTableWriter() fileholder.writeTempFile() # Test that the table was created properly assert tabledefn.name is 'test_table' assert tabledefn.delimiter is '-' assert tabledefn.uptype is 1 assert tabledefn.size is 8000 assert tabledefn.rows is 10 assert tabledefn.coldefns[0] is coldefn assert tabledefn.coldefns[1] is coldefn2 # Write table to text file stream = open(TmpFilename, 'rw') outstream = open('tempfile', 'wr') # Test that the text file was created and did not exit with error code assert tabledefn.toText(stream, outstream, 1, 0.0) is None # Close the write to text stream.close() outstream.close() # Write table to binary file stream = open(TmpFilename, 'rw') outstream = open('tempfileb', 'wr') # Version in toBinary does not appear to be handled properly #assert tabledefn.toBinary('tempfile', stream, outstream, 1, 0.0) is None # Test that getDefaultFSWTabDict exits without an error code # and does not erroneously produce a dictionary when none exists assert table.getDefaultFSWTabDict() is None # Create a new table dictionary tabdict = table.FSWTabDict() tabdict.add(tabledefn) tabdict.create('test', {'colnames': tabledefn.coldefns}) # Load a table definition to the dictionary tabdict.load('ait/core/test/testdata/val/testCmdValidator6.yaml') # Assert writing to text does not exit with an error code assert table.writeToText(tabdict, 'test_table', 'tempfileb', 0, 0.0) is None #having trouble with getting version from TmpFilename #assert table.writeToBinary(tabdict,'test_table',TmpFilename,0) is None stream.close() outstream.close() os.unlink('tempfile') os.unlink('tempfileb') os.unlink('test_table_table00.txt') fileholder.tearDown()
# Get command line arguments args = vars(parser.parse_args()) binfile = args['binfile'] dictpath = args['fswtabdict'] tabletype = args['tabletype'] verbose = args['verbose'] version = args['version'] # Grab default table dictionary if dictpath is not None: dictCache = table.FSWTabDictCache(filename=dictpath) try: filename = dictCache.filename except IOError, e: msg = 'Could not load default table dictionary "%s": %s' log.error(msg, filename, str(e)) fswtabdict = table.getDefaultFSWTabDict() # Check if cmddict exists if fswtabdict is not None: # Write out the table file using the command dictionary table.writeToText(fswtabdict, tabletype, binfile, verbose, version) log.end() if __name__ == '__main__': main()