Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pdftocgen/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def main():
print(usage_s, file=sys.stderr)
sys.exit(2)

recipe_file: TextIO = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8', errors='ignore')
recipe_file: TextIO = None
readable: bool = False
vpos: bool = False
out: TextIO = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='ignore')
Expand Down Expand Up @@ -129,6 +129,8 @@ def main():
print(usage_s, file=sys.stderr)
sys.exit(1)

if not recipe_file:
recipe_file = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8', errors='ignore')
path_in: str = args[0]
# done parsing arguments

Expand Down
4 changes: 3 additions & 1 deletion pdftocio/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def main():
print(usage_s, file=sys.stderr)
sys.exit(2)

toc_file: TextIO = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8', errors='ignore')
toc_file: TextIO = None
print_toc: bool = False
readable: bool = False
out: Optional[str] = None
Expand Down Expand Up @@ -131,6 +131,8 @@ def main():
print(usage_s, file=sys.stderr)
sys.exit(1)

if not toc_file:
toc_file = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8', errors='ignore')
path_in: str = args[0]
# done parsing arguments

Expand Down
13 changes: 9 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 @@ -23,8 +23,13 @@ def parse_entry(entry: List) -> ToCEntry:
*entry[indent + 2:] # vpos
)
return toc_entry
except ValueError as e:
print(f"Unable to parse toc entry {entry} from line {nLine};",
f"Couldn't convert '{entry[indent + 1]}' to a page number.",
file=sys.stderr)
raise e
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 +40,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)))
Loading