gnucash master: Multiple changes pushed

John Ralls jralls at code.gnucash.org
Fri May 29 20:15:28 EDT 2020


Updated	 via  https://github.com/Gnucash/gnucash/commit/30450e4d (commit)
	 via  https://github.com/Gnucash/gnucash/commit/3884e24e (commit)
	 via  https://github.com/Gnucash/gnucash/commit/1e9b5a57 (commit)
	 via  https://github.com/Gnucash/gnucash/commit/c23ca6fb (commit)
	 via  https://github.com/Gnucash/gnucash/commit/caac8501 (commit)
	 via  https://github.com/Gnucash/gnucash/commit/b34913fe (commit)
	from  https://github.com/Gnucash/gnucash/commit/1ea284d8 (commit)



commit 30450e4d8748f4a56ec59d755441980153c133a6
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Fri May 29 17:13:59 2020 -0700

    Check all known report templates instead of just custom reports.
    
    Allows users to run any report from the command-line.

diff --git a/gnucash/report/report-core.scm b/gnucash/report/report-core.scm
index fdc1faa0b..8db4eb14e 100644
--- a/gnucash/report/report-core.scm
+++ b/gnucash/report/report-core.scm
@@ -801,12 +801,13 @@ not found.")))
 
 (define-public (gnc:cmdline-run-report report export-type output-file dry-run?)
   (let ((template (or (gnc:find-report-template report)
-                      (let lp ((custom-templates (gnc:custom-report-templates-list)))
-                        (cond
-                         ((null? custom-templates) #f)
-                         ((equal? (gnc:report-template-name (cdar custom-templates))
-                                  report) (cdar custom-templates))
-                         (else (lp (cdr custom-templates))))))))
+                      (let ((retval #f))
+                        (hash-for-each
+                         (lambda (report-guid template)
+                           (when (equal? (gnc:report-template-name template) report)
+                             (set! retval template)))
+                         *gnc:_report-templates_*)
+                        retval))))
 
     (define (run-report output-port)
       (display

commit 3884e24eb93de20ec4acc9706077e120412540ea
Author: John Ralls <jralls at ceridwen.us>
Date:   Fri May 29 17:11:38 2020 -0700

    Convert args passed to run reports to const std::string&.

diff --git a/gnucash/gnucash-cli.cpp b/gnucash/gnucash-cli.cpp
index c85bb7f86..809e2230f 100644
--- a/gnucash/gnucash-cli.cpp
+++ b/gnucash/gnucash-cli.cpp
@@ -97,16 +97,20 @@ Gnucash::GnucashCli::configure_program_options (void)
     quotes_options.add_options()
     ("add-price-quotes", bpo::bool_switch(),
      N_("Add price quotes to given GnuCash datafile.\n"))
+    ("namespace", bpo::value<std::string>(),
+     N_("Regular expression determining which namespace commodities will be retrieved"));
+    m_opt_desc->add (quotes_options);
+
+    bpo::options_description report_options(_("Run Report Options"));
+    report_options.add_options()
     ("run-report", bpo::value<std::string>(),
      N_("Runs a report\n"))
     ("export-type", bpo::value<std::string>(),
      N_("Specify export type\n"))
     ("output-file", bpo::value<std::string>(),
-     N_("Output file for report\n"))
-    ("namespace", bpo::value<std::string>(),
-     N_("Regular expression determining which namespace commodities will be retrieved"));
+     N_("Output file for report\n"));
+    m_opt_desc->add (report_options);
 
-    m_opt_desc->add (quotes_options);
 }
 
 int
diff --git a/gnucash/gnucash-commands.cpp b/gnucash/gnucash-commands.cpp
index 5db1ef0e3..61ee94318 100644
--- a/gnucash/gnucash-commands.cpp
+++ b/gnucash/gnucash-commands.cpp
@@ -45,10 +45,10 @@ extern "C" {
 namespace bl = boost::locale;
 
 struct run_report_args {
-    const char *file_to_load;
-    const char *run_report;
-    const char *export_type;
-    const char *output_file;
+    const std::string& file_to_load;
+    const std::string& run_report;
+    const std::string& export_type;
+    const std::string& output_file;
 };
 
 /* This static indicates the debugging module that this .o belongs to.  */
@@ -138,6 +138,7 @@ scm_run_report (void *data,
     scm_c_eval_string("(debug-set! stack 200000)");
     scm_c_use_module ("gnucash utilities");
     scm_c_use_module ("gnucash app-utils");
+    scm_c_use_module ("gnucash report");
     scm_c_use_module ("gnucash reports");
 
     // gnc_report_init ();
@@ -145,12 +146,13 @@ scm_run_report (void *data,
     // load_user_config();
     gnc_prefs_init ();
     qof_event_suspend ();
-    datafile = args->file_to_load;
+    datafile = args->file_to_load.c_str();
 
     cmdline = scm_c_eval_string ("gnc:cmdline-run-report");
-    report = scm_from_utf8_string (args->run_report);
-    type = args->export_type ? scm_from_utf8_string (args->export_type) : SCM_BOOL_F;
-    file = args->output_file ? scm_from_utf8_string (args->output_file) : SCM_BOOL_F;
+    report = scm_from_utf8_string (args->run_report.c_str());
+
+    type = !args->export_type.empty() ? scm_from_utf8_string (args->export_type.c_str()) : SCM_BOOL_F;
+    file = !args->output_file.empty() ? scm_from_utf8_string (args->output_file.c_str()) : SCM_BOOL_F;
 
     /* dry-run? is #t: try report, check validity of options */
     if (scm_is_false (scm_call_4 (cmdline, report, type, file, SCM_BOOL_T)))
@@ -203,13 +205,13 @@ Gnucash::add_quotes (std::string &uri)
 }
 
 int
-Gnucash::run_report (const std::string file_to_load, std::string &run_report,
-                     std::string &export_type, std::string &output_file)
+Gnucash::run_report (const std::string& file_to_load,
+                     const std::string& run_report,
+                     const std::string& export_type,
+                     const std::string& output_file)
 {
-    auto args = run_report_args { file_to_load.c_str(),
-                                  run_report.c_str(),
-                                  export_type.c_str(),
-                                  output_file.c_str() };
+    auto args = run_report_args { file_to_load, run_report,
+                                  export_type, output_file };
     if (not run_report.empty())
         scm_boot_guile (0, nullptr, scm_run_report, &args);
 
diff --git a/gnucash/gnucash-commands.hpp b/gnucash/gnucash-commands.hpp
index 8e74a51d6..9c59d74b6 100644
--- a/gnucash/gnucash-commands.hpp
+++ b/gnucash/gnucash-commands.hpp
@@ -30,8 +30,10 @@
 namespace Gnucash {
 
     int add_quotes (std::string &uri);
-    int run_report (const std::string file_to_load, std::string &run_report,
-                    std::string &export_type, std::string &output_file);
+    int run_report (const std::string& file_to_load,
+                    const std::string& run_report,
+                    const std::string& export_type,
+                    const std::string& output_file);
 
 }
 #endif

commit 1e9b5a57070e2f9f05eef07afc3fe176225468d2
Author: John Ralls <jralls at ceridwen.us>
Date:   Fri May 29 16:03:25 2020 -0700

    gnucash-cli: Actually run the report.

diff --git a/gnucash/gnucash-cli.cpp b/gnucash/gnucash-cli.cpp
index 762857d47..c85bb7f86 100644
--- a/gnucash/gnucash-cli.cpp
+++ b/gnucash/gnucash-cli.cpp
@@ -126,7 +126,18 @@ Gnucash::GnucashCli::start ([[maybe_unused]] int argc, [[maybe_unused]] char **a
             return Gnucash::add_quotes (m_file_to_load);
     }
 
-
+    if (!m_run_report.empty())
+    {
+        if (m_file_to_load.empty())
+        {
+            std::cerr << bl::translate("Missing data file parameter") << "\n\n"
+                      << *m_opt_desc.get();
+            return 1;
+        }
+        else
+            return Gnucash::run_report(m_file_to_load, m_run_report,
+                                       m_export_type, m_output_file);
+    }
     return 1;
 }
 

commit c23ca6fb12d18bc4e8298e834fa13086fcbee990
Author: John Ralls <jralls at ceridwen.us>
Date:   Fri May 29 16:00:52 2020 -0700

    Make run-report member variable names consistent.

diff --git a/gnucash/gnucash-cli.cpp b/gnucash/gnucash-cli.cpp
index 4e44bca38..762857d47 100644
--- a/gnucash/gnucash-cli.cpp
+++ b/gnucash/gnucash-cli.cpp
@@ -56,9 +56,9 @@ namespace Gnucash {
         void configure_program_options (void);
 
         bool m_add_quotes;
-        std::string run_report;
-        std::string export_type;
-        std::string output_file;
+        std::string m_run_report;
+        std::string m_export_type;
+        std::string m_output_file;
     };
 
 }
@@ -80,13 +80,13 @@ Gnucash::GnucashCli::parse_command_line (int argc, char **argv)
         as<std::string>().c_str());
 
     if (m_opt_map.count ("run-report"))
-        run_report = m_opt_map["run-report"].as<std::string>();
+        m_run_report = m_opt_map["run-report"].as<std::string>();
 
     if (m_opt_map.count ("export-type"))
-        export_type = m_opt_map["export-type"].as<std::string>();
+        m_export_type = m_opt_map["export-type"].as<std::string>();
 
     if (m_opt_map.count ("output-file"))
-        output_file = m_opt_map["output-file"].as<std::string>();
+        m_output_file = m_opt_map["output-file"].as<std::string>();
 }
 
 // Define command line options specific to gnucash-cli.

commit caac8501047ef6997cbc4fe7d7cdb8a2505e9d6c
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Fri May 29 23:49:59 2020 +0800

    instead of saved-reports only, dump in-menu? reports alphabetically
    
    @derekatkins request

diff --git a/gnucash/report/report-core.scm b/gnucash/report/report-core.scm
index 1014f62b6..fdc1faa0b 100644
--- a/gnucash/report/report-core.scm
+++ b/gnucash/report/report-core.scm
@@ -817,12 +817,19 @@ not found.")))
 
     (cond
      ((not template)
-      (stderr-log "Cannot find report ~s. Valid reports are:\n" report)
+      (stderr-log "Cannot find ~s. Valid reports:\n" report)
       (for-each
-       (lambda (template)
-         (stderr-log "* ~a\n" (gnc:report-template-name (cdr template))))
-       (gnc:custom-report-templates-list))
-      #f)
+       (lambda (pair)
+         (when (gnc:report-template-in-menu? (cdr pair))
+           (stderr-log "* ~a ~a\n"
+                       (if (gnc:report-template-parent-type (cdr pair)) "C" " ")
+                       (gnc:report-template-name (cdr pair)))))
+       (sort (hash-map->list cons *gnc:_report-templates_*)
+             (lambda (a b)
+               (string<?
+                (gnc:report-template-name (cdr a))
+                (gnc:report-template-name (cdr b))))))
+      (stderr-log "\n"))
      (export-type (template-export report template export-type output-file dry-run?))
      (dry-run? #t)
      (output-file

commit b34913feda395910587dbe26058978821da5e9b5
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Fri May 29 20:13:54 2020 +0800

    [cli-reports] run reports from cli

diff --git a/gnucash/gnucash-cli.cpp b/gnucash/gnucash-cli.cpp
index 607392ba7..4e44bca38 100644
--- a/gnucash/gnucash-cli.cpp
+++ b/gnucash/gnucash-cli.cpp
@@ -56,6 +56,9 @@ namespace Gnucash {
         void configure_program_options (void);
 
         bool m_add_quotes;
+        std::string run_report;
+        std::string export_type;
+        std::string output_file;
     };
 
 }
@@ -75,6 +78,15 @@ Gnucash::GnucashCli::parse_command_line (int argc, char **argv)
     if (m_opt_map.count ("namespace"))
         gnc_prefs_set_namespace_regexp(m_opt_map["namespace"].
         as<std::string>().c_str());
+
+    if (m_opt_map.count ("run-report"))
+        run_report = m_opt_map["run-report"].as<std::string>();
+
+    if (m_opt_map.count ("export-type"))
+        export_type = m_opt_map["export-type"].as<std::string>();
+
+    if (m_opt_map.count ("output-file"))
+        output_file = m_opt_map["output-file"].as<std::string>();
 }
 
 // Define command line options specific to gnucash-cli.
@@ -85,6 +97,12 @@ Gnucash::GnucashCli::configure_program_options (void)
     quotes_options.add_options()
     ("add-price-quotes", bpo::bool_switch(),
      N_("Add price quotes to given GnuCash datafile.\n"))
+    ("run-report", bpo::value<std::string>(),
+     N_("Runs a report\n"))
+    ("export-type", bpo::value<std::string>(),
+     N_("Specify export type\n"))
+    ("output-file", bpo::value<std::string>(),
+     N_("Output file for report\n"))
     ("namespace", bpo::value<std::string>(),
      N_("Regular expression determining which namespace commodities will be retrieved"));
 
diff --git a/gnucash/gnucash-commands.cpp b/gnucash/gnucash-commands.cpp
index c855cd7ab..5db1ef0e3 100644
--- a/gnucash/gnucash-commands.cpp
+++ b/gnucash/gnucash-commands.cpp
@@ -44,6 +44,13 @@ extern "C" {
 
 namespace bl = boost::locale;
 
+struct run_report_args {
+    const char *file_to_load;
+    const char *run_report;
+    const char *export_type;
+    const char *output_file;
+};
+
 /* This static indicates the debugging module that this .o belongs to.  */
 static QofLogModule log_module = GNC_MOD_GUI;
 
@@ -108,6 +115,84 @@ fail:
     gnc_shutdown(1);
 }
 
+static void
+report_session_percentage (const char *message, double percent)
+{
+    static double previous = 0.0;
+    if ((percent - previous) < 5.0)
+        return;
+    fprintf (stderr, "\r%3.0f%% complete...", percent);
+    previous = percent;
+    return;
+}
+
+static void
+scm_run_report (void *data,
+                [[maybe_unused]] int argc, [[maybe_unused]] char **argv)
+{
+    auto args = static_cast<run_report_args*>(data);
+    QofSession *session = NULL;
+    SCM cmdline, report, type, file;
+    const gchar *datafile;
+
+    scm_c_eval_string("(debug-set! stack 200000)");
+    scm_c_use_module ("gnucash utilities");
+    scm_c_use_module ("gnucash app-utils");
+    scm_c_use_module ("gnucash reports");
+
+    // gnc_report_init ();
+    // load_system_config();
+    // load_user_config();
+    gnc_prefs_init ();
+    qof_event_suspend ();
+    datafile = args->file_to_load;
+
+    cmdline = scm_c_eval_string ("gnc:cmdline-run-report");
+    report = scm_from_utf8_string (args->run_report);
+    type = args->export_type ? scm_from_utf8_string (args->export_type) : SCM_BOOL_F;
+    file = args->output_file ? scm_from_utf8_string (args->output_file) : SCM_BOOL_F;
+
+    /* dry-run? is #t: try report, check validity of options */
+    if (scm_is_false (scm_call_4 (cmdline, report, type, file, SCM_BOOL_T)))
+        goto fail;
+
+    fprintf (stderr, "Loading datafile %s...\n", datafile);
+
+    session = gnc_get_current_session ();
+    if (!session) goto fail;
+
+    qof_session_begin (session, datafile, TRUE, FALSE, FALSE);
+    if (qof_session_get_error (session) != ERR_BACKEND_NO_ERR) goto fail;
+
+    qof_session_load (session, report_session_percentage);
+    if (qof_session_get_error (session) != ERR_BACKEND_NO_ERR) goto fail;
+
+    fprintf (stderr, "\n");
+
+    /* dry-run? is #f: run the report */
+    scm_call_4 (cmdline, report, type, file, SCM_BOOL_F);
+
+    qof_session_end (session);
+    if (qof_session_get_error (session) != ERR_BACKEND_NO_ERR) goto fail;
+
+    qof_session_destroy (session);
+
+    qof_event_resume ();
+    gnc_shutdown (0);
+    return;
+fail:
+    if (session)
+    {
+        if (qof_session_get_error (session) != ERR_BACKEND_NO_ERR)
+            g_warning ("Session Error: %d %s",
+                       qof_session_get_error (session),
+                       qof_session_get_error_message (session));
+        qof_session_destroy (session);
+    }
+    qof_event_resume ();
+    gnc_shutdown (1);
+}
+
 int
 Gnucash::add_quotes (std::string &uri)
 {
@@ -116,3 +201,17 @@ Gnucash::add_quotes (std::string &uri)
 
     return 0;
 }
+
+int
+Gnucash::run_report (const std::string file_to_load, std::string &run_report,
+                     std::string &export_type, std::string &output_file)
+{
+    auto args = run_report_args { file_to_load.c_str(),
+                                  run_report.c_str(),
+                                  export_type.c_str(),
+                                  output_file.c_str() };
+    if (not run_report.empty())
+        scm_boot_guile (0, nullptr, scm_run_report, &args);
+
+    return 0;
+}
diff --git a/gnucash/gnucash-commands.hpp b/gnucash/gnucash-commands.hpp
index 819e60632..8e74a51d6 100644
--- a/gnucash/gnucash-commands.hpp
+++ b/gnucash/gnucash-commands.hpp
@@ -30,6 +30,8 @@
 namespace Gnucash {
 
     int add_quotes (std::string &uri);
+    int run_report (const std::string file_to_load, std::string &run_report,
+                    std::string &export_type, std::string &output_file);
 
 }
 #endif
diff --git a/gnucash/report/report-core.scm b/gnucash/report/report-core.scm
index 077c2965c..1014f62b6 100644
--- a/gnucash/report/report-core.scm
+++ b/gnucash/report/report-core.scm
@@ -772,3 +772,62 @@ not found.")))
       (gnc:debug "Renaming report " template-guid)
       (gnc:report-template-set-name templ new-name)
       (gnc:save-all-reports))))
