mirror of
https://github.com/firewalkwithm3/Sensor-Watch.git
synced 2024-11-22 19:20:30 +08:00
Added output to watch face script
changed `--author_name` to `--author-name` to match typical style of command line arguments changed some function names and implementations to improve readability changed to `sys.exit` instead of exit as `sys.exit` is recommended for use in programs and `exit` is intended for use in the interactive shell (this can cause issues for some terminal emulators)
This commit is contained in:
parent
a3085cb136
commit
e33530e1d0
|
@ -22,6 +22,7 @@
|
|||
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import datetime
|
||||
import argparse
|
||||
|
||||
|
@ -40,7 +41,7 @@ def replace_placeholders(contents, args):
|
|||
return modified_contents
|
||||
|
||||
|
||||
def write_output_file(args, file_type, output_dir):
|
||||
def write_modified_template(args, file_type, output_dir):
|
||||
with open(f"template.{file_type}", 'r') as file_template:
|
||||
file_contents = file_template.read()
|
||||
modified_template = replace_placeholders(file_contents, args)
|
||||
|
@ -48,47 +49,54 @@ def write_output_file(args, file_type, output_dir):
|
|||
if not os.path.exists(file_path):
|
||||
with open(file_path, 'w') as output_file:
|
||||
output_file.write(modified_template)
|
||||
print(f"Created {file_path}.")
|
||||
else:
|
||||
print(f"Generation failed: the watch face \"{args.watch_face_name}\" already exists at {file_path}. Unable to generate new files. Exiting...")
|
||||
exit(0)
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
def write_include_file(file_path, file_type, indicator_line, line_to_insert):
|
||||
def update_include_file(file_path, indicator_line, line_to_insert):
|
||||
with open(file_path, 'r+') as include_file:
|
||||
include_contents = include_file.readlines()
|
||||
if line_to_insert in include_contents:
|
||||
if line_to_insert not in include_contents:
|
||||
new_face_index = include_contents.index(indicator_line)
|
||||
include_contents.insert(new_face_index, line_to_insert)
|
||||
include_file.seek(0)
|
||||
include_file.writelines(include_contents)
|
||||
print(f"Updated {file_path}.")
|
||||
else:
|
||||
print(f"Generation failed: {file_path} already has an entry for {line_to_insert.strip()}. Unable to generate new files. Exiting...")
|
||||
exit(0)
|
||||
new_face_index = include_contents.index(indicator_line)
|
||||
include_contents.insert(new_face_index, line_to_insert)
|
||||
include_file.seek(0)
|
||||
include_file.writelines(include_contents)
|
||||
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Create a new watch face.")
|
||||
parser.add_argument("watch_face_type", metavar="face_type", type=str, choices=["complication", "clock"], help="The type of watch face to create, either \"complication\" or \"clock\"")
|
||||
parser.add_argument("watch_face_name", metavar="face_name", type=str, help="The name of the watch face")
|
||||
parser.add_argument("--author_name", metavar="author_name", type=str, nargs='*', help="The name of the author")
|
||||
parser.add_argument("--author-name", metavar="author_name", type=str, nargs='*', help="The name of the author")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
name_valid = re.fullmatch(r'[a-zA-Z_][a-zA-Z0-9_]*', args.watch_face_name)
|
||||
if name_valid == None:
|
||||
if name_valid is None:
|
||||
print(f"Generation failed: {args.watch_face_name} is not a valid C variable name. Exiting...")
|
||||
exit(0)
|
||||
|
||||
sys.exit(0)
|
||||
|
||||
if not args.author_name:
|
||||
print("Note: the \"--author-name\" argument was not supplied via command line. \"<#author_name#>\" will not be replaced in generated template.")
|
||||
|
||||
line_to_insert = f"#include \"{args.watch_face_name}_face.h\"\n"
|
||||
write_include_file(f"..{os.sep}movement_faces.h", "h", INCLUDE_INDICATOR, line_to_insert)
|
||||
|
||||
update_include_file(f"..{os.sep}movement_faces.h", INCLUDE_INDICATOR, line_to_insert)
|
||||
|
||||
line_to_insert = f" ..{os.sep}watch_faces{os.sep}{args.watch_face_type}{os.sep}{args.watch_face_name}_face.c \\\n"
|
||||
write_include_file(f"..{os.sep}make{os.sep}Makefile", "c", MAKEFILE_INDICATOR, line_to_insert)
|
||||
|
||||
update_include_file(f"..{os.sep}make{os.sep}Makefile", MAKEFILE_INDICATOR, line_to_insert)
|
||||
|
||||
output_dir = f"..{os.sep}watch_faces{os.sep}{args.watch_face_type}{os.sep}"
|
||||
write_output_file(args, "h", output_dir)
|
||||
write_output_file(args, "c", output_dir)
|
||||
write_modified_template(args, "h", output_dir)
|
||||
write_modified_template(args, "c", output_dir)
|
||||
|
||||
print(f"Successfully generated the {args.watch_face_name} {args.watch_face_type} and updated include files.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
|
Loading…
Reference in a new issue