spei-python 1.0.2__py3-none-any.whl → 1.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
spei/resources/cda.py CHANGED
@@ -4,7 +4,7 @@ from lxml import etree
4
4
  from pydantic import BaseModel, ConfigDict
5
5
 
6
6
  from spei import types
7
- from spei.types.common import DateInt, InstitutionCode
7
+ from spei.resources.validators import normalize_invalid_chars
8
8
  from spei.utils import to_pascal_case, to_snake_case # noqa: WPS347
9
9
 
10
10
  SOAP_NS = 'http://schemas.xmlsoap.org/soap/envelope/'
@@ -69,27 +69,6 @@ class CDA(BaseModel):
69
69
  op_nom_prov_rem_nac: Optional[str] = None
70
70
  model_config = ConfigDict(use_enum_values=True)
71
71
 
72
- def build_xml(self) -> etree._Element: # noqa: WPS437
73
- mensaje = etree.Element(
74
- etree.QName(ENVIO_PRAXIS_NS, 'datosCda'),
75
- idCda=str(self.id),
76
- idMensaje=str(self.mensaje_id),
77
- nsmap={'env': ENVIO_PRAXIS_NS},
78
- )
79
-
80
- elements = self.dict(exclude_none=True, exclude={'id', 'mensaje_id'})
81
-
82
- for element, value in elements.items(): # noqa: WPS110
83
- if element in self.model_fields:
84
- upper_camel_case_element = to_pascal_case(element)
85
- subelement = etree.SubElement(
86
- mensaje,
87
- etree.QName(ENVIO_PRAXIS_NS, upper_camel_case_element),
88
- ) # noqa: E501
89
- subelement.text = str(value)
90
-
91
- return mensaje
92
-
93
72
  @classmethod
94
73
  def parse_xml(cls, mensaje_element):
95
74
  genera_cda = mensaje_element.find('generaCda')
@@ -106,3 +85,24 @@ class CDA(BaseModel):
106
85
  cda_data[tag] = element.text
107
86
 
108
87
  return cls(**cda_data)
88
+
89
+ def build_xml(self) -> etree._Element: # noqa: WPS437
90
+ mensaje = etree.Element(
91
+ etree.QName(ENVIO_PRAXIS_NS, 'datosCda'),
92
+ idCda=str(self.id),
93
+ idMensaje=str(self.mensaje_id),
94
+ nsmap={'env': ENVIO_PRAXIS_NS},
95
+ )
96
+
97
+ elements = self.dict(exclude_none=True, exclude={'id', 'mensaje_id'})
98
+
99
+ for element, value in elements.items(): # noqa: WPS110
100
+ if element in self.__fields__:
101
+ pascal_case_element = to_pascal_case(element)
102
+ subelement = etree.SubElement(
103
+ mensaje,
104
+ etree.QName(ENVIO_PRAXIS_NS, pascal_case_element),
105
+ )
106
+ subelement.text = normalize_invalid_chars(value)
107
+
108
+ return mensaje
spei/resources/orden.py CHANGED
@@ -6,13 +6,12 @@ from lxml.etree import _Element # noqa: WPS450, WPS458
6
6
  from pydantic import BaseModel, ConfigDict, field_validator
7
7
 
8
8
  from spei import types
9
- from spei.resources.validators import validate_alphanumeric
10
- from spei.types.common import DateInt, InstitutionCode
9
+ from spei.resources.validators import normalize_invalid_chars
11
10
  from spei.utils import to_camel_case, to_pascal_case, to_snake_case # noqa: WPS347
12
11
 
13
12
  SOAP_NS = 'http://schemas.xmlsoap.org/soap/envelope/'
14
13
  PRAXIS_NS = 'http://www.praxis.com.mx/'
