source: nscp/modules/PythonScript/script_wrapper.cpp @ f881d47

0.4.00.4.10.4.2
Last change on this file since f881d47 was f881d47, checked in by Michael Medin <michael@…>, 22 months ago

Fixed some linux issues and readded grammar as it works on non-virtual-machine (memory issues?)

  • Property mode set to 100644
File size: 7.9 KB
Line 
1#include "stdafx.h"
2
3#include <strEx.h>
4#include "script_wrapper.hpp"
5
6using namespace boost::python;
7
8boost::shared_ptr<script_wrapper::functions> script_wrapper::functions::instance;
9
10
11void script_wrapper::log_msg(std::wstring x) {
12        NSC_LOG_ERROR_STD(utf8::cvt<std::wstring>(x));
13}
14
15void script_wrapper::log_exception() {
16        PyErr_Print();
17        boost::python::object sys(boost::python::handle<>(PyImport_ImportModule("sys")));
18        boost::python::object err = sys.attr("stderr");
19        std::string err_text = boost::python::extract<std::string>(err.attr("getvalue")());
20        NSC_LOG_ERROR_STD(utf8::cvt<std::wstring>(err_text));
21        PyErr_Clear();
22}
23void script_wrapper::function_wrapper::register_simple_function(std::string name, PyObject* callable, std::string desc) {
24        try {
25                core->registerCommand(utf8::cvt<std::wstring>(name), utf8::cvt<std::wstring>(desc));
26                functions::get()->simple_functions[name] = callable;
27                NSC_LOG_MESSAGE_STD(_T("Added simple command: ") + utf8::cvt<std::wstring>(name) + _T(" to list of: ") + get_commands());
28        } catch (...) {
29                NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(name));
30        }
31}
32void script_wrapper::function_wrapper::register_function(std::string name, PyObject* callable, std::string desc) {
33        try {
34        core->registerCommand(utf8::cvt<std::wstring>(name), utf8::cvt<std::wstring>(desc));
35        functions::get()->normal_functions[name] = callable;
36        NSC_LOG_MESSAGE_STD(_T("Added simple command: ") + utf8::cvt<std::wstring>(name) + _T(" to list of: ") + get_commands());
37        } catch (...) {
38                NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(name));
39        }
40}
41int script_wrapper::function_wrapper::exec(const std::string cmd, const std::string &request, std::string &response) const {
42        try {
43                functions::function_map_type::iterator it = functions::get()->normal_functions.find(cmd);
44                if (it == functions::get()->normal_functions.end()) {
45                        NSC_LOG_ERROR_STD(_T("Failed to find python function: ") + utf8::cvt<std::wstring>(cmd));
46                        return NSCAPI::returnIgnored;
47                }
48                tuple ret = boost::python::call<tuple>(it->second, cmd, request);
49                if (ret.ptr() == Py_None) {
50                        return NSCAPI::returnUNKNOWN;
51                }
52                int ret_code = NSCAPI::returnUNKNOWN;
53                if (len(ret) > 0)
54                        ret_code = extract<int>(ret[0]);
55                if (len(ret) > 1)
56                        response = extract<std::string>(ret[1]);
57                return ret_code;
58        } catch( error_already_set e) {
59                log_exception();
60                return NSCAPI::returnUNKNOWN;
61        }
62}
63
64int script_wrapper::function_wrapper::exec_simple(const std::string cmd, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) const {
65        try {
66                functions::function_map_type::iterator it = functions::get()->simple_functions.find(cmd);
67                if (it == functions::get()->simple_functions.end()) {
68                        NSC_LOG_ERROR_STD(_T("Failed to find python function: ") + utf8::cvt<std::wstring>(cmd));
69                        return NSCAPI::returnIgnored;
70                }
71
72                boost::python::list l;
73                BOOST_FOREACH(std::wstring a, arguments) {
74                        l.append(utf8::cvt<std::string>(a));
75                }
76                tuple ret = boost::python::call<tuple>(it->second, l);
77                if (ret.ptr() == Py_None) {
78                        msg = _T("None");
79                        return NSCAPI::returnUNKNOWN;
80                }
81                int ret_code = NSCAPI::returnUNKNOWN;
82                if (len(ret) > 0)
83                        ret_code = extract<int>(ret[0]);
84                if (len(ret) > 1)
85                        msg = utf8::cvt<std::wstring>(extract<std::string>(ret[1]));
86                if (len(ret) > 2)
87                        perf = utf8::cvt<std::wstring>(extract<std::string>(ret[2]));
88                return ret_code;
89        } catch( error_already_set e) {
90                log_exception();
91                msg = _T("Exception in: ") + utf8::cvt<std::wstring>(cmd);
92                return NSCAPI::returnUNKNOWN;
93        }
94}
95
96bool script_wrapper::function_wrapper::has_function(const std::string command) {
97        return functions::get()->normal_functions.find(command) != functions::get()->normal_functions.end();
98}
99bool script_wrapper::function_wrapper::has_simple(const std::string command) {
100        return functions::get()->simple_functions.find(command) != functions::get()->simple_functions.end();
101}
102std::wstring script_wrapper::function_wrapper::get_commands() {
103        std::wstring str;
104        BOOST_FOREACH(const functions::function_map_type::value_type& i, functions::get()->normal_functions) {
105                std::wstring tmp = utf8::cvt<std::wstring>(i.first);
106                strEx::append_list(str, tmp, _T(", "));
107        }
108        BOOST_FOREACH(const functions::function_map_type::value_type& i, functions::get()->simple_functions) {
109                std::wstring tmp = utf8::cvt<std::wstring>(i.first);
110                strEx::append_list(str, tmp, _T(", "));
111        }
112        return str;
113}
114
115
116
117std::list<std::wstring> script_wrapper::command_wrapper::convert(list lst) {
118        std::list<std::wstring> ret;
119        for (int i = 0;i<len(lst);i++)
120                ret.push_back(utf8::cvt<std::wstring>(extract<std::string>(lst[i])));
121        return ret;
122}
123tuple script_wrapper::command_wrapper::simple_query(std::string command, list args) {
124        std::wstring msg, perf;
125        int ret = core->InjectSimpleCommand(utf8::cvt<std::wstring>(command), convert(args), msg, perf);
126        return make_tuple(ret,utf8::cvt<std::string>(msg), utf8::cvt<std::string>(perf));
127}
128tuple script_wrapper::command_wrapper::query(std::string command, std::string request) {
129        std::string response;
130        int ret = core->InjectCommand(utf8::cvt<std::wstring>(command), request, response);
131        return make_tuple(ret,response);
132}
133
134
135std::string script_wrapper::settings_wrapper::get_string(std::string path, std::string key, std::string def) {
136        return utf8::cvt<std::string>(core->getSettingsString(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), utf8::cvt<std::wstring>(def)));
137}
138void script_wrapper::settings_wrapper::set_string(std::string path, std::string key, std::string value) {
139        core->SetSettingsString(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), utf8::cvt<std::wstring>(value));
140}
141bool script_wrapper::settings_wrapper::get_bool(std::string path, std::string key, bool def) {
142        return core->getSettingsBool(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), def);
143}
144void script_wrapper::settings_wrapper::set_bool(std::string path, std::string key, bool value) {
145        core->SetSettingsInt(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), value);
146}
147int script_wrapper::settings_wrapper::get_int(std::string path, std::string key, int def) {
148        return core->getSettingsInt(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), def);
149}
150void script_wrapper::settings_wrapper::set_int(std::string path, std::string key, int value) {
151        core->SetSettingsInt(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), value);
152}
153std::list<std::string> script_wrapper::settings_wrapper::get_section(std::string path) {
154        std::list<std::string> ret;
155        BOOST_FOREACH(std::wstring s, core->getSettingsSection(utf8::cvt<std::wstring>(path))) {
156                ret.push_back(utf8::cvt<std::string>(s));
157        }
158        return ret;
159}
160void script_wrapper::settings_wrapper::save() {
161        core->settings_save();
162}
163
164NSCAPI::settings_type script_wrapper::settings_wrapper::get_type(std::string stype) {
165        if (stype == "string" || stype == "str" || stype == "s")
166                return NSCAPI::key_string;
167        if (stype == "integer" || stype == "int" || stype == "i")
168                return NSCAPI::key_integer;
169        if (stype == "bool" || stype == "b")
170                return NSCAPI::key_bool;
171        NSC_LOG_ERROR_STD(_T("Invalid settings type"));
172        return NSCAPI::key_string;
173}
174void script_wrapper::settings_wrapper::settings_register_key(std::string path, std::string key, std::string stype, std::string title, std::string description, std::string defaultValue) {
175        NSCAPI::settings_type type = get_type(stype);
176        core->settings_register_key(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(key), type, utf8::cvt<std::wstring>(title), utf8::cvt<std::wstring>(description), utf8::cvt<std::wstring>(defaultValue), false);
177}
178void script_wrapper::settings_wrapper::settings_register_path(std::string path, std::string title, std::string description) {
179        core->settings_register_path(utf8::cvt<std::wstring>(path), utf8::cvt<std::wstring>(title), utf8::cvt<std::wstring>(description), false);
180}
Note: See TracBrowser for help on using the repository browser.