diff --git a/requirements.txt b/requirements.txt index e3a46cb..9a6c85a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,28 +1,2 @@ -bleach==4.1.0 -certifi==2023.7.22 -cffi==1.15.0 -charset-normalizer==2.0.7 -colorama==0.4.4 -coverage==6.0.2 -cryptography==39.0.1 -docutils==0.18 -idna==3.3 -importlib-metadata==4.8.1 -jeepney==0.7.1 -keyring==23.2.1 -packaging==21.0 -pkginfo==1.7.1 -pycparser==2.20 -Pygments==2.10.0 -pyparsing==3.0.2 -readme-renderer==30.0 -requests==2.26.0 -requests-toolbelt==0.9.1 -rfc3986==1.5.0 -SecretStorage==3.3.1 -six==1.16.0 -tqdm==4.62.3 -twine==3.4.2 -urllib3==1.26.7 -webencodings==0.5.1 -zipp==3.6.0 +pytest==7.4.4 +pytest-cov==4.1.0 diff --git a/setup.py b/setup.py index 8e0a8e4..0239f29 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,6 @@ import setuptools - -with open("README.md", "r") as fh: +with open("README.md") as fh: long_description = fh.read() setuptools.setup( diff --git a/validate_docbr/BaseDoc.py b/validate_docbr/BaseDoc.py index bd437eb..3f5f47b 100644 --- a/validate_docbr/BaseDoc.py +++ b/validate_docbr/BaseDoc.py @@ -1,10 +1,11 @@ -from abc import ABC +from abc import ABC, abstractmethod from typing import List class BaseDoc(ABC): """Classe base para todas as classes referentes a documentos.""" + @abstractmethod def validate(self, doc: str = '') -> bool: """Método para validar o documento desejado.""" pass @@ -13,18 +14,24 @@ def validate_list(self, docs: List[str]) -> List[bool]: """Método para validar uma lista de documentos desejado.""" return [self.validate(doc) for doc in docs] + @abstractmethod def generate(self, mask: bool = False) -> str: """Método para gerar um documento válido.""" pass - def generate_list(self, n: int = 1, mask: bool = False, repeat: bool = False) -> list: + def generate_list( + self, + n: int = 1, + mask: bool = False, + repeat: bool = False + ) -> list: """Gerar uma lista do mesmo documento.""" doc_list = [] if n <= 0: return doc_list - for i in range(n): + for _ in range(n): doc_list.append(self.generate(mask)) while not repeat: @@ -32,24 +39,30 @@ def generate_list(self, n: int = 1, mask: bool = False, repeat: bool = False) -> unique_values = len(doc_set) if unique_values < n: - doc_list = list(doc_set) + self.generate_list((n - unique_values), mask, repeat) + doc_list = list(doc_set) + self.generate_list( + (n - unique_values), + mask, + repeat + ) else: repeat = True return doc_list + @abstractmethod def mask(self, doc: str = '') -> str: """Mascara o documento enviado""" pass def _only_digits(self, doc: str = '') -> str: """Remove os outros caracteres que não sejam dígitos.""" - return "".join([x for x in doc if x.isdigit()]) + return ''.join([x for x in doc if x.isdigit()]) def _validate_input(self, input: str, valid_characters: List = None) -> bool: """Validar input. Caso ele possua apenas dígitos e caracteres válidos, retorna True. - Caso possua algum caractere que não seja dígito ou caractere válido, retorna False.""" + Caso possua algum caractere que não seja dígito ou caractere válido, + retorna False.""" if valid_characters is None: valid_characters = ['.', '-', '/', ' '] diff --git a/validate_docbr/CNH.py b/validate_docbr/CNH.py index 39b23b0..bb2a603 100644 --- a/validate_docbr/CNH.py +++ b/validate_docbr/CNH.py @@ -1,7 +1,8 @@ -from .BaseDoc import BaseDoc from random import sample from typing import Union +from .BaseDoc import BaseDoc + class CNH(BaseDoc): """Classe referente ao Carteira Nacional de Habilitação (CNH).""" @@ -35,7 +36,7 @@ def generate(self, mask: bool = False) -> str: def mask(self, doc: str = '') -> str: """Coloca a máscara de CNH na variável doc.""" - return "{} {} {} {}".format(doc[:3], doc[3:6], doc[6:9], doc[9:]) + return f"{doc[:3]} {doc[3:6]} {doc[6:9]} {doc[9:]}" def _generate_first_digit(self, doc: Union[str, list]) -> str: """Gerar o primeiro dígito verificador da CNH.""" diff --git a/validate_docbr/CNPJ.py b/validate_docbr/CNPJ.py index 1bac731..004679b 100644 --- a/validate_docbr/CNPJ.py +++ b/validate_docbr/CNPJ.py @@ -1,7 +1,8 @@ -from .BaseDoc import BaseDoc from random import sample from typing import Union +from .BaseDoc import BaseDoc + class CNPJ(BaseDoc): """Classe referente ao Cadastro Nacional da Pessoa Jurídica (CNPJ).""" @@ -21,11 +22,7 @@ def validate(self, doc: str = '') -> bool: if len(doc) != 14: return False - for i in range(10): - if doc.count("{}".format(i)) == 14: - return False - - return self._generate_first_digit(doc) == doc[12]\ + return self._generate_first_digit(doc) == doc[12] \ and self._generate_second_digit(doc) == doc[13] def generate(self, mask: bool = False) -> str: @@ -43,7 +40,7 @@ def generate(self, mask: bool = False) -> str: def mask(self, doc: str = '') -> str: """Coloca a máscara de CNPJ na variável doc.""" - return "{}.{}.{}/{}-{}".format(doc[:2], doc[2:5], doc[5:8], doc[8:12], doc[-2:]) + return f"{doc[:2]}.{doc[2:5]}.{doc[5:8]}/{doc[8:12]}-{doc[-2:]}" def _generate_first_digit(self, doc: Union[str, list]) -> str: """Gerar o primeiro dígito verificador do CNPJ.""" @@ -53,11 +50,7 @@ def _generate_first_digit(self, doc: Union[str, list]) -> str: sum += int(doc[i]) * self.weights_first[i] sum = sum % 11 - - if sum < 2: - sum = 0 - else: - sum = 11 - sum + sum = 0 if sum < 2 else 11 - sum return str(sum) @@ -69,10 +62,6 @@ def _generate_second_digit(self, doc: Union[str, list]) -> str: sum += int(doc[i]) * self.weights_second[i] sum = sum % 11 - - if sum < 2: - sum = 0 - else: - sum = 11 - sum + sum = 0 if sum < 2 else 11 - sum return str(sum) diff --git a/validate_docbr/CNS.py b/validate_docbr/CNS.py index 6809867..242833e 100644 --- a/validate_docbr/CNS.py +++ b/validate_docbr/CNS.py @@ -1,6 +1,7 @@ -from .BaseDoc import BaseDoc from random import sample +from .BaseDoc import BaseDoc + class CNS(BaseDoc): """Classe referente ao Cartão Nacional de Saúde (CNS).""" @@ -44,13 +45,13 @@ def generate(self, mask: bool = False) -> str: else: cns = self._generate_second_case(cns) - cns = "".join(cns) + cns = ''.join(cns) return self.mask(cns) if mask else cns def mask(self, doc: str = '') -> str: """Coloca a máscara de CPF na variável doc.""" - return "{} {} {} {}".format(doc[:3], doc[3:7], doc[7:11], doc[-4:]) + return f"{doc[:3]} {doc[3:7]} {doc[7:11]} {doc[-4:]}" def _generate_first_case(self, cns: list, generate_random=False) -> list: """Gera um CNS válido para os casos que se inicia com 1 ou 2.""" @@ -95,7 +96,8 @@ def _generate_second_case(self, cns: list) -> list: return self._change_cns(cns, 15 - diff, diff) def _change_cns(self, cns: list, i: int, val: int) -> list: - """Altera o CNS recursivamente para que atenda as especificações de validade dele.""" + """Altera o CNS recursivamente para que atenda as especificações de + validade dele.""" if val == 0: if self._check_cns_valid(cns): return cns @@ -108,7 +110,7 @@ def _change_cns(self, cns: list, i: int, val: int) -> list: i += 1 return self._change_cns(cns, i, val) - if not cns[i] == '9': + if cns[i] != '9': cns[i] = str(int(cns[i]) + 1) val -= (15 - i) else: diff --git a/validate_docbr/CPF.py b/validate_docbr/CPF.py index 9ce8e22..152137f 100644 --- a/validate_docbr/CPF.py +++ b/validate_docbr/CPF.py @@ -1,6 +1,7 @@ +from random import sample from typing import List + from .BaseDoc import BaseDoc -from random import sample class CPF(BaseDoc): @@ -41,7 +42,7 @@ def generate(self, mask: bool = False) -> str: def mask(self, doc: str = '') -> str: """Coloca a máscara de CPF na variável doc.""" - return "{}.{}.{}-{}".format(doc[:3], doc[3:6], doc[6:9], doc[-2:]) + return f"{doc[:3]}.{doc[3:6]}.{doc[6:9]}-{doc[-2:]}" def _generate_first_digit(self, doc: list) -> str: """Gerar o primeiro dígito verificador do CPF.""" diff --git a/validate_docbr/Certidao.py b/validate_docbr/Certidao.py index f64c7e2..296f21d 100644 --- a/validate_docbr/Certidao.py +++ b/validate_docbr/Certidao.py @@ -1,6 +1,7 @@ -from .BaseDoc import BaseDoc from random import sample +from .BaseDoc import BaseDoc + class Certidao(BaseDoc): """Classe referente a Certidão de Nascimento/Casamento/Óbito.""" diff --git a/validate_docbr/PIS.py b/validate_docbr/PIS.py index d896ba1..3c17ea4 100644 --- a/validate_docbr/PIS.py +++ b/validate_docbr/PIS.py @@ -1,7 +1,8 @@ -from .BaseDoc import BaseDoc from random import sample from typing import Union +from .BaseDoc import BaseDoc + class PIS(BaseDoc): """Classe referente ao PIS/NIS/PASEP/NIT.""" @@ -33,7 +34,7 @@ def generate(self, mask: bool = False) -> str: def mask(self, doc: str = '') -> str: """Coloca a máscara de PIS/NIS/PASEP/NIT na variável doc.""" - return "{}.{}.{}-{}".format(doc[:3], doc[3:8], doc[8:10], doc[10:]) + return f"{doc[:3]}.{doc[3:8]}.{doc[8:10]}-{doc[10:]}" def _generate_digit(self, doc: Union[str, list]) -> str: """Gerar o dígito verificador do PIS/NIS/PASEP/NIT.""" diff --git a/validate_docbr/RENAVAM.py b/validate_docbr/RENAVAM.py index cc6726e..805cb32 100644 --- a/validate_docbr/RENAVAM.py +++ b/validate_docbr/RENAVAM.py @@ -1,7 +1,8 @@ -from .BaseDoc import BaseDoc from random import sample from typing import Union +from .BaseDoc import BaseDoc + class RENAVAM(BaseDoc): """Classe referente ao Registro Nacional de Veículos Automotores (RENAVAM).""" @@ -33,7 +34,7 @@ def generate(self, mask: bool = False) -> str: def mask(self, doc: str = '') -> str: """Coloca a máscara de Renavam na variável doc.""" - return "{}-{}".format(doc[:10], doc[10]) + return f"{doc[:10]}-{doc[10]}" def _generate_last_digit(self, doc: Union[str, list]) -> str: """Gerar o dígito verificador do Renavam.""" diff --git a/validate_docbr/TituloEleitoral.py b/validate_docbr/TituloEleitoral.py index f7b26c4..78f187a 100644 --- a/validate_docbr/TituloEleitoral.py +++ b/validate_docbr/TituloEleitoral.py @@ -1,7 +1,8 @@ -from .BaseDoc import BaseDoc from random import sample from typing import List +from .BaseDoc import BaseDoc + class TituloEleitoral(BaseDoc): """Classe referente ao Título eleitoral""" @@ -24,9 +25,13 @@ def validate(self, doc: str = '') -> bool: return False first_check_digit = self._compute_first_check_digit(doc_digits=doc_digits) - second_check_digit = self._compute_second_check_digit(doc_digits=doc_digits, first_check_digit=first_check_digit) + second_check_digit = self._compute_second_check_digit( + doc_digits=doc_digits, + first_check_digit=first_check_digit + ) - return first_check_digit == doc_digits[-2] and second_check_digit == doc_digits[-1] + return first_check_digit == doc_digits[-2] \ + and second_check_digit == doc_digits[-1] def generate(self, mask: bool = False) -> str: """Método para gerar um título eleitoral válido.""" @@ -36,8 +41,10 @@ def generate(self, mask: bool = False) -> str: document_digits.extend(map(int, state_identifier)) first_check_digit = self._compute_first_check_digit(doc_digits=document_digits) - second_check_digit = self._compute_second_check_digit(doc_digits=document_digits, - first_check_digit=first_check_digit) + second_check_digit = self._compute_second_check_digit( + doc_digits=document_digits, + first_check_digit=first_check_digit + ) document_digits.extend([first_check_digit, second_check_digit]) document = ''.join(map(str, document_digits)) @@ -49,14 +56,17 @@ def generate(self, mask: bool = False) -> str: def mask(self, doc: str = '') -> str: """Mascara o documento enviado""" - return '{} {} {}'.format(doc[0:4], doc[4:8], doc[8:]) + return f'{doc[0:4]} {doc[4:8]} {doc[8:]}' def _compute_first_check_digit(self, doc_digits: List[int]) -> int: """Método que calcula o primeiro dígito verificador.""" doc_digits_to_consider = doc_digits[self.first_check_digit_doc_slice] terms = [ doc_digit * multiplier - for doc_digit, multiplier in zip(doc_digits_to_consider, self.first_check_digit_weights) + for doc_digit, multiplier in zip( + doc_digits_to_consider, + self.first_check_digit_weights + ) ] total = sum(terms) @@ -66,12 +76,20 @@ def _compute_first_check_digit(self, doc_digits: List[int]) -> int: return total % 11 - def _compute_second_check_digit(self, doc_digits: List[int], first_check_digit: int) -> int: + def _compute_second_check_digit( + self, + doc_digits: List[int], + first_check_digit: int + ) -> int: """Método que calcula o segundo dígito verificador.""" - doc_digits_to_consider = doc_digits[self.second_check_digit_doc_slice] + [first_check_digit] + doc_digits_to_consider = doc_digits[self.second_check_digit_doc_slice] \ + + [first_check_digit] terms = [ doc_digit * multiplier - for doc_digit, multiplier in zip(doc_digits_to_consider, self.second_check_digit_weights) + for doc_digit, multiplier in zip( + doc_digits_to_consider, + self.second_check_digit_weights + ) ] total = sum(terms) diff --git a/validate_docbr/__init__.py b/validate_docbr/__init__.py index 4919f0b..ac18fed 100644 --- a/validate_docbr/__init__.py +++ b/validate_docbr/__init__.py @@ -1,10 +1,23 @@ from .BaseDoc import BaseDoc -from .CPF import CPF -from .CNPJ import CNPJ +from .Certidao import Certidao from .CNH import CNH +from .CNPJ import CNPJ from .CNS import CNS +from .CPF import CPF +from .generic import validate_docs from .PIS import PIS -from .TituloEleitoral import TituloEleitoral -from .Certidao import Certidao from .RENAVAM import RENAVAM -from .generic import validate_docs +from .TituloEleitoral import TituloEleitoral + +__all__ = [ + 'BaseDoc', + 'CPF', + 'CNPJ', + 'CNH', + 'CNS', + 'PIS', + 'TituloEleitoral', + 'Certidao', + 'RENAVAM', + 'validate_docs', +] diff --git a/validate_docbr/generic.py b/validate_docbr/generic.py index b883b19..9dd71a7 100644 --- a/validate_docbr/generic.py +++ b/validate_docbr/generic.py @@ -1,5 +1,6 @@ import inspect from typing import List, Tuple, Type + from .BaseDoc import BaseDoc