Skip to content

Commit 45b11b8

Browse files
[gnucash-commands.cpp] don't use m_, extract python funcs, modify argv
now argv will have either datafile or "" -- this is accepted by python and guile alike
1 parent 1c036b1 commit 45b11b8

File tree

1 file changed

+60
-58
lines changed

1 file changed

+60
-58
lines changed

gnucash/gnucash-commands.cpp

+60-58
Original file line numberDiff line numberDiff line change
@@ -577,81 +577,81 @@ run_guile_cli (void *data, [[maybe_unused]] int argc, [[maybe_unused]] char **ar
577577
}
578578

579579

580+
#ifdef HAVE_PYTHON_H
581+
static void
582+
python_cleanup (PyConfig& config, PyStatus& status)
583+
{
584+
if (qof_book_session_not_saved (gnc_get_current_book()))
585+
std::cerr << _("Book is readonly. Unsaved changes will be lost.") << std::endl;
586+
gnc_clear_current_session ();
587+
588+
PyConfig_Clear(&config);
589+
if (PyStatus_IsExit(status))
590+
gnc_shutdown_cli (status.exitcode);
591+
592+
Py_ExitStatusException(status);
593+
}
594+
580595
static void
581596
run_python_cli (int argc, char **argv, scripting_args* args)
582597
{
583-
#ifdef HAVE_PYTHON_H
584598
PyConfig config;
585599
PyConfig_InitPythonConfig(&config);
586600

587601
PyStatus status = PyConfig_SetBytesArgv(&config, argc, argv);
588-
try
589-
{
590-
if (PyStatus_Exception(status))
591-
throw;
602+
if (PyStatus_Exception(status))
603+
python_cleanup (config, status);
592604

593-
status = Py_InitializeFromConfig(&config);
594-
if (PyStatus_Exception(status))
595-
throw;
605+
status = Py_InitializeFromConfig(&config);
606+
if (PyStatus_Exception(status))
607+
python_cleanup (config, status);
596608

597-
PyConfig_Clear(&config);
609+
PyConfig_Clear(&config);
598610

599-
if (args->script)
600-
{
601-
auto script_filename = args->script->c_str();
602-
PINFO ("Running python script %s...", script_filename);
603-
auto fp = fopen (script_filename, "r");
604-
if (fp)
605-
PyRun_SimpleFileEx (fp, script_filename, 1);
606-
else
607-
std::cerr << bl::format (_("Unable to load Python script {1}")) % script_filename
608-
<< std::endl;
609-
}
610-
if (args->interactive)
611-
{
612-
std::cout << _("Welcome to Gnucash Interactive Python Session") << std::endl;
613-
PyRun_InteractiveLoop (stdin, "foo");
614-
}
615-
Py_Finalize();
616-
cleanup_and_exit_with_save ();
611+
if (args->script)
612+
{
613+
auto script_filename = args->script->c_str();
614+
PINFO ("Running python script %s...", script_filename);
615+
auto fp = fopen (script_filename, "rb");
616+
if (!fp)
617+
std::cerr << bl::format (_("Unable to load Python script {1}")) % script_filename
618+
<< std::endl;
619+
else if (PyRun_SimpleFileEx (fp, script_filename, 1) != 0)
620+
std::cerr << bl::format (_("Python script {1} execution failed.")) % script_filename
621+
<< std::endl;
617622
}
618-
catch (const std::exception&)
623+
if (args->interactive)
619624
{
620-
if (qof_book_session_not_saved (gnc_get_current_book()))
621-
std::cerr << _("Book is readonly. Unsaved changes will be lost.") << std::endl;
622-
gnc_clear_current_session ();
623-
624-
PyConfig_Clear(&config);
625-
if (PyStatus_IsExit(status))
626-
gnc_shutdown_cli (status.exitcode);
627-
628-
Py_ExitStatusException(status);
625+
std::cout << _("Welcome to Gnucash Interactive Python Session") << std::endl;
626+
PyRun_InteractiveLoop (stdin, "foo");
629627
}
630-
#endif
628+
Py_Finalize();
629+
cleanup_and_exit_with_save ();
631630
}
631+
#endif
632632

633633
int
634-
Gnucash::run_scripting (std::vector<std::string> m_script_args,
635-
const bo_str& m_file_to_load,
636-
std::string& m_language,
637-
const bo_str& m_script,
638-
bool m_open_readwrite,
639-
bool m_interactive)
634+
Gnucash::run_scripting (std::vector<std::string> script_args,
635+
const bo_str& file_to_load,
636+
std::string& language,
637+
const bo_str& script,
638+
bool open_readwrite,
639+
bool interactive)
640640
{
641641
std::vector<std::string> errors;
642642
static const std::vector<std::string> languages = { "guile", "python" };
643643

644-
if (m_open_readwrite && !m_file_to_load)
644+
if (open_readwrite && !file_to_load)
645645
errors.push_back (_ ("--readwrite: missing datafile!"));
646646

647-
if (m_script && (!boost::filesystem::is_regular_file (*m_script)))
648-
errors.push_back ((bl::format (_("--script: {1} is not a file")) % *m_script).str());
647+
if (script && (!boost::filesystem::is_regular_file (*script)))
648+
errors.push_back ((bl::format (_("--script: {1} is not a file")) % *script).str());
649649

650650
if (std::none_of (languages.begin(), languages.end(),
651-
[&m_language](auto& lang){ return m_language == lang; }))
651+
[&language](auto& lang){ return language == lang; }))
652652
errors.push_back (_ ("--language: must be 'python' or 'guile'"));
653653
#ifndef HAVE_PYTHON_H
654-
else if (m_language == "python")
654+
else if (language == "python")
655655
errors.push_back (_("--language: python wasn't compiled in this build"));
656656
#endif
657657

@@ -663,28 +663,30 @@ Gnucash::run_scripting (std::vector<std::string> m_script_args,
663663
gnc_shutdown_cli (1);
664664
}
665665

666-
std::vector<const char*> newArgv {"gnucash-cli"};
667-
std::transform (m_script_args.begin(), m_script_args.end(), std::back_inserter(newArgv),
666+
std::vector<const char*> newArgv =
667+
{ "gnucash-cli", file_to_load ? file_to_load->c_str() : ""};
668+
std::transform (script_args.begin(), script_args.end(), std::back_inserter(newArgv),
668669
[](const std::string& s) { return s.c_str(); });
669-
// note the vector<const char*> is valid as long as m_script_args's strings are not damaged!
670+
// note the vector<const char*> is valid as long as script_args's strings are not damaged!
670671

671672
gnc_prefs_init ();
672673
gnc_ui_util_init();
673-
if (m_file_to_load)
674-
load_file (*m_file_to_load, m_open_readwrite);
674+
if (file_to_load && boost::filesystem::is_regular_file (*file_to_load))
675+
load_file (*file_to_load, open_readwrite);
675676

676-
scripting_args args { m_script, m_interactive };
677-
if (m_language == "guile")
677+
scripting_args args { script, interactive };
678+
if (language == "guile")
678679
{
679680
scm_boot_guile (newArgv.size(), (char**)newArgv.data(), run_guile_cli, &args);
680681
return 0; // never reached...
681682
}
682-
else if (m_language == "python")
683+
#ifdef HAVE_PYTHON_H
684+
else if (language == "python")
683685
{
684686
run_python_cli (newArgv.size(), (char**)newArgv.data(), &args);
685687
return 0; // never reached...
686688
}
687-
689+
#endif
688690

689691
return 0; // never reached
690692
}

0 commit comments

Comments
 (0)