gnucash stable: Multiple changes pushed

John Ralls jralls at code.gnucash.org
Sat Sep 21 13:09:05 EDT 2024


Updated	 via  https://github.com/Gnucash/gnucash/commit/6ba1adbf (commit)
	 via  https://github.com/Gnucash/gnucash/commit/9c9ad7c2 (commit)
	 via  https://github.com/Gnucash/gnucash/commit/a23506c8 (commit)
	from  https://github.com/Gnucash/gnucash/commit/e4d2443c (commit)



commit 6ba1adbfdf656a0b54a916a7960d048af6dc0698
Merge: e4d2443cf4 9c9ad7c271
Author: John Ralls <jralls at ceridwen.us>
Date:   Sat Sep 21 10:05:33 2024 -0700

    Merge John Ralls's 'financeapi' into stable.


commit 9c9ad7c271c88ab0585f0442f985cf451e5ff5ed
Author: John Ralls <jralls at ceridwen.us>
Date:   Fri Sep 20 14:55:41 2024 -0700

    Replace GncFQQuoteSource api-key members with a bp::environment one.

diff --git a/libgnucash/app-utils/gnc-quotes.cpp b/libgnucash/app-utils/gnc-quotes.cpp
index 83c4ab785a..e86c76e690 100644
--- a/libgnucash/app-utils/gnc-quotes.cpp
+++ b/libgnucash/app-utils/gnc-quotes.cpp
@@ -63,7 +63,9 @@
 
 static const QofLogModule log_module = "gnc.price-quotes";
 static const char* av_api_env = "ALPHAVANTAGE_API_KEY";
+static const char* av_api_key = "alphavantage-api-key";
 static const char* yh_api_env = "FINANCEAPI_API_KEY";
+static const char* yh_api_key = "yhfinance-api-key";
 
 namespace bl = boost::locale;
 namespace bp = boost::process;
@@ -131,8 +133,7 @@ class GncFQQuoteSource final : public GncQuoteSource
     std::string c_fq_wrapper;
     std::string m_version;
     StrVec m_sources;
-    std::string m_av_api_key;
-    std::string m_yh_api_key;
+    bp::environment m_env;
 public:
     GncFQQuoteSource();
     ~GncFQQuoteSource() = default;
@@ -141,7 +142,7 @@ public:
     QuoteResult get_quotes(const std::string&) const override;
 private:
     QuoteResult run_cmd (const StrVec& args, const std::string& json_string) const;
-
+    void set_api_key(const char* api_pref, const char* api_env);
 };
 
 static void show_quotes(const bpt::ptree& pt, const StrVec& commodities, bool verbose);
@@ -150,28 +151,9 @@ static std::string parse_quotesource_error(const std::string& line);
 
 static const std::string empty_string{};
 
-static inline std::string
-get_api_key(const char* prefskey, const char* envvar)
-{
-    std::string keyval{};
-    auto key = gnc_prefs_get_string ("general.finance-quote", prefskey);
-    if (!(key && *key))
-    {
-        g_free (key);
-        key = g_strdup(getenv(envvar));
-    }
-
-    if (key)
-    {
-        keyval = std::string(key);
-        g_free (key);
-    }
-    return keyval;
-}
-
 GncFQQuoteSource::GncFQQuoteSource() :
 c_cmd{bp::search_path("perl")},
-m_version{}, m_sources{}, m_av_api_key{}, m_yh_api_key{}
+m_version{}, m_sources{}, m_env{boost::this_process::environment()}
 {
     char *bindir = gnc_path_get_bindir();
     c_fq_wrapper = std::string(bindir) + "/finance-quote-wrapper";
@@ -203,10 +185,8 @@ m_version{}, m_sources{}, m_av_api_key{}, m_yh_api_key{}
     m_sources = std::move(sources);
     std::sort (m_sources.begin(), m_sources.end());
 
-    m_av_api_key = get_api_key("alphavantage-api-key", av_api_env);
-    if (m_av_api_key.empty())
-        PWARN("No Alpha Vantage API key set, currency quotes and other AlphaVantage based quotes won't work.");
-    m_yh_api_key = get_api_key("yhfinance-api-key", yh_api_env);
+    set_api_key(av_api_key, av_api_env);
+    set_api_key(yh_api_key, yh_api_env);
 }
 
 QuoteResult
