-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvop2csv.py
executable file
·125 lines (95 loc) · 3.84 KB
/
vop2csv.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
#!/usr/bin/python3
#own
from vop2csv_parse import parse_arguments
from vop2unzip import unzip_file
#native
from collections import defaultdict
from itertools import chain
from pathlib import Path
import csv
import os
def print_debug(string: str):
if args.debug :
print("DBG:", string)
# Isole le numéro du téléphone du nom de fichier
get_phone = lambda x : x.split('-')[-1][:-4]
def process_file(number: str, mobile_files: list[str], data_files: list[str], margin: int) -> int:
mobile_file_list = [n for n in mobile_files if number in n]
data_file_list = [n for n in data_files if number in n]
# Récupère la première ligne correspondante du fichier source (Nom, etc..)
infos = [c for c in source if number in c][0]
print_debug("client: {}".format(infos))
to_write = [infos[2], infos[0], infos[3]]
#{ Peut être transformé en fonction
if not mobile_file_list:
print_debug("{} no mobile plan".format(number))
to_write.append(0.0)
else :
with open(mobile_file_list[0]) as file:
reader =csv.reader(file)
total = 0
next(reader)
for row in reader:
if ("RG9000.mobiledata" not in row[9]):
total += float(row[7])
mobile_margin = round(total / ((100 - margin) * 0.01), 2)
to_write.append(mobile_margin)
#{ Peut être transformé en la même fonction
if not data_file_list:
print_debug("{} no data plan".format(number))
to_write.append(0.0)
else:
with open(data_file_list[0]) as file:
reader =csv.reader(file)
total = 0
next(reader)
for row in reader:
total += float(row[6])
data_margin = round(total / ((100 - margin) * 0.01), 2)
to_write.append(data_margin)
line_total = to_write[3] + to_write[4]
if line_total != 0 :
writer.writerow(to_write)
return line_total
if __name__ == '__main__':
args = parse_arguments()
source_dico = defaultdict(list)
# Récupère les numéros du fichier source dans un dictionnaire par clients
with open(args.source_file) as source_file :
source_reader = csv.reader(source_file)
next(source_reader)
source = [r for r in source_reader]
for row in source:
source_dico[row[1]].append(row[0])
# Dézip le dossier
dir = unzip_file(args.zip_file)
# Récupère les fichiers MOBILES & DATA
mobile_files = [dir + "/" + f for f in os.listdir(dir)
if "DATA" not in f and get_phone(f) in chain(*source_dico.values())]
data_files = [dir + "/" + f for f in os.listdir(dir)
if "DATA" in f and get_phone(f) in chain(*source_dico.values())]
# Marge à calculer
margin = int(args.margin) if args.margin else 36
# Récupère la date via le dossier dézipé
date = dir.split('/')[-1]
out_dir = args.output_dir if args.output_dir else "out"
client: str
for client in source_dico:
written = 0
# Créé le fichier et le dossier parent
Path("{}/{}".format(out_dir, client)).mkdir(parents=True, exist_ok=True)
csv_file = "{}/{}/Hors_Forfait_{}_{}.csv".format(out_dir, client,
client.replace(' ', '_').replace('.', ''), date)
# Ouvre fichier >>-->
output = open(csv_file, 'w+')
# Créé le writer et
writer = csv.writer(output)
writer.writerow(["Client", "Numéro", "Puce", "Total mobile marginé", "Total data marginé"])
for x in source_dico[client]:
print_debug("looking at {}".format(x))
written += process_file(x, mobile_files, data_files, margin)
# Ferme fichier <--<<
output.close()
# Supprime le fichier si aucune puce forfait mobile
if (written == 0):
os.remove(csv_file)