From c3a5fcfa262bf91e9677630b7fa156c813375939 Mon Sep 17 00:00:00 2001 From: heck Date: Sat, 5 Dec 2020 22:14:53 +0100 Subject: [PATCH] first draft - engine header file parser. (create data structure to represent engine functions, structs and enums - to generate DSL when we have a definition) --- gen/extract.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100755 gen/extract.py diff --git a/gen/extract.py b/gen/extract.py new file mode 100755 index 0000000..c01c504 --- /dev/null +++ b/gen/extract.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import re +import os +import pprint + +def create_paths_list(dirname, filenames): + paths = [] + for basename in filenames: + path = dirname + basename + paths.append(path) + return paths + + +def read_files(paths): + content = [] + for path in paths: + file_info = read_file(path) + content.append(file_info) + return content + + +def read_file(path): + with open(path) as f: + file_content = f.read() + item = {"path": path, + "content": file_content} + return item + + +def extract_functions(file_content): + pattr = re.compile("DYNAMIC_API.*?\);", re.DOTALL) + res = pattr.findall(file_content) + return res + + +def main(): + # Input + prefix = r"/Users/heck/local-default/" + filenames = ["pEpEngine.h", + "keymanagement.h"] + + # Output + out_dir = "data/" + + basename = prefix + r"include/pEp/" + + # Create out dir + if not os.path.isdir(out_dir): + os.makedirs(out_dir) + + # Parse input + paths = create_paths_list(basename, filenames) + headers = read_files(paths) + + # Process and create data structure + for header in headers: + print("processing path: " + header.get("path") + "...") + basename = os.path.basename(header.get("path")) + + # add outpath + outpath = out_dir + basename + header["outpath"] = outpath + + # add functions + functions = extract_functions(header.get("content")) + header["functions"] = functions + + + # Output + for header in headers: + with open(header.get("outpath"), "w+") as f: + pprint.pp(header) + +if __name__ == "__main__": + main()