File: BasicDirectoryCreator.cpp

package info (click to toggle)
odil 0.13.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,476 kB
  • sloc: cpp: 55,982; python: 3,947; javascript: 460; xml: 182; makefile: 99; sh: 36
file content (116 lines) | stat: -rw-r--r-- 3,629 bytes parent folder | download | duplicates (5)
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
/*************************************************************************
 * odil - Copyright (C) Universite de Strasbourg
 * Distributed under the terms of the CeCILL-B license, as published by
 * the CEA-CNRS-INRIA. Refer to the LICENSE file or to
 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
 * for details.
 ************************************************************************/

#include <algorithm>
#include <string>
#include <vector>

#include <pybind11/pybind11.h>

#include "odil/BasicDirectoryCreator.h"

#include "opaque_types.h"
#include "type_casters.h"

namespace
{

pybind11::list get_files(odil::BasicDirectoryCreator const & creator)
{
    pybind11::list result;
    for(auto const & file: creator.files)
    {
        result.append(file);
    }

    return result;
}

void set_files(
    odil::BasicDirectoryCreator & creator,
    pybind11::sequence const & files_python)
{
    std::vector<std::string> files_cpp(pybind11::len(files_python));
    std::transform(
        files_python.begin(), files_python.end(), files_cpp.begin(),
        [](pybind11::handle const & h) { return pybind11::cast<std::string>(h); });
    creator.files = files_cpp;
}

pybind11::dict
get_extra_record_keys(odil::BasicDirectoryCreator const & creator)
{
    pybind11::dict result;
    for(auto const & item: creator.extra_record_keys)
    {
        pybind11::list values;
        for(auto const & value_cpp: item.second)
        {
            pybind11::list value_python;
            value_python.append(value_cpp.first);
            value_python.append(value_cpp.second);
            values.append(value_python);
        }
        result[pybind11::cast(item.first)] = values;
    }
    return result;
}

void 
set_extra_record_keys(
    odil::BasicDirectoryCreator & creator, 
    pybind11::dict const & extra_record_keys_python)
{
    for(auto const item: extra_record_keys_python)
    {
        auto const level = pybind11::cast<std::string>(item.first);
        auto const extra_record_keys = item.second;

        std::vector<odil::BasicDirectoryCreator::RecordKey> value(
            pybind11::len(extra_record_keys));
        std::transform(
            extra_record_keys.begin(), extra_record_keys.end(), value.begin(),
            [](pybind11::handle const & h)
            {
                auto const tag_and_type = h.cast<pybind11::sequence>();
                auto const tag = pybind11::cast<odil::Tag>(tag_and_type[0]);
                auto const type = pybind11::cast<int>(tag_and_type[1]);
                return odil::BasicDirectoryCreator::RecordKey(tag, type);
            });

        creator.extra_record_keys[level] = value;
    }
}

}

void wrap_BasicDirectoryCreator(pybind11::module & m)
{
    using namespace pybind11;
    using namespace odil;

    class_<BasicDirectoryCreator>(m, "BasicDirectoryCreator")
        .def(
            init(
                [](
                    std::string const & root, sequence const & files,
                    dict const & extra_record_keys)
                {
                    BasicDirectoryCreator creator(root);
                    set_files(creator, files);
                    set_extra_record_keys(creator, extra_record_keys);
                    return creator;
                }),
            "root"_a, "files"_a, "extra_record_keys"_a=dict())
        .def_readwrite("root", &BasicDirectoryCreator::root)
        .def_property("files", &get_files, &set_files)
        .def_property(
            "extra_record_keys", &get_extra_record_keys, &set_extra_record_keys)
        .def("__call__", &BasicDirectoryCreator::operator())
    ;
}