Skip to content

Commit

Permalink
Makes use of default namespace declarations
Browse files Browse the repository at this point in the history
and applies now canonical expression of empty/null prefixes where
the expressions aren't explicitly tested.
  • Loading branch information
funkyfuture committed Jan 22, 2025
1 parent f370278 commit 278f055
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 30 deletions.
2 changes: 1 addition & 1 deletion _delb/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1883,7 +1883,7 @@ def attributes(self) -> TagAttributes:
.. _Clark notation: http://www.jclark.com/xml/xmlns.htm
>>> DefaultStringOptions.namespaces = {None: "http://namespace"}
>>> DefaultStringOptions.namespaces = {"": "http://namespace"}
>>> node = new_tag_node(
... "node",
... namespace="http://namespace",
Expand Down
2 changes: 1 addition & 1 deletion delb/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class ResolveCopyOf(Transformation):
def transform(self):
for node in self.root.css_select(
"*[copyOf]", namespaces={None: "http://www.tei-c.org/ns/1.0"}
"*[copyOf]", namespaces={"": "http://www.tei-c.org/ns/1.0"}
):
source_id = node["copyOf"]
source_node = self.origin_document.xpath(
Expand Down
4 changes: 1 addition & 3 deletions tests/test_css_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
def test_css_select_or(files_path):
document = Document(files_path / "tei_stevenson_treasure_island.xml")

result = document.css_select(
"titleStmt title, titleStmt author", namespaces={None: TEI_NAMESPACE}
)
result = document.css_select("titleStmt title, titleStmt author")

assert len(result) == 2
assert {x.local_name for x in result} == {"author", "title"}
Expand Down
8 changes: 2 additions & 6 deletions tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,13 @@ def test_set_root():
def test_xpath(files_path):
document = Document(files_path / "marx_manifestws_1848.TEI-P5.xml")

for i, page_break in enumerate(
document.xpath("//pb", namespaces={None: TEI_NAMESPACE})
):
for i, page_break in enumerate(document.xpath("//pb")):
assert isinstance(page_break, TagNode)
assert page_break.universal_name == "{http://www.tei-c.org/ns/1.0}pb"

assert i == 22

for j, page_break in enumerate(
document.xpath('//pb[@n="I"]', namespaces={None: TEI_NAMESPACE})
):
for j, page_break in enumerate(document.xpath('//pb[@n="I"]')):
assert isinstance(page_break, TagNode)
assert page_break.universal_name == "{http://www.tei-c.org/ns/1.0}pb"
assert page_break.attributes["n"] == "I"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_reduce_whitespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_nodes_in_between():
def test_samples_from_Manifest_der_kommunistischen_Partei(files_path): # noqa: N802
document = Document(files_path / "marx_manifestws_1848.TEI-P5.xml")
document.reduce_whitespace()
imprints = document.xpath("//docImprint", namespaces={None: TEI_NAMESPACE})
imprints = document.xpath("//docImprint")

assert imprints[0].full_text == "Veröffentlicht im Februar 1848."
assert "46, Liverpool Street" in imprints[1].full_text
Expand Down
10 changes: 4 additions & 6 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_empty_below_default_namespace():
root = Document("<root xmlns='http://fo.org/'/>").root
root.append_children(new_tag_node(local_name="node", namespace=None))
# just to be explicit here:
DefaultStringOptions.namespace = {None: "http://fo.org/"}
DefaultStringOptions.namespace = {"": "http://fo.org/"}
assert str(root) == '<ns0:root xmlns:ns0="http://fo.org/"><node/></ns0:root>'


Expand Down Expand Up @@ -248,7 +248,7 @@ def test_significant_whitespace_is_saved(result_file, format_options, out):
({}, new_tag_node, ("n", {"x": '"'}), """<n x="&quot;"/>"""),
({}, new_tag_node, ("n", {"x": '"&"'}), r"""<n x="&quot;&amp;&quot;"/>"""),
(
{None: "http://namespace"},
{"": "http://namespace"},
new_tag_node,
("node", {("http://namespace", "bar"): "x"}),
"""<node xmlns:ns0="http://namespace" ns0:bar="x"/>""",
Expand Down Expand Up @@ -396,7 +396,7 @@ def test_text_document_production(
)
def test_text_width(files_path, indentation, text_width, out):
document = Document(files_path / "marx_manifestws_1848.TEI-P5.xml")
paragraph = document.xpath("//p", namespaces={None: TEI_NAMESPACE})[14]
paragraph = document.xpath("//p")[14]
format_options = FormatOptions(indentation=indentation, width=text_width)
DefaultStringOptions.format_options = format_options
assert paragraph.serialize(format_options=format_options) == str(paragraph) == out
Expand Down Expand Up @@ -432,9 +432,7 @@ def test_text_with_milestone_tag(files_path, text_width, expected):
files_path / "tei_stevenson_treasure_island.xml",
parser_options=ParserOptions(reduce_whitespace=True),
)
paragraph = document.css_select(
"fileDesc sourceDesc p", namespaces={None: TEI_NAMESPACE}
).first
paragraph = document.css_select("fileDesc sourceDesc p").first
assert "\t" not in paragraph.full_text

DefaultStringOptions.format_options = FormatOptions(
Expand Down
8 changes: 2 additions & 6 deletions tests/test_subclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,14 @@ def __class_test__(root: TagNode, config: types.SimpleNamespace) -> bool:

@property
def title(self) -> str:
return self.css_select(
'titleStmt title[type="main"]', namespaces={None: TEI_NAMESPACE}
).first.full_text
return self.css_select('titleStmt title[type="main"]').first.full_text


class DTABfDocument(TEIDocument):
@staticmethod
def __class_test__(root, config) -> bool:
if TEIDocument.__class_test__(root, config):
for node in root.css_select(
"publicationStmt publisher[xml|id]", namespaces={None: TEI_NAMESPACE}
):
for node in root.css_select("publicationStmt publisher[xml|id]"):
if node.id == "DTACorpusPublisher":
return True
return False
Expand Down
4 changes: 1 addition & 3 deletions tests/test_tag_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,7 @@ def test_getitem():

def test_id_property(files_path):
document = Document(files_path / "marx_manifestws_1848.TEI-P5.xml")
publisher = document.css_select(
"publicationStmt publisher", namespaces={None: TEI_NAMESPACE}
).first
publisher = document.css_select("publicationStmt publisher").first

assert publisher.id == "DTACorpusPublisher"

Expand Down
4 changes: 1 addition & 3 deletions tests/test_xpath_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ def test_as_sequences(queries_sample):

def test_document_order(files_path):
document = Document(files_path / "marx_manifestws_1848.TEI-P5.xml")
ordered_nodes = document.xpath(
"//pb", namespaces={None: TEI_NAMESPACE}
).in_document_order()
ordered_nodes = document.xpath("//pb").in_document_order()
assert all(n.local_name == "pb" for n in ordered_nodes)
assert_nodes_are_in_document_order(*ordered_nodes)

Expand Down

0 comments on commit 278f055

Please sign in to comment.