Skip to content

Commit 98e6416

Browse files
committed
fix ruff check
1 parent deac1e1 commit 98e6416

File tree

11 files changed

+32
-39
lines changed

11 files changed

+32
-39
lines changed

src/rtflite/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""rtflite: A Python library for creating RTF documents."""
22

33
from .attributes import TableAttributes
4+
from .convert import LibreOfficeConverter
45
from .encode import RTFDocument
56
from .encoding import RTFEncodingEngine
67
from .input import (
@@ -17,7 +18,6 @@
1718
)
1819
from .pagination import PageBreakCalculator, RTFPagination
1920
from .strwidth import get_string_width
20-
from .convert import LibreOfficeConverter
2121

2222
__version__ = "0.0.1"
2323

src/rtflite/encoding/engine.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from typing import TYPE_CHECKING
44

5-
from .base import EncodingStrategy
65
from .unified_encoder import UnifiedRTFEncoder
76

87
if TYPE_CHECKING:

src/rtflite/encoding/renderer.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def render(self, document: Any, page: PageContext) -> list[str]:
9393
)
9494
):
9595
col_idx = 0
96-
# Try to find column index for legacy reasons, though likely unused if we handle spanning cleanly
96+
# Find col index for legacy reasons; likely unused with clean spanning.
9797
if document.rtf_body.page_by and isinstance(document.df, pl.DataFrame):
9898
try:
9999
col_idx = document.df.columns.index(document.rtf_body.page_by[0])
@@ -223,15 +223,15 @@ def _render_column_headers(self, document: Any, page: PageContext) -> list[str]:
223223

224224
# Adjust col_rel_width if needed (logic from PaginatedStrategy)
225225
# Since we are using page.data which is already sliced/processed,
226-
# we might need to adjust widths if they were originally defined for full table
226+
# Might need to adjust widths if defined for full table.
227227
if document.rtf_body.col_rel_width is not None:
228228
# If body has specific widths, try to map them.
229-
# For simplicity in this refactor, if we have header text now, we proceed.
229+
# If header text exists, proceed.
230230
pass
231231

232232
# Remove columns if necessary (page_by/subline_by)
233-
# Note: If we just populated from page.data, page.data already has columns removed!
234-
# So we only need to filter if text came from original document and has extra columns.
233+
# Note: page.data already has columns removed if populated from it.
234+
# Filter only if text is from original document with extra columns.
235235
if isinstance(header_copy.text, pl.DataFrame):
236236
cols_remove = set()
237237
if document.rtf_body.page_by:
@@ -299,7 +299,7 @@ def _render_body(self, document: Any, page: PageContext) -> list[str]:
299299

300300
if page_rel_row > prev_row:
301301
segment = page_df[prev_row:page_rel_row]
302-
# We must use internal _encode method of TableAttributes (attrs are already finalized)
302+
# Use internal _encode method (attributes already finalized).
303303
# Note: We need to ensure page_attrs is the TableAttributes object
304304
elements.extend(
305305
page_attrs._encode(segment, col_widths, row_offset=prev_row)

src/rtflite/encoding/unified_encoder.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
from __future__ import annotations
2+
13
from typing import Any
24

3-
import polars as pl
5+
from rtflite import RTFDocument
46

57
from ..attributes import BroadcastValue
6-
from .base import EncodingStrategy
78
from ..pagination.processor import PageFeatureProcessor
89
from ..pagination.strategies import PageContext, PaginationContext, StrategyRegistry
910
from ..pagination.strategies.defaults import DefaultPaginationStrategy
@@ -15,6 +16,7 @@
1516
from ..services.figure_service import RTFFigureService
1617
from ..services.grouping_service import grouping_service
1718
from ..type_guards import is_single_body
19+
from .base import EncodingStrategy
1820
from .renderer import PageRenderer
1921

2022

@@ -40,7 +42,7 @@ def encode(self, document: Any) -> str:
4042
# 1. Figure-only handling
4143
if document.df is None:
4244
# Reuse the logic from previous implementation, adapted slightly
43-
# For now, we can't easily unify figure-only documents into the same pipeline
45+
# Unifying figure-only documents into one pipeline is not straightforward.
4446
# without a "FigurePaginationStrategy".
4547
# So we defer to a helper method similar to the old one.
4648
return self._encode_figure_only(document)
@@ -112,7 +114,7 @@ def encode(self, document: Any) -> str:
112114

113115
# Handle case where no pages are generated (e.g. empty dataframe)
114116
if not pages:
115-
# Create a single empty page to ensure document structure (title, etc.) is rendered
117+
# Create empty page to ensure document structure (title, etc.) is rendered.
116118
# We use processed_df which might be empty but has correct schema
117119
pages = [
118120
PageContext(
@@ -127,7 +129,7 @@ def encode(self, document: Any) -> str:
127129
]
128130

129131
# Post-pagination fixup: Replace data with processed data (sliced correctly)
130-
# The strategy used original_df for calculation, but we want to render processed_df
132+
# Strategy used original_df, but we render processed_df.
131133
# (which has removed columns).
132134
if is_single_body(document.rtf_body):
133135
self._apply_data_post_processing(pages, processed_df, document.rtf_body)
@@ -205,13 +207,14 @@ def _apply_data_post_processing(self, pages, processed_df, rtf_body):
205207
p.data = restored.slice(curr, rows)
206208
curr += rows
207209

208-
def _encode_figure_only(self, document):
210+
def _encode_figure_only(self, document: RTFDocument):
209211
# (Legacy support for figure only)
210212
# For brevity, I will rely on the existing FigureService logic if possible
211213
# or just reproduce the simple loop.
212-
# ... (Implementation omitted for brevity, would match PaginatedStrategy._encode_figure_only_document_with_pagination)
214+
# Matches PaginatedStrategy._encode_figure_only_document_with_pagination.
213215
# Since the user wants a WORKING system, I must implement it.
214216
from copy import deepcopy
217+
215218
from ..figure import rtf_read_figure
216219

217220
if not document.rtf_figure or not document.rtf_figure.figures:
@@ -318,7 +321,7 @@ def _encode_figure_only(self, document):
318321
parts.append("\n\n}")
319322
return "".join([p for p in parts if p])
320323

321-
def _encode_multi_section(self, document: "RTFDocument") -> str:
324+
def _encode_multi_section(self, document: RTFDocument) -> str:
322325
"""Encode a multi-section document where sections are concatenated row by row.
323326
324327
Args:
@@ -329,7 +332,6 @@ def _encode_multi_section(self, document: "RTFDocument") -> str:
329332
"""
330333
from copy import deepcopy
331334

332-
from ..attributes import BroadcastValue
333335
from ..input import RTFColumnHeader
334336
from ..type_guards import (
335337
is_flat_header_list,

src/rtflite/pagination/core.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from collections.abc import Mapping, Sequence
2-
from typing import Any
32

43
import polars as pl
54
from pydantic import BaseModel, ConfigDict, Field

src/rtflite/pagination/processor.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ def process(self, document: Any, page: PageContext) -> PageContext:
1919

2020
def _should_show_element(self, element_location: str, page: PageContext) -> bool:
2121
"""Determine if an element should be shown on a specific page."""
22-
if element_location == "all":
23-
return True
24-
elif element_location == "first":
25-
return page.is_first_page
26-
elif element_location == "last":
27-
return page.is_last_page
28-
else:
29-
return False
22+
logic = {
23+
"all": True,
24+
"first": page.is_first_page,
25+
"last": page.is_last_page,
26+
}
27+
return logic.get(element_location, False)
3028

3129
def _apply_pagination_borders(self, document, page: PageContext) -> Any:
3230
"""Apply proper borders for paginated context following r2rtf design."""

src/rtflite/pagination/strategies/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abc import ABC, abstractmethod
2-
from typing import Any, Protocol
2+
from typing import Any
33

44
import polars as pl
55
from pydantic import BaseModel, ConfigDict, Field

src/rtflite/pagination/strategies/defaults.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import polars as pl
2-
31
from ..core import PageBreakCalculator, RTFPagination
42
from .base import PageContext, PaginationContext, PaginationStrategy
53

src/rtflite/pagination/strategies/grouping.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Any, Sequence
1+
from collections.abc import Sequence
2+
from typing import Any
23

34
import polars as pl
45

@@ -38,7 +39,7 @@ def paginate(self, context: PaginationContext) -> list[PageContext]:
3839
page_df = context.df.slice(start_row, end_row - start_row + 1)
3940

4041
is_first = page_num == 0
41-
# Logic for repeating headers: if pageby_header is True, or if it's the first page
42+
# Repeating headers: if pageby_header is True, or if it's the first page.
4243
needs_header = context.rtf_body.pageby_header or is_first
4344

4445
page_ctx = PageContext(
@@ -117,7 +118,7 @@ class SublineStrategy(PageByStrategy):
117118
"""Pagination strategy for subline_by (forces new pages and special headers)."""
118119

119120
def paginate(self, context: PaginationContext) -> list[PageContext]:
120-
# Subline strategy acts like page_by but uses subline_by columns and forces new_page=True
121+
# Subline strategy uses subline_by columns and forces new_page=True.
121122
subline_by = context.rtf_body.subline_by
122123

123124
# Initialize calculator

src/rtflite/pagination/strategies/registry.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
from typing import Type
2-
31
from .base import PaginationStrategy
42

53

64
class StrategyRegistry:
75
"""Registry for pagination strategies."""
86

9-
_strategies: dict[str, Type[PaginationStrategy]] = {}
7+
_strategies: dict[str, type[PaginationStrategy]] = {}
108

119
@classmethod
12-
def register(cls, name: str, strategy_cls: Type[PaginationStrategy]) -> None:
10+
def register(cls, name: str, strategy_cls: type[PaginationStrategy]) -> None:
1311
"""Register a new strategy."""
1412
cls._strategies[name] = strategy_cls
1513

1614
@classmethod
17-
def get(cls, name: str) -> Type[PaginationStrategy]:
15+
def get(cls, name: str) -> type[PaginationStrategy]:
1816
"""Get a strategy by name."""
1917
if name not in cls._strategies:
2018
raise ValueError(f"Strategy '{name}' not found in registry.")

0 commit comments

Comments
 (0)