@@ -228,13 +208,7 @@ GncFQQuoteSource::run_cmd (const StrVec& args, const std::string& json_string) c
         boost::asio::io_service svc;
 
         auto input_buf = bp::buffer (json_string);
-        auto curr_env{boost::this_process::environment()};
-        bp::environment new_env{curr_env};
 	bp::child process;
-	if (!m_av_api_key.empty())
-            new_env[av_api_env] = m_av_api_key;
-	if (!m_yh_api_key.empty())
-            new_env[yh_api_env] = m_yh_api_key;
         process = bp::child(c_cmd, args,
                             bp::std_out > out_buf,
                             bp::std_err > err_buf,
@@ -242,7 +216,7 @@ GncFQQuoteSource::run_cmd (const StrVec& args, const std::string& json_string) c
 #ifdef BOOST_WINDOWS_API
                             bp::windows::create_no_window,
 #endif
-                            new_env,
+                            m_env,
                             svc);
 
 	svc.run();
@@ -281,6 +255,24 @@ GncFQQuoteSource::run_cmd (const StrVec& args, const std::string& json_string) c
     return QuoteResult (cmd_result, std::move(out_vec), std::move(err_vec));
 }
 
+void
+GncFQQuoteSource::set_api_key(const char* api_key, const char* api_env)
+{
+    auto key = gnc_prefs_get_string("general.finance-quote", api_key);
+    if (key && *key)
+    {
+        m_env[api_env] = key;
+        g_free(key);
+    }
+    else
+    {
+        if (api_key == av_api_key && m_env.find(api_env) == m_env.end())
+            PWARN("No Alpha Vantage API key set, currency quotes and other "
+                  "AlphaVantage based quotes won't work.");
+        g_free(key);
+    }
+}
+
 /* GncQuotes implementation */
 GncQuotesImpl::GncQuotesImpl() : m_quotesource{new GncFQQuoteSource},
                                  m_sources{}, m_failures{},
@@ -1040,9 +1032,7 @@ GncQuotes::GncQuotes ()
     try
     {
         m_impl = std::make_unique<GncQuotesImpl>();
-    }
-    catch (const GncQuoteSourceError& err)
-    {
+    } catch (const GncQuoteSourceError &err) {
         throw(GncQuoteException(err.what()));
     }
 }

commit a23506c88a3538fbd077e33ae445dc6d35c744fa
Author: John Ralls <jralls at ceridwen.us>
Date:   Thu Sep 19 13:00:33 2024 -0700

    Add YH Finance (FINANCEAPI) API Key to Quotes infrastructure.
    
    And add financeapi to known sources.

diff --git a/gnucash/gschemas/org.gnucash.GnuCash.general.finance-quote.gschema.xml.in b/gnucash/gschemas/org.gnucash.GnuCash.general.finance-quote.gschema.xml.in
index 2248bc3d07..402f4bf261 100644
--- a/gnucash/gschemas/org.gnucash.GnuCash.general.finance-quote.gschema.xml.in
+++ b/gnucash/gschemas/org.gnucash.GnuCash.general.finance-quote.gschema.xml.in
@@ -3,7 +3,12 @@
     <key name="alphavantage-api-key" type="s">
       <default>''</default>
       <summary>Alpha Vantage API key</summary>
-      <description>To retrieve online quotes from Alphavantage, this key needs to be set. A key can be retrieved from the Alpha Vantage website.</description>
+      <description>Alpha Vantage requires an API key to use their service. You can obtain a key by registering for a free account at the Alpha Vantage website, https://alphavantage.co/support/#api-key.</description>
+    </key>
+    <key name="yhfinance-api-key" type="s">
+      <default>''</default>
+      <summary>YH Finance (FinanceAPI) API key</summary>
+      <description>YH Finance requires an API key to use their FinanceAPI service. You can obtain a key by registering for a free account at the YH Finance website, https://financeapi.net/pricing.</description>
     </key>
   </schema>
 </schemalist>
diff --git a/gnucash/gtkbuilder/dialog-preferences.glade b/gnucash/gtkbuilder/dialog-preferences.glade
index 6600457410..08f8fb2c85 100644
--- a/gnucash/gtkbuilder/dialog-preferences.glade
+++ b/gnucash/gtkbuilder/dialog-preferences.glade
@@ -3656,10 +3656,10 @@ many months before the current month</property>
                 <property name="row-spacing">3</property>
                 <property name="column-spacing">6</property>
                 <child>
