Rewrite of generate_config in python

This commit is contained in:
Juan José Gutiérrez de Quevedo Pérez 2025-03-25 13:13:54 +01:00
parent e9c7141d40
commit 25dc10cbfd
5 changed files with 117 additions and 128 deletions

View file

@ -15,7 +15,7 @@ steps:
- name: build hermes
image: alpine
commands:
- apk add -t hermes-build-deps --no-cache perl graphviz doxygen gcc make openssl-dev libspf2-dev cmake g++ sqlite-dev gettext-dev
- apk add -t hermes-build-deps --no-cache graphviz doxygen gcc make openssl-dev libspf2-dev cmake g++ sqlite-dev gettext-dev python3
- cmake -B build_dir -D BUILD_DOCS=ON
- cmake --build build_dir
- name: docker image build
@ -41,7 +41,7 @@ steps:
- name: build hermes
image: alpine
commands:
- apk add -t hermes-build-deps --no-cache perl graphviz doxygen gcc make openssl-dev libspf2-dev cmake g++ sqlite-dev gettext-dev
- apk add -t hermes-build-deps --no-cache graphviz doxygen gcc make openssl-dev libspf2-dev cmake g++ sqlite-dev gettext-dev python3
- cmake -B build_dir
- cmake --build build_dir
- name: docker image build

View file

@ -52,9 +52,9 @@ include_directories(
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Configfile.cpp
COMMAND cpp ${OPT_DEFS} ${CMAKE_CURRENT_SOURCE_DIR}/src/Configfile.tmpl -I
${CMAKE_CURRENT_BINARY_DIR} |
${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_config.pl
python ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_config.py
DEPENDS src/Configfile.cpp.in src/Configfile.h.in src/Configfile.tmpl
docs/hermes-options.html.in scripts/generate_config.pl)
docs/hermes-options.html.in scripts/generate_config.py)
# doxygen

View file

@ -1,7 +1,7 @@
FROM alpine:3.18
ADD . /hermes
WORKDIR /hermes
RUN apk add --no-cache perl graphviz doxygen gcc make openssl-dev libspf2-dev cmake g++ sqlite-dev gettext-dev
RUN apk add --no-cache graphviz doxygen gcc make openssl-dev libspf2-dev cmake g++ sqlite-dev gettext-dev python3
RUN cmake -B build
RUN cmake --build build
RUN mkdir /hermes-installation

View file

