Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 18 additions & 60 deletions packtools/sps/models/accessibility_data.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,6 @@
import re


class XMLAccessibilityData:

def __init__(self, xmltree):
self.xmltree = xmltree

@property
def transcripts(self):
if not hasattr(self, '_transcripts') or not self._transcripts:
self._transcripts = {}
for item in self.xmltree.xpath(".//sec[@sec-type='transcript']"):
transcript = Transcript(item)
self._transcripts[transcript.transcript_id] = transcript.data
return self._transcripts

@property
def misplaced_alt_texts(self):
"""
Detecta elementos <alt-text> que estão fora dos elementos permitidos.
Segundo SPS 1.9/1.10, <alt-text> só pode estar dentro de:
<graphic>, <inline-graphic>, <media>, <inline-media>
"""
valid_parents = ("graphic", "inline-graphic", "media", "inline-media")
misplaced = []

# Procura TODOS os <alt-text> no documento
for alt_text in self.xmltree.xpath(".//alt-text"):
parent = alt_text.getparent()
if parent is not None and parent.tag not in valid_parents:
# <alt-text> está no lugar errado!
misplaced.append({
"tag": "alt-text",
"parent_tag": parent.tag,
"parent_id": parent.get("id"),
"alt_text": alt_text.text,
"alt_text_length": len(alt_text.text or ""),
"expected_location": valid_parents,
"current_location": parent.tag,
})

return misplaced

@property
def data(self):
# CORREÇÃO: XPath simplificado para evitar duplicatas
# Captura apenas os elementos base que podem conter dados de acessibilidade
xpaths = [
".//graphic",
".//inline-graphic",
".//media",
".//inline-media",
]
for item in self.xmltree.xpath("|".join(xpaths)):
model = AccessibilityData(item)
model.transcript_data = self.transcripts.get(model.xref_sec_rid)
yield model.data

# NOVO: Dados dos elementos mal posicionados
for misplaced in self.misplaced_alt_texts:
yield misplaced
import warnings as _warnings


class AccessibilityData:
Expand Down Expand Up @@ -211,3 +152,20 @@ def data(self):
"speakers": self.speaker_data,
"tag": self.node.tag,
}


def __getattr__(name):
_moved = {
"XMLAccessibilityData": "packtools.sps.validation.models.accessibility_data",
}
if name in _moved:
import importlib
_warnings.warn(
f"{name} has moved to {_moved[name]}. "
f"Importing from packtools.sps.models.accessibility_data is deprecated.",
DeprecationWarning,
stacklevel=2,
)
mod = importlib.import_module(_moved[name])
return getattr(mod, name)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
29 changes: 17 additions & 12 deletions packtools/sps/models/app_group.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings as _warnings

from packtools.sps.models.article_and_subarticles import Fulltext
from packtools.sps.models.label_and_caption import LabelAndCaption
from packtools.sps.models.media import Media
Expand Down Expand Up @@ -31,15 +33,18 @@ def data(self):
}


class XmlAppGroup:
def __init__(self, xml_tree):
self.xml_tree = xml_tree

@property
def data(self):
for node in self.xml_tree.xpath(".|.//sub-article"):
full_text = Fulltext(node)

for app_node in node.xpath("./back//app"):
app_data = App(app_node).data
yield {**app_data, **full_text.attribs_parent_prefixed}
def __getattr__(name):
_moved = {
"XmlAppGroup": "packtools.sps.validation.models.app_group",
}
if name in _moved:
import importlib
_warnings.warn(
f"{name} has moved to {_moved[name]}. "
f"Importing from packtools.sps.models.app_group is deprecated.",
DeprecationWarning,
stacklevel=2,
)
mod = importlib.import_module(_moved[name])
return getattr(mod, name)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
47 changes: 16 additions & 31 deletions packtools/sps/models/article_data_availability.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,21 @@
from packtools.sps.models.article_and_subarticles import Fulltext