+
+(define (stderr-log tmpl . args)
+  (apply format (current-error-port) tmpl args)
+  #f)
+
+(define (template-export report template export-type output-file dry-run?)
+  (let* ((report-guid (gnc:report-template-report-guid template))
+         (parent-template-guid (gnc:report-template-parent-type template))
+         (parent-template (hash-ref *gnc:_report-templates_* parent-template-guid))
+         (parent-export-thunk (gnc:report-template-export-thunk parent-template))
+         (parent-export-types (gnc:report-template-export-types parent-template)))
+
+    (cond
+     ((not parent-export-thunk) (stderr-log "Report ~s has no export code\n" report))
+     ((not parent-export-types) (stderr-log "Report ~s has no export-types\n" report))
+     ((not (assoc export-type parent-export-types))
+      (stderr-log "Export-type disallowed: ~a. Allowed types: ~a\n"
+                  export-type (string-join (map car parent-export-types) ", ")))
+     ((not output-file) (stderr-log "No output file specified\n"))
+     (dry-run? #t)
+     (else
+      (display "Running export..." (current-error-port))
+      (parent-export-thunk
+       (gnc-report-find (gnc:make-report report-guid))
+       (assoc-ref parent-export-types export-type) output-file)
+      (display "done!\n" (current-error-port))))))
+
+(define-public (gnc:cmdline-run-report report export-type output-file dry-run?)
+  (let ((template (or (gnc:find-report-template report)
+                      (let lp ((custom-templates (gnc:custom-report-templates-list)))
+                        (cond
+                         ((null? custom-templates) #f)
+                         ((equal? (gnc:report-template-name (cdar custom-templates))
+                                  report) (cdar custom-templates))
+                         (else (lp (cdr custom-templates))))))))
+
+    (define (run-report output-port)
+      (display
+       (gnc:report-render-html
+        (gnc-report-find
+         (gnc:make-report
+          (gnc:report-template-report-guid template))) #t) output-port))
+
+    (cond
+     ((not template)
+      (stderr-log "Cannot find report ~s. Valid reports are:\n" report)
+      (for-each
+       (lambda (template)
+         (stderr-log "* ~a\n" (gnc:report-template-name (cdr template))))
+       (gnc:custom-report-templates-list))
+      #f)
+     (export-type (template-export report template export-type output-file dry-run?))
+     (dry-run? #t)
+     (output-file
+      (format (current-error-port) "Saving report to ~a..." output-file)
+      (call-with-output-file output-file run-report)
+      (display "complete!\n" (current-error-port)))
+     (else
+      (run-report (current-output-port))))))



Summary of changes:
 gnucash/gnucash-cli.cpp        |  37 ++++++++++++++-
 gnucash/gnucash-commands.cpp   | 101 +++++++++++++++++++++++++++++++++++++++++
 gnucash/gnucash-commands.hpp   |   4 ++
 gnucash/report/report-core.scm |  67 +++++++++++++++++++++++++++
 4 files changed, 207 insertions(+), 2 deletions(-)



More information about the gnucash-changes mailing list