everysk-lib 1.10.2__cp312-cp312-win_amd64.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.
- everysk/__init__.py +30 -0
- everysk/_version.py +683 -0
- everysk/api/__init__.py +61 -0
- everysk/api/api_requestor.py +167 -0
- everysk/api/api_resources/__init__.py +23 -0
- everysk/api/api_resources/api_resource.py +371 -0
- everysk/api/api_resources/calculation.py +779 -0
- everysk/api/api_resources/custom_index.py +42 -0
- everysk/api/api_resources/datastore.py +81 -0
- everysk/api/api_resources/file.py +42 -0
- everysk/api/api_resources/market_data.py +223 -0
- everysk/api/api_resources/parser.py +66 -0
- everysk/api/api_resources/portfolio.py +43 -0
- everysk/api/api_resources/private_security.py +42 -0
- everysk/api/api_resources/report.py +65 -0
- everysk/api/api_resources/report_template.py +39 -0
- everysk/api/api_resources/tests.py +115 -0
- everysk/api/api_resources/worker_execution.py +64 -0
- everysk/api/api_resources/workflow.py +65 -0
- everysk/api/api_resources/workflow_execution.py +93 -0
- everysk/api/api_resources/workspace.py +42 -0
- everysk/api/http_client.py +63 -0
- everysk/api/tests.py +32 -0
- everysk/api/utils.py +262 -0
- everysk/config.py +451 -0
- everysk/core/_tests/serialize/test_json.py +336 -0
- everysk/core/_tests/serialize/test_orjson.py +295 -0
- everysk/core/_tests/serialize/test_pickle.py +48 -0
- everysk/core/cloud_function/main.py +78 -0
- everysk/core/cloud_function/tests.py +86 -0
- everysk/core/compress.py +245 -0
- everysk/core/datetime/__init__.py +12 -0
- everysk/core/datetime/calendar.py +144 -0
- everysk/core/datetime/date.py +424 -0
- everysk/core/datetime/date_expression.py +299 -0
- everysk/core/datetime/date_mixin.py +1475 -0
- everysk/core/datetime/date_settings.py +30 -0
- everysk/core/datetime/datetime.py +713 -0
- everysk/core/exceptions.py +435 -0
- everysk/core/fields.py +1176 -0
- everysk/core/firestore.py +555 -0
- everysk/core/fixtures/_settings.py +29 -0
- everysk/core/fixtures/other/_settings.py +18 -0
- everysk/core/fixtures/user_agents.json +88 -0
- everysk/core/http.py +691 -0
- everysk/core/lists.py +92 -0
- everysk/core/log.py +709 -0
- everysk/core/number.py +37 -0
- everysk/core/object.py +1469 -0
- everysk/core/redis.py +1021 -0
- everysk/core/retry.py +51 -0
- everysk/core/serialize.py +674 -0
- everysk/core/sftp.py +414 -0
- everysk/core/signing.py +53 -0
- everysk/core/slack.py +127 -0
- everysk/core/string.py +199 -0
- everysk/core/tests.py +240 -0
- everysk/core/threads.py +199 -0
- everysk/core/undefined.py +70 -0
- everysk/core/unittests.py +73 -0
- everysk/core/workers.py +241 -0
- everysk/sdk/__init__.py +23 -0
- everysk/sdk/base.py +98 -0
- everysk/sdk/brutils/cnpj.py +391 -0
- everysk/sdk/brutils/cnpj_pd.py +129 -0
- everysk/sdk/engines/__init__.py +26 -0
- everysk/sdk/engines/cache.py +185 -0
- everysk/sdk/engines/compliance.py +37 -0
- everysk/sdk/engines/cryptography.py +69 -0
- everysk/sdk/engines/expression.cp312-win_amd64.pyd +0 -0
- everysk/sdk/engines/expression.pyi +55 -0
- everysk/sdk/engines/helpers.cp312-win_amd64.pyd +0 -0
- everysk/sdk/engines/helpers.pyi +26 -0
- everysk/sdk/engines/lock.py +120 -0
- everysk/sdk/engines/market_data.py +244 -0
- everysk/sdk/engines/settings.py +19 -0
- everysk/sdk/entities/__init__.py +23 -0
- everysk/sdk/entities/base.py +784 -0
- everysk/sdk/entities/base_list.py +131 -0
- everysk/sdk/entities/custom_index/base.py +209 -0
- everysk/sdk/entities/custom_index/settings.py +29 -0
- everysk/sdk/entities/datastore/base.py +160 -0
- everysk/sdk/entities/datastore/settings.py +17 -0
- everysk/sdk/entities/fields.py +375 -0
- everysk/sdk/entities/file/base.py +215 -0
- everysk/sdk/entities/file/settings.py +63 -0
- everysk/sdk/entities/portfolio/base.py +248 -0
- everysk/sdk/entities/portfolio/securities.py +241 -0
- everysk/sdk/entities/portfolio/security.py +580 -0
- everysk/sdk/entities/portfolio/settings.py +97 -0
- everysk/sdk/entities/private_security/base.py +226 -0
- everysk/sdk/entities/private_security/settings.py +17 -0
- everysk/sdk/entities/query.py +603 -0
- everysk/sdk/entities/report/base.py +214 -0
- everysk/sdk/entities/report/settings.py +23 -0
- everysk/sdk/entities/script.py +310 -0
- everysk/sdk/entities/secrets/base.py +128 -0
- everysk/sdk/entities/secrets/script.py +119 -0
- everysk/sdk/entities/secrets/settings.py +17 -0
- everysk/sdk/entities/settings.py +48 -0
- everysk/sdk/entities/tags.py +174 -0
- everysk/sdk/entities/worker_execution/base.py +307 -0
- everysk/sdk/entities/worker_execution/settings.py +63 -0
- everysk/sdk/entities/workflow_execution/base.py +113 -0
- everysk/sdk/entities/workflow_execution/settings.py +32 -0
- everysk/sdk/entities/workspace/base.py +99 -0
- everysk/sdk/entities/workspace/settings.py +27 -0
- everysk/sdk/settings.py +67 -0
- everysk/sdk/tests.py +105 -0
- everysk/sdk/worker_base.py +47 -0
- everysk/server/__init__.py +9 -0
- everysk/server/applications.py +63 -0
- everysk/server/endpoints.py +516 -0
- everysk/server/example_api.py +69 -0
- everysk/server/middlewares.py +80 -0
- everysk/server/requests.py +62 -0
- everysk/server/responses.py +119 -0
- everysk/server/routing.py +64 -0
- everysk/server/settings.py +36 -0
- everysk/server/tests.py +36 -0
- everysk/settings.py +98 -0
- everysk/sql/__init__.py +9 -0
- everysk/sql/connection.py +232 -0
- everysk/sql/model.py +376 -0
- everysk/sql/query.py +417 -0
- everysk/sql/row_factory.py +63 -0
- everysk/sql/settings.py +49 -0
- everysk/sql/utils.py +129 -0
- everysk/tests.py +23 -0
- everysk/utils.py +81 -0
- everysk/version.py +15 -0
- everysk_lib-1.10.2.dist-info/.gitignore +5 -0
- everysk_lib-1.10.2.dist-info/METADATA +326 -0
- everysk_lib-1.10.2.dist-info/RECORD +137 -0
- everysk_lib-1.10.2.dist-info/WHEEL +5 -0
- everysk_lib-1.10.2.dist-info/licenses/LICENSE.txt +9 -0
- everysk_lib-1.10.2.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
###############################################################################
|
|
2
|
+
#
|
|
3
|
+
# (C) Copyright 2026 EVERYSK TECHNOLOGIES
|
|
4
|
+
#
|
|
5
|
+
# This is an unpublished work containing confidential and proprietary
|
|
6
|
+
# information of EVERYSK TECHNOLOGIES. Disclosure, use, or reproduction
|
|
7
|
+
# without authorization of EVERYSK TECHNOLOGIES is prohibited.
|
|
8
|
+
#
|
|
9
|
+
###############################################################################
|
|
10
|
+
import re
|
|
11
|
+
import secrets
|
|
12
|
+
import string
|
|
13
|
+
|
|
14
|
+
from everysk.core.fields import IntField, TupleField
|
|
15
|
+
|
|
16
|
+
###############################################################################
|
|
17
|
+
# Globals
|
|
18
|
+
###############################################################################
|
|
19
|
+
|
|
20
|
+
CNPJ_LENGTH: IntField = IntField(default=14, readonly=True)
|
|
21
|
+
CNPJ_BASE_LENGTH: IntField = IntField(default=12, readonly=True)
|
|
22
|
+
CNPJ_D2: IntField = IntField(default=2, readonly=True)
|
|
23
|
+
|
|
24
|
+
WEIGHTS_DV1: TupleField = TupleField(default=(5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2), readonly=True)
|
|
25
|
+
WEIGHTS_DV2: TupleField = TupleField(default=(6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2), readonly=True)
|
|
26
|
+
|
|
27
|
+
BASE_VALUES = string.ascii_uppercase + string.digits
|
|
28
|
+
BASE_VALUES_REGEX = r'[A-Z0-9]'
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class CNPJError(ValueError):
|
|
32
|
+
"""
|
|
33
|
+
Exception raised for errors encountered during the parsing or validation of a CNPJ (Cadastro Nacional da Pessoa Jurídica).
|
|
34
|
+
|
|
35
|
+
Attributes:
|
|
36
|
+
message (str): Explanation of the error.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class CNPJ:
|
|
41
|
+
"""
|
|
42
|
+
Base class for handling Brazilian CNPJ (Cadastro Nacional da Pessoa Jurídica) documents.
|
|
43
|
+
|
|
44
|
+
This class provides methods for sanitizing, normalizing, validating, formatting, and generating CNPJ numbers,
|
|
45
|
+
including support for the new alphanumeric base format and the updated verification digit (DV) calculation rules.
|
|
46
|
+
|
|
47
|
+
cnpj : str | int | float | None
|
|
48
|
+
The CNPJ value to be processed. Can be a string, integer, float, or None.
|
|
49
|
+
|
|
50
|
+
Attributes
|
|
51
|
+
----------
|
|
52
|
+
firm : str
|
|
53
|
+
The main firm identifier portion of the CNPJ (first 8 digits).
|
|
54
|
+
subsidiary : str
|
|
55
|
+
The branch/subsidiary identifier portion of the CNPJ (next 4 digits).
|
|
56
|
+
dv : str
|
|
57
|
+
The verification digits (last 2 digits) of the CNPJ.
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
def __init__(self, cnpj: str | int | float | None) -> None:
|
|
61
|
+
"""
|
|
62
|
+
Initialize the instance with a CNPJ value.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
cnpj (str | int | float | None): The CNPJ value, which can be a string, integer, float, or None.
|
|
66
|
+
"""
|
|
67
|
+
self._input_cnpj = cnpj
|
|
68
|
+
self.cnpj = cnpj
|
|
69
|
+
|
|
70
|
+
def __str__(self) -> str:
|
|
71
|
+
"""
|
|
72
|
+
Returns a string representation of the object by formatting it.
|
|
73
|
+
If formatting fails or returns None, an empty string is returned.
|
|
74
|
+
|
|
75
|
+
Returns:
|
|
76
|
+
str: The formatted string representation of the object, or an empty string if formatting fails.
|
|
77
|
+
"""
|
|
78
|
+
return self.cnpj
|
|
79
|
+
|
|
80
|
+
def __repr__(self) -> str:
|
|
81
|
+
"""
|
|
82
|
+
Return a string representation of the object, displaying the class name and the value of the 'cnpj' attribute.
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
str: A string in the format "<ClassName>(cnpj='<cnpj_value>')".
|
|
86
|
+
"""
|
|
87
|
+
return f"{self.__class__.__name__}('{self.cnpj}')"
|
|
88
|
+
|
|
89
|
+
def _raise_cnpj_exception(self, message: str) -> None:
|
|
90
|
+
"""
|
|
91
|
+
Raises a CNPJError with the provided error message.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
message (str): The error message to include in the exception.
|
|
95
|
+
|
|
96
|
+
Raises:
|
|
97
|
+
CNPJError: Always raised with the given message.
|
|
98
|
+
"""
|
|
99
|
+
raise CNPJError(message)
|
|
100
|
+
|
|
101
|
+
@property
|
|
102
|
+
def firm(self) -> str:
|
|
103
|
+
"""
|
|
104
|
+
Returns the base part of the firm's CNPJ (Cadastro Nacional da Pessoa Jurídica) as a string.
|
|
105
|
+
The CNPJ is sanitized and zero-filled if necessary, then truncated to exclude the last 4 digits.
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
str: The base CNPJ string, excluding the branch identifier.
|
|
109
|
+
"""
|
|
110
|
+
if self.is_valid():
|
|
111
|
+
return self.cnpj[: CNPJ_BASE_LENGTH.default - 4]
|
|
112
|
+
return None
|
|
113
|
+
|
|
114
|
+
@property
|
|
115
|
+
def subsidiary(self) -> str:
|
|
116
|
+
"""
|
|
117
|
+
Returns the subsidiary portion of the CNPJ number as a string.
|
|
118
|
+
|
|
119
|
+
This property sanitizes the CNPJ value (optionally zero-filling it), then extracts and returns the last 4 digits, which represent the subsidiary code according to the CNPJ format.
|
|
120
|
+
|
|
121
|
+
Returns:
|
|
122
|
+
str: The 4-digit subsidiary code from the CNPJ.
|
|
123
|
+
"""
|
|
124
|
+
if self.is_valid():
|
|
125
|
+
return self.cnpj[CNPJ_BASE_LENGTH.default - 4 : CNPJ_BASE_LENGTH.default]
|
|
126
|
+
return None
|
|
127
|
+
|
|
128
|
+
@property
|
|
129
|
+
def dv(self) -> str:
|
|
130
|
+
"""
|
|
131
|
+
Returns the 'dv' (check digit) portion of the sanitized identifier string.
|
|
132
|
+
|
|
133
|
+
The method first sanitizes the identifier (optionally zero-filling it), then slices
|
|
134
|
+
the string from the position defined by `CNPJ_BASE_LENGTH.default` to extract the check digit(s).
|
|
135
|
+
|
|
136
|
+
Returns:
|
|
137
|
+
str: The check digit(s) of the sanitized identifier.
|
|
138
|
+
"""
|
|
139
|
+
if self.is_valid():
|
|
140
|
+
return self.cnpj[CNPJ_BASE_LENGTH.default :]
|
|
141
|
+
return None
|
|
142
|
+
|
|
143
|
+
def sanitize(self, *, zfill: bool = True) -> str | None:
|
|
144
|
+
"""
|
|
145
|
+
Sanitize a CNPJ string.
|
|
146
|
+
|
|
147
|
+
- Removes non-alphanumeric characters
|
|
148
|
+
- Uppercase letters
|
|
149
|
+
- If zfill=True, left-pads with zeros until length == 14
|
|
150
|
+
|
|
151
|
+
Parameters
|
|
152
|
+
----------
|
|
153
|
+
zfill : bool, default=True
|
|
154
|
+
Whether to left-pad the sanitized value to length 14.
|
|
155
|
+
|
|
156
|
+
Returns
|
|
157
|
+
-------
|
|
158
|
+
str | None
|
|
159
|
+
Sanitized CNPJ or None if input is None.
|
|
160
|
+
|
|
161
|
+
Raises
|
|
162
|
+
------
|
|
163
|
+
TypeError
|
|
164
|
+
If `cnpj` is not str|None.
|
|
165
|
+
"""
|
|
166
|
+
sanitized = self._input_cnpj
|
|
167
|
+
|
|
168
|
+
if sanitized in {None, True, False, ''}:
|
|
169
|
+
self.cnpj = None
|
|
170
|
+
|
|
171
|
+
else:
|
|
172
|
+
if isinstance(sanitized, float):
|
|
173
|
+
sanitized = int(sanitized)
|
|
174
|
+
|
|
175
|
+
if isinstance(sanitized, int):
|
|
176
|
+
sanitized = str(sanitized)
|
|
177
|
+
|
|
178
|
+
if not isinstance(sanitized, str):
|
|
179
|
+
raise TypeError('CNPJ must be a string, integer, float, or None')
|
|
180
|
+
|
|
181
|
+
sanitized = ''.join(ch for ch in sanitized.strip() if ch.isalnum()).upper()
|
|
182
|
+
|
|
183
|
+
if zfill:
|
|
184
|
+
sanitized = sanitized.zfill(CNPJ_LENGTH.default)
|
|
185
|
+
|
|
186
|
+
self.cnpj = sanitized
|
|
187
|
+
|
|
188
|
+
return self.cnpj
|
|
189
|
+
|
|
190
|
+
def normalize(self, *, zfill: bool = False, errors: str = 'raise') -> str | None:
|
|
191
|
+
"""
|
|
192
|
+
Normalize a CNPJ with structural validation only (no DV check).
|
|
193
|
+
|
|
194
|
+
Always:
|
|
195
|
+
- sanitizes input
|
|
196
|
+
- applies zfill to reach length 14
|
|
197
|
+
|
|
198
|
+
Structural rules:
|
|
199
|
+
- first 12 chars: alphanumeric
|
|
200
|
+
- last 2 chars: digits
|
|
201
|
+
|
|
202
|
+
Parameters
|
|
203
|
+
----------
|
|
204
|
+
zfill : bool, default=False
|
|
205
|
+
Whether to left-pad the sanitized value to length 14.
|
|
206
|
+
errors : {'raise', 'coerce', 'ignore'}, default='raise'
|
|
207
|
+
Behavior when parsing fails while normalizing ``self.cnpj``.
|
|
208
|
+
|
|
209
|
+
Returns
|
|
210
|
+
-------
|
|
211
|
+
str | None
|
|
212
|
+
Sanitized 14-character CNPJ, None, or original input.
|
|
213
|
+
"""
|
|
214
|
+
if errors not in {'raise', 'coerce', 'ignore'}:
|
|
215
|
+
raise ValueError("errors must be one of: 'raise', 'coerce', 'ignore'")
|
|
216
|
+
|
|
217
|
+
try:
|
|
218
|
+
self.sanitize(zfill=zfill)
|
|
219
|
+
|
|
220
|
+
if self.cnpj is None:
|
|
221
|
+
self._raise_cnpj_exception('CNPJ is None.')
|
|
222
|
+
|
|
223
|
+
if not self.is_valid(check_dv=False):
|
|
224
|
+
self._raise_cnpj_exception('CNPJ validation failed.')
|
|
225
|
+
|
|
226
|
+
except CNPJError:
|
|
227
|
+
if errors == 'coerce':
|
|
228
|
+
self.cnpj = None
|
|
229
|
+
if errors == 'raise':
|
|
230
|
+
raise
|
|
231
|
+
|
|
232
|
+
return self.cnpj
|
|
233
|
+
|
|
234
|
+
def _ascii48_value(self, ch: str) -> int:
|
|
235
|
+
"""
|
|
236
|
+
Converts a single alphanumeric character to its ASCII code minus 48.
|
|
237
|
+
|
|
238
|
+
This method is used internally for mapping characters according to the new DV rule.
|
|
239
|
+
It validates that the input is a single alphanumeric character and raises a CNPJError
|
|
240
|
+
if the input is invalid.
|
|
241
|
+
|
|
242
|
+
Args:
|
|
243
|
+
ch (str): A single character string to be converted.
|
|
244
|
+
|
|
245
|
+
Returns:
|
|
246
|
+
int: The ASCII value of the uppercase version of `ch`, minus 48.
|
|
247
|
+
|
|
248
|
+
Raises:
|
|
249
|
+
CNPJError: If `ch` is not a single character or is not alphanumeric.
|
|
250
|
+
"""
|
|
251
|
+
if len(ch) != 1:
|
|
252
|
+
self._raise_cnpj_exception(f'Invalid character {ch!r}: must be a single character')
|
|
253
|
+
|
|
254
|
+
if not ch.isalnum():
|
|
255
|
+
self._raise_cnpj_exception(f'Invalid character {ch!r}: must be alphanumeric')
|
|
256
|
+
|
|
257
|
+
return ord(ch.upper()) - 48
|
|
258
|
+
|
|
259
|
+
def _calc_dv(self, payload: str, weights: tuple[int, ...]) -> str:
|
|
260
|
+
"""
|
|
261
|
+
Calculates a single check digit (DV) using the modulo 11 algorithm.
|
|
262
|
+
Se o resto for 0 ou 1, o D2 é 0.
|
|
263
|
+
Se o resto for diferente de 0 ou 1, o D2 é 11 - resto.
|
|
264
|
+
|
|
265
|
+
Args:
|
|
266
|
+
payload (str): The input string for which the check digit is to be calculated.
|
|
267
|
+
weights (tuple[int, ...]): A tuple of integer weights to be applied to each character in the payload.
|
|
268
|
+
|
|
269
|
+
Returns:
|
|
270
|
+
str: The calculated check digit as a string. Returns '0' if the result is less than 2, otherwise returns (11 - result) as a string.
|
|
271
|
+
|
|
272
|
+
Note:
|
|
273
|
+
This is an internal method used for check digit calculation in Brazilian document validation.
|
|
274
|
+
"""
|
|
275
|
+
total = sum(self._ascii48_value(ch) * w for ch, w in zip(payload, weights, strict=True))
|
|
276
|
+
result = total % 11
|
|
277
|
+
return '0' if result < CNPJ_D2.default else str(11 - result)
|
|
278
|
+
|
|
279
|
+
def _calc_dvs_from_base(self, base: str) -> str:
|
|
280
|
+
"""
|
|
281
|
+
Calculates and returns the two check digits (DVs) for a given 12-character CNPJ base string.
|
|
282
|
+
|
|
283
|
+
Args:
|
|
284
|
+
base (str): A 12-character alphanumeric string representing the CNPJ base.
|
|
285
|
+
|
|
286
|
+
Returns:
|
|
287
|
+
str: The two calculated check digits concatenated as a string.
|
|
288
|
+
|
|
289
|
+
Raises:
|
|
290
|
+
CNPJError: If the base is not exactly 12 alphanumeric characters.
|
|
291
|
+
"""
|
|
292
|
+
if len(base) != CNPJ_BASE_LENGTH.default or not all(ch.isalnum() for ch in base):
|
|
293
|
+
self._raise_cnpj_exception('Base must be exactly 12 alphanumeric characters')
|
|
294
|
+
|
|
295
|
+
dv1 = self._calc_dv(base, WEIGHTS_DV1.default)
|
|
296
|
+
dv2 = self._calc_dv(f'{base}{dv1}', WEIGHTS_DV2.default)
|
|
297
|
+
return f'{dv1}{dv2}'
|
|
298
|
+
|
|
299
|
+
def is_valid(self, *, check_dv: bool = False) -> bool:
|
|
300
|
+
"""
|
|
301
|
+
Validate a *sanitized* CNPJ (14 chars).
|
|
302
|
+
If instantiated with a not sanitized CNPJ, call self.sanitize() first.
|
|
303
|
+
|
|
304
|
+
Parameters
|
|
305
|
+
----------
|
|
306
|
+
check_dv : bool, default=False
|
|
307
|
+
If False, only structural checks are performed.
|
|
308
|
+
If True, structural + DV check (módulo 11 with ASCII-48 mapping).
|
|
309
|
+
|
|
310
|
+
Returns
|
|
311
|
+
-------
|
|
312
|
+
bool
|
|
313
|
+
True if valid, else False.
|
|
314
|
+
"""
|
|
315
|
+
if not isinstance(self.cnpj, str) or len(self.cnpj) != CNPJ_LENGTH.default:
|
|
316
|
+
return False
|
|
317
|
+
|
|
318
|
+
base, dvs = self.cnpj[: CNPJ_BASE_LENGTH.default], self.cnpj[CNPJ_BASE_LENGTH.default :]
|
|
319
|
+
|
|
320
|
+
if not dvs.isdigit():
|
|
321
|
+
return False
|
|
322
|
+
|
|
323
|
+
if not re.fullmatch(rf'{BASE_VALUES_REGEX}{{{CNPJ_BASE_LENGTH.default}}}', base):
|
|
324
|
+
return False
|
|
325
|
+
|
|
326
|
+
if not check_dv:
|
|
327
|
+
return True
|
|
328
|
+
|
|
329
|
+
try:
|
|
330
|
+
return dvs == self._calc_dvs_from_base(base)
|
|
331
|
+
except CNPJError:
|
|
332
|
+
return False
|
|
333
|
+
|
|
334
|
+
def format(self, errors: str = 'raise') -> str | None:
|
|
335
|
+
"""
|
|
336
|
+
Format a CNPJ as 'AA.AAA.AAA/AAAA-DD'.
|
|
337
|
+
|
|
338
|
+
Delegates parsing to cls.normalize().
|
|
339
|
+
|
|
340
|
+
Parameters
|
|
341
|
+
----------
|
|
342
|
+
errors : {'raise', 'coerce', 'ignore'}, default='raise'
|
|
343
|
+
Normalization error behavior.
|
|
344
|
+
|
|
345
|
+
Returns
|
|
346
|
+
-------
|
|
347
|
+
str | None
|
|
348
|
+
Formatted CNPJ, None, or original input.
|
|
349
|
+
"""
|
|
350
|
+
self.normalize(zfill=True, errors=errors)
|
|
351
|
+
if self.cnpj is None or not isinstance(self.cnpj, str):
|
|
352
|
+
return self.cnpj
|
|
353
|
+
return f'{self.cnpj[0:2]}.{self.cnpj[2:5]}.{self.cnpj[5:8]}/{self.cnpj[8:12]}-{self.cnpj[12:]}'
|
|
354
|
+
|
|
355
|
+
@staticmethod
|
|
356
|
+
def generate_random(valid_dv: bool = True) -> str: # noqa: FBT001, FBT002
|
|
357
|
+
"""
|
|
358
|
+
Generate a random CNPJ using the new alphanumeric base format.
|
|
359
|
+
|
|
360
|
+
Parameters
|
|
361
|
+
----------
|
|
362
|
+
valid_dv : bool, default=True
|
|
363
|
+
If True, generate a DV-valid CNPJ.
|
|
364
|
+
If False, generate a structurally valid but DV-invalid CNPJ.
|
|
365
|
+
|
|
366
|
+
Returns
|
|
367
|
+
-------
|
|
368
|
+
str
|
|
369
|
+
Sanitized 14-character CNPJ.
|
|
370
|
+
"""
|
|
371
|
+
base = ''.join(secrets.choice(BASE_VALUES) for _ in range(CNPJ_BASE_LENGTH.default))
|
|
372
|
+
|
|
373
|
+
# To avoid calling cls from a staticmethod, compute using local helpers:
|
|
374
|
+
# We'll re-use the module-level logic through a lightweight inline calc.
|
|
375
|
+
def ascii48_value(ch: str) -> int:
|
|
376
|
+
return ord(ch.upper()) - 48
|
|
377
|
+
|
|
378
|
+
def calc_dv(payload: str, weights: tuple[int, ...]) -> str:
|
|
379
|
+
total = sum(ascii48_value(ch) * w for ch, w in zip(payload, weights, strict=True))
|
|
380
|
+
rest = total % 11
|
|
381
|
+
return '0' if rest < CNPJ_D2.default else str(11 - rest)
|
|
382
|
+
|
|
383
|
+
dv1 = calc_dv(base, WEIGHTS_DV1.default)
|
|
384
|
+
dv2 = calc_dv(f'{base}{dv1}', WEIGHTS_DV2.default)
|
|
385
|
+
dvs = f'{dv1}{dv2}'
|
|
386
|
+
|
|
387
|
+
if valid_dv:
|
|
388
|
+
return CNPJ(f'{base}{dvs}')
|
|
389
|
+
|
|
390
|
+
invalid_second_dv = str((int(dvs[1]) + secrets.randbelow(9) + 1) % 10)
|
|
391
|
+
return CNPJ(f'{base}{dvs[0]}{invalid_second_dv}')
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
###############################################################################
|
|
2
|
+
#
|
|
3
|
+
# (C) Copyright 2026 EVERYSK TECHNOLOGIES
|
|
4
|
+
#
|
|
5
|
+
# This is an unpublished work containing confidential and proprietary
|
|
6
|
+
# information of EVERYSK TECHNOLOGIES. Disclosure, use, or reproduction
|
|
7
|
+
# without authorization of EVERYSK TECHNOLOGIES is prohibited.
|
|
8
|
+
#
|
|
9
|
+
################################################################################
|
|
10
|
+
import pandas as pd
|
|
11
|
+
|
|
12
|
+
from everysk.sdk.brutils.cnpj import CNPJ
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@pd.api.extensions.register_series_accessor('cnpj')
|
|
16
|
+
class CNPJAccessor:
|
|
17
|
+
"""
|
|
18
|
+
A pandas accessor class for handling CNPJ (Cadastro Nacional da Pessoa Jurídica) operations on pandas Series.
|
|
19
|
+
|
|
20
|
+
Parameters
|
|
21
|
+
----------
|
|
22
|
+
pandas_obj : pandas.Series
|
|
23
|
+
The pandas Series object containing CNPJ values.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(self, pandas_obj: pd.Series) -> None:
|
|
27
|
+
"""
|
|
28
|
+
Initializes the class with a pandas Series object.
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
pandas_obj (pd.Series): The pandas Series to be associated with the instance.
|
|
32
|
+
"""
|
|
33
|
+
self._obj = pandas_obj
|
|
34
|
+
|
|
35
|
+
def sanitize(self, *, zfill: bool = True) -> pd.Series:
|
|
36
|
+
"""
|
|
37
|
+
Sanitizes each CNPJ value in the Series.
|
|
38
|
+
|
|
39
|
+
This method applies the `CNPJ.sanitize` function to each element of the Series,
|
|
40
|
+
removing any non-numeric characters and optionally zero-padding the result to 14 digits.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
zfill (bool, optional): If True, pads the sanitized CNPJ with leading zeros to ensure a length of 14 digits.
|
|
44
|
+
Defaults to True.
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
pd.Series: A Series containing the sanitized CNPJ values.
|
|
48
|
+
"""
|
|
49
|
+
return self._obj.apply(lambda x: CNPJ(x).sanitize(zfill=zfill))
|
|
50
|
+
|
|
51
|
+
def normalize(self, *, zfill: bool = False, errors: str = 'raise') -> pd.Series:
|
|
52
|
+
"""
|
|
53
|
+
Normalize CNPJ numbers in the Series.
|
|
54
|
+
|
|
55
|
+
This method applies normalization to each CNPJ value in the Series, optionally zero-filling
|
|
56
|
+
the result to 14 digits. Invalid CNPJ values can be handled according to the `errors` parameter.
|
|
57
|
+
|
|
58
|
+
Parameters
|
|
59
|
+
----------
|
|
60
|
+
zfill : bool, default False
|
|
61
|
+
If True, pad the CNPJ with leading zeros to ensure a length of 14 digits.
|
|
62
|
+
errors : {'raise', 'ignore', 'coerce'}, default 'raise'
|
|
63
|
+
Specifies how to handle invalid CNPJ values:
|
|
64
|
+
- 'raise': Raise an exception for invalid values.
|
|
65
|
+
- 'ignore': Return the original value for invalid entries.
|
|
66
|
+
- 'coerce': Replace invalid values with NaN.
|
|
67
|
+
|
|
68
|
+
Returns
|
|
69
|
+
-------
|
|
70
|
+
pd.Series
|
|
71
|
+
A pandas Series with normalized CNPJ numbers.
|
|
72
|
+
"""
|
|
73
|
+
return self._obj.apply(lambda x: CNPJ(x).normalize(zfill=zfill, errors=errors))
|
|
74
|
+
|
|
75
|
+
def is_valid(self, *, check_dv: bool = False) -> pd.Series:
|
|
76
|
+
"""
|
|
77
|
+
Check if each CNPJ in the Series is valid.
|
|
78
|
+
|
|
79
|
+
Parameters
|
|
80
|
+
----------
|
|
81
|
+
check_dv : bool, optional
|
|
82
|
+
If True, also checks the verification digits (DV) of the CNPJ. Default is False.
|
|
83
|
+
|
|
84
|
+
Returns
|
|
85
|
+
-------
|
|
86
|
+
pd.Series
|
|
87
|
+
A boolean Series indicating whether each CNPJ is valid.
|
|
88
|
+
"""
|
|
89
|
+
return self._obj.apply(lambda x: CNPJ(x).is_valid(check_dv=check_dv))
|
|
90
|
+
|
|
91
|
+
def format(self, *, errors: str = 'raise') -> pd.Series:
|
|
92
|
+
"""
|
|
93
|
+
Formats each value in the Series as a CNPJ string.
|
|
94
|
+
|
|
95
|
+
Parameters
|
|
96
|
+
----------
|
|
97
|
+
errors : str, default 'raise'
|
|
98
|
+
Specifies how to handle parsing errors. If 'raise', an exception is raised for invalid CNPJ values.
|
|
99
|
+
|
|
100
|
+
Returns
|
|
101
|
+
-------
|
|
102
|
+
pd.Series
|
|
103
|
+
A Series with each value formatted as a CNPJ string.
|
|
104
|
+
|
|
105
|
+
Examples
|
|
106
|
+
--------
|
|
107
|
+
>>> s = pd.Series(['12345678000195', '11222333000181'])
|
|
108
|
+
>>> s.cnpj.format()
|
|
109
|
+
0 12.345.678/0001-95
|
|
110
|
+
1 11.222.333/0001-81
|
|
111
|
+
dtype: object
|
|
112
|
+
"""
|
|
113
|
+
return self._obj.apply(lambda x: CNPJ(x).format(errors=errors))
|
|
114
|
+
|
|
115
|
+
def generate(self, *, valid_dv: bool = True) -> pd.Series:
|
|
116
|
+
"""
|
|
117
|
+
Generate a pandas Series of random CNPJ numbers.
|
|
118
|
+
|
|
119
|
+
Each element in the Series is a randomly generated CNPJ string.
|
|
120
|
+
The validity of the check digits (DV) can be controlled via the `valid_dv` parameter.
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
valid_dv (bool, optional): If True, generates CNPJs with valid check digits.
|
|
124
|
+
If False, generates CNPJs with invalid check digits. Defaults to True.
|
|
125
|
+
|
|
126
|
+
Returns:
|
|
127
|
+
pd.Series: A pandas Series containing randomly generated CNPJ strings.
|
|
128
|
+
"""
|
|
129
|
+
return self._obj.apply(lambda _: str(CNPJ.generate_random(valid_dv=valid_dv)))
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
###############################################################################
|
|
2
|
+
#
|
|
3
|
+
# (C) Copyright 2025 EVERYSK TECHNOLOGIES
|
|
4
|
+
#
|
|
5
|
+
# This is an unpublished work containing confidential and proprietary
|
|
6
|
+
# information of EVERYSK TECHNOLOGIES. Disclosure, use, or reproduction
|
|
7
|
+
# without authorization of EVERYSK TECHNOLOGIES is prohibited.
|
|
8
|
+
#
|
|
9
|
+
###############################################################################
|
|
10
|
+
|
|
11
|
+
###############################################################################
|
|
12
|
+
# Imports
|
|
13
|
+
###############################################################################
|
|
14
|
+
from everysk.core.string import import_from_string
|
|
15
|
+
|
|
16
|
+
###############################################################################
|
|
17
|
+
# __init__ Implementation
|
|
18
|
+
###############################################################################
|
|
19
|
+
def __getattr__(_name: str):
|
|
20
|
+
from everysk.config import settings # pylint: disable=import-outside-toplevel
|
|
21
|
+
modules = settings.EVERYSK_SDK_ENGINES_MODULES_PATH
|
|
22
|
+
|
|
23
|
+
if _name in modules:
|
|
24
|
+
return import_from_string(modules[_name])
|
|
25
|
+
|
|
26
|
+
raise AttributeError(f"cannot import name '{_name}' from everysk.sdk.engines")
|