class DataAvailability:
def __init__(self, xmltree):
self.xmltree = xmltree
import warnings as _warnings

@property
def items(self):
xpath_query = './body//sec[@sec-type="data-availability"] | ./body//fn[@fn-type="data-availability"] | ./back//sec[@sec-type="data-availability"] | ./back//fn[@fn-type="data-availability"]'

for node in self.xmltree.xpath(
". | ./sub-article[@article-type='translation']"
):
fulltext = Fulltext(node)
items = fulltext.node.xpath(xpath_query)
if len(items) == 0:
yield fulltext.attribs_parent_prefixed
else:
for item in items:
data = {
"tag": item.tag,
"specific_use": item.get("specific-use"),
"label": item.findtext("label"),
"text": " ".join(item.xpath("./p//text()")),
}
data.update(fulltext.attribs_parent_prefixed)
yield data

@property
def items_by_lang(self):
d = {}
for item in self.items:
d.setdefault(item["parent_lang"], [])
d[item["parent_lang"]].append(item)
return d
def __getattr__(name):
_moved = {
"DataAvailability": "packtools.sps.validation.models.article_data_availability",
}
if name in _moved:
import importlib
_warnings.warn(
f"{name} has moved to {_moved[name]}. "
f"Importing from packtools.sps.models.article_data_availability is deprecated.",
DeprecationWarning,
stacklevel=2,
)
mod = importlib.import_module(_moved[name])
return getattr(mod, name)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
30 changes: 18 additions & 12 deletions packtools/sps/models/author_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,21 @@ def corresp_data(self):
yield data


class XMLAuthorNotes:
def __init__(self, xml_tree):
self.xml_tree = xml_tree

def article_author_notes(self):
group = FulltextAuthorNotes(self.xml_tree.find("."))
return {"corresp_data": list(group.corresp_data), "fns": list(group.items)}

def sub_article_author_notes(self):
for sub_article in self.xml_tree.xpath(".//sub-article"):
group = FulltextAuthorNotes(sub_article)
yield {"corresp_data": list(group.corresp_data), "fns": list(group.items)}
import warnings as _warnings


def __getattr__(name):
_moved = {
"XMLAuthorNotes": "packtools.sps.validation.models.author_notes",
}
if name in _moved:
import importlib
_warnings.warn(
f"{name} has moved to {_moved[name]}. "
f"Importing from packtools.sps.models.author_notes is deprecated.",
DeprecationWarning,
stacklevel=2,
)
mod = importlib.import_module(_moved[name])
return getattr(mod, name)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
41 changes: 18 additions & 23 deletions packtools/sps/models/fig.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,26 +196,21 @@ def figs(self):
yield put_parent_context(data, self.lang, self.article_type, self.parent, self.parent_id)


class ArticleFigs:
def __init__(self, xml_tree):
self.xml_tree = xml_tree

@property
def get_all_figs(self):
yield from self.get_article_figs
yield from self.get_sub_article_translation_figs
yield from self.get_sub_article_non_translation_figs

@property
def get_article_figs(self):
yield from Figs(self.xml_tree.find(".")).figs()

@property
def get_sub_article_translation_figs(self):
for node in self.xml_tree.xpath(".//sub-article[@article-type='translation']"):
yield from Figs(node).figs()

@property
def get_sub_article_non_translation_figs(self):
for node in self.xml_tree.xpath(".//sub-article[@article-type!='translation']"):
yield from Figs(node).figs()
import warnings as _warnings


def __getattr__(name):
_moved = {
"ArticleFigs": "packtools.sps.validation.models.fig",
}
if name in _moved:
import importlib
_warnings.warn(
f"{name} has moved to {_moved[name]}. "
f"Importing from packtools.sps.models.fig is deprecated.",
DeprecationWarning,
stacklevel=2,
)
mod = importlib.import_module(_moved[name])
return getattr(mod, name)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
37 changes: 18 additions & 19 deletions packtools/sps/models/fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,21 @@ def __init__(self, node):
super().__init__(node, "fn-group", FnGroup)


