/ src / assemble.py
assemble.py
 1  import sys
 2  from contextlib import contextmanager
 3  
 4  import adafruit_pioasm
 5  import click
 6  
 7  
 8  @contextmanager
 9  def temporary_stdout(filename):
10      old_stdout = sys.stdout
11      try:
12          with open(filename, "w", encoding="utf-8") as sys.stdout:
13              yield sys.stdout
14      finally:
15          sys.stdout = old_stdout
16  
17  @click.command
18  @click.argument("infile")
19  @click.argument("outfile")
20  def main(infile, outfile):
21      program_name = infile.rpartition("/")[2].partition(".")[0]
22      print(program_name)
23      program = adafruit_pioasm.Program.from_file(infile, build_debuginfo=True)
24  
25      with temporary_stdout(outfile):
26          program.print_c_program(program_name)
27  
28  if __name__ == '__main__':
29      main()