diff --git a/exporter.py b/exporter.py index cd9229c..d9fc8e3 100644 --- a/exporter.py +++ b/exporter.py @@ -22,14 +22,17 @@ _IMPL_PATH = ( def _load_impl(): + module_name = __name__ if __name__ != "__main__" else "_xtb_wealthfolio_exporter_impl" script_dir = _IMPL_PATH.parent if str(script_dir) not in sys.path: sys.path.insert(0, str(script_dir)) - spec = importlib.util.spec_from_file_location(__name__, _IMPL_PATH) + spec = importlib.util.spec_from_file_location(module_name, _IMPL_PATH) if spec is None or spec.loader is None: raise ImportError(f"Could not load XTB Wealthfolio implementation at {_IMPL_PATH}") module = importlib.util.module_from_spec(spec) - sys.modules[__name__] = module + sys.modules[module_name] = module + if __name__ != "__main__": + sys.modules[__name__] = module spec.loader.exec_module(module) return module diff --git a/skills/xtb-portfolio-review/scripts/main.py b/skills/xtb-portfolio-review/scripts/main.py index 6d08a49..f9bef3c 100644 --- a/skills/xtb-portfolio-review/scripts/main.py +++ b/skills/xtb-portfolio-review/scripts/main.py @@ -2,6 +2,7 @@ import argparse import contextlib import io import re +import warnings from dataclasses import dataclass, field from datetime import datetime, date, timedelta from html import escape @@ -37,6 +38,18 @@ COST_FALLBACK_NOTES = { ), } + +def suppress_openpyxl_default_style_warning() -> None: + warnings.filterwarnings( + "ignore", + message=r"Workbook contains no default style, apply openpyxl's default", + category=UserWarning, + module=r"openpyxl\.styles\.stylesheet", + ) + + +suppress_openpyxl_default_style_warning() + # XTB "Type" values that represent trading activity (not cash transfers). TRADE_TYPE_RE = re.compile( r"stock\s*(purchase|sale|buy|sell)|\bopen\b|\bclose\b", diff --git a/skills/xtb-wealthfolio-export/scripts/main.py b/skills/xtb-wealthfolio-export/scripts/main.py index ba4699a..0ddd29e 100644 --- a/skills/xtb-wealthfolio-export/scripts/main.py +++ b/skills/xtb-wealthfolio-export/scripts/main.py @@ -2,6 +2,7 @@ import argparse import contextlib import io import re +import warnings from dataclasses import dataclass, field from datetime import datetime, date, timedelta from html import escape @@ -37,6 +38,18 @@ COST_FALLBACK_NOTES = { ), } + +def suppress_openpyxl_default_style_warning() -> None: + warnings.filterwarnings( + "ignore", + message=r"Workbook contains no default style, apply openpyxl's default", + category=UserWarning, + module=r"openpyxl\.styles\.stylesheet", + ) + + +suppress_openpyxl_default_style_warning() + # XTB "Type" values that represent trading activity (not cash transfers). TRADE_TYPE_RE = re.compile( r"stock\s*(purchase|sale|buy|sell)|\bopen\b|\bclose\b", diff --git a/test_portfolio.py b/test_portfolio.py index 895b706..e92b7b2 100644 --- a/test_portfolio.py +++ b/test_portfolio.py @@ -1,3 +1,5 @@ +import warnings + import pandas as pd import pytest @@ -44,6 +46,27 @@ def cash_row(type_, instrument, amount, comment="", time="2026-01-15 10:00:00"): # Generic helpers # --------------------------------------------------------------------------- class TestHelpers: + def test_suppresses_openpyxl_default_style_warning_only(self): + with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter("default") + main.suppress_openpyxl_default_style_warning() + warnings.warn_explicit( + "Workbook contains no default style, apply openpyxl's default", + UserWarning, + "stylesheet.py", + 237, + module="openpyxl.styles.stylesheet", + ) + warnings.warn_explicit( + "Another workbook warning", + UserWarning, + "stylesheet.py", + 237, + module="openpyxl.styles.stylesheet", + ) + + assert [str(w.message) for w in caught] == ["Another workbook warning"] + def test_clean_columns_normalizes(self): df = pd.DataFrame(columns=["Open Price", "Profit/Loss", " Ticker "]) out = clean_columns(df)