From c46caed9c3fad50533264e1d0b769dc0c4565bdc Mon Sep 17 00:00:00 2001 From: Mario Mauerer Date: Wed, 20 Dec 2023 11:20:44 +0100 Subject: [PATCH] un-hardcoded a datetime-formatted string --- config.py | 3 ++- profit_src/parsing.py | 17 +++++++++++++++-- profit_src/profit.py | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/config.py b/config.py index 47002e5..135975e 100644 --- a/config.py +++ b/config.py @@ -95,7 +95,8 @@ class ProfitConfig: # Delimiter used in all text- and csv-files throughout PROFIT DELIMITER = ";" - # Date-format used throughout PROFIT + # Date-format used throughout PROFIT. Make sure to retain the formatting (e.g., %d., %m., %Y) and only change the + # order of these substrings if desired (e.g., in the US, the format "%m.%d.%Y" might be more common). FORMAT_DATE = "%d.%m.%Y" # The length of tabs as measured in spaces. In interactive mode, PROFIT tries to keep the formatting of the diff --git a/profit_src/parsing.py b/profit_src/parsing.py index 6565965..6fa5503 100644 --- a/profit_src/parsing.py +++ b/profit_src/parsing.py @@ -111,8 +111,7 @@ class ParsingConfig: # Strings for asset transactions-headers: # These are used for accounts and investments: - # This dateformat should be the same as the one specified in config.py # Todo un-hardcode this. - STRING_DATE = "Date(DD.MM.YYYY)" + STRING_DATE = None # This one will be set up in the constructor, as it is format-specific. STRING_ACTION = "Action" STRING_AMOUNT = "Amount" STRING_BALANCE = "Balance" @@ -150,6 +149,20 @@ class ParsingConfig: COLUMN_WIDTHS_INVESTMENTS = [20, 12, 12, 12, 12, 12, 12, 1] # date, action, quantity, price, cost, payout, # balance, notes + def __init__(self, profit_config): + """Constructor, to set up some profit-config-specific topics""" + + # Convert the datetime-format-string from PROFIT to the appropriate name for the "Date" Column in the + # CSV files: + date_format = profit_config.FORMAT_DATE + format_mappings = {'%d': 'DD', '%m': 'MM', '%Y': 'YYYY'} + for key, value in format_mappings.items(): + if key not in date_format: + raise RuntimeError(f"Specifier '{key}' not found in the date format string of config.py.") + date_format = date_format.replace(key, value) + self.STRING_DATE = f"Date({date_format})" # (this will take precedent over the None-defined class attribute) + + class AccountFile: """Creates the structure/template for an account-file. diff --git a/profit_src/profit.py b/profit_src/profit.py index 3254ad8..627e08f 100644 --- a/profit_src/profit.py +++ b/profit_src/profit.py @@ -118,7 +118,7 @@ def main(config): storage = MarketDataMain(storage_path, config.FORMAT_DATE, analyzer) # Initialize the file parser config: - parsing_config = parsing.ParsingConfig() + parsing_config = parsing.ParsingConfig(config) # Sanity checks: if len(config.ASSET_GROUPNAMES) != len(config.ASSET_GROUPS):