-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_pdf.py
382 lines (339 loc) · 12.7 KB
/
generate_pdf.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import os
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import List
import pdfkit
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
class RepoToPDF:
"""
This class convert repository to pdf where this must be already cloned
"""
# Standard Stuffs
gitignore_stuff = [
".pdf",
"__init__.py",
"__pycache__",
".gitignore",
".git/",
"LICENSE",
".github/",
"README.md",
"requirements.txt",
"pyproject.toml", "poetry.lock",
"Pipfile", "Pipfile.lock",
".idea/",
"env-sample",
".flake8",
".yml", ".xml", ".txt",
"setup.cfg", "Procfile"
"pytest.ini",
]
# Django Stuffs
gitignore_stuff += [
"manage.py",
"migrations/",
"static/",
"test_files/",
"test/",
"tests/",
"testes/",
"test.py",
"tests.py",
"wsgi.py",
"asgi.py",
"settings.py",
]
def __init__(self, directory, style="colorful"):
self.directory = Path(str(directory))
self.style = style
self.name_repository = str(directory).strip(os.sep).split(os.sep)[-1]
self.ignored_files = RepoToPDF.gitignore_stuff + self.ignore_files()
self.files_to_convert = self.select_files(self.directory)
self.tree = self.create_tree(self.directory)
def ignore_files(self) -> List[str]:
"""
Scrap the gitignore file it it exists and prepare all the stuffs that must be ignored.
:return: List of strings, that depicts the files/folders scrapped from .gitignore file.
"""
gitignore_content = []
if self.directory.joinpath(".gitignore").exists():
with self.directory.joinpath(".gitignore").open() as fp:
content = fp.readlines()
for i in content:
line = i.strip()
if len(line) > 0 and "#" not in line:
gitignore_content.append(line.strip("*"))
else:
print(
"Since the .gitignore file doesn't exists, all the files will be considered"
)
return gitignore_content
def add_ignore_files(self, new_ignore_stuff: List[str]) -> None:
self.ignored_files += new_ignore_stuff
def select_files(self, directory: Path, files_selected=[]) -> List[Path]:
"""
Discover which files are allowed, then return a list with them.
:param directory: Path
:param files_selected: List
:return: List of paths of selected/filtered files
"""
for i in sorted(directory.iterdir()):
if not self.must_ignore(i) and i.is_dir():
self.select_files(i)
elif not self.must_ignore(i) and i.is_file():
# print(f'Valid file -> {i}')
files_selected.append(i)
return files_selected
def must_ignore(self, filename: Path) -> bool:
"""
Validates if a file must be ignored or not from certain logic.
:param filename: Path
Ex.: PosixPath('/home/alfonso/PycharmProjects/xml-to-json/manage.py')
:return: True or False
"""
if filename.is_dir() and f"{filename.name}/" in self.ignored_files:
return True
elif (
filename.is_file()
and filename.suffix in self.ignored_files
or filename.name in self.ignored_files
):
return True
def create_tree(self, startpath: Path) -> str:
"""
Create a tree of the folder
:param startpath: Path of the folder
:return: string that depicts the tree of the folder
"""
# With a criteria (skip hidden files)
def is_not_hidden(path):
# filtered_path = False
ignore = ("__pycache__/", "__init__.py")
if (
path.name.startswith(".")
or path.name.startswith("__")
or path.name in ignore
):
return False
return True
paths = DisplayablePath.make_tree(
Path(startpath), criteria=is_not_hidden
)
tree = ""
for path in paths:
tree += path.displayable() + "\n"
return tree
def generate_html(self, file: Path, header_path=False) -> str:
"""
Generate a html file according to a file received in a directory
:param file: Path. File from the directory input
:param header_path: boolean. Determines if the file will
have the directory in the first line
:return: str. A string that depicts the html file
"""
path_file = ""
if header_path:
for i, parent in enumerate(file.parts):
if parent == self.name_repository:
path_file = "/".join(file.parts[i:])
break
try:
with open(file, 'r', encoding='cp850', errors='replace') as fp:
content = fp.read()
except UnicodeEncodeError as e:
print(f"File {file.stem} unreadable\n{e}")
content = 'Unreadable content'
except Exception as e:
print(e)
content = e
content = f'{path_file} \n{content}' if content else f'{file} \nEmpty File'
lexer = get_lexer_by_name("python", stripall=True)
formatter = HtmlFormatter(
full=True,
style=self.style,
filename=file.name,
linenos=True,
linenostart=0,
)
return highlight(content, lexer, formatter)
def multiple_html_to_pdf(self, temp_folder: Path) -> None:
"""
converts multiple html files to a single pdf
args: path to directory containing html files
:param temp_folder: Path . Temp folder where the html are.
:return: Nothing. This function create a pdf in the self.directory path
"""
empty_html = "<html><head></head><body></body></html>"
for file in sorted(temp_folder.iterdir(), key=lambda d: len(d.stem)):
if file.suffix == ".html":
# append html files
with open(file, 'r', encoding='cp850', errors='replace') as f:
html = f.read()
empty_html = empty_html.replace(
"</body></html>", html + "</body></html>"
)
# save merged html
with TemporaryDirectory() as temp:
merged_file = Path(temp) / "00.html"
with merged_file.open(mode="w+", encoding='cp850', errors='replace') as merged:
merged.write(empty_html)
try:
# config = pdfkit.configuration(wkhtmltopdf='C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')
# Creates pdf
pdfkit.from_file(
str(merged_file),
output_path=f"{str(self.directory)}/{self.name_repository}.pdf",
options={
"encoding": "UTF-8",
"margin-top": "0.15in",
"margin-right": "0.45in",
"margin-bottom": "0.15in",
"margin-left": "0.45in",
},
)
print(
f"File {str(self.directory)}{os.sep}{self.name_repository}.pdf generated with success!"
)
except OSError:
print("Please install wkhtmltopdf"
" https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf")
def generate_pdf(self):
# Creates temp folder
with TemporaryDirectory() as temp_dir:
temp_dir = Path(temp_dir)
for file in self.files_to_convert:
html_content = self.generate_html(file, header_path=True)
name_html_file = "_".join(
file.parts[file.parts.index(self.name_repository):]
).replace(file.name, f"{file.stem}.html")
html_file = temp_dir / name_html_file
# Creates html temp files
with html_file.open(mode="w+", encoding='cp850', errors='replace') as file:
file.write(html_content)
tree_file = temp_dir / "tree.txt"
with tree_file.open(mode="w+", encoding='cp850', errors='replace') as tree:
tree.write(self.tree)
html_content = self.generate_html(tree_file)
html_file = temp_dir / "00_tree.html"
with html_file.open(mode="w+", encoding='cp850', errors='replace') as html:
html.write(html_content)
# Create pdf from html temp files in temp_dir
self.multiple_html_to_pdf(temp_dir)
class DisplayablePath(object):
display_filename_prefix_middle = "├──"
display_filename_prefix_last = "└──"
display_parent_prefix_middle = " "
display_parent_prefix_last = "│ "
def __init__(self, path, parent_path, is_last):
self.path = Path(str(path))
self.parent = parent_path
self.is_last = is_last
if self.parent:
self.depth = self.parent.depth + 1
else:
self.depth = 0
@property
def displayname(self):
if self.path.is_dir():
return self.path.name + "/"
return self.path.name
@classmethod
def make_tree(cls, root, parent=None, is_last=False, criteria=None):
root = Path(str(root))
criteria = criteria or cls._default_criteria
displayable_root = cls(root, parent, is_last)
yield displayable_root
children = sorted(
list(path for path in root.iterdir() if criteria(path)),
key=lambda s: str(s).lower(),
)
count = 1
for path in children:
is_last = count == len(children)
if path.is_dir():
yield from cls.make_tree(
path,
parent=displayable_root,
is_last=is_last,
criteria=criteria,
)
else:
yield cls(path, displayable_root, is_last)
count += 1
@classmethod
def _default_criteria(cls, path):
return True
@property
def displayname(self):
if self.path.is_dir():
return self.path.name + "/"
return self.path.name
def displayable(self):
if self.parent is None:
return self.displayname
_filename_prefix = (
self.display_filename_prefix_last
if self.is_last
else self.display_filename_prefix_middle
)
parts = ["{!s} {!s}".format(_filename_prefix, self.displayname)]
parent = self.parent
while parent and parent.parent is not None:
parts.append(
self.display_parent_prefix_middle
if parent.is_last
else self.display_parent_prefix_last
)
parent = parent.parent
return "".join(reversed(parts))
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
prog="Repository to PDF",
description="This program convert a repository to PDF",
epilog="Enjoy the program! :)",
)
parser.add_argument("dir", type=Path, help="Path of the repository.")
parser.add_argument(
"--style",
default="colorful",
type=str,
help="Style for the PDF. Choose a style -> https://pygments.org/styles/",
)
parser.add_argument(
"--ignore",
type=str,
nargs='?',
help="Add files and/or folders for ignoring.",
)
# Creating a Namespace object
args = parser.parse_args()
try:
if args.style:
choices = [
"default", "bw",
"sas", "xcode",
"autumn", "borland",
"arduino", "igor",
"lovelace", "pastie",
"rainbow_dash",
"emacs", "tango",
"colorful",
"rrt", "algol",
"abap",
]
if not args.style in choices:
raise NameError
if Path(args.dir).exists():
repo = RepoToPDF(args.dir, style=args.style)
repo.generate_pdf()
else:
raise FileNotFoundError
except FileNotFoundError:
print("Invalid Folder !")
except NameError:
print(
f"Invalid Style, please choose one of nexts :\n{', '.join(choices)}"
)