class XMLFns:
def __init__(self, xml_tree):
self.xml_tree = xml_tree

def article_fn_groups_notes(self):
yield from FulltextFnGroups(self.xml_tree.find(".")).items

def sub_article_fn_groups_notes(self):
for sub_article in self.xml_tree.xpath(".//sub-article"):
yield from FulltextFnGroups(sub_article).items

@property
def fn_edited_by(self):
for item in self.xml_tree.xpath(". | .//sub-article"):
fulltext = Fulltext(item)
for node in fulltext.node.xpath("*//fn[@fn-type='edited-by']"):
data = fulltext.attribs_parent_prefixed
data.update(Fn(node).data)
yield data
import warnings as _warnings


def __getattr__(name):
_moved = {
"XMLFns": "packtools.sps.validation.models.fn",
}
if name in _moved:
import importlib
_warnings.warn(
f"{name} has moved to {_moved[name]}. "
f"Importing from packtools.sps.models.fn is deprecated.",
DeprecationWarning,
stacklevel=2,
)
mod = importlib.import_module(_moved[name])
return getattr(mod, name)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
96 changes: 18 additions & 78 deletions packtools/sps/models/formula.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,81 +182,21 @@ def data(self):
}


class ArticleFormulas:
"""
Represents an article with its associated formulas, grouped by language.

Parameters:
xml_tree (xml.etree.ElementTree.ElementTree): The parsed XML document representing the article.
"""

def __init__(self, xml_tree):
"""
Initializes an ArticleFormulas object.

**Parameters:**
xml_tree (xml.etree.ElementTree.ElementTree): The parsed XML document representing the article.
"""
self.xml_tree = xml_tree

@property
def disp_formula_items(self):
"""
Generator that yields formulas with their respective parent context.

Yields:
dict: A dictionary containing the formula data along with its parent context,
including language and article type details.
"""
for node, lang, article_type, parent, parent_id in get_parent_context(self.xml_tree):
for item in node.xpath(".//disp-formula"):
formula = Formula(item)
data = formula.data
parent_data = put_parent_context(data, lang, article_type, parent, parent_id)
yield parent_data

@property
def inline_formula_items(self):
"""
Generator that yields inline formulas with their respective parent context.

Yields:
dict: A dictionary containing the formula data along with its parent context,
including language and article type details.
"""
for node, lang, article_type, parent, parent_id in get_parent_context(self.xml_tree):
for item in node.xpath(".//inline-formula"):
formula = Formula(item)
data = formula.data
parent_data = put_parent_context(data, lang, article_type, parent, parent_id)
yield parent_data

@property
def disp_formula_items_by_lang(self):
"""
Returns a dictionary of formulas grouped by language.

Returns:
dict: A dictionary where keys are language codes (str) and values are
dictionaries with formula data within that language context.
"""
langs = {}
for item in self.disp_formula_items:
lang = item.get("parent_lang")
langs[lang] = item
return langs

@property
def inline_formula_items_by_lang(self):
"""
Returns a dictionary of inline formulas grouped by language.

Returns:
dict: A dictionary where keys are language codes (str) and values are
dictionaries with formula data within that language context.
"""
langs = {}
for item in self.inline_formula_items:
lang = item.get("parent_lang")
langs[lang] = item
return langs
import warnings as _warnings


def __getattr__(name):
_moved = {
"ArticleFormulas": "packtools.sps.validation.models.formula",
}
if name in _moved:
import importlib
_warnings.warn(
f"{name} has moved to {_moved[name]}. "
f"Importing from packtools.sps.models.formula is deprecated.",
DeprecationWarning,
stacklevel=2,
)
mod = importlib.import_module(_moved[name])
return getattr(mod, name)
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
Loading