-                  <object class="GtkLabel" id="apilabel">
+                  <object class="GtkLabel" id="avapilabel">
                     <property name="visible">True</property>
                     <property name="can-focus">False</property>
-                    <property name="tooltip-text" translatable="yes">To retrieve online quotes from Alphavantage, this key needs to be set. A key can be retrieved from the Alpha Vantage website.</property>
+                    <property name="tooltip-text" translatable="yes">Alpha Vantage requires an API key to use their service. You can obtain a key by registering for a free account at the Alpha Vantage website, https://www.alphavantage.co/support/#api-key.</property>
                     <property name="label" translatable="yes">Alpha Vantage API key</property>
                   </object>
                   <packing>
@@ -3671,7 +3671,7 @@ many months before the current month</property>
                   <object class="GtkEntry" id="pref/general.finance-quote/alphavantage-api-key">
                     <property name="visible">True</property>
                     <property name="can-focus">True</property>
-                    <property name="tooltip-text" translatable="yes">To retrieve online quotes from Alphavantage, this key needs to be set. A key can be retrieved from the Alpha Vantage website.</property>
+                    <property name="tooltip-text" translatable="yes">Alpha Vantage requires an API key to use their service. You can obtain a key by registering for a free account at the Alpha Vantage website, https://alphavantage.co/support/#api-key.</property>
                     <property name="hexpand">True</property>
                   </object>
                   <packing>
@@ -3679,6 +3679,30 @@ many months before the current month</property>
                     <property name="top-attach">1</property>
                   </packing>
                 </child>
+                <child>
+                  <object class="GtkLabel" id="yhapilabel">
+                    <property name="visible">True</property>
+                    <property name="can-focus">False</property>
+                    <property name="tooltip-text" translatable="yes">YH Finance requires an API key to use their FinanceAPI service. You can obtain a key by registering for a free account at the YH Finance website, https://financeapi.net/pricing.</property>
+                    <property name="label" translatable="yes">YH Finance (FinanceAPI) API key</property>
+                  </object>
+                  <packing>
+                    <property name="left-attach">0</property>
+                    <property name="top-attach">2</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkEntry" id="pref/general.finance-quote/yhfinance-api-key">
+                    <property name="visible">True</property>
+                    <property name="can-focus">True</property>
+                    <property name="tooltip-text" translatable="yes">YH Finance requires an API key to use their FinanceAPI service. You can obtain a key by registering for a free account at the YH Finance website, https://financeapi.net/pricing.</property>
+                    <property name="hexpand">True</property>
+                  </object>
+                  <packing>
+                    <property name="left-attach">1</property>
+                    <property name="top-attach">2</property>
+                  </packing>
+                </child>
                 <child>
                   <object class="GtkLabel" id="label22">
                     <property name="visible">True</property>
diff --git a/libgnucash/app-utils/gnc-quotes.cpp b/libgnucash/app-utils/gnc-quotes.cpp
index 50d4eaac7e..83c4ab785a 100644
--- a/libgnucash/app-utils/gnc-quotes.cpp
+++ b/libgnucash/app-utils/gnc-quotes.cpp
@@ -20,6 +20,7 @@
  * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
 \ *******************************************************************/
 
+#include <boost/process/environment.hpp>
 #include <config.h>
 #include <qoflog.h>
 
@@ -61,6 +62,8 @@
 #include <qofbook.h>
 
 static const QofLogModule log_module = "gnc.price-quotes";
+static const char* av_api_env = "ALPHAVANTAGE_API_KEY";
+static const char* yh_api_env = "FINANCEAPI_API_KEY";
 
 namespace bl = boost::locale;
 namespace bp = boost::process;
@@ -128,7 +131,8 @@ class GncFQQuoteSource final : public GncQuoteSource
     std::string c_fq_wrapper;
     std::string m_version;
     StrVec m_sources;
-    std::string m_api_key;
+    std::string m_av_api_key;
+    std::string m_yh_api_key;
 public:
     GncFQQuoteSource();
     ~GncFQQuoteSource() = default;
@@ -146,9 +150,28 @@ static std::string parse_quotesource_error(const std::string& line);
 
 static const std::string empty_string{};
 
