mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
[price-quotes] Convert long quote parsing lambda to a regular function.
To begin separating price parsing from inserting in the price db.
This commit is contained in:
parent
dd8316714b
commit
37dfab7f31
@ -95,6 +95,7 @@ private:
|
|||||||
void query_fq (void);
|
void query_fq (void);
|
||||||
void parse_quotes (void);
|
void parse_quotes (void);
|
||||||
std::string comm_vec_to_json_string(void) const;
|
std::string comm_vec_to_json_string(void) const;
|
||||||
|
GNCPrice* parse_one_quote(const bpt::ptree&, gnc_commodity*);
|
||||||
|
|
||||||
std::unique_ptr<GncQuoteSource> m_quotesource;
|
std::unique_ptr<GncQuoteSource> m_quotesource;
|
||||||
CommVec m_comm_vec;
|
CommVec m_comm_vec;
|
||||||
@ -351,45 +352,19 @@ GncQuotesImpl::query_fq (void)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
GNCPrice*
|
||||||
GncQuotesImpl::parse_quotes (void)
|
GncQuotesImpl::parse_one_quote(const bpt::ptree& pt, gnc_commodity* comm)
|
||||||
{
|
{
|
||||||
bpt::ptree pt;
|
|
||||||
std::istringstream ss {m_fq_answer};
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
bpt::read_json (ss, pt);
|
|
||||||
}
|
|
||||||
catch (bpt::json_parser_error &e) {
|
|
||||||
m_cmd_result = -1;
|
|
||||||
m_error_msg = m_error_msg +
|
|
||||||
_("Failed to parse result returned by Finance::Quote.") + "\n" +
|
|
||||||
_("Error message:") + "\n" +
|
|
||||||
e.what() + "\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
catch (...) {
|
|
||||||
m_cmd_result = -1;
|
|
||||||
m_error_msg = m_error_msg +
|
|
||||||
_("Failed to parse result returned by Finance::Quote.") + "\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto pricedb = gnc_pricedb_get_db (m_book);
|
|
||||||
std::for_each(m_comm_vec.begin(), m_comm_vec.end(),
|
|
||||||
[this, &pt, &pricedb] (gnc_commodity *comm)
|
|
||||||
{
|
|
||||||
auto comm_ns = gnc_commodity_get_namespace (comm);
|
auto comm_ns = gnc_commodity_get_namespace (comm);
|
||||||
auto comm_mnemonic = gnc_commodity_get_mnemonic (comm);
|
auto comm_mnemonic = gnc_commodity_get_mnemonic (comm);
|
||||||
if (gnc_commodity_equiv(comm, m_dflt_curr) ||
|
if (gnc_commodity_equiv(comm, m_dflt_curr) ||
|
||||||
(!comm_mnemonic || (strcmp (comm_mnemonic, "XXX") == 0)))
|
(!comm_mnemonic || (strcmp (comm_mnemonic, "XXX") == 0)))
|
||||||
return;
|
return nullptr;
|
||||||
if (pt.find (comm_mnemonic) == pt.not_found())
|
if (pt.find (comm_mnemonic) == pt.not_found())
|
||||||
{
|
{
|
||||||
PINFO("Skipped %s:%s - Finance::Quote didn't return any data.",
|
PINFO("Skipped %s:%s - Finance::Quote didn't return any data.",
|
||||||
comm_ns, comm_mnemonic);
|
comm_ns, comm_mnemonic);
|
||||||
return;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string key = comm_mnemonic;
|
std::string key = comm_mnemonic;
|
||||||
@ -430,14 +405,14 @@ GncQuotesImpl::parse_quotes (void)
|
|||||||
PWARN("Skipped %s:%s - Finance::Quote returned fetch failure.\nReason %s",
|
PWARN("Skipped %s:%s - Finance::Quote returned fetch failure.\nReason %s",
|
||||||
comm_ns, comm_mnemonic,
|
comm_ns, comm_mnemonic,
|
||||||
(errmsg ? errmsg->c_str() : "unknown"));
|
(errmsg ? errmsg->c_str() : "unknown"));
|
||||||
return;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!price_str)
|
if (!price_str)
|
||||||
{
|
{
|
||||||
PWARN("Skipped %s:%s - Finance::Quote didn't return a valid price",
|
PWARN("Skipped %s:%s - Finance::Quote didn't return a valid price",
|
||||||
comm_ns, comm_mnemonic);
|
comm_ns, comm_mnemonic);
|
||||||
return;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
GncNumeric price;
|
GncNumeric price;
|
||||||
@ -449,7 +424,7 @@ GncQuotesImpl::parse_quotes (void)
|
|||||||
{
|
{
|
||||||
PWARN("Skipped %s:%s - failed to parse returned price '%s'",
|
PWARN("Skipped %s:%s - failed to parse returned price '%s'",
|
||||||
comm_ns, comm_mnemonic, price_str->c_str());
|
comm_ns, comm_mnemonic, price_str->c_str());
|
||||||
return;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inverted)
|
if (inverted)
|
||||||
@ -459,7 +434,7 @@ GncQuotesImpl::parse_quotes (void)
|
|||||||
{
|
{
|
||||||
PWARN("Skipped %s:%s - Finance::Quote didn't return a currency",
|
PWARN("Skipped %s:%s - Finance::Quote didn't return a currency",
|
||||||
comm_ns, comm_mnemonic);
|
comm_ns, comm_mnemonic);
|
||||||
return;
|
return nullptr;
|
||||||
}
|
}
|
||||||
boost::to_upper (*currency_str);
|
boost::to_upper (*currency_str);
|
||||||
auto commodity_table = gnc_commodity_table_get_table (m_book);
|
auto commodity_table = gnc_commodity_table_get_table (m_book);
|
||||||
@ -469,7 +444,7 @@ GncQuotesImpl::parse_quotes (void)
|
|||||||
{
|
{
|
||||||
PWARN("Skipped %s:%s - failed to parse returned currency '%s'",
|
PWARN("Skipped %s:%s - failed to parse returned currency '%s'",
|
||||||
comm_ns, comm_mnemonic, currency_str->c_str());
|
comm_ns, comm_mnemonic, currency_str->c_str());
|
||||||
return;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string iso_date_str = GncDate().format ("%Y-%m-%d");
|
std::string iso_date_str = GncDate().format ("%Y-%m-%d");
|
||||||
@ -513,11 +488,46 @@ GncQuotesImpl::parse_quotes (void)
|
|||||||
gnc_price_set_source (gnc_price, PRICE_SOURCE_FQ);
|
gnc_price_set_source (gnc_price, PRICE_SOURCE_FQ);
|
||||||
gnc_price_set_typestr (gnc_price, price_type.c_str());
|
gnc_price_set_typestr (gnc_price, price_type.c_str());
|
||||||
gnc_price_set_value (gnc_price, price);
|
gnc_price_set_value (gnc_price, price);
|
||||||
gnc_pricedb_add_price (pricedb, gnc_price);
|
|
||||||
gnc_price_commit_edit (gnc_price);
|
gnc_price_commit_edit (gnc_price);
|
||||||
gnc_price_unref (gnc_price);
|
return gnc_price;
|
||||||
});
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
GncQuotesImpl::parse_quotes (void)
|
||||||
|
{
|
||||||
|
bpt::ptree pt;
|
||||||
|
std::istringstream ss {m_fq_answer};
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
bpt::read_json (ss, pt);
|
||||||
|
}
|
||||||
|
catch (bpt::json_parser_error &e) {
|
||||||
|
m_cmd_result = -1;
|
||||||
|
m_error_msg = m_error_msg +
|
||||||
|
_("Failed to parse result returned by Finance::Quote.") + "\n" +
|
||||||
|
_("Error message:") + "\n" +
|
||||||
|
e.what() + "\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
catch (...) {
|
||||||
|
m_cmd_result = -1;
|
||||||
|
m_error_msg = m_error_msg +
|
||||||
|
_("Failed to parse result returned by Finance::Quote.") + "\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto pricedb{gnc_pricedb_get_db(m_book)};
|
||||||
|
for (auto comm : m_comm_vec)
|
||||||
|
{
|
||||||
|
auto price{parse_one_quote(pt, comm)};
|
||||||
|
if (!price)
|
||||||
|
continue;
|
||||||
|
gnc_price_begin_edit (price);
|
||||||
|
gnc_pricedb_add_price(pricedb, price);
|
||||||
|
gnc_price_commit_edit(price);
|
||||||
|
gnc_price_unref (price);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user