Skip to content

Commit

Permalink
Update xmlgen.py
Browse files Browse the repository at this point in the history
  • Loading branch information
pwittich committed Feb 9, 2024
1 parent 25e0613 commit b06b563
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/xmlgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
import argparse
import os
import pprint
from typing import IO, Any, Tuple
from typing import IO, Any, Tuple, List
import yaml

import utils # local import

# CONSTANTS
ZM_NUM_ENTRIES = 1024
C_OUTPUT_FILE = "ZynqMon_addresses.c"
C_OUTPUT_HEADER = "ZynqMon_addresses.h"

def sensor_size(thedict: dict) -> int:
"""calculate the size of the sensor in bytes"""
sz = 2 # default size
Expand All @@ -41,7 +46,7 @@ def type_to_format(thedict: dict) -> str:
return None
return fmt

def open_C_file(fname: str) -> IO[Any]:
def open_C_file(fname: str, header_fname: str) -> IO[Any]:
"""open the C file and write the header"""
fout = open(fname, 'w', encoding='ascii')
print(r"// This file is generated by mcu_generate.py", file=fout)
Expand All @@ -54,11 +59,10 @@ def open_C_file(fname: str) -> IO[Any]:
print(r"//", file=fout)
print("#include \"Tasks.h\"", file=fout)
# include the header file we will write later
header_fname = args.output.replace('.c', '.h')
print(f"#include \"{header_fname}\"", file=fout)
return fout

def write_C_header(fout: IO[Any], fname: str) -> None:
def write_C_header(header_fname: str, ZMON_VALID_ENTRIES: List[int]) -> None:
"""write the header file"""
with open(header_fname, encoding="ascii", mode='w') as fheader:
print(r"// This file is generated by mcu_generate.py", file=fheader)
Expand Down Expand Up @@ -140,11 +144,14 @@ def main():
if args.directory:
print('Output directory:', args.directory)


# load YAML input file into memory
with open(args.input_file, encoding='ascii') as f:
y = yaml.load(f, Loader=yaml.FullLoader)

# This is the parent(root) tag onto which other tags would be
# open C output file for writing
c_fout = open_C_file(args.directory + '/' + C_OUTPUT_FILE, C_OUTPUT_HEADER)

# start XML tree. This is the parent(root) tag onto which other tags will be
# created
cm = ET.Element('node')
cm.set('id', 'CM')
Expand All @@ -169,6 +176,10 @@ def main():

if args.verbose:
print("category_size:", category_size)

# write the C call to generate the filling of the data structures
write_C_call(c_fout, c, sensor_count)

while True:
# if there are devices
if device:
Expand All @@ -194,7 +205,10 @@ def main():
if args.verbose:
print("writing to file", out_name)
tree.write(out_name)

# write header file
write_C_header(args.directory + '/' + C_OUTPUT_HEADER, [sensor_count, sensor_count])
pprinter.pprint(summary)


if __name__ == "__main__":
main()

0 comments on commit b06b563

Please sign in to comment.