-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileProcessor.py
421 lines (352 loc) · 14.8 KB
/
FileProcessor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import re
import json
import requests
import codecs
import configparser
class FileToConfluenceProcessor:
def __init__(self):
# Este es el patron para detectar el constructor y extraer los campos en los que se guarda el concepto
self.const_pat = re.compile(r"#region Constructor", re.IGNORECASE)
# Este es el patron para detectar la región donde se definen las dependencias
self.depen_pat = re.compile(r"typeof", re.IGNORECASE)
# Este es el patrón de inicio de la zona a copiar
self.begin_pat = re.compile(r"#region C.+lculo", re.IGNORECASE)
# Si aparece este patron en el medio tenemos que considerar que tenemos que encontrar más de un patron de fin
self.mid_pat = re.compile(r"#region", re.IGNORECASE)
# Este es el patron para cerrar la zona a copiar
self.end_pat = re.compile(r"#endregion", re.IGNORECASE)
self.config = configparser.ConfigParser()
self.config.read("config.ini")
self.auth = (
self.config["CONFLUENCE"]["USER_CONF"],
self.config["CONFLUENCE"]["API_TOKEN"],
)
self.html_storage_txt = self._read_template()
def parse_file(self, file_path):
"""
Parse a file to extract concept-specific information.
Args:
file_path (str): The path to the file to be parsed.
Returns:
dict: A dictionary containing concept-specific data, including text, dependencies, ID, and fields.
Usage:
concept_data = parse_file(file_path)
"""
with codecs.open(file_path, encoding="utf-8") as reader:
copying = False
region_count = 0
buffer = {
"textConcepto": [],
"listDepend": [],
"idConcepto": "",
"campoGrata": "",
"campoAumentos": "",
}
line = reader.readline()
while line:
if self._parse_constructor(line, reader, buffer):
pass
elif self._parse_dependencies(line, reader, buffer):
pass
else:
copying, region_count = self._parse_calculation(line, copying, region_count, buffer)
line = reader.readline()
return buffer
def _parse_constructor(self, line, reader, buffer):
if self.const_pat.search(line):
while line.find("public") == -1:
line = reader.readline().strip()
line_camposbbdd = line.split(",")
try:
buffer["idConcepto"] = line_camposbbdd[1].strip()[1:-1]
if line_camposbbdd[3].find("string.Empty") == -1:
buffer["campoGrata"] = re.sub(
"[^A-Za-z0-9_]+", "", line_camposbbdd[3]
)
if line_camposbbdd[4].find("string.Empty") == -1:
buffer["campoAumentos"] = re.sub(
"[^A-Za-z0-9_]+", "", line_camposbbdd[4]
)
except IndexError:
pass
return True
return False
def _parse_dependencies(self, line, reader, buffer):
if self.depen_pat.search(line):
while self.depen_pat.search(line):
buffer["listDepend"].extend(re.findall("G?[0-9]+", line))
line = reader.readline().strip()
return True
return False
def _parse_calculation(self, line, copying, region_count, buffer):
if self.begin_pat.search(line):
copying = True
elif self.mid_pat.search(line) and copying:
region_count += 1
elif self.end_pat.search(line):
if region_count > 0:
region_count -= 1
else:
copying = False
if copying:
buffer["textConcepto"].append(line)
return copying, region_count
def format_concepto(self, dict_concepto):
# Implement the formatting logic here
"""
Format and customize the content of a Confluence page with concept-specific information.
Args:
dictConcepto (dict): A dictionary containing concept-specific data.
Returns:
str: The formatted page content as a string.
Usage:
formatted_content = format_concepto(html_storage_txt, dictConcepto)
"""
# Inserto el código del concepto
s = self.html_storage_txt.split("</ac:structured-macro>", maxsplit=1)
retorno = (
s[0]
+ "<ac:plain-text-body><![CDATA["
+ "".join(dict_concepto["textConcepto"])
+ "]]></ac:plain-text-body></ac:structured-macro>"
+ s[1]
)
# Armo la sección de links a dependencias
txt_dependencias = ", ".join(
[
'<ac:link><ri:page ri:content-title="Concepto '
+ str(x)
+ '" /><ac:plain-text-link-body><![CDATA['
+ str(x)
+ "]]></ac:plain-text-link-body></ac:link >"
for x in dict_concepto["listDepend"]
]
)
patron = "Conceptos que usa</strong></p></th><td>"
index_depen = retorno.find(patron)
if index_depen != -1:
retorno = (
retorno[0 : (index_depen + len(patron))]
+ txt_dependencias
+ retorno[(index_depen + len(patron) + 5) : len(retorno)]
)
# Armo la sección de páginas relacionadas por labels
patron = '<ac:parameter ac:name="labels">'
index_depen = retorno.find(patron)
if index_depen != -1:
# se le suma 9 al inicio porque es el largo de la palabra "concepto_" que es lo que tienen todos los labels
retorno = (
retorno[0 : (index_depen + len(patron) + 9)]
+ dict_concepto["idConcepto"]
+ retorno[(index_depen + len(patron) + 9 + 5) : len(retorno)]
)
# Armo la sección de campos de la base donde se guarda el concepto
patron = "Campos BBDD (G / A)</strong></p></th><td>"
index_depen = retorno.find(patron)
if index_depen != -1:
try:
retorno2 = (
retorno[0 : (index_depen + len(patron))]
+ dict_concepto["campoGrata"]
)
except KeyError as name:
retorno2 = retorno[0 : (index_depen + len(patron))]
try:
retorno2 = (
retorno2
+ " / "
+ dict_concepto["campoAumentos"]
+ retorno[(index_depen + len(patron) + 5) : len(retorno)]
)
except KeyError as name:
retorno2 = (
retorno2 + retorno[(index_depen + len(patron) + 5) : len(retorno)]
)
return retorno2
def search_page(self, concepto):
# Implement the search page logic here
"""
Search for a Confluence page in Confluence Cloud by title.
Args:
concepto (str): The title of the page to search for. In my particular case I'm adding "Concepto" to the value received.
Returns:
dict or None: Page information as a dictionary if found, or None if not found.
Raises:
requests.exceptions.RequestException: If an error occurs while searching for the page.
Exception: If an unexpected error occurs.
Usage:
page_info = search_page(config, auth, 'Concept Title')
"""
try:
base_url = self.config["CONFLUENCE"]["BASE_URL"]
space = self.config["CONFLUENCE"]["SPACE_CONF"]
url = f"{base_url}content?spaceKey={space}&title=Concepto%20{concepto}"
response = requests.get(url, auth=self.auth)
if response.status_code == 200:
data = response.json()
if data["size"] > 0:
return data["results"][0] # Return the first result if found
else:
return None # Page not found
elif response.status_code == 404:
return None # Page not found
else:
raise requests.exceptions.RequestException(
f"Error searching for the page: {response.status_code}"
)
except requests.exceptions.RequestException as e:
print(f"An error occurred while searching for the page: {str(e)}")
raise
except Exception as ex:
print(f"An unexpected error occurred: {str(ex)}")
raise
def add_page(self, id_concepto, texto_concepto):
# Implement the add page logic here
"""
Add a new Confluence page with the provided content.
Args:
idConcepto (str): The identifier for the concept to create the page for.
textoConcepto (str): The content to be added to the page.
Raises:
Exception: If an error occurs while adding the page.
Usage:
add_page(config, auth, 'ConceptID', 'Page content goes here')
"""
try:
data = {
"type": "page",
"title": str("Concepto " + id_concepto),
"ancestors": [
{
"type": "page",
"id": str(self.config["CONFLUENCE"]["PAG_PADRE_CONF"]),
}
],
"space": {"key": self.config["CONFLUENCE"]["SPACE_CONF"]},
"body": {
"storage": {
"representation": "storage",
"value": str(texto_concepto),
}
},
}
data = json.dumps(data)
url = "{base}content/".format(base=self.config["CONFLUENCE"]["BASE_URL"])
r = requests.post(
url,
data=data,
auth=self.auth,
headers={"Content-Type": "application/json"},
)
r.raise_for_status() # Raise an exception for HTTP errors
print("Page added successfully.")
except requests.exceptions.RequestException as e:
print(f"An error occurred while adding the page: {str(e)}")
except Exception as ex:
print(f"An unexpected error occurred: {str(ex)}")
def upd_page(self, result, texto_concepto):
# Implement the update page logic here
"""
Update an existing Confluence page with new content.
Args:
result (dict): Page information obtained from get_page_info.
texto_concepto (str): The updated content for the page.
Raises:
Exception: If an error occurs while updating the page.
Usage:
upd_page(page_info, 'Updated content goes here')
"""
try:
info = self._get_page_info(result["id"])
ver = int(info["version"]["number"]) + 1
data = {
"id": result["id"],
"type": "page",
"title": result["title"],
"version": {"number": ver},
"body": {
"storage": {
"representation": "storage",
"value": texto_concepto,
}
},
}
data = json.dumps(data)
url = "{base}content/{pageid}".format(
base=self.config["CONFLUENCE"]["BASE_URL"], pageid=result["id"]
)
r = requests.put(
url,
data=data,
auth=self.auth,
headers={
"Content-Type": "application/json",
"USER-AGENT": self.config["CONFLUENCE"]["USER_AGENT"],
},
)
r.raise_for_status() # Raise an exception for HTTP errors
print(f"Page '{result['title']}' updated successfully.")
print(f"URL: {self.config['CONFLUENCE']['VIEW_URL']}{result['id']}")
except requests.exceptions.RequestException as e:
print(f"An error occurred while updating the page: {str(e)}")
except Exception as ex:
print(f"An unexpected error occurred: {str(ex)}")
def _get_page_info(self, pageid):
"""
Retrieve information about a Confluence page using its ID.
Args:
pageid (str): The ID of the Confluence page to retrieve information for.
Returns:
dict: Page information as a dictionary, including title, version, and more.
Raises:
requests.exceptions.RequestException: If an error occurs while fetching page information.
Exception: If an unexpected error occurs.
Usage:
page_info = get_page_info('12345678')
"""
try:
url = "{base}content/{pageid}".format(
base=self.config["CONFLUENCE"]["BASE_URL"], pageid=pageid
)
r = requests.get(url, auth=self.auth)
r.raise_for_status() # Raise an exception for HTTP errors
return r.json()
except requests.exceptions.RequestException as e:
print(f"An error occurred while fetching page info: {str(e)}")
return None
except Exception as ex:
print(f"An unexpected error occurred: {str(ex)}")
return None
def _read_template(self):
"""
Read and retrieve the template content for Confluence pages.
Returns:
str: The template content as a string.
Raises:
requests.exceptions.RequestException: If an error occurs while fetching the template.
Exception: If an unexpected error occurs.
Usage:
template_content = read_template(config, auth)
"""
try:
url = "{base}template/{page_id}".format(
base=self.config["CONFLUENCE"]["BASE_URL"],
page_id=self.config["CONFLUENCE"]["PAG_TEMPLATE"],
)
r = requests.get(
url,
auth=self.auth,
headers={
"Content-Type": "application/json",
"USER-AGENT": self.config["CONFLUENCE"]["USER_AGENT"],
},
)
r.raise_for_status() # Raise an exception for HTTP errors (e.g., 404, 500, etc.)
json_text = r.text
return json.loads(json_text)["body"]["storage"]["value"]
except requests.exceptions.RequestException as e:
print(f"An error occurred while fetching the template: {str(e)}")
return None
except Exception as ex:
print(f"An unexpected error occurred: {str(ex)}")
return None