source: nscp/modules/PythonScript/script_wrapper.hpp @ a44cb15

0.4.00.4.10.4.2
Last change on this file since a44cb15 was a44cb15, checked in by Michael Medin <michael@…>, 21 months ago
  • Implemented registration of channels (so no longer faked)
  • Added settings key to change the NSCAAgent channel name
  • Addded proper channel handling to PythonScript module
  • Improved error handling in channels API
  • Rewrote wrapper API to use templates and classes instead of macros (ish)
  • Improved the internal plugin wrapping API to support multiple plugin load
  • Fixed so PythonScript module supports multiple plugin load (with new argument for plugin_id)
  • Added API for registrying routers and handling routing (almost there now)
  • Fixed issue with messages due to new API
  • Property mode set to 100644
File size: 5.7 KB
Line 
1#pragma once
2
3#include <boost/python.hpp>
4
5namespace script_wrapper {
6        using namespace boost::python;
7       
8
9        enum status {
10                OK = NSCAPI::returnOK,
11                WARN = NSCAPI::returnWARN,
12                CRIT = NSCAPI::returnCRIT,
13                UNKNOWN = NSCAPI::returnUNKNOWN,
14        };
15
16        void log_exception();
17        void log_msg(std::wstring x);
18        //std::string get_alias();
19
20        std::list<std::wstring> convert(boost::python::list lst);
21        boost::python::list convert(std::list<std::wstring> lst);
22
23
24        struct functions {
25                typedef std::map<std::string,PyObject*> function_map_type;
26                function_map_type simple_functions;
27                function_map_type normal_functions;
28
29                function_map_type simple_cmdline;
30                function_map_type normal_cmdline;
31
32                function_map_type simple_handler;
33                function_map_type normal_handler;
34
35                static boost::shared_ptr<functions> instance;
36                static boost::shared_ptr<functions> get() {
37                        if (!instance)
38                                instance = boost::shared_ptr<functions>(new functions());
39                        return instance;
40                }
41                static void destroy() {
42                        instance.reset();
43                }
44        };
45
46        struct function_wrapper {
47        private:
48                nscapi::core_wrapper* core;
49                unsigned int plugin_id;
50                function_wrapper() {}
51        public:
52                function_wrapper(const function_wrapper &other) : core(other.core) {}
53                function_wrapper& operator=(const function_wrapper &other) {
54                        core = other.core;
55                        return *this;
56                }
57                function_wrapper(nscapi::core_wrapper* core, unsigned int plugin_id) : core(core), plugin_id(plugin_id) {}
58                typedef std::map<std::string,PyObject*> function_map_type;
59                //typedef boost::python::tuple simple_return;
60
61
62                static boost::shared_ptr<function_wrapper> create(unsigned int plugin_id) {
63                        return boost::shared_ptr<function_wrapper>(new function_wrapper(nscapi::plugin_singleton->get_core(), plugin_id));
64                }
65
66                void register_simple_cmdline(std::string name, PyObject* callable);
67                void register_cmdline(std::string name, PyObject* callable);
68                void register_simple_function(std::string name, PyObject* callable, std::string desc);
69                void register_function(std::string name, PyObject* callable, std::string desc);
70                void subscribe_function(std::string channel, PyObject* callable);
71                void subscribe_simple_function(std::string channel, PyObject* callable);
72                int exec_simple(const std::string wcmd, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) const;
73                int exec(const std::string wcmd, const std::string &request, std::string &response) const;
74                bool has_function(const std::string command);
75                bool has_simple(const std::string command);
76
77                int handle_simple_exec(const std::string wcmd, std::list<std::wstring> arguments, std::wstring &result) const;
78                int handle_exec(const std::string wcmd, const std::string &request, std::string &response) const;
79                bool has_cmdline(const std::string command);
80                bool has_simple_cmdline(const std::string command);
81
82
83                int handle_simple_message(const std::string channel, const std::string wcmd, int code, std::wstring &msg, std::wstring &perf) const;
84                int handle_message(const std::string channel, const std::string wcmd, std::string &message) const;
85                bool has_message_handler(const std::string command);
86                bool has_simple_message_handler(const std::string command);
87
88                std::wstring get_commands();
89        };
90        struct command_wrapper {
91        private:
92                nscapi::core_wrapper* core;
93        public:
94                command_wrapper() : core(NULL) {}
95                command_wrapper(const command_wrapper &other) : core(other.core) {}
96                command_wrapper& operator=(const command_wrapper &other) {
97                        core = other.core;
98                        return *this;
99                }
100                command_wrapper(nscapi::core_wrapper* core) : core(core) {}
101
102        public:
103                static boost::shared_ptr<command_wrapper> create() {
104                        return boost::shared_ptr<command_wrapper>(new command_wrapper(nscapi::plugin_singleton->get_core()));
105                }
106
107                tuple simple_query(std::string command, boost::python::list args);
108                tuple query(std::string command, std::string request);
109                object simple_exec(std::string command, boost::python::list args);
110                tuple exec(std::string command, std::string request);
111                void simple_submit(std::string channel, std::string command, status code, std::string message, std::string perf);
112                void submit() {}
113        };
114
115        struct settings_wrapper {
116        private:
117                nscapi::core_wrapper* core;
118        public:
119                settings_wrapper() : core(NULL) {}
120                settings_wrapper(const settings_wrapper &other) : core(other.core) {}
121                settings_wrapper& operator=(const settings_wrapper &other) {
122                        core = other.core;
123                        return *this;
124                }
125                settings_wrapper(nscapi::core_wrapper* core) : core(core) {}
126
127        public:
128                static boost::shared_ptr<settings_wrapper> create() {
129                        return boost::shared_ptr<settings_wrapper>(new settings_wrapper(nscapi::plugin_singleton->get_core()));
130                }
131               
132
133                std::string get_string(std::string path, std::string key, std::string def = "");
134                void set_string(std::string path, std::string key, std::string value);
135                bool get_bool(std::string path, std::string key, bool def);
136                void set_bool(std::string path, std::string key, bool value);
137                int get_int(std::string path, std::string key, int def);
138                void set_int(std::string path, std::string key, int value);
139                std::list<std::string> get_section(std::string path);
140                void save();
141                NSCAPI::settings_type get_type(std::string stype);
142                void settings_register_key(std::string path, std::string key, std::string stype, std::string title, std::string description, std::string defaultValue);
143                void settings_register_path(std::string path, std::string title, std::string description);
144        };
145
146
147        class PyInitializer {
148        public:  PyInitializer()  { Py_Initialize(); } 
149                         ~PyInitializer() { Py_Finalize(); }
150        private: 
151                PyInitializer(const PyInitializer &); 
152                PyInitializer & operator=(const PyInitializer &);
153        };
154
155
156
157
158}
Note: See TracBrowser for help on using the repository browser.