File: xmock_interpreter.cpp

package info (click to toggle)
xeus 5.2.6-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 9,764 kB
  • sloc: cpp: 7,406; makefile: 157; python: 25
file content (114 lines) | stat: -rw-r--r-- 4,191 bytes parent folder | download | duplicates (2)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/***************************************************************************
* Copyright (c) 2016, Johan Mabille and Sylvain Corlay                     *
*                                                                          *
* Distributed under the terms of the BSD 3-Clause License.                 *
*                                                                          *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#include <iostream>

#include "nlohmann/json.hpp"

#include "xmock_interpreter.hpp"

#include "xeus/xhelper.hpp"
#include "xeus/xguid.hpp"

namespace nl = nlohmann;

namespace xeus
{

    void xmock_interpreter::configure_impl()
    {
        auto handle_comm_opened = [](xeus::xcomm&& comm, const xeus::xmessage&) {
            std::cout << "Comm opened for target: " << comm.target().name() << std::endl;
        };
        comm_manager().register_comm_target("test_target", handle_comm_opened);
        using function_type = std::function<void(xeus::xcomm&&, const xeus::xmessage&)>;
    }

    void xmock_interpreter::execute_request_impl(send_reply_callback cb,
                                                 int execution_counter,
                                                 const std::string& code,
                                                 execute_request_config /*config*/,
                                                 nl::json /*user_expressions*/)
    {
        if (code.compare("hello, world") == 0)
        {
            publish_stream("stdout", code);
        }

        if (code.compare("error") == 0)
        {
            publish_stream("stderr", code);
        }

        if (code.compare("?") == 0)
        {
            std::string html_content = R"(<iframe class="xpyt-iframe-pager" src="
                                            https://xeus.readthedocs.io"></iframe>)";

            auto payload = nl::json::array();
            payload[0] = nl::json::object({
                            {"data", {
                                {"text/plain", "https://xeus.readthedocs.io"},
                                {"text/html", html_content}}
                            },
                            {"source", "page"},
                            {"start", 0}
                        });

            cb(xeus::create_successful_reply(payload));
        }

        nl::json pub_data;
        pub_data["text/plain"] = code;
        publish_execution_result(execution_counter, std::move(pub_data), nl::json::object());

        cb(xeus::create_successful_reply());
    }

    nl::json xmock_interpreter::complete_request_impl(const std::string& /* code */,
                                                      int /* cursor_pos */)
    {
        return xeus::create_complete_reply({"a.test1", "a.test2"}, 2, 6);
    }

    nl::json xmock_interpreter::inspect_request_impl(const std::string& /* code */,
                                                     int /* cursor_pos */,
                                                     int /* detail_level */)
    {
        return xeus::create_inspect_reply(true, {{"text/plain", ""}}, {{"text/plain", ""}});
    }

    nl::json xmock_interpreter::is_complete_request_impl(const std::string& code)
    {
        nl::json result = xeus::create_is_complete_reply(code);
        if (code.compare("incomplete") == 0)
        {
            result["indent"] = "   ";
        }
        return result;
    }

    nl::json xmock_interpreter::kernel_info_request_impl()
    {
        return xeus::create_info_reply("",
                                       "cpp_test",
                                       "1.0.0",
                                       "cpp",
                                       "14.0.0",
                                       "text/x-c++src",
                                       ".cpp",
                                       "",
                                       "",
                                       "",
                                       "test_kernel");
    }

    void xmock_interpreter::shutdown_request_impl()
    {
    }
}