File: test_python_execute_script.cc

package info (click to toggle)
glom 1.30.4-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 41,260 kB
  • sloc: ansic: 160,257; cpp: 72,338; javascript: 9,331; sh: 4,971; xml: 476; makefile: 315; perl: 236
file content (121 lines) | stat: -rw-r--r-- 3,956 bytes parent folder | download | duplicates (3)
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
115
116
117
118
119
120
121
#include <glom/libglom/init.h>
#include <glom/python_embed/glom_python.h>
#include <iostream>

//Store results from the callbacks and check them later:
Glib::ustring result_table_name_list;
Glib::ustring result_table_name_details;

//This can't be a normal global variable,
//because it would be initialized before we have initialized the glib type system.
static Gnome::Gda::Value& get_result_primary_key_value_details_instance()
{
  static Gnome::Gda::Value result_primary_key_value_details;
  return result_primary_key_value_details;
}

Glib::ustring result_report_name;
bool result_printed_layout = false;
bool result_started_new_record = false;

static void on_script_ui_show_table_list(const Glib::ustring& table_name)
{
  //std::cout << "debug: " << G_STRFUNC << ": table_name=" << table_name << std::endl;
  result_table_name_list = table_name;
}

static void on_script_ui_show_table_details(const Glib::ustring& table_name, const Gnome::Gda::Value& primary_key_value)
{
  //std::cout << "debug: " << G_STRFUNC << ": table_name=" << table_name
  //  << ", primary_key_value=" << primary_key_value.to_string() << std::endl;
  result_table_name_details = table_name;
  get_result_primary_key_value_details_instance() = primary_key_value;
}

static void on_script_ui_print_report(const Glib::ustring& report_name)
{
  result_report_name = report_name;;
}

static void on_script_ui_print_layout()
{
  result_printed_layout = true;
}

static void on_script_ui_start_new_record()
{
  result_started_new_record = true;
}

int main()
{
  Glom::libglom_init(); //Also initializes python.

  const Glib::ustring table_name_input = "sometable";
  const Glib::ustring table_name_details_input = "artists";
  const Gnome::Gda::Value primary_key_value_input(123);
  const Glib::ustring report_name_input = "somereport";

  //Just some code to make sure that the python API exists:
  const Glib::ustring script =
    "table_name = record.table_name;\n"
    "ui.show_table_list(table_name);\n"
    "ui.show_table_details(\"" + table_name_details_input + "\", " + primary_key_value_input.to_string() + ");\n"
    "ui.print_report(\"" + report_name_input + "\");\n"
    "ui.print_layout();\n"
    "ui.start_new_record();\n";
  Glom::type_map_fields field_values;
  Glib::RefPtr<Gnome::Gda::Connection> connection;

  Glom::PythonUICallbacks callbacks;
  callbacks.m_slot_show_table_list =
    &on_script_ui_show_table_list;
  callbacks.m_slot_show_table_details =
    &on_script_ui_show_table_details;
  callbacks.m_slot_print_report =
    &on_script_ui_print_report;
  callbacks.m_slot_print_layout =
    &on_script_ui_print_layout;
  callbacks.m_slot_start_new_record =
    &on_script_ui_start_new_record;

  //Execute a python script:
  Glib::ustring error_message;
  try
  {
    Glom::glom_execute_python_function_implementation(
      script, field_values,
      0 /* document */, table_name_input,
      std::shared_ptr<Glom::Field>(), Gnome::Gda::Value(), // primary key details. Not used in this test.
      connection,
      callbacks,
      error_message);
  }
  catch(const std::exception& ex)
  {
    std::cerr << G_STRFUNC << ": Exception: " << ex.what() << std::endl;
    return EXIT_FAILURE;
  }
  catch(const boost::python::error_already_set& ex)
  {
    std::cerr << G_STRFUNC << ": Exception: boost::python::error_already_set" << std::endl;
    return EXIT_FAILURE;
  }

  if(!error_message.empty())
  {
    std::cerr << G_STRFUNC << ": Python Error: " << error_message << std::endl;
  }

  g_assert(error_message.empty());

  //Check that the callbacks received the expected values:
  g_assert(result_table_name_list == table_name_input);
  g_assert(result_table_name_details == table_name_details_input);
  g_assert(get_result_primary_key_value_details_instance() == primary_key_value_input);
  g_assert(result_report_name == report_name_input);
  g_assert(result_printed_layout);
  g_assert(result_started_new_record);

  return EXIT_SUCCESS;
}