-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathembed_config.py
executable file
·142 lines (92 loc) · 5.16 KB
/
embed_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
A simple script to overwrite the config_generator.cpp source file with code for generating a default config.
This script opens the default_config.json file and loads it as a JSON object, then C++ code is generated for creating a
completely default config that is based off of the JSON file.
For running this script, first argument after the script name should be the config file path, and the second should be
the path for the config_generator.cpp file.
"""
import json
import random
import sys
import re
import string
CONFIG_FILE_PATH = sys.argv[1]
GENERATOR_FILE_PATH = sys.argv[2]
config = {}
with open(sys.argv[1]) as file:
config = json.load(file)
GENERATOR_FILE_CONTENTS = """/**
This header gets auto-generated by the CMake prebuild step for creating a default configuration.
Do not edit this file by hand! If you want to change the default config, edit the `default_config.json` file.
*/
#include "sonne/pch.hpp"
#include "sonne/config_generator.hpp"
#include "sonne/config.hpp"
using namespace Sonne;
std::shared_ptr<Config> Sonne::GenerateDefaultConfig()
{
std::shared_ptr<Config> config = std::make_shared<Config>();
"""
# an indent of four spaces for the lines in the function
FUNCTION_INDENT = " "
def get_cpp_bool(bool):
"""
Simply converts a Python boolean value to a string representation of a C++ bool
"""
return "true" if bool else "false"
if "ignoreHidden" in config:
GENERATOR_FILE_CONTENTS += FUNCTION_INDENT + "config->SetIgnoreHidden({});\n".format(get_cpp_bool(config["ignoreHidden"]))
if "columns" in config:
GENERATOR_FILE_CONTENTS += FUNCTION_INDENT + "config->SetColumns({});\n".format(int(config["columns"]))
if "ignore" in config:
ignoreArray = config["ignore"]
for ignore in ignoreArray:
shouldIgnore = (ignore[0] != '!')
print(ignore)
# if we should not ignore this path, remove the exclaimation point from the path
if not shouldIgnore:
ignore = ignore[1:]
GENERATOR_FILE_CONTENTS += "\n" + FUNCTION_INDENT + "config->AddIgnored({}, {});".format(ignore, get_cpp_bool(shouldIgnore))
if "languages" in config:
languages = config["languages"]
for language in languages:
GENERATOR_FILE_CONTENTS += "\n" # add extra newline to separate
# if the language contains any special characters or numbers, just remove them for the variable name
var_name = re.sub('\W+', '', language["name"])
var_name = re.sub('\s+', '_', var_name)
# instantiate the language pointer
GENERATOR_FILE_CONTENTS += FUNCTION_INDENT + "std::shared_ptr<Language> {} = std::make_shared<Language>();\n\n".format(var_name)
# set the language name in the struct
GENERATOR_FILE_CONTENTS += FUNCTION_INDENT + "{}->name = \"{}\";\n".format(var_name, language["name"])
# start printing out a vector of extensions for the language to match to
GENERATOR_FILE_CONTENTS += FUNCTION_INDENT + "{}->extensions = {{\n".format(var_name)
extensionIdx = 0
extensions = language["extensions"]
for extension in extensions:
extensionIdx += 1
GENERATOR_FILE_CONTENTS += FUNCTION_INDENT + FUNCTION_INDENT + "\"{}\"{}\n".format(extension, "," if extensionIdx != len(extensions) else "")
GENERATOR_FILE_CONTENTS += FUNCTION_INDENT + "};\n\n";
if "lineComment" in language:
GENERATOR_FILE_CONTENTS += FUNCTION_INDENT + "{}->lineComment = \"{}\";\n".format(var_name, language["lineComment"])
if "blockCommentBegin" in language:
GENERATOR_FILE_CONTENTS += FUNCTION_INDENT + "{}->blockCommentBegin = \"{}\";\n".format(var_name, language["blockCommentBegin"])
if "blockCommentEnd" in language:
GENERATOR_FILE_CONTENTS += FUNCTION_INDENT + "{}->blockCommentEnd = \"{}\";\n".format(var_name, language["blockCommentEnd"])
# if it exists, print out a vector of string delimiters
if "stringDelimiters" in language:
delimiters = language["stringDelimiters"]
GENERATOR_FILE_CONTENTS += FUNCTION_INDENT + "{}->stringDelimiters = {{\n".format(var_name)
delimiterIdx = 0
for delimiter in delimiters:
delimiterIdx += 1
if delimiter == "\"":
delimiter = "\\\"" # escape the quote for compilation
GENERATOR_FILE_CONTENTS += FUNCTION_INDENT + FUNCTION_INDENT + "\"{}\"{}\n".format(delimiter, "," if delimiterIdx != len(delimiters) else "")
GENERATOR_FILE_CONTENTS += FUNCTION_INDENT + "};\n";
GENERATOR_FILE_CONTENTS += "\n" # separate final line and add line
GENERATOR_FILE_CONTENTS += FUNCTION_INDENT + "config->AddLanguage({});\n".format(var_name) # add a new line for the next language or next line
GENERATOR_FILE_CONTENTS += "\n" + FUNCTION_INDENT + "return config;\n"
GENERATOR_FILE_CONTENTS += "}" # finally, end with the closing of the function
with open(sys.argv[2], 'w') as file:
file.write(GENERATOR_FILE_CONTENTS)
file.close()