-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ProvideOutputFile contextmamager and linter satiyfication
- Loading branch information
1 parent
cf51699
commit beb4247
Showing
5 changed files
with
109 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/env python3 | ||
# -*- encoding: utf-8 -*- | ||
|
||
from contextlib import contextmanager | ||
import os | ||
from pypdf import PdfReader, PdfWriter | ||
import requests | ||
import sys | ||
import tempfile | ||
from . import config | ||
|
||
|
||
@contextmanager | ||
def ProvideOutputFile(output_file: str): | ||
# store the online PDF in a temporary file which will automatically be deleted when this contextmanager will be left | ||
with tempfile.TemporaryFile() as temp: | ||
|
||
# download online form and store it in a temp file | ||
try: | ||
r: requests.Response = requests.get(config.MILOG_FORM_URL, allow_redirects=True) | ||
except Exception as e: | ||
print(f"Exception when downloading PSE-Hiwi Formular -> {e}\n") | ||
sys.exit(os.EX_UNAVAILABLE) | ||
|
||
temp.write(r.content) | ||
temp.seek(0) # move cursor back to the beginning of the file | ||
|
||
pdf_reader = PdfReader(temp) | ||
pdf_writer = PdfWriter(clone_from=pdf_reader) # to copy everything else pdf_writer= PdfWriter();pdf_writer.append(pdf_reader) | ||
|
||
fields = pdf_reader.get_form_text_fields() # get the field names from the form in the pdf | ||
|
||
try: | ||
yield pdf_writer, fields | ||
finally: | ||
with open(output_file, 'wb') as output_file: # write file | ||
pdf_writer.write(output_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.