-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_cantidad.py
More file actions
115 lines (85 loc) · 3.06 KB
/
Copy pathparser_cantidad.py
File metadata and controls
115 lines (85 loc) · 3.06 KB
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
import re
from config import CantidadSolicitada
class ParserCantidad:
"""
Esta clase solo interpreta texto.
No imprime, no pide input y no sabe nada del menú.
Ejemplos:
Enter -> generar todo
100 -> 100 combinaciones
100mb -> tantas combinaciones como quepan en 100 MB
1gb -> tantas combinaciones como quepan en 1 GB
"""
def convertir_numero_local(self, texto: str) -> float:
texto = texto.strip().replace("_", "")
if "," in texto and "." not in texto:
partes = texto.split(",")
if len(partes[-1]) == 3 and len(partes) > 1:
texto = texto.replace(",", "")
else:
texto = texto.replace(",", ".")
elif "," in texto and "." in texto:
texto = texto.replace(",", "")
return float(texto)
def parsear_tamano_a_bytes(self, texto: str):
texto = texto.strip().lower().replace(" ", "")
patron = r"^([0-9]+(?:[.,][0-9]+)?)((?:k|m|g|t|p)?b|bytes?)$"
coincidencia = re.match(patron, texto)
if not coincidencia:
return None
numero_texto = coincidencia.group(1)
unidad = coincidencia.group(2)
numero = self.convertir_numero_local(numero_texto)
unidades = {
"b": 1,
"byte": 1,
"bytes": 1,
"kb": 1024,
"mb": 1024 ** 2,
"gb": 1024 ** 3,
"tb": 1024 ** 4,
"pb": 1024 ** 5,
}
return int(numero * unidades[unidad])
def parsear(
self,
entrada: str,
total: int,
bytes_linea: int
) -> CantidadSolicitada:
entrada = entrada.strip().lower()
if entrada == "":
return CantidadSolicitada(
cantidad=total,
modo="COMPLETO",
bytes_solicitados=None
)
bytes_solicitados = self.parsear_tamano_a_bytes(entrada)
if bytes_solicitados is not None:
combinaciones = bytes_solicitados // bytes_linea
if combinaciones <= 0:
raise ValueError(
"El tamaño indicado no alcanza ni para una combinación."
)
if combinaciones > total:
combinaciones = total
return CantidadSolicitada(
cantidad=combinaciones,
modo="POR PESO",
bytes_solicitados=bytes_solicitados
)
entrada_limpia = entrada.replace(",", "").replace("_", "")
if entrada_limpia.isdigit():
combinaciones = int(entrada_limpia)
if combinaciones <= 0:
raise ValueError("La cantidad debe ser mayor que cero.")
if combinaciones > total:
combinaciones = total
return CantidadSolicitada(
cantidad=combinaciones,
modo="POR CANTIDAD",
bytes_solicitados=None
)
raise ValueError(
"Formato inválido. Usa algo como 100, 1000000, 100mb, 1gb o 1.5gb."
)