@ -1,123 +0,0 @@
#!/usr/bin/perl -w
# this small script generates the Configfile class from the
# Configfile.cpp.in and Configfile.h.in. this way when we want
# to add a new option to the config file, we just have to put it
# on Configfile.tmpl and automagically it will appear on our code
# It will also generate an example hermesrc from the same info.
# 2007-04-17 Now it also generates an html document for our webpage
my $hvar="";
my $cppvar1="",$cppvar2="",$cppvar3="",$conf_example="",$htmlvar="";
open HTMLIN, "<../docs/hermes-options.html.in";
$htmltempl=join("",<HTMLIN>);
close HTMLIN;
while(<>)
{
chomp;
if(! /^#/ && ! /^\t*$/ && ! /^\*/)
{
s/^\s+//;s/\s+$//;
@_=split ",";
my $camelcased=&camel_case($_[1]);
my $type=$_[0];
$type="list<string>" if($type =~ /list/);
$hvar1.="$type $_[1];\n";
$hvar2.="$type& get$camelcased();\n";
if($type =~ /list/)
{
$cppvar1.="$_[1]=Configfile::parseAsList($_[2]);\n";
}
else
{
$cppvar1.="$_[1]=$_[2];\n";
}
$cppvar2.="PARSE_".uc($_[0])."(\"$_[1]\",$_[1])\n";
$cppvar3.="GET_VAR(get$camelcased,$_[1],$type&)\n";
$conf_example.="$_[1] = $_[2]\n\n";
my $htmltemp=$htmltempl;
$htmltemp =~ s/%type%/$_[0]/;
$htmltemp =~ s/%name%/$_[1]/g;
$htmltemp =~ s/%default%/$_[2]/;
$htmltemp =~ s/%explanation%/$htmlexpl/;
$htmlexpl="";
$htmlvar.=$htmltemp;
}
else
{
if(/^\*clean\*$/) # clean restarts our htmlexpl contents
{
$htmlexpl="";
}
else
{
if(/^\*/)
{
s/^\*$//;
s/^\*/#/;
$conf_example.="$_\n";
chomp;
s/^#//;
s/>/&gt;/;
$htmlexpl.="$_\n";
}
}
}
}
chomp $cppvar1;
chomp $cppvar2;
chomp $cppvar3;
chomp $hvar1;
chomp $hvar2;
chomp $conf_example;
open CPPIN, "<../src/Configfile.cpp.in";
$cppstr=join("",<CPPIN>);
close CPPIN;
open CPPOUT, ">Configfile.cpp";
$cppstr =~ s/%templ_default_values%/$cppvar1/;
$cppstr =~ s/%templ_parsevars%/$cppvar2/;
$cppstr =~ s/%templ_getmethods%/$cppvar3/;
print CPPOUT $cppstr;
close CPPOUT;
open HIN, "<../src/Configfile.h.in";
$hstr=join("",<HIN>);
close HIN;
open HOUT, ">Configfile.h";
$hstr =~ s/%templ_privateattribs%/$hvar1/;
$hstr =~ s/%templ_publicmethods%/$hvar2/;
print HOUT $hstr;
close HOUT;
open RCEX, ">../dists/hermesrc.example";
print RCEX $conf_example;
close RCEX;
open HTML, ">../docs/hermes-options.html";
print HTML $htmlvar;
close HTML;
sub camel_case()
{
my $str=shift;
my $outstr="";
for($i=0;$i<length($str);$i++)
{
my $letter=substr($str,$i,1);
if($letter eq "_")
{
$i++;
$outstr.=uc(substr($str,$i,1));
}
else
{
$outstr.=$letter;
}
}
return ucfirst($outstr);
}

112
scripts/generate_config.py Normal file
View file

@ -0,0 +1,112 @@
#!/usr/bin/env python3
import sys
import re
import string
def camel_case(str_):
"""Convert snake_case to CamelCase."""
return string.capwords(str_, "_").replace("_", "")
def main():
# Read input files
with open('../docs/hermes-options.html.in', 'r') as f:
html_templ = f.read()
hvar1 = ""
hvar2 = ""
cppvar1 = ""
cppvar2 = ""
cppvar3 = ""
conf_example = ""
htmlvar = ""
htmlexpl = ""
# Process input
for line in sys.stdin:
line = line.strip()
if not line or line.startswith('#') or line.startswith('*'):
if line == '*clean*':
htmlexpl = ""
elif line.startswith('*'):
line = line.replace('*', '#')
conf_example += line + "\n"
# Convert line for HTML
line_html = line.lstrip('#').replace('>', '&gt;')
htmlexpl += line_html + "\n"
continue
parts = line.split(',')
parts = [p.strip() for p in parts]
type_str = parts[0]
var_name = parts[1]
default_val = parts[2]
# Modify type for lists
if 'list' in type_str:
type_str = 'list<string>'
camel_name = camel_case(var_name)
# Generate header variables
hvar1 += f"{type_str} {var_name};\n"
hvar2 += f"{type_str}& get{camel_name}();\n"
# Generate cpp variables
if 'list' in type_str:
cppvar1 += f"{var_name} = Configfile::parseAsList({default_val});\n"
else:
cppvar1 += f"{var_name} = {default_val};\n"
cppvar2 += f"PARSE_{parts[0].upper()}(\"{var_name}\", {var_name})\n"
cppvar3 += f"GET_VAR(get{camel_name}, {var_name}, {type_str}&)\n"
# Generate config example
conf_example += f"{var_name} = {default_val}\n\n"
# Generate HTML
html_temp = html_templ.replace('%type%', parts[0]) \
.replace('%name%', var_name) \
.replace('%default%', default_val) \
.replace('%explanation%', htmlexpl)
htmlvar += html_temp
htmlexpl = ""
# Clean up variables
for var in [cppvar1, cppvar2, cppvar3, hvar1, hvar2, conf_example]:
var = var.rstrip()
# Read and write Configfile.cpp
with open('../src/Configfile.cpp.in', 'r') as f:
cpp_str = f.read()
cpp_str = cpp_str.replace('%templ_default_values%', cppvar1) \
.replace('%templ_parsevars%', cppvar2) \
.replace('%templ_getmethods%', cppvar3)
with open('Configfile.cpp', 'w') as f:
f.write(cpp_str)
# Read and write Configfile.h
with open('../src/Configfile.h.in', 'r') as f:
h_str = f.read()
h_str = h_str.replace('%templ_privateattribs%', hvar1) \
.replace('%templ_publicmethods%', hvar2)
with open('Configfile.h', 'w') as f:
f.write(h_str)
# Write hermesrc.example
with open('../dists/hermesrc.example', 'w') as f:
f.write(conf_example)
# Write hermes-options.html
with open('../docs/hermes-options.html', 'w') as f:
f.write(htmlvar)
if __name__ == "__main__":
main()