15
- _WHITESPACE_PATTERN = re.compile(r'\s+')
14
+ _DATE_PATTERN = re.compile(r'^\d{4}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$')
16
15
  _CAMEL_CASE_FIELDS = frozenset(
17
16
  (
18
17
  'op_rastreo_ori',
@@ -177,7 +176,7 @@ class Orden(BaseModel):
177
176
  subelement.text = str(value)
178
177
  continue
179
178
 
180
- text = self._process_text(str(value), element)
179
+ text = normalize_invalid_chars(str(value))
181
180
 
182
181
  if element in _CAMEL_CASE_FIELDS:
183
182
  subelement = etree.SubElement(
@@ -223,7 +222,7 @@ class Orden(BaseModel):
223
222
  return
224
223
 
225
224
  tag = to_snake_case(element.tag)
226
- stripped = _WHITESPACE_PATTERN.sub(' ', element.text.strip())
225
+ stripped = element.text.strip()
227
226
 
228
227
  if tag == 'op_ins_clave':
229
228
  orden_data['op_ins_clave_ord'] = stripped
@@ -233,13 +232,5 @@ class Orden(BaseModel):
233
232
  orden_data['op_firma_dig'] = stripped
234
233
  return
235
234
 
236
- if tag in cls.model_fields:
237
- orden_data[tag] = cls._process_text(stripped, tag)
238
-
239
- @classmethod
240
- def _process_text(cls, text: str, tag: str) -> Optional[str]:
241
- """Process and validate the text content of an XML element."""
242
- try:
243
- return validate_alphanumeric(text)
244
- except ValueError as error:
245
- raise ValueError(f'{error} in {tag}') from error
235
+ if tag in cls.__fields__:
236
+ orden_data[tag] = stripped
@@ -29,9 +29,9 @@ class Respuesta(BaseModel):
29
29
  respuesta = etree.Element('respuesta')
30
30
 
31
31
  for element, value in self.dict(exclude={'categoria'}).items(): # noqa: WPS110
32
- if element in self.model_fields:
33
- upper_camel_case_element = to_pascal_case(element)
34
- subelement = etree.SubElement(respuesta, upper_camel_case_element)
32
+ if element in self.__fields__:
33
+ pascal_case_element = to_pascal_case(element)
34
+ subelement = etree.SubElement(respuesta, pascal_case_element)
35
35
  subelement.text = str(value)
36
36
 
37
37
  return respuesta
@@ -1,4 +1,12 @@
1
- # From “APÉNDICE B: TABLA DE CARACTERES VÁLIDOS Y CÓDIGOS ASCII RESPECTIVOS”
1
+ import logging
2
+ from typing import Any
3
+
4
+ from unidecode import unidecode
5
+
6
+ # Set up basic configuration for logging
7
+ logging.basicConfig(level=logging.WARNING)
8
+
9
+ # From "APÉNDICE B: TABLA DE CARACTERES VÁLIDOS Y CÓDIGOS ASCII RESPECTIVOS"
2
10
  # in SPEI Manual.
3
11
  VALID_CHARACTERS = frozenset(
4
12
  (
@@ -105,3 +113,34 @@ def validate_alphanumeric(text: str) -> str:
105
113
  raise ValueError(f'Invalid characters found: {invalid_chars}')
106
114
 
107
115
  return text
116
+
117
+
118
+ def normalize_invalid_chars(text: Any) -> str:
119
+ """Converts a text to a valid string.
120
+
121
+ Selectively replacing invalid characters with their ASCII
122
+ equivalents. Valid characters are preserved as-is, only invalid ones
123
+ are converted using unidecode. If the conversion is not possible,
124
+ the character is replaced with a '?' character.
125
+ """
126
+ result = []
127
+ text_str = str(text)
128
+
129
+ for char in text_str:
130
+ if char in VALID_CHARACTERS:
131
+ result.append(char)
132
+ else:
133
+ # Convert only this character to ASCII and validate it
134
+ ascii_char = unidecode(char)
135
+ # Take first char in case unidecode returns multiple chars
136
+ first_ascii = ascii_char[0] if ascii_char else ''
137
+ if first_ascii and first_ascii in VALID_CHARACTERS:
138
+ result.append(first_ascii)
139
+ else:
140
+ # Log a warning and replace with '?'
141
+ logging.warning(
142
+ f'Character "{char}" cannot be converted to a valid character, replacing with "?"', # noqa: E501
143
+ )
144
+ result.append('?')
145
+
146
+ return ''.join(result)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: spei-python
3
- Version: 1.0.2
3
+ Version: 1.0.3
4
4
  Summary:
5
5
  Author: tech
6
6
  Author-email: tech@fondeadora.com
@@ -18,13 +18,13 @@ spei/requests/orden.py,sha256=TIkTDr1pVvJ4TOX6hUXQhUparOOAVAJY_zv_bxPrxvU,1602
18
18
  spei/requests/respuesta.py,sha256=oH0EU4qJ7cPmYjMg5v_UBbZMf6ZdrXA7sZY7fOMtzs8,1724
19
19
  spei/resources/__init__.py,sha256=cJL9KI0ZBKHcDN5me8yuvschQMW1HH_bpLS7TnLXZw4,487
20
20
  spei/resources/acuse.py,sha256=JfX05461ryOUbFwU3Jps2AoiC4yFKv3p2GouVTyEHS8,5566
21
- spei/resources/cda.py,sha256=Epb4Yri-vqE-itpoXso1mFIMLDbolWG1r3ZtvWrnD3w,3344
21
+ spei/resources/cda.py,sha256=EyGZjY7udqVpmHpZU-D6D88PZNRg-Fq5QX_uWFLhazI,3345
22
22
  spei/resources/ensesion.py,sha256=0kxyW-lFHilELYv-9PSVjkG1mp-gOSSC206501KlwSE,1587
23
23
  spei/resources/mensaje.py,sha256=5vb_kD2l_QILTAPmlG1u33H3nJLD5KDpPw0_PRfY6GM,1783
24
- spei/resources/orden.py,sha256=Gg9vcE6QqY3v6MBippmmQ27U5d9084J-xBkW2BPQaVw,8634
24
+ spei/resources/orden.py,sha256=H45mY-weaug1NHRxnHspKU9VDwxBGDRK3LW5mnT3ItE,8246
25
25
  spei/resources/payments.py,sha256=0m9o9A3TQkO8KPNPQWcaje_6yQWcPqTk4b7-MzQhVh4,8930
26
- spei/resources/respuesta.py,sha256=5ChH5pY3fyBCUFVPjRyNxx8MwBSH5dHa3lNzPdCXsLk,1648
27
- spei/resources/validators.py,sha256=7HPEwAR1DCUnJoT_knGptb167zfdCDYIDp-DAWtAGOk,1613
26
+ spei/resources/respuesta.py,sha256=_9RHKNHAKRnbzxIoI5Prymgxt2KOg6mkoYIyBU41gao,1636
27
+ spei/resources/validators.py,sha256=jvX8lfCaHQg0WBoKSnig7_t8tX--Ab5GaiCbg3IvBs8,2925
28
28
  spei/responses/__init__.py,sha256=WxHb0KJ1sI5m6iLfxtf3h8MnqubQlxdtiaarXJvZdkM,247
29
29
  spei/responses/acuse.py,sha256=ZGfP45XYvDSDbgfSKWeraDpFApDc0oks05_tQH-bwzM,650
30
30
  spei/responses/orden.py,sha256=a-GE62-OBWVNpW2RuVKgNqWnY2cNy88shUDLQAL21dU,1068
@@ -35,6 +35,6 @@ spei/types/common.py,sha256=4nn_ITxSUelAcryk9s68cMv40IXdyJ3HezQwTdF6nGg,1231
35
35
  spei/types/sice.py,sha256=xFSoiLklRNWXoWwZXpSN8-SdkliNK23_MZbhcOc7a9s,222
36
36
  spei/types/spei.py,sha256=GX3jbGe9B_1_TYNguYfA0TkPLQiwTOB-91SR0lQisJ0,4678
37
37
  spei/utils.py,sha256=DtJAWf3tUvjRFC2G-L3ylm7ogBGwugNaBZtJI52jDME,455
38
- spei_python-1.0.2.dist-info/METADATA,sha256=hpH-sTxxb0Yb4qxE7Glhntkfdpt73rEQVbmVufmlgHA,3953
39
- spei_python-1.0.2.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
40
- spei_python-1.0.2.dist-info/RECORD,,
38
+ spei_python-1.0.3.dist-info/METADATA,sha256=I1uEtd5qSZAnlH4RUVim2GizMNFOC6nZ1PNovyeUaaI,3953
39
+ spei_python-1.0.3.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
40
+ spei_python-1.0.3.dist-info/RECORD,,