diff --git a/src/pricehist/outputs/beancount.py b/src/pricehist/outputs/beancount.py index 5be3711..366cc86 100644 --- a/src/pricehist/outputs/beancount.py +++ b/src/pricehist/outputs/beancount.py @@ -1,3 +1,25 @@ +""" +Beancount output + +Supports the `Beancount `_ plain text accounting +format. + +The default output should be valid for Beancount. Customizing it via formatting +options may generate invalid output, so users should keep the requirements of +the Beancount format in mind. + +Relevant sections of the Beancount documentation: + +* `Commodities / Currencies `_ +* `Prices `_ +* `Fetching Prices in Beancount `_ + +Classes: + + Beancount + +""" + from pricehist.format import Format from .baseoutput import BaseOutput @@ -7,19 +29,9 @@ class Beancount(BaseOutput): def format(self, series, source=None, fmt=Format()): output = "" for price in series.prices: - # TODO warn if fmt settings make an invalid number (not . for decimal) - # TODO warn if fmt settings make an invalid quote (not right/rightspace) date = fmt.format_date(price.date) base = fmt.base or series.base quote = fmt.quote or series.quote quote_amount = fmt.format_quote_amount(quote, price.amount) output += f"{date} price {base} {quote_amount}\n" return output - - -# NOTE: Beancount always has commodity to the right. It seems to be possible to -# skip the space, according to https://plaintextaccounting.org/quickref/#h.n4b87oz9ku6t - -# https://beancount.github.io/docs/fetching_prices_in_beancount.html -# https://beancount.github.io/docs/beancount_language_syntax.html#commodities-currencies -# https://beancount.github.io/docs/beancount_language_syntax.html#comments diff --git a/src/pricehist/outputs/csv.py b/src/pricehist/outputs/csv.py index 14fc101..0a63f4a 100644 --- a/src/pricehist/outputs/csv.py +++ b/src/pricehist/outputs/csv.py @@ -1,3 +1,20 @@ +""" +CSV output + +Comma Separated Values output is easily processed with other command-line tools +or imported into a spreadsheet or database. + +Python's `csv `_ module is used to +produce Excel-style CSV output, except with UNIX-style line endings. The field +delimiter can be set with a formatting option, and date, number and base/quote +formatting options will be respected. + +Classes: + + CSV + +""" + import csv import io @@ -7,7 +24,7 @@ from .baseoutput import BaseOutput class CSV(BaseOutput): - def format(self, series, source=None, fmt=Format()): + def format(self, series, source, fmt=Format()): output = io.StringIO() writer = csv.writer( output, diff --git a/src/pricehist/outputs/gnucashsql.py b/src/pricehist/outputs/gnucashsql.py index abffb20..0701908 100644 --- a/src/pricehist/outputs/gnucashsql.py +++ b/src/pricehist/outputs/gnucashsql.py @@ -51,10 +51,10 @@ from .baseoutput import BaseOutput class GnuCashSQL(BaseOutput): - def format(self, series, source=None, fmt=Format()): + def format(self, series, source, fmt=Format()): base = fmt.base or series.base quote = fmt.quote or series.quote - src = f"pricehist:{source.id()}" + src = source.id() self._warn_about_backslashes( { @@ -150,10 +150,10 @@ class GnuCashSQL(BaseOutput): def _sql_str(self, s): # Documentation regarding SQL string literals: - # - https://www.sqlite.org/lang_expr.html#literal_values_constants_ - # - https://mariadb.com/kb/en/string-literals/ - # - https://dev.mysql.com/doc/refman/8.0/en/string-literals.html - # - https://www.postgresql.org/docs/devel/sql-syntax-lexical.html + # * https://www.sqlite.org/lang_expr.html#literal_values_constants_ + # * https://mariadb.com/kb/en/string-literals/ + # * https://dev.mysql.com/doc/refman/8.0/en/string-literals.html + # * https://www.postgresql.org/docs/devel/sql-syntax-lexical.html escaped = s.replace("'", "''") quoted = f"'{escaped}'" return quoted diff --git a/src/pricehist/outputs/ledger.py b/src/pricehist/outputs/ledger.py index 054c998..6682677 100644 --- a/src/pricehist/outputs/ledger.py +++ b/src/pricehist/outputs/ledger.py @@ -1,3 +1,30 @@ +""" +Ledger output + +Supports both `Ledger `_ and +`hledger `_ plain text accounting formats. + +By default the output should be valid for Ledger, but can be customized for +hledger or other variants via formatting options. Invalid variants are +possible, so the user should be familiar with the requirements of the target +format. + +Relevant sections of the Ledger manual: + +* `Commodities and Currencies `_ +* `Commoditized Amounts `_ + +Relevant sections of the hledger manual: + +* `Declaring market prices `_: +* `Declaring commodities The commodity may be any non-numeric string that does not contain a - # > period, comma, forward slash or at-sign. It may appear before or after - # > the amount, although it is assumed that symbols appearing before the - # > amount refer to currencies, while non-joined symbols appearing after the - # > amount refer to commodities. - - # https://www.ledger-cli.org/3.0/doc/ledger3.html#Commoditized-Amounts - # > A commoditized amount is an integer amount which has an associated - # > commodity. This commodity can appear before or after the amount, and may - # > or may not be separated from it by a space. Most characters are allowed - # > in a commodity name, except for the following: - # > - Any kind of white-space - # > - Numerical digits - # > - Punctuation: .,;:?! - # > - Mathematical and logical operators: -+*/^&|= - # > - Bracketing characters: <>[](){} - # > - The at symbol: @ - # > And yet, any of these may appear in a commodity name if it is - # > surrounded by double quotes