source: nscp/modules/PythonScript/script_wrapper.hpp @ 39c73cd

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

2011-08-13 MickeM

  • Added support for command line execution to PythonScript module
  • Readded support for specifying module on command line with --client mode
  • Fixed some issues with the NRPEClient module


2011-08-12 MickeM

  • Finnished (rough) adding back command line exec (with modern API)
  • Fixed so installer uses correct name for dll:s (now Server not Listsener)
  • Property mode set to 100644
File size: 4.9 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
19        struct functions {
20                typedef std::map<std::string,PyObject*> function_map_type;
21                function_map_type simple_functions;
22                function_map_type normal_functions;
23
24                function_map_type simple_cmdline;
25                function_map_type normal_cmdline;
26
27
28                static boost::shared_ptr<functions> instance;
29                static boost::shared_ptr<functions> get() {
30                        if (!instance)
31                                instance = boost::shared_ptr<functions>(new functions());
32                        return instance;
33                }
34                static void destroy() {
35                        instance.reset();
36                }
37        };
38
39        struct function_wrapper {
40        private:
41                nscapi::core_wrapper* core;
42                function_wrapper() {}
43        public:
44                function_wrapper(const function_wrapper &other) : core(other.core) {}
45                function_wrapper& operator=(const function_wrapper &other) {
46                        core = other.core;
47                        return *this;
48                }
49                function_wrapper(nscapi::core_wrapper* core) : core(core) {}
50                typedef std::map<std::string,PyObject*> function_map_type;
51                //function_map_type simple_functions;
52                //function_map_type functions;
53                typedef boost::python::tuple simple_return;
54
55
56                static boost::shared_ptr<function_wrapper> create() {
57                        return boost::shared_ptr<function_wrapper>(new function_wrapper(nscapi::plugin_singleton->get_core()));
58                }
59
60                void register_simple_cmdline(std::string name, PyObject* callable);
61                void register_cmdline(std::string name, PyObject* callable);
62                void register_simple_function(std::string name, PyObject* callable, std::string desc);
63                void register_function(std::string name, PyObject* callable, std::string desc);
64                void subscribe_function() {}
65                void subscribe_simple_function() {}
66                int exec_simple(const std::string wcmd, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) const;
67                int exec(const std::string wcmd, const std::string &request, std::string &response) const;
68                bool has_function(const std::string command);
69                bool has_simple(const std::string command);
70
71                int exec_simple_cmdline(const std::string wcmd, std::list<std::wstring> arguments, std::wstring &result) const;
72                int exec_cmdline(const std::string wcmd, const std::string &request, std::string &response) const;
73                bool has_cmdline(const std::string command);
74                bool has_simple_cmdline(const std::string command);
75
76                std::wstring get_commands();
77        };
78        struct command_wrapper {
79        private:
80                nscapi::core_wrapper* core;
81        public:
82                command_wrapper() : core(NULL) {}
83                command_wrapper(const command_wrapper &other) : core(other.core) {}
84                command_wrapper& operator=(const command_wrapper &other) {
85                        core = other.core;
86                        return *this;
87                }
88                command_wrapper(nscapi::core_wrapper* core) : core(core) {}
89
90        public:
91                static boost::shared_ptr<command_wrapper> create() {
92                        return boost::shared_ptr<command_wrapper>(new command_wrapper(nscapi::plugin_singleton->get_core()));
93                }
94
95                std::list<std::wstring> convert(boost::python::list lst);
96                tuple simple_query(std::string command, boost::python::list args);
97                tuple query(std::string command, std::string request);
98                void simple_exec() {}
99                void exec() {}
100                void simple_submit() {}
101                void submit() {}
102        };
103
104        struct settings_wrapper {
105        private:
106                nscapi::core_wrapper* core;
107        public:
108                settings_wrapper() : core(NULL) {}
109                settings_wrapper(const settings_wrapper &other) : core(other.core) {}
110                settings_wrapper& operator=(const settings_wrapper &other) {
111                        core = other.core;
112                        return *this;
113                }
114                settings_wrapper(nscapi::core_wrapper* core) : core(core) {}
115
116        public:
117                static boost::shared_ptr<settings_wrapper> create() {
118                        return boost::shared_ptr<settings_wrapper>(new settings_wrapper(nscapi::plugin_singleton->get_core()));
119                }
120               
121
122                std::string get_string(std::string path, std::string key, std::string def = "");
123                void set_string(std::string path, std::string key, std::string value);
124                bool get_bool(std::string path, std::string key, bool def);
125                void set_bool(std::string path, std::string key, bool value);
126                int get_int(std::string path, std::string key, int def);
127                void set_int(std::string path, std::string key, int value);
128                std::list<std::string> get_section(std::string path);
129                void save();
130                NSCAPI::settings_type get_type(std::string stype);
131                void settings_register_key(std::string path, std::string key, std::string stype, std::string title, std::string description, std::string defaultValue);
132                void settings_register_path(std::string path, std::string title, std::string description);
133        };
134
135
136        class PyInitializer {
137        public:  PyInitializer()  { Py_Initialize(); } 
138                         ~PyInitializer() { Py_Finalize(); }
139        private: 
140                PyInitializer(const PyInitializer &); 
141                PyInitializer & operator=(const PyInitializer &);
142        };
143
144
145
146
147}
Note: See TracBrowser for help on using the repository browser.