Skip to content

Instantly share code, notes, and snippets.

@uroboro
Last active December 1, 2023 07:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uroboro/240cb3122b00e0faa70e449250ff614f to your computer and use it in GitHub Desktop.
Save uroboro/240cb3122b00e0faa70e449250ff614f to your computer and use it in GitHub Desktop.
➤ python json_output.py -d xxx -t T1000 -o - file1.swift file2.swift
{"file1.swift": {"object": "xxx/file1.swift.T1000.o", "dependencies": "xxx/file1.swift.T1000.Td"}, "file2.swift": {"object": "xxx/file2.swift.T1000.o", "dependencies": "xxx/file2.swift.T1000.Td"}, "": {"swift-dependencies": "xxx/master.swiftdeps"}}⏎
import json
from argparse import ArgumentParser
from argparse import Namespace
from sys import stdout
from typing import Dict
from typing import List
def generate_json(directory: str, tag: str, files: List[str]) -> Dict[str, Dict[str, str]]:
mapping = {
file_name: {"object": f"{directory}/{file_name}.{tag}.o", "dependencies": f"{directory}/{file_name}.{tag}.Td"}
for file_name in files
}
mapping[""] = {"swift-dependencies": f"{directory}/master.swiftdeps"}
return mapping
def print_json_to_file(mapping: Dict[str, Dict[str, str]], output: str) -> None:
if output == "-":
json.dump(mapping, fp=stdout)
else:
with open(output, "w") as file:
json.dump(mapping, fp=file)
def main(arguments: Namespace) -> None:
mapping = generate_json(directory=arguments.directory, tag=arguments.tag, files=arguments.file)
print_json_to_file(mapping, output=arguments.output)
if __name__ == "__main__":
arg_parser = ArgumentParser(description="Logos cli")
arg_parser.add_argument("-d", "--directory", required=True, help="Source directory")
arg_parser.add_argument("-t", "--tag", required=True, help="File tag")
arg_parser.add_argument("-o", "--output", default="-", help="Output file")
arg_parser.add_argument("file", nargs="+", help="Input files")
main(arg_parser.parse_args())
➤ python json_output.py -h
usage: json_output.py [-h] -d DIRECTORY -t TAG [-o OUTPUT] file [file ...]
Logos cli
positional arguments:
file Input files
optional arguments:
-h, --help show this help message and exit
-d DIRECTORY, --directory DIRECTORY
Source directory
-t TAG, --tag TAG File tag
-o OUTPUT, --output OUTPUT
Output file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment