Skip to content

Commit

Permalink
add feature: include line number in toc parse error message
Browse files Browse the repository at this point in the history
  • Loading branch information
outis committed Oct 20, 2024
1 parent f312589 commit 969e408
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pdftocio/tocparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

from typing import IO, List
from fitzutils import ToCEntry
from itertools import takewhile
from itertools import count, takewhile


def parse_entry(entry: List) -> ToCEntry:
def parse_entry(entry: List, nLine: int) -> ToCEntry:
"""parse a row in csv to a toc entry"""

# a somewhat weird hack, csv reader would read spaces as an empty '', so we
Expand All @@ -24,7 +24,7 @@ def parse_entry(entry: List) -> ToCEntry:
)
return toc_entry
except IndexError as e:
print(f"Unable to parse toc entry {entry};",
print(f"Unable to parse toc entry {entry} from line {nLine};",
f"Need at least {indent + 2} parts but only have {len(entry)}.",
"Make sure the page number is present.",
file=sys.stderr)
Expand All @@ -35,4 +35,4 @@ def parse_toc(file: IO) -> List[ToCEntry]:
"""Parse a toc file to a list of toc entries"""
reader = csv.reader(file, lineterminator='\n',
delimiter=' ', quoting=csv.QUOTE_NONNUMERIC)
return list(map(parse_entry, reader))
return list(map(parse_entry, reader, count(1)))

0 comments on commit 969e408

Please sign in to comment.