51 #define PY_SSIZE_T_CLEAN
56 #include "cetlib/search_path.h"
57 #include "cetlib_except/exception.h"
61 #include "fhiclcpp/ParameterSet.h"
62 #include "fhiclcpp/ParameterSetWalker.h"
64 #include "fhiclcpp/ParameterSetRegistry.h"
65 #include "fhiclcpp/make_ParameterSet.h"
73 std::string python_to_cxx_str(
PyObject* obj) {
74 if(PyBytes_Check(obj))
75 return std::string(PyBytes_AsString(obj));
76 else if(PyUnicode_Check(obj)) {
78 return std::string(PyBytes_AsString(bytes));
81 throw cet::exception(
"fclmodule") <<
"String has wrong type." << std::endl;
86 Py_ssize_t python_string_len(
PyObject* obj) {
87 if(PyBytes_Check(obj))
88 return PyBytes_Size(obj);
89 else if(PyUnicode_Check(obj)) {
91 return PyBytes_Size(bytes);
94 throw cet::exception(
"fclmodule") <<
"String has wrong type." << std::endl;
101 PyObject* cxx_str_to_python(
const std::string&
s) {
103 #if PY_MAJOR_VERSION >= 3
104 result = PyUnicode_FromString(s.c_str());
106 result = PyBytes_FromString(s.c_str());
159 _stack.emplace_back(PyDict_New());
174 throw cet::exception(
"fclmodule") <<
"Result stack has wrong size: "
175 <<
_stack.size() << std::endl;
176 if(!PyDict_Check(
_stack[0]))
177 throw cet::exception(
"fclmodule") <<
"Result stack has wrong type." << std::endl;
202 _stack.emplace_back(dict);
238 const std::string&
atom = std::any_cast<
const std::string&>(any);
242 std::string lcatom(atom);
244 [](
unsigned char c){
return std::tolower(c);});
248 if(lcatom == std::string(
"true") || lcatom == std::string(
"\"true\"")) {
252 else if(lcatom == std::string(
"false") || lcatom == std::string(
"\"false\"")) {
259 size_t n = atom.size();
260 if(pyval == 0 && n >= 2 && atom[0] ==
'"' && atom[n-1] ==
'"') {
261 std::string
s = atom.substr(1, n-2);
262 pyval = cxx_str_to_python(s);
268 std::istringstream iss(atom);
270 iss >> std::noskipws >> i;
271 if(iss.eof()
and !iss.fail())
272 pyval = PyLong_FromLong(i);
278 std::istringstream iss(atom);
280 iss >> std::noskipws >>
x;
281 if(iss.eof()
and !iss.fail())
282 pyval = PyFloat_FromDouble(x);
288 pyval = cxx_str_to_python(atom);
304 throw cet::exception(
"fclmodule") <<
"Result stack has wrong size: "
305 <<
_stack.size() << std::endl;
306 if(!PyDict_Check(
_stack.back()))
307 throw cet::exception(
"fclmodule") <<
"Result stack has wrong type." << std::endl;
325 throw cet::exception(
"fclmodule") <<
"Result stack has wrong size: "
326 <<
_stack.size() << std::endl;
327 if(!PyList_Check(
_stack.back()))
328 throw cet::exception(
"fclmodule") <<
"Result stack has wrong type." << std::endl;
348 throw cet::exception(
"fclmodule") <<
"No parent object." << std::endl;
351 if(PyDict_Check(parent)) {
355 PyObject* keyobj = cxx_str_to_python(key);
356 PyDict_SetItem(parent, keyobj, pyobj);
359 else if(PyList_Check(parent)) {
363 PyList_Append(parent, pyobj);
370 throw cet::exception(
"fclmodule") <<
"Parent object is not dictionary or list." << std::endl;
375 unsigned int maxlen,
unsigned int depth)
401 std::ostringstream ss;
403 if(PyBytes_Check(obj)) {
407 ss <<
"\"" << PyBytes_AsString(obj) <<
"\"";
410 else if(PyUnicode_Check(obj)) {
415 ss <<
"\"" << PyBytes_AsString(bytes) <<
"\"";
418 else if(PyDict_Check(obj)) {
428 int n = PyList_Size(keys);
430 for(
int i=0; i<
n; ++i) {
431 PyObject* key = PyList_GetItem(keys, i);
432 int keylen = python_string_len(key);
433 if(keylen > keymaxlen)
439 bool outer = (pos == 0 && indent == 0);
445 for(
int i=0; i<
n; ++i) {
446 PyObject* key = PyList_GetItem(keys, i);
448 std::string ks = python_to_cxx_str(key);
449 ss << std::setw(indent) <<
""
450 << std::setw(keymaxlen) <<
std::left << ks <<
" : "
451 <<
format(value, indent + keymaxlen + 3, indent+2, maxlen, depth+1)
457 ss << std::setw(indent-1) <<
std::right <<
'}';
463 else if(PyList_Check(obj) || PyTuple_Check(obj)) {
471 if(PyList_Check(obj)) {
474 n = PyList_Size(obj);
479 n = PyTuple_Size(obj);
484 std::string sep(1, open_seq);
485 unsigned int break_indent = pos+1;
486 for(
int i=0; i<
n; ++i) {
489 PyObject* ele = PySequence_GetItem(obj, i);
493 std::string f =
format(ele, pos, break_indent, maxlen, depth+1);
497 std::string::size_type fs = f.size();
498 std::string::size_type n1 = std::min(f.find(
'\n'), fs);
505 bool force_break = PyList_Check(ele) || PyTuple_Check(ele) || PyDict_Check(ele);
506 if(i > 0 && (force_break || pos + n1 > maxlen)) {
507 ss <<
'\n' << std::setw(break_indent) <<
"";
509 f =
format(ele, pos, break_indent, maxlen, depth+1);
520 std::string::size_type n2 = f.find_last_of(
'\n');
526 sep = std::string(
", ");
541 PyObject* pystr = PyObject_Str(obj);
542 std::string
s = python_to_cxx_str(pystr);
546 if(s == std::string(
"True"))
548 else if(s == std::string(
"False"))
571 if(!PyArg_ParseTuple(args,
"s", &fclname))
573 std::string fclstr(fclname);
579 std::string pathvar(
"FHICL_FILE_PATH");
580 cet::filepath_lookup maker(pathvar);
581 auto pset = fhicl::ParameterSet::make(fclstr, maker);
583 pset.walk(converter);
584 result = converter.
result();
586 catch(cet::exception&
e) {
587 PyErr_SetString(PyExc_IOError, e.what());
614 int n = PySequence_Length(args);
626 PyObject* obj = PySequence_GetItem(args, 0);
627 std::string
s =
format(obj, 0, 0, 80, 0);
628 result = cxx_str_to_python(s);
639 {
"make_pset",
make_pset, METH_VARARGS,
"Convert fcl ParameterSet to python dictionary"},
640 {
"pretty",
pretty, METH_VARARGS,
"Convert dictionary parameter set to fcl text"},
646 #if PY_MAJOR_VERSION >= 3
648 static struct PyModuleDef fclmodule = {
649 PyModuleDef_HEAD_INIT,
659 return PyModule_Create(&fclmodule);
void atom(key_t const &key, any_t const &any)
process_name opflash particleana ie x
void exit_table(key_t const &key, any_t const &any)
void exit_sequence(key_t const &key, any_t const &any)
static std::string format(PyObject *obj, unsigned int pos, unsigned int indent, unsigned int maxlen, unsigned int depth)
std::vector< PyObject * > _stack
static PyObject * make_pset(PyObject *self, PyObject *args)
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
void enter_table(key_t const &key, any_t const &any)
PyObject * result() const
static PyObject * pretty(PyObject *self, PyObject *args)
then echo File list $list not found else cat $list while read file do echo $file sed s
static struct PyMethodDef fclmodule_methods[]
byte bytes
Alias for common language habits.
void enter_sequence(key_t const &key, any_t const &any)
void add_object(key_t const &key, PyObject *pyobj)