This repository has been archived by the owner on Jan 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
crear_organigrama.py
executable file
·81 lines (62 loc) · 1.74 KB
/
crear_organigrama.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import re
from glob import glob
import xlrd
from api import Jnj2
re_space = re.compile(r" +")
def parse(cell):
if not cell:
return None
v = cell.value
if isinstance(v, float):
return int(v) if v.is_integer() else v
if isinstance(v, str):
v = v.strip()
v = re_space.sub(" ", v)
if v.isdigit():
return int(v)
return v if len(v) else None
return v
class Org:
def __init__(self, codigo, descripcion):
self.codigo = codigo
self.descripcion = descripcion
self.hijos = set()
def get_hijos(self):
return sorted(self.hijos, key=lambda o: (o.descripcion, o.codigo))
def __eq__(self, o):
self.codigo == o.codigo
def __hash__(self):
return self.codigo.__hash__()
organismos = []
dict_organ = {}
def get_org(i, d):
if i is None:
return None
o = dict_organ.get(i, None)
if o is None:
o = Org(i, d)
dict_organ[i] = o
return o
for x in glob("fuentes/RPT-*-PF*.xls"):
org = Org(os.path.basename(x), "")
wb = xlrd.open_workbook(x, logfile=open(os.devnull, 'w'))
sh = wb.sheet_by_index(0)
for rx in range(1, sh.nrows):
r = [parse(c) for c in sh.row(rx)]
if len(r) > 1 and isinstance(r[0], int):
iM, dM, iC, dC, iU, dU = r[0:6]
oM = get_org(iM, dM)
oC = get_org(iC, dC)
oU = get_org(iU, dU)
org.hijos.add(oM)
if oC:
oM.hijos.add(oC)
oC.hijos.add(oU)
else:
oM.hijos.add(oU)
organismos.append(org)
j2 = Jnj2("j2/", "docs/")
j2.save("organigrama.html", organismos=organismos)