Exposing vector of strings for loading parameters (#147)

This commit is contained in:
Erik Fröjdh 2020-08-27 16:17:56 +02:00 committed by GitHub
parent a1e06ca7a9
commit 7eafceb0f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 20 deletions

View File

@ -119,9 +119,10 @@ class Detector(CppDetectorApi):
return NotImplementedError("parameters is set only")
@parameters.setter
def parameters(self, fname):
fname = ut.make_string_path(fname)
self.loadParameters(fname)
def parameters(self, value):
if isinstance(value, str):
value = ut.make_string_path(value)
self.loadParameters(value)
@property
def hostname(self):

View File

@ -36,6 +36,10 @@ void init_det(py::module &m) {
(void (Detector::*)(const std::string &)) &
Detector::loadParameters,
py::arg())
.def("loadParameters",
(void (Detector::*)(const std::vector<std::string> &)) &
Detector::loadParameters,
py::arg())
.def("getHostname",
(Result<std::string>(Detector::*)(sls::Positions) const) &
Detector::getHostname,

View File

@ -53,6 +53,8 @@ class Detector {
/** Shared memory not freed prior. Set up per measurement. */
void loadParameters(const std::string &fname);
void loadParameters(const std::vector<std::string>& parameters);
Result<std::string> getHostname(Positions pos = {}) const;
/* Frees shared memory, adds detectors to the list */

View File

@ -60,30 +60,32 @@ void Detector::loadConfig(const std::string &fname) {
}
void Detector::loadParameters(const std::string &fname) {
CmdProxy proxy(this);
CmdParser parser;
std::ifstream input_file;
input_file.open(fname.c_str(), std::ios_base::in);
if (!input_file.is_open()) {
std::ifstream input_file(fname);
if (!input_file) {
throw RuntimeError("Could not open configuration file " + fname +
" for reading");
}
std::string current_line;
while (input_file.good()) {
getline(input_file, current_line);
if (current_line.find('#') != std::string::npos) {
current_line.erase(current_line.find('#'));
std::vector<std::string> parameters;
for (std::string line; std::getline(input_file, line);) {
if (line.find('#') != std::string::npos) {
line.erase(line.find('#'));
}
LOG(logDEBUG1) << "current_line after removing comments:\n\t"
<< current_line;
if (current_line.length() > 1) {
if (line.length() > 1) {
parameters.push_back(line);
}
}
loadParameters(parameters);
}
void Detector::loadParameters(const std::vector<std::string> &parameters) {
CmdProxy proxy(this);
CmdParser parser;
for (const auto &current_line : parameters) {
parser.Parse(current_line);
proxy.Call(parser.command(), parser.arguments(),
parser.detector_id(), defs::PUT_ACTION);
proxy.Call(parser.command(), parser.arguments(), parser.detector_id(),
defs::PUT_ACTION);
}
}
input_file.close();
}
Result<std::string> Detector::getHostname(Positions pos) const {
return pimpl->Parallel(&Module::getHostname, pos);