fix(utils/defaultConfig): defaultConfig generator ignores comments now
This commit is contained in:
@@ -1,11 +1,27 @@
|
|||||||
opac:
|
Poly:
|
||||||
lowTemp:
|
Solver:
|
||||||
numeric:
|
ViewInitialGuess: false
|
||||||
maxIter: 10
|
|
||||||
Probe:
|
Probe:
|
||||||
GLVis:
|
GLVis:
|
||||||
Visualization: true
|
Visualization: true
|
||||||
Host: localhost
|
Host: localhost
|
||||||
Port: '19916'
|
Port: 19916
|
||||||
LogManager:
|
LogManager:
|
||||||
DefaultLogName: 4DSSE.log
|
DefaultLogName: 4DSSE.log
|
||||||
|
|
||||||
|
# This file was auto-generated by generateDefaultConfig.py
|
||||||
|
# Do not modify this file directly.
|
||||||
|
|
||||||
|
#=================================== TYPE HINTS ===================================
|
||||||
|
|
||||||
|
# Poly:
|
||||||
|
# Solver:
|
||||||
|
# ViewInitialGuess: bool
|
||||||
|
# Probe:
|
||||||
|
# GLVis:
|
||||||
|
# Visualization: bool
|
||||||
|
# Host: std::string
|
||||||
|
# Port: int
|
||||||
|
# LogManager:
|
||||||
|
# DefaultLogName: std::string
|
||||||
|
#
|
||||||
|
|||||||
@@ -1,6 +1,24 @@
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import yaml
|
import yaml
|
||||||
|
from io import StringIO
|
||||||
|
|
||||||
|
from tree_sitter import Language, Parser
|
||||||
|
import tree_sitter_cpp as tscpp
|
||||||
|
|
||||||
|
CPP_LANGUAGE = Language(tscpp.language())
|
||||||
|
|
||||||
|
def remove_comments_cpp(source_code):
|
||||||
|
parser = Parser(CPP_LANGUAGE)
|
||||||
|
tree = parser.parse(source_code.encode())
|
||||||
|
def extract_code(node):
|
||||||
|
if node.type in ['comment']:
|
||||||
|
return ''
|
||||||
|
elif node.children:
|
||||||
|
return ''.join(extract_code(child) for child in node.children)
|
||||||
|
return source_code[node.start_byte:node.end_byte]
|
||||||
|
extractedCode = extract_code(tree.root_node)
|
||||||
|
return extractedCode
|
||||||
|
|
||||||
# Regular expression to match .get<T>("a:b:c", x)
|
# Regular expression to match .get<T>("a:b:c", x)
|
||||||
pattern = re.compile(r'\.get<([^>]+)>\("([^"]+)",\s*([^);]+)\)')
|
pattern = re.compile(r'\.get<([^>]+)>\("([^"]+)",\s*([^);]+)\)')
|
||||||
@@ -11,17 +29,17 @@ def parse_value(value, type_hint):
|
|||||||
|
|
||||||
# Handle common types
|
# Handle common types
|
||||||
if type_hint in {"int", "long"}:
|
if type_hint in {"int", "long"}:
|
||||||
return int(value)
|
return (int(value), type_hint)
|
||||||
elif type_hint in {"float", "double"}:
|
elif type_hint in {"float", "double"}:
|
||||||
return float(value)
|
return (float(value), type_hint)
|
||||||
elif type_hint == "bool":
|
elif type_hint == "bool":
|
||||||
return value.lower() in {"true", "1"}
|
return (value.lower() in {"true", "1"}, type_hint)
|
||||||
elif value.startswith('"') and value.endswith('"'):
|
elif value.startswith('"') and value.endswith('"'):
|
||||||
return value.strip('"') # Remove quotes for string literals
|
return (value.strip('"'), type_hint) # Remove quotes for string literals
|
||||||
elif value.startswith("'") and value.endswith("'"):
|
elif value.startswith("'") and value.endswith("'"):
|
||||||
return value.strip("'") # Remove single quotes
|
return (value.strip("'"), type_hint) # Remove single quotes
|
||||||
|
|
||||||
return value # Return as-is if unsure
|
return (value, type_hint) # Return as-is if unsure
|
||||||
|
|
||||||
def insert_into_dict(hierarchy_dict, keys, value, type_hint):
|
def insert_into_dict(hierarchy_dict, keys, value, type_hint):
|
||||||
"""Recursively inserts values into a nested dictionary."""
|
"""Recursively inserts values into a nested dictionary."""
|
||||||
@@ -42,19 +60,51 @@ def scan_files(directory):
|
|||||||
if file.endswith(('.cpp', '.h', '.c', '.hpp')):
|
if file.endswith(('.cpp', '.h', '.c', '.hpp')):
|
||||||
file_path = os.path.join(root, file)
|
file_path = os.path.join(root, file)
|
||||||
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
||||||
for line in f:
|
noCommentSourceCode = remove_comments_cpp(f.read())
|
||||||
match = pattern.search(line)
|
matchs = pattern.finditer(noCommentSourceCode)
|
||||||
if match:
|
for match in matchs:
|
||||||
type_hint, hierarchy, value = match.groups()
|
print(f"Match: {match.group()}")
|
||||||
keys = hierarchy.split(":")
|
type_hint, hierarchy, value = match.groups()
|
||||||
insert_into_dict(hierarchy_dict, keys, value, type_hint)
|
keys = hierarchy.split(":")
|
||||||
|
insert_into_dict(hierarchy_dict, keys, value, type_hint)
|
||||||
|
|
||||||
return hierarchy_dict
|
return hierarchy_dict
|
||||||
|
|
||||||
|
def split_dict_recursive(originalDict):
|
||||||
|
dataDict = {}
|
||||||
|
typeDict = {}
|
||||||
|
|
||||||
|
for key, value in originalDict.items():
|
||||||
|
if isinstance(value, dict): # If the value is another dictionary, recurse
|
||||||
|
dataDict[key], typeDict[key] = split_dict_recursive(value)
|
||||||
|
elif isinstance(value, tuple) and len(value) == 2: # If it's a tuple, split it
|
||||||
|
dataDict[key] = value[0] # First element is data
|
||||||
|
typeDict[key] = value[1] # Second element is type hint
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Unexpected structure in dictionary at key '{key}': {value}")
|
||||||
|
|
||||||
|
return dataDict, typeDict
|
||||||
|
|
||||||
def save_yaml(data, output_file):
|
def save_yaml(data, output_file):
|
||||||
"""Saves the nested dictionary to a YAML file."""
|
"""Saves the nested dictionary to a YAML file."""
|
||||||
|
options, types = split_dict_recursive(data)
|
||||||
with open(output_file, 'w', encoding='utf-8') as f:
|
with open(output_file, 'w', encoding='utf-8') as f:
|
||||||
yaml.dump(data, f, default_flow_style=False, sort_keys=False, indent=4)
|
yaml.dump(options, f, default_flow_style=False, sort_keys=False, indent=4)
|
||||||
|
with open(output_file, 'a') as f:
|
||||||
|
f.write("\n")
|
||||||
|
f.write("# This file was auto-generated by generateDefaultConfig.py\n")
|
||||||
|
f.write("# Do not modify this file directly.\n")
|
||||||
|
f.write('\n#')
|
||||||
|
f.write('='*35 + " TYPE HINTS " + '='*35 + '\n')
|
||||||
|
f.write('\n')
|
||||||
|
|
||||||
|
type_file = StringIO()
|
||||||
|
yaml.dump(types, type_file, default_flow_style=False, sort_keys=False, indent=4)
|
||||||
|
# write types yaml out put with # in front of each line
|
||||||
|
with open(output_file, 'a') as f:
|
||||||
|
for line in type_file.getvalue().split('\n'):
|
||||||
|
f.write(f"# {line}\n")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import argparse
|
import argparse
|
||||||
@@ -64,6 +114,7 @@ if __name__ == "__main__":
|
|||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
extracted_data = scan_files(args.directory)
|
extracted_data = scan_files(args.directory)
|
||||||
|
print(extracted_data)
|
||||||
save_yaml(extracted_data, args.output)
|
save_yaml(extracted_data, args.output)
|
||||||
|
|
||||||
print(f"✅ YAML file generated: {args.output}")
|
print(f"✅ YAML file generated: {args.output}")
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
Small tool to quickly generate a YAML config file which encodes all the default options used in 4DSSE. All c, c++, h, and hpp in the src directory are scanned and the config variables and their default values pulled out.
|
Small tool to quickly generate a YAML config file which encodes all the default options used in 4DSSE. All c, c++, h, and hpp in the src directory are scanned and the config variables and their default values pulled out.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
- You must have python installed
|
- You must have python installed along with tree-sitter and tree-sitter-cpp (for source code parsing)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
./generateDefaultConfig.sh
|
./generateDefaultConfig.sh
|
||||||
|
|||||||
Reference in New Issue
Block a user