Repository navigation
Expand file tree
/
Copy pathindexifyObjects.py
More file actions
120 lines (99 loc) · 2.77 KB
/
Copy pathindexifyObjects.py
File metadata and controls
120 lines (99 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from operator import attrgetter
from bisect import insort
from dataclasses import dataclass
class Bookmark:
"""A single PDF bookmark.
Attributes
__________
title: string
The text to be displayed in the index
page: int
The page number of the bookmark
level: int
The nesting level of the bookmark
"""
def __init__(self, title, page, level):
"""Creates a bookmark.
Parameters
__________
title: string
The text to be displayed in the index
page: int
The page number of the bookmark
level: int
The nesting level of the bookmark
"""
self.title = title
self.page = page
self.level = level
def __repr__(self):
return "Bookmark({title},{page},{level})".format(**self.__dict__)
def __str__(self):
"""Returns the bookmark formatted as:
---------------------------
BookmarkBegin
BookmarkTitle: {title}
BookmarkLevel: {level}
BookmarkPageNumber: {page}
--------------------------
"""
return """BookmarkBegin
BookmarkTitle: {title}
BookmarkLevel: {level}
BookmarkPageNumber: {page}""".format(**self.__dict__)
def __lt__(self, other):
if self.page < other.page:
return True
elif self.page >= other.page:
return False
else:
raise TypeError("Non-comparable")
class Index(list):
"""An index of a PDF file - a list of Bookmarks
The index is sorted according to page
"""
def __init__(self, *args):
"""Put all input into a list,
and then sort it by page number
"""
# todo: check values
# todo: add option to enter values directly
list.__init__(self, *args)
self.sort(key=attrgetter("page"))
def __repr__(self):
return f"Index({list.__repr__(self)})"
def __str__(self):
x = ""
for b in self:
x += str(b) + "\n"
return x
def add_entry(self, entry):
insort(self, entry)
@dataclass
class Info:
name: str
full: str
lines: list
index_lines: list
index_start: int
index_end: int
def update(self, index):
"""
Write the Index object 'index' to self
"""
# print(repr(index))
# print(index)
# print(str(index))
self.index_lines = str(index).split("\n")
# print(self.index_lines)
temp = (
self.lines[: self.index_start]
+ self.index_lines[:-1] # to avoid blank line
+ self.lines[self.index_end :]
)
self.lines = temp
self.full = "\n".join(self.lines)
def write(self):
"""
Generate an info file
"""