mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-11 04:17:15 +02:00
support for scoped enums and operators in Python
This commit is contained in:
@ -11,14 +11,29 @@ import subprocess
|
||||
|
||||
from parse import remove_comments
|
||||
|
||||
allow_bitwise_op = ["EthernetInterface"]
|
||||
|
||||
op_key = {"operator|": "__or__",
|
||||
"operator&" : "__and__"}
|
||||
|
||||
def single_line_enum(line):
|
||||
sub = line[line.find('{')+1:line.find('}')]
|
||||
return sub.strip().split(',')
|
||||
|
||||
def extract_enums(lines):
|
||||
|
||||
# deal with enum class EthernetInterface : int32_t
|
||||
# and normal enum burstMode {
|
||||
|
||||
line_iter = iter(lines)
|
||||
enums = {}
|
||||
for line in line_iter:
|
||||
#Hack away class enum defs
|
||||
if "class" in line:
|
||||
line = line.replace("class", "")
|
||||
line = line.replace(line[line.find(':'):line.find('{')], "")
|
||||
line = line.replace(" ", " ")
|
||||
|
||||
m = re.search("(?<=enum )\w+(?= {)", line)
|
||||
if m:
|
||||
enum_name = m.group()
|
||||
@ -44,25 +59,80 @@ def extract_enums(lines):
|
||||
pass
|
||||
fields = [f.strip() for f in fields]
|
||||
enums[enum_name] = fields
|
||||
|
||||
|
||||
#Loop again to find operators
|
||||
for key in enums:
|
||||
for line in lines:
|
||||
if key in line and "operator" in line:
|
||||
pos = line.find("operator")
|
||||
op_type = line[pos:pos+9]
|
||||
enums[key].append(op_type)
|
||||
return enums
|
||||
|
||||
|
||||
def generate_enum_string(enums):
|
||||
data = []
|
||||
for key, value in enums.items():
|
||||
data.append(f'py::enum_<slsDetectorDefs::{key}>(Defs, "{key}")\n')
|
||||
if key in allow_bitwise_op:
|
||||
tag=", py::arithmetic()"
|
||||
else:
|
||||
tag=""
|
||||
data.append(f'py::enum_<slsDetectorDefs::{key}>(Defs, "{key}"{tag})\n')
|
||||
operators = []
|
||||
for v in value:
|
||||
data.append(f'\t.value("{v}", slsDetectorDefs::{key}::{v})\n')
|
||||
data.append('.export_values();\n\n')
|
||||
if "operator" not in v:
|
||||
data.append(f'\t.value("{v}", slsDetectorDefs::{key}::{v})\n')
|
||||
else:
|
||||
operators.append(v)
|
||||
data.append('\t.export_values()')
|
||||
|
||||
#Here add the operators
|
||||
for op in operators:
|
||||
data.append(f"\n\t.def(\"{op_key[op]}\", py::overload_cast< const slsDetectorDefs::EthernetInterface&, const slsDetectorDefs::EthernetInterface&>(&{op}))")
|
||||
|
||||
|
||||
data.append(';\n\n')
|
||||
return ''.join(data)
|
||||
|
||||
|
||||
def remove_ifdefs(lines):
|
||||
"""Keeps C++ version of the code"""
|
||||
out = []
|
||||
it = iter(lines)
|
||||
skip = False
|
||||
for line in it:
|
||||
|
||||
if "#ifdef __cplusplus" in line:
|
||||
line = next(it)
|
||||
|
||||
if "#else" in line:
|
||||
skip = True
|
||||
|
||||
if "#endif" in line:
|
||||
skip = False
|
||||
|
||||
if not skip and "#endif" not in line:
|
||||
out.append(line)
|
||||
return out
|
||||
|
||||
|
||||
with open('../../slsSupportLib/include/sls/sls_detector_defs.h') as f:
|
||||
data = f.read()
|
||||
|
||||
data = remove_comments(data)
|
||||
data = data.splitlines()
|
||||
data = remove_ifdefs(data)
|
||||
enums = extract_enums(data)
|
||||
|
||||
|
||||
s = generate_enum_string(enums)
|
||||
|
||||
# print(s)
|
||||
# for i, line in enumerate(data):
|
||||
# print(i, line)
|
||||
|
||||
|
||||
with open('../src/enums_in.cpp') as f:
|
||||
data = f.read()
|
||||
|
||||
|
@ -2332,6 +2332,18 @@ class Detector(CppDetectorApi):
|
||||
ut.set_using_dict(self.setVetoReference, *args)
|
||||
|
||||
|
||||
@property
|
||||
@element
|
||||
def vetostream(self):
|
||||
return self.getVetoStream()
|
||||
|
||||
@vetostream.setter
|
||||
def vetostream(self, args):
|
||||
if not isinstance(args, tuple):
|
||||
args = (args,)
|
||||
ut.set_using_dict(self.setVetoStream, *args)
|
||||
|
||||
|
||||
"""
|
||||
Mythen3 specific
|
||||
"""
|
||||
|
@ -1074,6 +1074,16 @@ void init_det(py::module &m) {
|
||||
(void (Detector::*)(const bool, sls::Positions)) &
|
||||
Detector::setVeto,
|
||||
py::arg(), py::arg() = Positions{})
|
||||
.def("getVetoStream",
|
||||
(Result<defs::EthernetInterface>(Detector::*)(sls::Positions)
|
||||
const) &
|
||||
Detector::getVetoStream,
|
||||
py::arg() = Positions{})
|
||||
.def("setVetoStream",
|
||||
(void (Detector::*)(const defs::EthernetInterface,
|
||||
sls::Positions)) &
|
||||
Detector::setVetoStream,
|
||||
py::arg(), py::arg() = Positions{})
|
||||
.def("getADCConfiguration",
|
||||
(Result<int>(Detector::*)(const int, const int, sls::Positions)
|
||||
const) &
|
||||
|
@ -277,4 +277,20 @@ void init_enums(py::module &m) {
|
||||
.value("TIMING_EXTERNAL",
|
||||
slsDetectorDefs::timingSourceType::TIMING_EXTERNAL)
|
||||
.export_values();
|
||||
|
||||
py::enum_<slsDetectorDefs::EthernetInterface>(Defs, "EthernetInterface",
|
||||
py::arithmetic())
|
||||
.value("NONE", slsDetectorDefs::EthernetInterface::NONE)
|
||||
.value("I3GBE", slsDetectorDefs::EthernetInterface::I3GBE)
|
||||
.value("I10GBE", slsDetectorDefs::EthernetInterface::I10GBE)
|
||||
.value("ALL", slsDetectorDefs::EthernetInterface::ALL)
|
||||
.export_values()
|
||||
.def("__or__",
|
||||
py::overload_cast<const slsDetectorDefs::EthernetInterface &,
|
||||
const slsDetectorDefs::EthernetInterface &>(
|
||||
&operator|))
|
||||
.def("__and__",
|
||||
py::overload_cast<const slsDetectorDefs::EthernetInterface &,
|
||||
const slsDetectorDefs::EthernetInterface &>(
|
||||
&operator&));
|
||||
}
|
||||
|
Reference in New Issue
Block a user