Skip to content

Commit ae5360f

Browse files
committed
now use jsondiff to compare json + bug correction in Calc Statement
1 parent c0a3529 commit ae5360f

File tree

6 files changed

+28
-26
lines changed

6 files changed

+28
-26
lines changed

lotemplate/CalcTemplate.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from . import Template
3131
from lotemplate.Statement.CalcSearchStatement import CalcTextStatement
3232
import pdb
33+
from jsondiff import diff
3334

3435
class CalcTemplate(Template):
3536
formats = {
@@ -82,14 +83,11 @@ def scan(self, **kwargs) -> dict[str: dict[str, Union[str, list[str]]]]:
8283
should_close = kwargs.get("should_close", False)
8384
texts = {}
8485
#(Pdb) self.doc.getSheets().getElementNames()
85-
#('maF1', 'Feuille2')
86-
#(Pdb) self.doc.getSheets().getByName('maF1')
8786
for sheet in self.doc.getSheets():
8887
texts = texts | CalcTextStatement.scan(sheet)
8988
tables=CalcTableStatement.scan(self.doc)
90-
#range.getElementNames()
9189
#texts = CalcTextStatement.scan_Document_text(self.doc)
92-
90+
#pdb.set_trace()
9391
return texts | tables
9492

9593

@@ -100,17 +98,15 @@ def search_error(self, json_vars: dict[str, dict[str, Union[str, list[str]]]]) -
10098
:param json_vars: the given json variables
10199
:return: None
102100
"""
103-
104-
if json_vars == self.variables:
101+
notdiff=diff(json_vars, self.variables)
102+
if not notdiff:
105103
return
106-
107-
json_missing = [key for key in set(self.variables) - set(json_vars)]
108-
if json_missing:
109-
raise errors.JsonComparaisonError(
104+
else:
105+
raise errors.JsonComparaisonError(
110106
'missing_required_variable',
111-
f"The variable {json_missing[0]!r}, present in the template, "
112-
f"isn't present in the json.",
113-
dict(variable=json_missing[0])
107+
f"The json are not the same here is the diff "
108+
f"{notdiff}",
109+
dict(variable=notdiff)
114110
)
115111

116112
# when parsing the template, we assume that all vars are of type text. But it can also be of type html.

lotemplate/Statement/CalcSearchStatement.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import pdb
66

77
class CalcTextStatement:
8-
text_regex_as_string = r'\$(\w+(\(((?:\\.|.)*?)\))?)'
9-
text_regex = regex.compile(text_regex_as_string)
8+
text_regex_str = r'\$(\w+(\(((?:\\.|.)*?)\))?)'
9+
text_regex = regex.compile(text_regex_str)
10+
table_regex_str = r'\&(\w+(\(((?:\\.|.)*?)\))?)'
11+
table_regex = regex.compile(table_regex_str)
1012

1113
def __init__(self, text_string):
1214
self.text_string = text_string
@@ -23,12 +25,14 @@ def scan(component: XComponent, get_table=False) -> dict[str, dict[str, str]]:
2325
if component.getImplementationName()=="ScNamedRangeObj":
2426
#doc= component.getReferredCells().getSpreadsheet()
2527
doc=component.getReferredCells()
26-
CalcTextStatement.text_regex_as_string = r'\&(\w+(\(((?:\\.|.)*?)\))?)'
28+
regex_to_use=CalcTextStatement.table_regex_str
2729
else:
2830
doc=component
31+
regex_to_use=CalcTextStatement.text_regex_str
32+
2933
plain_vars = {}
3034
search = doc.createReplaceDescriptor()
31-
search.SearchString = CalcTextStatement.text_regex_as_string
35+
search.SearchString = regex_to_use
3236
search.SearchRegularExpression = True
3337
search.SearchCaseSensitive = False
3438
founded = doc.findAll(search)
@@ -64,3 +68,4 @@ def fill(doc: XComponent, variable: str, value: str) -> None:
6468
founded = doc.replaceAll(search)
6569

6670

71+

lotemplate/unittest/files/content/calc_table.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@
7474
},
7575
"Age": {
7676
"type": "table",
77-
"value": [12,13,14,14]
77+
"value": ["12","13","14","14"]
7878
},
7979
"poid": {
8080
"type": "table",
81-
"value": [100,110,111,112]
81+
"value": ["100","110","111","112"]
8282
},
8383
"poste": {
8484
"type": "table",
@@ -116,11 +116,11 @@
116116
},
117117
"Age": {
118118
"type": "table",
119-
"value": [12,13,14,14]
119+
"value": ["12","13","14","14"]
120120
},
121121
"poid": {
122122
"type": "table",
123-
"value": [100,110,111,112]
123+
"value": ["100","110","111","112"]
124124
},
125125
"poste": {
126126
"type": "table",

lotemplate/unittest/test_json_convertion.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ class Tables(unittest.TestCase):
123123
def test_valid(self):
124124
self.assertEqual(
125125
{
126-
"var": {"type": "table", "value": [""]},
127-
"var1": {"type": "table", "value": [""]},
128-
"var2": {"type": "table", "value": [""]}
126+
"var": {"type": "table", "value": []},
127+
"var1": {"type": "table", "value": []},
128+
"var2": {"type": "table", "value": []}
129129
},
130130
ot.convert_to_datas_template(file_to_dict("lotemplate/unittest/files/jsons/tab_valid.json"))
131131
)

lotemplate/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def get_cleaned_object(var_name: str, var_value: dict ) -> dict:
150150
:param var_value: the table
151151
:return: the cleaned table
152152
"""
153-
return dict
153+
return convert_to_datas_template(var_value)
154154

155155

156156
@check_type
@@ -161,7 +161,7 @@ def get_cleaned_table(var_name: str, var_value: list[str]) -> list[str]:
161161
:param var_value: the table
162162
:return: the cleaned table
163163
"""
164-
return [""]
164+
return []
165165

166166
@check_type
167167
def get_cleaned_image(var_name: str, var_value: str) -> str:

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ Werkzeug~=3.1.2
77
sorcery~=0.2.2
88
regex~=2024.11.6
99
pypdf~=5.1.0
10+
jsondiff~=2.2

0 commit comments

Comments
 (0)