Skip to content

Commit 1b3ba3d

Browse files
committed
minor bug fixes.
1 parent f2d6e23 commit 1b3ba3d

File tree

4 files changed

+66
-14
lines changed

4 files changed

+66
-14
lines changed

extraction_methods/plugins/geometry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class GeometryInput(Input):
4040
] = Field(
4141
description="Type of geometry to be produced.",
4242
)
43-
coordinates: list[str | float] = Field(
43+
coordinates: list[Any] = Field(
4444
description="list of coordinates to convert to geometry. Ordering is respected.",
4545
)
4646
output_key: str = Field(

extraction_methods/plugins/lambda.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
__contact__ = "[email protected]"
1313

1414

15-
import ast
1615
import logging
1716
from typing import Any
1817

@@ -86,7 +85,7 @@ def run(self, body: dict[str, Any]) -> dict[str, Any]:
8685

8786
output_body = body.copy()
8887

89-
function = ast.literal_eval(self.input.function)
88+
function = eval(self.input.function) # nosec B307
9089

9190
result = function(*self.input.args, **self.input.kwargs)
9291

extraction_methods/plugins/regex_rename.py

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# Python imports
1616
import logging
1717
import re
18+
from collections.abc import KeysView
1819
from typing import Any
1920

2021
from pydantic import Field
@@ -46,6 +47,10 @@ class RegexRenameInput(Input):
4647
regex_swaps: list[RegexOutputKey] = Field(
4748
description="Regex and output key combinations.",
4849
)
50+
nest_delimiter: str = Field(
51+
default="",
52+
description="delimiter for nested term.",
53+
)
4954

5055

5156
class RegexRenameExtract(ExtractionMethod):
@@ -76,13 +81,59 @@ class RegexRenameExtract(ExtractionMethod):
7681

7782
input_class = RegexRenameInput
7883

84+
def matching_keys(self, keys: KeysView[str], key_regex: str) -> list[str]:
85+
"""
86+
Find all keys that match regex
87+
88+
:param keys: dictionary keys to test
89+
:type keys: KeysView
90+
:param key_regex: regex to test against
91+
:type key_regex: str
92+
93+
:return: matching keys
94+
:rtype: list
95+
"""
96+
97+
regex = re.compile(key_regex)
98+
99+
return list(filter(regex.match, keys))
100+
101+
def rename(
102+
self, body: dict[str, Any], key_parts: list[str], output_key: str
103+
) -> dict[str, Any]:
104+
"""
105+
Rename terms
106+
107+
:param body: current body
108+
:type body: dict
109+
:param key_parts: key parts seperated by delimiter
110+
:type key_parts: list
111+
112+
:return: dict
113+
:rtype: update body
114+
"""
115+
116+
for key in self.matching_keys(body.keys(), key_parts[0]):
117+
118+
if len(key_parts) > 1:
119+
body[key] = self.rename(body[key], key_parts[1:], output_key)
120+
121+
else:
122+
body[output_key] = body[key]
123+
del body[key]
124+
125+
return body
126+
79127
@update_input
80128
def run(self, body: dict[str, Any]) -> dict[str, Any]:
81129

82-
output = body.copy()
83-
for key in body.keys():
84-
for swap in self.input.regex_swaps:
85-
if re.fullmatch(rf"{swap.regex}", key):
86-
output[swap.output_key] = body[key]
130+
for swap in self.input.regex_swaps:
131+
nest = (
132+
swap.regex.split(self.input.delimiter)
133+
if self.input.delimiter
134+
else [swap.regex]
135+
)
136+
137+
body = self.rename(body, nest, swap.output_key)
87138

88-
return output
139+
return body

extraction_methods/plugins/xml.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
__contact__ = "[email protected]"
1313

1414
import logging
15+
import os.path
1516

1617
# Python imports
1718
from collections import defaultdict
1819

1920
# Package imports
2021
from typing import Any
2122

22-
from lxml.etree import ElementTree # nosec B410
23+
from lxml import etree # nosec B410
2324
from pydantic import Field
2425

2526
from extraction_methods.core.extraction_method import ExtractionMethod
@@ -114,13 +115,14 @@ def run(self, body: dict[str, Any]) -> dict[str, Any]:
114115

115116
# Extract the keys
116117
try:
117-
if isinstance(self.input.input_term, str):
118-
xml_file = ElementTree.parse(self.input.input_term)
118+
119+
if os.path.isfile(self.input.input_term):
120+
xml_file = etree.parse(self.input.input_term)
119121

120122
else:
121-
xml_file = ElementTree.XML(self.input.input_term)
123+
xml_file = etree.XML(self.input.input_term.encode("ascii", "ignore"))
122124

123-
except (ElementTree.ParseError, FileNotFoundError, TypeError):
125+
except (etree.ParseError, FileNotFoundError, TypeError):
124126
return body
125127

126128
output: dict[str, list[str]] = defaultdict(list)

0 commit comments

Comments
 (0)