-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathvdf_python_bindings.cpp
More file actions
55 lines (47 loc) · 1.38 KB
/
Copy pathvdf_python_bindings.cpp
File metadata and controls
55 lines (47 loc) · 1.38 KB
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
#include "vdf_parser.hpp"
#include <fstream>
#include <string>
#include <pybind11/pybind11.h>
#include <pybind11/stl_bind.h>
namespace py = pybind11;
struct python_object
{
py::dict dict;
std::string name;
void add_attribute(std::string key, std::string value)
{
dict[py::cast(std::move(key))] = py::cast(std::move(value));
}
void add_child(std::unique_ptr<python_object> child)
{
std::string n = std::move(child->name);
dict[py::cast(n)] = std::move(child->dict);
}
void set_name(std::string n) { name = std::move(n); }
};
py::dict py_read_file(const char *filename)
{
std::ifstream input(filename);
auto obj = tyti::vdf::read<python_object>(input);
if (obj.name.empty())
return obj.dict;
auto result = py::dict();
result[py::cast(obj.name)] = std::move(obj.dict);
return result;
}
py::dict py_read(const std::string &filename)
{
auto obj = tyti::vdf::read<python_object>(std::begin(filename),
std::end(filename));
if (obj.name.empty())
return obj.dict;
auto result = py::dict();
result[py::cast(obj.name)] = std::move(obj.dict);
return result;
}
PYBIND11_MODULE(vdf, m)
{
m.doc() = "Read and Write Valve's vdf files.";
m.def("read", &py_read, "Read vdf from memory");
m.def("read_file", &py_read_file, "Read vdf file");
}