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

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

2011-08-14 MickeM

  • Rename Function to Registry in PythonScript API as well as some other function renames
  • Started to clean up the helpers around the API
  • Added support for execute to PythonScripts? to execute commands
  • BUG: just realised that static plugin instances prevent multiple instances :) Will fix but not now as it is not important (for me)...
  • Added initial support for channels to PythonScript Core still lacks support for subscribing to arbitrary channel
  • Property mode set to 100644
File size: 5.6 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                function_wrapper() {}
50        public:
51                function_wrapper(const function_wrapper &other) : core(other.core) {}
52                function_wrapper& operator=(const function_wrapper &other) {
53                        core = other.core;
54                        return *this;
55                }
56                function_wrapper(nscapi::core_wrapper* core) : core(core) {}
57                typedef std::map<std::string,PyObject*> function_map_type;
58                //typedef boost::python::tuple simple_return;
59
60
61                static boost::shared_ptr<function_wrapper> create() {
62                        return boost::shared_ptr<function_wrapper>(new function_wrapper(nscapi::plugin_singleton->get_core()));
63                }
64
65                void register_simple_cmdline(std::string name, PyObject* callable);
66                void register_cmdline(std::string name, PyObject* callable);
67                void register_simple_function(std::string name, PyObject* callable, std::string desc);
68                void register_function(std::string name, PyObject* callable, std::string desc);
69                void subscribe_function(std::string channel, PyObject* callable);
70                void subscribe_simple_function(std::string channel, PyObject* callable);
71                int exec_simple(const std::string wcmd, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) const;
72                int exec(const std::string wcmd, const std::string &request, std::string &response) const;
73                bool has_function(const std::string command);
74                bool has_simple(const std::string command);
75
76                int handle_simple_exec(const std::string wcmd, std::list<std::wstring> arguments, std::wstring &result) const;
77                int handle_exec(const std::string wcmd, const std::string &request, std::string &response) const;
78                bool has_cmdline(const std::string command);
79                bool has_simple_cmdline(const std::string command);
80
81
82                int handle_simple_message(const std::string channel, const std::string wcmd, int code, std::wstring &msg, std::wstring &perf) const;
83                int handle_message(const std::string channel, const std::string wcmd, std::string &message) const;
84                bool has_message_handler(const std::string command);
85                bool has_simple_message_handler(const std::string command);
86
87                std::wstring get_commands();
88        };
89        struct command_wrapper {
90        private:
91                nscapi::core_wrapper* core;
92        public:
93                command_wrapper() : core(NULL) {}
94                command_wrapper(const command_wrapper &other) : core(other.core) {}
95                command_wrapper& operator=(const command_wrapper &other) {
96                        core = other.core;
97                        return *this;
98                }
99                command_wrapper(nscapi::core_wrapper* core) : core(core) {}
100
101        public:
102                static boost::shared_ptr<command_wrapper> create() {
103                        return boost::shared_ptr<command_wrapper>(new command_wrapper(nscapi::plugin_singleton->get_core()));
104                }
105
106                tuple simple_query(std::string command, boost::python::list args);
107                tuple query(std::string command, std::string request);
108                object simple_exec(std::string command, boost::python::list args);
109                tuple exec(std::string command, std::string request);
110                void simple_submit(std::string channel, std::string command, status code, std::string message, std::string perf);
111                void submit() {}
112        };
113
114        struct settings_wrapper {
115        private:
116                nscapi::core_wrapper* core;
117        public:
118                settings_wrapper() : core(NULL) {}
119                settings_wrapper(const settings_wrapper &other) : core(other.core) {}
120                settings_wrapper& operator=(const settings_wrapper &other) {
121                        core = other.core;
122                        return *this;
123                }
124                settings_wrapper(nscapi::core_wrapper* core) : core(core) {}
125
126        public:
127                static boost::shared_ptr<settings_wrapper> create() {
128                        return boost::shared_ptr<settings_wrapper>(new settings_wrapper(nscapi::plugin_singleton->get_core()));
129                }
130               
131
132                std::string get_string(std::string path, std::string key, std::string def = "");
133                void set_string(std::string path, std::string key, std::string value);
134                bool get_bool(std::string path, std::string key, bool def);
135                void set_bool(std::string path, std::string key, bool value);
136                int get_int(std::string path, std::string key, int def);
137                void set_int(std::string path, std::string key, int value);
138                std::list<std::string> get_section(std::string path);
139                void save();
140                NSCAPI::settings_type get_type(std::string stype);
141                void settings_register_key(std::string path, std::string key, std::string stype, std::string title, std::string description, std::string defaultValue);
142                void settings_register_path(std::string path, std::string title, std::string description);
143        };
144
145
146        class PyInitializer {
147        public:  PyInitializer()  { Py_Initialize(); } 
148                         ~PyInitializer() { Py_Finalize(); }
149        private: 
150                PyInitializer(const PyInitializer &); 
151                PyInitializer & operator=(const PyInitializer &);
152        };
153
154
155
156
157}
Note: See TracBrowser for help on using the repository browser.