-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
__init__.py
69 lines (53 loc) · 2.01 KB
/
__init__.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
import os
import ssl
import urllib.request
from typing import List
from PyPDF2.generic import DictionaryObject, IndirectObject
def get_pdf_from_url(url: str, name: str) -> bytes:
"""
Download a PDF from a URL and return its contents.
This function makes sure the PDF is not downloaded too often.
This function is a last resort for PDF files where we are uncertain if
we may add it for testing purposes to https://github.com/py-pdf/sample-files
:param str url: location of the PDF file
:param str name: unique name across all files
"""
if url.startswith("file://"):
with open(url[7:].replace("\\", "/"), "rb") as fp:
return fp.read()
cache_dir = os.path.join(os.path.dirname(__file__), "pdf_cache")
if not os.path.exists(cache_dir):
os.mkdir(cache_dir)
cache_path = os.path.join(cache_dir, name)
if not os.path.exists(cache_path):
ssl._create_default_https_context = ssl._create_unverified_context
with urllib.request.urlopen(url) as response, open(
cache_path, "wb"
) as out_file:
out_file.write(response.read())
with open(cache_path, "rb") as fp:
data = fp.read()
return data
def _strip_position(line: str) -> str:
"""
Remove the location information.
The message
WARNING PyPDF2._reader:_utils.py:364 Xref table not zero-indexed.
becomes
Xref table not zero-indexed.
"""
line = ".py".join(line.split(".py:")[1:])
line = " ".join(line.split(" ")[1:])
return line
def normalize_warnings(caplog_text: str) -> List[str]:
return [_strip_position(line) for line in caplog_text.strip().split("\n")]
class ReaderDummy:
def __init__(self, strict=False):
self.strict = strict
def get_object(self, indirect_reference):
class DummyObj:
def get_object(self):
return self
return DictionaryObject()
def get_reference(self, obj):
return IndirectObject(idnum=1, generation=1, pdf=self)