+static inline std::string
+get_api_key(const char* prefskey, const char* envvar)
+{
+    std::string keyval{};
+    auto key = gnc_prefs_get_string ("general.finance-quote", prefskey);
+    if (!(key && *key))
+    {
+        g_free (key);
+        key = g_strdup(getenv(envvar));
+    }
+
+    if (key)
+    {
+        keyval = std::string(key);
+        g_free (key);
+    }
+    return keyval;
+}
+
 GncFQQuoteSource::GncFQQuoteSource() :
 c_cmd{bp::search_path("perl")},
-m_version{}, m_sources{}, m_api_key{}
+m_version{}, m_sources{}, m_av_api_key{}, m_yh_api_key{}
 {
     char *bindir = gnc_path_get_bindir();
     c_fq_wrapper = std::string(bindir) + "/finance-quote-wrapper";
@@ -180,20 +203,10 @@ m_version{}, m_sources{}, m_api_key{}
     m_sources = std::move(sources);
     std::sort (m_sources.begin(), m_sources.end());
 
-    auto av_key = gnc_prefs_get_string ("general.finance-quote", "alphavantage-api-key");
-    if (!(av_key && *av_key))
-    {
-        g_free (av_key);
-        av_key = g_strdup(getenv("ALPHAVANTAGE_API_KEY"));
-    }
-
-    if (av_key)
-    {
-        m_api_key = std::string(av_key);
-        g_free (av_key);
-    }
-    else
+    m_av_api_key = get_api_key("alphavantage-api-key", av_api_env);
+    if (m_av_api_key.empty())
         PWARN("No Alpha Vantage API key set, currency quotes and other AlphaVantage based quotes won't work.");
+    m_yh_api_key = get_api_key("yhfinance-api-key", yh_api_env);
 }
 
 QuoteResult
@@ -215,26 +228,22 @@ GncFQQuoteSource::run_cmd (const StrVec& args, const std::string& json_string) c
         boost::asio::io_service svc;
 
         auto input_buf = bp::buffer (json_string);
+        auto curr_env{boost::this_process::environment()};
+        bp::environment new_env{curr_env};
 	bp::child process;
-	if (m_api_key.empty())
-	    process = bp::child(c_cmd, args,
-				bp::std_out > out_buf,
-				bp::std_err > err_buf,
-				bp::std_in < input_buf,
-#ifdef BOOST_WINDOWS_API
-                                bp::windows::create_no_window,
-#endif
-				svc);
-	else
-	    process = bp::child(c_cmd, args,
-				bp::std_out > out_buf,
-				bp::std_err > err_buf,
-				bp::std_in < input_buf,
+	if (!m_av_api_key.empty())
+            new_env[av_api_env] = m_av_api_key;
+	if (!m_yh_api_key.empty())
+            new_env[yh_api_env] = m_yh_api_key;
+        process = bp::child(c_cmd, args,
+                            bp::std_out > out_buf,
+                            bp::std_err > err_buf,
+                            bp::std_in < input_buf,
 #ifdef BOOST_WINDOWS_API
-                                bp::windows::create_no_window,
+                            bp::windows::create_no_window,
 #endif
-				bp::env["ALPHAVANTAGE_API_KEY"] = m_api_key,
-				svc);
+                            new_env,
+                            svc);
 
 	svc.run();
 	process.wait();
diff --git a/libgnucash/engine/gnc-commodity.cpp b/libgnucash/engine/gnc-commodity.cpp
index caaeea8c0a..f6bd0155b9 100644
--- a/libgnucash/engine/gnc-commodity.cpp
+++ b/libgnucash/engine/gnc-commodity.cpp
@@ -217,6 +217,7 @@ static QuoteSourceList single_quote_sources =
     { false, SOURCE_SINGLE, "US Govt. Thrift Savings Plan", "tsp" },
     { false, SOURCE_SINGLE, "Yahoo as JSON", "yahoo_json" },
     { false, SOURCE_SINGLE, "Yahoo Web", "yahooweb" },
+    { false, SOURCE_SINGLE, "YH Finance (FinanceAPI)", "financeapi" },
 };
 
 static QuoteSourceList multiple_quote_sources =



Summary of changes:
 ...sh.GnuCash.general.finance-quote.gschema.xml.in |  7 ++-
 gnucash/gtkbuilder/dialog-preferences.glade        | 30 ++++++++-
 libgnucash/app-utils/gnc-quotes.cpp                | 73 +++++++++++-----------
 libgnucash/engine/gnc-commodity.cpp                |  1 +
 4 files changed, 70 insertions(+), 41 deletions(-)



More information about the gnucash-changes mailing list