
4 changed files with 69 additions and 38 deletions
@ -1,3 +1,56 @@ |
|||
# -*- coding: utf-8 -*- |
|||
from . import generate_cid |
|||
|
|||
from . import c_2_ast |
|||
from . import ast_2_cid |
|||
from . import cid_2_yml |
|||
from . import utils |
|||
|
|||
import os |
|||
|
|||
|
|||
class CIDTools: |
|||
|
|||
def __init__(self, libclang_path, header_filename, out_dir=None): |
|||
self.header = self._create_header(header_filename, out_dir) |
|||
self.c2ast = c_2_ast.C2AST(libclang_path) |
|||
|
|||
# out-dir is in-dir if not specified |
|||
def _create_header(self, path, out_dir=None): |
|||
header = {"path": "", |
|||
"dir": "", |
|||
"filename": "", |
|||
"out_dir": "", |
|||
"sourcecode": "", |
|||
"ast": "", |
|||
"cid": "", |
|||
"yml": ""} |
|||
|
|||
header["path"] = path |
|||
header["dir"] = os.path.dirname(path) |
|||
header["filename"] = os.path.basename(path) |
|||
|
|||
header["out_dir"] = header["dir"] |
|||
if out_dir: |
|||
header["out_dir"] = out_dir |
|||
|
|||
header["sourcecode"] = utils.read_file(path) |
|||
return header |
|||
|
|||
|
|||
def extract(self, var_names, func_names, debug_ast=False, debug_cid=False, debug_yml=False): |
|||
# ast |
|||
self.header["ast"] = self.c2ast.parse(self.header["path"], follow_includes=True) |
|||
if debug_ast: |
|||
utils.write_json(self.header["ast"], self.header["out_dir"] + "/" + self.header["filename"] + ".ast.json") |
|||
|
|||
# cid |
|||
self.header["cid"] = ast_2_cid.extract_cid_selectively(self.header["ast"], var_names, func_names) |
|||
if debug_cid: |
|||
utils.write_json(self.header["cid"], self.header["out_dir"] + "/" + self.header["filename"] + ".cid.json") |
|||
|
|||
# yml |
|||
self.header["yml"] = cid_2_yml.generate_yml(self.header["cid"]) |
|||
if debug_yml: |
|||
utils.write_string(self.header["yml"], self.header["out_dir"] + "/" + self.header["filename"] + ".cid.yml") |
|||
|
|||
return self.header |
|||
|
Loading…
Reference in new issue