Skip to content

Commit

Permalink
fix black, flake8 and mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jerome-poisson committed Dec 10, 2024
1 parent 9695aa9 commit 8c3b9d8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 44 deletions.
26 changes: 14 additions & 12 deletions apps/publish/enqueue/enqueue_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import io
import json
import logging
from typing import cast
import elasticapm
import content_api

Expand Down Expand Up @@ -515,34 +514,37 @@ def queue_transmission(
if formatted_docs is None:
# Either caching is not available for this formatter, or it's the first time that we format
# this document.
formatted_docs = formatter.format(
formatted_docs = []
format_ret = formatter.format(
self.filter_document(doc) if embed_package_items else filtered_document.copy(),
subscriber,
subscriber_codes.get(subscriber[ID_FIELD]),
)

for idx, publish_data in enumerate(formatted_docs):
for publish_data in format_ret:
if not isinstance(publish_data, dict):
pub_seq_num, formatted_doc = publish_data
formatted_docs[idx] = {
"published_seq_num": pub_seq_num,
"formatted_item": formatted_doc,
}
formatted_docs.append(
{
"published_seq_num": pub_seq_num,
"formatted_item": formatted_doc,
}
)
else:
assert (
"published_seq_num" in publish_data and "formatted_item" in publish_data
), "missing keys in publish_data"

formatted_docs = cast(list[dict], formatted_docs)
formatted_docs.append(publish_data)

if formatter.use_cache:
cache[(formatter, doc[ID_FIELD])] = formatted_docs
else:
# We have cached documents, we still need to update ``published_seq_num``.
resource_service = superdesk.get_resource_service("subscribers")
published_seq_num = resource_service.generate_sequence_number(subscriber)
for formatted_doc in formatted_docs:
formatted_doc["publish_seq_num"] = published_seq_num
published_seq_num = resource_service.generate_sequence_number(subscriber) # type: ignore

for doc in formatted_docs:
doc["publish_seq_num"] = published_seq_num

for publish_queue_item in formatted_docs:
publish_queue_item["item_id"] = doc["item_id"]
Expand Down
1 change: 1 addition & 0 deletions superdesk/publish/formatters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def get_formatter(format_type: str, article: dict) -> Formatter | None:
for formatter_instance in get_all_formatters():
if formatter_instance.can_format(format_type, article):
return formatter_instance
return None


def get_all_formatters() -> List[Formatter]:
Expand Down
5 changes: 3 additions & 2 deletions superdesk/publish/formatters/email_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# at https://www.sourcefabric.org/superdesk/license

import json
from typing import Any
import superdesk

from superdesk.publish.formatters import Formatter
Expand Down Expand Up @@ -55,11 +56,11 @@ def _inject_dateline(self, formatted_article):
formatted_article["body_html"] = sd_etree.to_string(body_html_elem)

# TODO-ASYNC: Support async formatters in publishing code
async def format(self, article: dict, subscriber: dict, codes: list | None = None) -> list[tuple[int, str] | dict]:
async def format(self, article: dict, subscriber: dict, codes: list | None = None) -> list[tuple[int, str] | dict]: # type: ignore
formatted_article = deepcopy(article)
remove_all_embeds(formatted_article)
pub_seq_num = superdesk.get_resource_service("subscribers").generate_sequence_number(subscriber)
doc = {}
doc: dict[str, Any] = {}
try:
if formatted_article.get(FORMAT) == FORMATS.HTML:
if formatted_article.get("dateline", {}).get("text"):
Expand Down
48 changes: 24 additions & 24 deletions superdesk/publish/formatters/idml_formatter/package/styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ def _add_paragraph_style_group_headings(self):
},
)
properties = etree.SubElement(paragraphstyle, "Properties")
etree.SubElement(properties, "BasedOn", attrib={"type": "string"}).text = (
"ParagraphStyle/$ID/NormalParagraphStyle"
)
etree.SubElement(
properties, "BasedOn", attrib={"type": "string"}
).text = "ParagraphStyle/$ID/NormalParagraphStyle"
etree.SubElement(properties, "PreviewColor", attrib={"type": "enumeration"}).text = "Nothing"
etree.SubElement(properties, "ParagraphShadingColor", attrib={"type": "object"}).text = "Color/Black"
etree.SubElement(properties, "Leading", attrib={"type": "unit"}).text = "40"
Expand All @@ -456,9 +456,9 @@ def _add_paragraph_style_group_headings(self):
},
)
properties = etree.SubElement(paragraphstyle, "Properties")
etree.SubElement(properties, "BasedOn", attrib={"type": "string"}).text = (
"ParagraphStyle/$ID/NormalParagraphStyle"
)
etree.SubElement(
properties, "BasedOn", attrib={"type": "string"}
).text = "ParagraphStyle/$ID/NormalParagraphStyle"
etree.SubElement(properties, "PreviewColor", attrib={"type": "enumeration"}).text = "Nothing"
etree.SubElement(properties, "ParagraphShadingColor", attrib={"type": "object"}).text = "Color/Black"
etree.SubElement(properties, "Leading", attrib={"type": "unit"}).text = "30"
Expand All @@ -476,9 +476,9 @@ def _add_paragraph_style_group_headings(self):
},
)
properties = etree.SubElement(paragraphstyle, "Properties")
etree.SubElement(properties, "BasedOn", attrib={"type": "string"}).text = (
"ParagraphStyle/$ID/NormalParagraphStyle"
)
etree.SubElement(
properties, "BasedOn", attrib={"type": "string"}
).text = "ParagraphStyle/$ID/NormalParagraphStyle"
etree.SubElement(properties, "PreviewColor", attrib={"type": "enumeration"}).text = "Nothing"
etree.SubElement(properties, "ParagraphShadingColor", attrib={"type": "object"}).text = "Color/Black"
etree.SubElement(properties, "Leading", attrib={"type": "unit"}).text = "20"
Expand All @@ -496,9 +496,9 @@ def _add_paragraph_style_group_headings(self):
},
)
properties = etree.SubElement(paragraphstyle, "Properties")
etree.SubElement(properties, "BasedOn", attrib={"type": "string"}).text = (
"ParagraphStyle/$ID/NormalParagraphStyle"
)
etree.SubElement(
properties, "BasedOn", attrib={"type": "string"}
).text = "ParagraphStyle/$ID/NormalParagraphStyle"
etree.SubElement(properties, "PreviewColor", attrib={"type": "enumeration"}).text = "Nothing"
etree.SubElement(properties, "ParagraphShadingColor", attrib={"type": "object"}).text = "Color/Black"
etree.SubElement(properties, "Leading", attrib={"type": "unit"}).text = "14"
Expand All @@ -516,9 +516,9 @@ def _add_paragraph_style_group_headings(self):
},
)
properties = etree.SubElement(paragraphstyle, "Properties")
etree.SubElement(properties, "BasedOn", attrib={"type": "string"}).text = (
"ParagraphStyle/$ID/NormalParagraphStyle"
)
etree.SubElement(
properties, "BasedOn", attrib={"type": "string"}
).text = "ParagraphStyle/$ID/NormalParagraphStyle"
etree.SubElement(properties, "PreviewColor", attrib={"type": "enumeration"}).text = "Nothing"
etree.SubElement(properties, "ParagraphShadingColor", attrib={"type": "object"}).text = "Color/Black"
etree.SubElement(properties, "Leading", attrib={"type": "unit"}).text = "11"
Expand All @@ -536,9 +536,9 @@ def _add_paragraph_style_group_headings(self):
},
)
properties = etree.SubElement(paragraphstyle, "Properties")
etree.SubElement(properties, "BasedOn", attrib={"type": "string"}).text = (
"ParagraphStyle/$ID/NormalParagraphStyle"
)
etree.SubElement(
properties, "BasedOn", attrib={"type": "string"}
).text = "ParagraphStyle/$ID/NormalParagraphStyle"
etree.SubElement(properties, "PreviewColor", attrib={"type": "enumeration"}).text = "Nothing"
etree.SubElement(properties, "ParagraphShadingColor", attrib={"type": "object"}).text = "Color/Black"
etree.SubElement(properties, "Leading", attrib={"type": "unit"}).text = "9"
Expand Down Expand Up @@ -674,9 +674,9 @@ def _add_paragraph_style_group_custom(self):
},
)
properties = etree.SubElement(paragraphstyle, "Properties")
etree.SubElement(properties, "BasedOn", attrib={"type": "string"}).text = (
"ParagraphStyle/$ID/NormalParagraphStyle"
)
etree.SubElement(
properties, "BasedOn", attrib={"type": "string"}
).text = "ParagraphStyle/$ID/NormalParagraphStyle"
etree.SubElement(properties, "PreviewColor", attrib={"type": "enumeration"}).text = "Nothing"
etree.SubElement(properties, "ParagraphShadingColor", attrib={"type": "object"}).text = "Color/Black"
etree.SubElement(properties, "Leading", attrib={"type": "unit"}).text = "48"
Expand All @@ -695,9 +695,9 @@ def _add_paragraph_style_group_custom(self):
},
)
properties = etree.SubElement(paragraphstyle, "Properties")
etree.SubElement(properties, "BasedOn", attrib={"type": "string"}).text = (
"ParagraphStyle/$ID/NormalParagraphStyle"
)
etree.SubElement(
properties, "BasedOn", attrib={"type": "string"}
).text = "ParagraphStyle/$ID/NormalParagraphStyle"
etree.SubElement(properties, "PreviewColor", attrib={"type": "enumeration"}).text = "Nothing"
etree.SubElement(properties, "ParagraphShadingColor", attrib={"type": "object"}).text = "Color/Black"
etree.SubElement(properties, "Leading", attrib={"type": "unit"}).text = "20"
7 changes: 1 addition & 6 deletions superdesk/publish/formatters/ninjs_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,7 @@ def __init__(self):
self.can_export = True
self.internal_renditions = get_app_config("NINJS_COMMON_RENDITIONS", []) + ["original"]

def format(
self,
article: dict,
subscriber: dict,
codes: list|None=None
) -> list[tuple[int, str]|dict]:
def format(self, article: dict, subscriber: dict, codes: list | None = None) -> list[tuple[int, str] | dict]:
try:
pub_seq_num = superdesk.get_resource_service("subscribers").generate_sequence_number(subscriber)

Expand Down

0 comments on commit 8c3b9d8

Please sign in to comment.