|
13 | 13 |
|
14 | 14 |
|
15 | 15 | from . import errors |
16 | | - |
| 16 | +from sorcery import dict_of |
17 | 17 | from lotemplate.Statement.CalcTableStatement import CalcTableStatement |
18 | 18 | from .Template import Template |
19 | 19 | from lotemplate.Statement.CalcSearchStatement import CalcTextStatement |
| 20 | +from lotemplate.Statement.CalcImageStatement import CalcImageStatement |
20 | 21 | from jsondiff import diff |
21 | | - |
22 | 22 | class CalcTemplate(Template): |
23 | 23 | formats = { |
24 | 24 | "ods": "calc8", |
@@ -68,14 +68,29 @@ def scan(self, **kwargs) -> dict[str: dict[str, Union[str, list[str]]]]: |
68 | 68 | """ |
69 | 69 |
|
70 | 70 | #should_close = kwargs.get("should_close", False) |
71 | | - texts = {} |
| 71 | + texts = {} |
| 72 | + images = {} |
72 | 73 | #(Pdb) self.doc.getSheets().getElementNames() |
73 | 74 | for sheet in self.doc.getSheets(): |
74 | 75 | texts = texts | CalcTextStatement.scan(sheet) |
| 76 | + images = images | CalcImageStatement.scan_image(sheet) |
75 | 77 | tables=CalcTableStatement.scan(self.doc) |
76 | | - #texts = CalcTextStatement.scan_Document_text(self.doc) |
77 | | - #pdb.set_trace() |
78 | | - return texts | tables |
| 78 | + variables_list = list(texts.keys()) + list(tables.keys()) + list(images.keys()) |
| 79 | + duplicates = [variable for variable in variables_list if variables_list.count(variable) > 1] |
| 80 | + |
| 81 | + if duplicates: |
| 82 | + first_type = "text" if duplicates[0] in texts.keys() else "image" |
| 83 | + second_type = "table" if duplicates[0] in tables.keys() else "image" |
| 84 | + self.close() |
| 85 | + raise errors.TemplateError( |
| 86 | + 'duplicated_variable', |
| 87 | + f"The variable {duplicates[0]!r} is mentioned two times, but " |
| 88 | + f"for two different types: {first_type!r}, and {second_type!r}", |
| 89 | + dict_of(first_type, second_type, variable=duplicates[0]) |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | + return texts | tables | images |
79 | 94 |
|
80 | 95 |
|
81 | 96 | def search_error(self, json_vars: dict[str, dict[str, Union[str, list[str]]]]) -> None: |
@@ -132,6 +147,9 @@ def fill(self, variables: dict[str, dict[str, Union[str, list[str]]]]) -> None: |
132 | 147 | if details['type'] == 'text': |
133 | 148 | for sheet in self.doc.getSheets(): |
134 | 149 | CalcTextStatement.fill(sheet, "$" + var, details['value']) |
| 150 | + elif details['type'] == 'image': |
| 151 | + for sheet in self.doc.getSheets(): |
| 152 | + CalcImageStatement.image_fill(sheet,self.cnx.graphic_provider,"$" + var, details['value']) |
135 | 153 | elif details['type'] == "object" and CalcTableStatement.isTableVar(var) : |
136 | 154 | objects[var]=details |
137 | 155 | for var, details in objects.items(): |
|
0 commit comments