Skip to content

Commit a5db364

Browse files
committed
fixed compute
1 parent 432720a commit a5db364

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

satdigitalinvoice/mycfdi.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import hashlib
33
import json
44
import logging
5+
import re
56
import os
67
from collections.abc import Mapping
78
from datetime import datetime
@@ -52,6 +53,31 @@ def generate_pseudo_random_guid(cadena):
5253
return UUID(hash_object.hexdigest())
5354

5455

56+
def sanitize_filename(filename: str, replacement: str = "") -> str:
57+
"""
58+
Removes or replaces characters that are invalid in filenames.
59+
60+
Args:
61+
filename (str): The input string.
62+
replacement (str): What to put instead of invalid characters (default "_").
63+
64+
Returns:
65+
str: A safe filename.
66+
"""
67+
# Characters forbidden in Windows filenames: \ / : * ? " < > |
68+
invalid_chars = r'[\\/:*?"<>|]+'
69+
70+
# Replace invalid characters
71+
safe = re.sub(invalid_chars, replacement, filename)
72+
73+
# Optionally, remove other special characters (keeping alphanumeric, space, _, -, .)
74+
safe = re.sub(r'[^a-zA-Z0-9 _.\-]', replacement, safe)
75+
76+
# # Remove leading/trailing whitespace and dots (Windows restriction)
77+
# safe = safe.strip(" .")
78+
79+
return safe
80+
5581
class MyCFDI(SatCFDI):
5682
local_db = None
5783
base_dir = None # type: str
@@ -131,7 +157,7 @@ def filename(self):
131157
match self.tag:
132158
case '{http://www.sat.gob.mx/cfd/3}Comprobante' | '{http://www.sat.gob.mx/cfd/4}Comprobante':
133159
path = "{3:%Y}/{3:%Y-%m}/facturas/{4}_{0}_[{1}]_{2}".format(
134-
self.name,
160+
sanitize_filename(self.name),
135161
code_str(self["TipoDeComprobante"]),
136162
self.uuid,
137163
self["Fecha"],

0 commit comments

Comments
 (0)