Format Python code with black

This commit is contained in:
kpj
2022-02-27 15:26:41 +01:00
parent 13885968e3
commit fc695896dd
136 changed files with 6142 additions and 3811 deletions

View File

@@ -4,8 +4,7 @@ import dateutil.parser
from django.conf import settings
from documents.parsers import DocumentParser, ParseError, \
make_thumbnail_from_pdf
from documents.parsers import DocumentParser, ParseError, make_thumbnail_from_pdf
from tika import parser
@@ -21,15 +20,18 @@ class TikaDocumentParser(DocumentParser):
self.archive_path = self.convert_to_pdf(document_path, file_name)
return make_thumbnail_from_pdf(
self.archive_path, self.tempdir, self.logging_group)
self.archive_path, self.tempdir, self.logging_group
)
def extract_metadata(self, document_path, mime_type):
tika_server = settings.PAPERLESS_TIKA_ENDPOINT
try:
parsed = parser.from_file(document_path, tika_server)
except Exception as e:
self.log("warning", f"Error while fetching document metadata for "
f"{document_path}: {e}")
self.log(
"warning",
f"Error while fetching document metadata for " f"{document_path}: {e}",
)
return []
return [
@@ -37,8 +39,9 @@ class TikaDocumentParser(DocumentParser):
"namespace": "",
"prefix": "",
"key": key,
"value": parsed['metadata'][key]
} for key in parsed['metadata']
"value": parsed["metadata"][key],
}
for key in parsed["metadata"]
]
def parse(self, document_path, mime_type, file_name=None):
@@ -56,11 +59,12 @@ class TikaDocumentParser(DocumentParser):
self.text = parsed["content"].strip()
try:
self.date = dateutil.parser.isoparse(
parsed["metadata"]["Creation-Date"])
self.date = dateutil.parser.isoparse(parsed["metadata"]["Creation-Date"])
except Exception as e:
self.log("warning", f"Unable to extract date for document "
f"{document_path}: {e}")
self.log(
"warning",
f"Unable to extract date for document " f"{document_path}: {e}",
)
self.archive_path = self.convert_to_pdf(document_path, file_name)
@@ -70,17 +74,19 @@ class TikaDocumentParser(DocumentParser):
url = gotenberg_server + "/forms/libreoffice/convert"
self.log("info", f"Converting {document_path} to PDF as {pdf_path}")
files = {"files": (file_name or os.path.basename(document_path),
open(document_path, "rb"))}
files = {
"files": (
file_name or os.path.basename(document_path),
open(document_path, "rb"),
)
}
headers = {}
try:
response = requests.post(url, files=files, headers=headers)
response.raise_for_status() # ensure we notice bad responses
except Exception as err:
raise ParseError(
f"Error while converting document to PDF: {err}"
)
raise ParseError(f"Error while converting document to PDF: {err}")
file = open(pdf_path, "wb")
file.write(response.content)