Clean up output comments.

This commit is contained in:
Chris Berkhout 2021-06-11 23:02:59 +02:00
parent 03386e9093
commit 7d473c7dba
4 changed files with 75 additions and 39 deletions

View file

@ -1,3 +1,25 @@
"""
Beancount output
Supports the `Beancount <https://beancount.github.io/>`_ 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 <https://beancount.github.io/docs/beancount_language_syntax.html#commodities-currencies>`_
* `Prices <https://beancount.github.io/docs/beancount_language_syntax.html#prices>`_
* `Fetching Prices in Beancount <https://beancount.github.io/docs/fetching_prices_in_beancount.html>`_
Classes:
Beancount
"""
from pricehist.format import Format from pricehist.format import Format
from .baseoutput import BaseOutput from .baseoutput import BaseOutput
@ -7,19 +29,9 @@ class Beancount(BaseOutput):
def format(self, series, source=None, fmt=Format()): def format(self, series, source=None, fmt=Format()):
output = "" output = ""
for price in series.prices: 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) date = fmt.format_date(price.date)
base = fmt.base or series.base base = fmt.base or series.base
quote = fmt.quote or series.quote quote = fmt.quote or series.quote
quote_amount = fmt.format_quote_amount(quote, price.amount) quote_amount = fmt.format_quote_amount(quote, price.amount)
output += f"{date} price {base} {quote_amount}\n" output += f"{date} price {base} {quote_amount}\n"
return output 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

View file

@ -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 <https://docs.python.org/3/library/csv.html>`_ 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 csv
import io import io
@ -7,7 +24,7 @@ from .baseoutput import BaseOutput
class CSV(BaseOutput): class CSV(BaseOutput):
def format(self, series, source=None, fmt=Format()): def format(self, series, source, fmt=Format()):
output = io.StringIO() output = io.StringIO()
writer = csv.writer( writer = csv.writer(
output, output,

View file

@ -51,10 +51,10 @@ from .baseoutput import BaseOutput
class GnuCashSQL(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 base = fmt.base or series.base
quote = fmt.quote or series.quote quote = fmt.quote or series.quote
src = f"pricehist:{source.id()}" src = source.id()
self._warn_about_backslashes( self._warn_about_backslashes(
{ {
@ -150,10 +150,10 @@ class GnuCashSQL(BaseOutput):
def _sql_str(self, s): def _sql_str(self, s):
# Documentation regarding SQL string literals: # Documentation regarding SQL string literals:
# - https://www.sqlite.org/lang_expr.html#literal_values_constants_ # * https://www.sqlite.org/lang_expr.html#literal_values_constants_
# - https://mariadb.com/kb/en/string-literals/ # * https://mariadb.com/kb/en/string-literals/
# - https://dev.mysql.com/doc/refman/8.0/en/string-literals.html # * https://dev.mysql.com/doc/refman/8.0/en/string-literals.html
# - https://www.postgresql.org/docs/devel/sql-syntax-lexical.html # * https://www.postgresql.org/docs/devel/sql-syntax-lexical.html
escaped = s.replace("'", "''") escaped = s.replace("'", "''")
quoted = f"'{escaped}'" quoted = f"'{escaped}'"
return quoted return quoted

View file

@ -1,3 +1,30 @@
"""
Ledger output
Supports both `Ledger <https://www.ledger-cli.org/>`_ and
`hledger <https://hledger.org/>`_ 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 <https://www.ledger-cli.org/3.0/doc/ledger3.html#Commodities-and-Currencies>`_
* `Commoditized Amounts <https://www.ledger-cli.org/3.0/doc/ledger3.html#Commoditized-Amounts>`_
Relevant sections of the hledger manual:
* `Declaring market prices <https://hledger.org/hledger.html#declaring-market-prices>`_:
* `Declaring commodities <https://hledger.org/hledger.html#declaring-commodities`_:
Classes:
Ledger
"""
from pricehist.format import Format from pricehist.format import Format
from .baseoutput import BaseOutput from .baseoutput import BaseOutput
@ -11,26 +38,6 @@ class Ledger(BaseOutput):
base = fmt.base or series.base base = fmt.base or series.base
quote = fmt.quote or series.quote quote = fmt.quote or series.quote
quote_amount = fmt.format_quote_amount(quote, price.amount) quote_amount = fmt.format_quote_amount(quote, price.amount)
output += f"P {date} {fmt.time} {base} {quote_amount}\n" timesep = " " if fmt.time else ""
output += f"P {date}{timesep}{fmt.time} {base} {quote_amount}\n"
return output return output
# https://www.ledger-cli.org/3.0/doc/ledger3.html#Commodities-and-Currencies
# > 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