├── data ├── .gitkeep └── motivo_situacao_cadastral.csv ├── logs └── .gitkeep ├── parser ├── __init__.py ├── utils.py ├── csv_reader.py ├── importer.py └── parsers.py ├── tests ├── __init__.py ├── test-files │ ├── MUNICCSV │ ├── PAISCSV │ ├── QUALSCSV │ ├── CNAECSV │ ├── NATJUCSV │ ├── SOCIOCSV │ ├── EMPRECSV │ ├── EMPRECSVBULK │ ├── SIMPLES.CSV │ └── ESTABELE └── test_parser.py ├── tools ├── __init__.py └── log.py ├── requirements.txt ├── .gitignore ├── schema └── mysql │ ├── drop-tables.sql │ └── create-tables.sql ├── .github └── workflows │ └── workflow.yml ├── download.sh ├── mysql_import.py ├── README.md └── LICENSE /data/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /logs/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /parser/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tools/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | mysql-connector-python 2 | tqdm -------------------------------------------------------------------------------- /tests/test-files/MUNICCSV: -------------------------------------------------------------------------------- 1 | "0001";"GUAJARA-MIRIM" -------------------------------------------------------------------------------- /tests/test-files/PAISCSV: -------------------------------------------------------------------------------- 1 | "000";"COLIS POSTAUX" -------------------------------------------------------------------------------- /tests/test-files/QUALSCSV: -------------------------------------------------------------------------------- 1 | "00";"NAO INFORMADA" -------------------------------------------------------------------------------- /tests/test-files/CNAECSV: -------------------------------------------------------------------------------- 1 | "0111301";"Cultivo de arroz" -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.log 3 | data/download 4 | data/output-extract -------------------------------------------------------------------------------- /tests/test-files/NATJUCSV: -------------------------------------------------------------------------------- 1 | "0000";"NATUREZA JURIDICA NAO INFORMADA" -------------------------------------------------------------------------------- /tests/test-files/SOCIOCSV: -------------------------------------------------------------------------------- 1 | "00000000";"2";"JOHN DOE";"***111111**";"30";"20101105";"0";"***111111**";"JOHN DOE 2";"15";"3" 2 | -------------------------------------------------------------------------------- /tests/test-files/EMPRECSV: -------------------------------------------------------------------------------- 1 | "11111111";"LOREM IPSUM";"2135";"50";"1000,00";"05";"" 2 | "11111111";"LOREM IPSUM";"2135";"50";"";"";"" -------------------------------------------------------------------------------- /tests/test-files/EMPRECSVBULK: -------------------------------------------------------------------------------- 1 | "11111112";"LOREM IPSUM";"2135";"50";"1000,00";"05";"" 2 | "11111113";"LOREM IPSUM";"2135";"50";"";"";"" -------------------------------------------------------------------------------- /tests/test-files/SIMPLES.CSV: -------------------------------------------------------------------------------- 1 | "00000000";"N";"20070701";"20180201";"N";"20100101";"20180201" 2 | "00000000";"N";"00000000";"00000000";"N";"00000000";"00000000" -------------------------------------------------------------------------------- /schema/mysql/drop-tables.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS empresa, socio, estabelecimento, optante_simples, cnae, municipio, natureza_juridica, qualificacao_socio, pais, motivo_situacao_cadastral; -------------------------------------------------------------------------------- /tests/test-files/ESTABELE: -------------------------------------------------------------------------------- 1 | "00000000";"0001";"41";"1";"FANTASIA";"2";"20051103";"1";"";"";"19940530";"4712100,4712100";"4712101,4712100";"RUA";"ROBERTO DE CAMPOS BICUDO";"44";"FRENTE";"CATIAPOA";"11370470";"SP";"7121";"13";"11111111";"13";"11111111";"13";"11111111";"test@hotmail.com";"";"" 2 | -------------------------------------------------------------------------------- /.github/workflows/workflow.yml: -------------------------------------------------------------------------------- 1 | name: Python application 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | - name: Set up Python 3.9 17 | uses: actions/setup-python@v2 18 | with: 19 | python-version: 3.9 20 | - name: Install dependencies 21 | run: | 22 | python -m pip install --upgrade pip 23 | if [ -f requirements.txt ]; then pip install -r requirements.txt; fi 24 | - name: Test 25 | run: | 26 | python -m unittest 27 | -------------------------------------------------------------------------------- /download.sh: -------------------------------------------------------------------------------- 1 | FILES_URL=http://200.152.38.155/CNPJ/dados_abertos_cnpj/ 2 | LAST_MONTH=$(curl -s $FILES_URL | sed -n 's/.*href="\([^"]*\).*/\1/p' | awk 'END{print}') 3 | 4 | DOWNLOAD_URL=$FILES_URL$LAST_MONTH 5 | DOWNLOAD_DIR=data/download 6 | EXTRACT_DIR=data/output-extract 7 | 8 | if [ ! -d $DOWNLOAD_DIR ]; then 9 | echo "Creating dirs" 10 | mkdir $DOWNLOAD_DIR $EXTRACT_DIR 11 | fi 12 | 13 | if [ -z "$(ls -A $DOWNLOAD_DIR)" ]; then 14 | echo "Downloading files" 15 | wget --execute="robots = off" --mirror --convert-links --no-parent $DOWNLOAD_URL -A '*.zip' -P $DOWNLOAD_DIR -nd 16 | else 17 | echo "Skipping download, directory is not empty \n" 18 | fi 19 | 20 | if [ -z "$(ls -A $EXTRACT_DIR)" ]; then 21 | echo "Extracting files" 22 | unzip $DOWNLOAD_DIR/\*.zip -d $EXTRACT_DIR 23 | else 24 | echo "Skipping extraction, directory is not empty \n" 25 | fi 26 | -------------------------------------------------------------------------------- /parser/utils.py: -------------------------------------------------------------------------------- 1 | def parse_date(unformatted_data): 2 | if len(unformatted_data) == 8: 3 | return '-'.join(( 4 | unformatted_data[0:4], 5 | unformatted_data[4:6], 6 | unformatted_data[6:8] 7 | )) 8 | else: 9 | return None 10 | 11 | def parse_valid_date_or_none(unformatted_data): 12 | if int(unformatted_data) > 0: 13 | return parse_date(unformatted_data) 14 | else: 15 | return None 16 | 17 | def parse_int(text): 18 | if text.isdigit(): 19 | return int(text) 20 | 21 | return None 22 | 23 | def parse_float(text): 24 | try: 25 | return float(text.replace(',', '.')) 26 | except ValueError: 27 | return None 28 | 29 | def parse_cnae(cnae_text_list): 30 | if len(cnae_text_list) > 0: 31 | cnaes = cnae_text_list.split(',') 32 | 33 | return cnaes[0] 34 | 35 | return None -------------------------------------------------------------------------------- /parser/csv_reader.py: -------------------------------------------------------------------------------- 1 | import csv 2 | 3 | DELIMITER = ';' 4 | ENCODING = 'ISO-8859-1' 5 | 6 | class CsvReader: 7 | def __init__(self, filename, log=None): 8 | self.filename = filename 9 | self.log = log 10 | self._file = None 11 | 12 | def open(self): 13 | self._file = open(self.filename, 'r', encoding=ENCODING) 14 | 15 | return self._file 16 | 17 | def read(self): 18 | for line in self._file: 19 | reader = csv.reader([line.replace('\0','')], delimiter=DELIMITER) 20 | for row in reader: 21 | return row 22 | 23 | def count_lines(self, chunk_size=65536): 24 | count = 0 25 | with self.open() as csvfile: 26 | while True: 27 | chunk = csvfile.read(chunk_size) 28 | if not chunk: 29 | break 30 | count += chunk.count('\n') 31 | 32 | return count 33 | 34 | def close(self): 35 | self._file.close() 36 | -------------------------------------------------------------------------------- /mysql_import.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | from parser.parsers import generate_parsers_from_files 4 | from parser.importer import MysqlImport 5 | from tools.log import Log 6 | 7 | DEFAULT_DIRECTORY = 'data/output-extract' 8 | 9 | def parse_args(): 10 | if len(sys.argv) < 5: 11 | print('usage: mysql_import.py ') 12 | 13 | exit() 14 | 15 | args = { 16 | 'host': sys.argv[1], 17 | 'port': sys.argv[2], 18 | 'user': sys.argv[3], 19 | 'password': sys.argv[4], 20 | 'database': sys.argv[5] 21 | } 22 | 23 | if len(sys.argv) > 6: 24 | args['directory'] = sys.argv[6] 25 | else: 26 | args['directory'] = DEFAULT_DIRECTORY 27 | 28 | return args 29 | 30 | 31 | args = parse_args() 32 | log = Log() 33 | sql = MysqlImport(args['host'], args['port'], args['user'], args['password'], args['database'], log) 34 | log.info('Creating schema') 35 | sql.run_script('schema/mysql/drop-tables.sql') 36 | sql.run_script('schema/mysql/create-tables.sql') 37 | 38 | log.info('Analyzing files') 39 | parsers = generate_parsers_from_files(args['directory'], log) 40 | 41 | if len(parsers) > 0: 42 | log.info('Found', len(parsers), 'files') 43 | else: 44 | log.info('No files found.') 45 | 46 | log.info('Truncating tables') 47 | for parser in parsers: 48 | sql.truncate_table(parser.TABLE) 49 | 50 | count = 0 51 | for parser in parsers: 52 | log.info('Importing file', parser.get_name(), '-', count + 1, 'of', len(parsers)) 53 | sql.run(parser) 54 | count += 1 55 | 56 | sql.close() 57 | -------------------------------------------------------------------------------- /parser/importer.py: -------------------------------------------------------------------------------- 1 | from mysql.connector.connection import MySQLConnection 2 | from mysql.connector.errors import IntegrityError, DataError 3 | from tqdm import tqdm 4 | 5 | class SqlImport(): 6 | 7 | def build_insert(self, parser, keys): 8 | sqlKeys = ','.join(keys) 9 | sqlValues = ','.join(['%s'] * len(keys)) 10 | 11 | return 'INSERT INTO ' + parser.TABLE + '(' + sqlKeys + ') VALUES (' + sqlValues + ')' 12 | 13 | 14 | class MysqlImport(SqlImport): 15 | BATCH_SIZE = 5000 16 | 17 | def __init__(self, host, port, user, password, db, log): 18 | self.context = MySQLConnection(host=host, port=port, user=user, password=password, database=db) 19 | self.cursor = self.context.cursor() 20 | self.log = log 21 | 22 | def run(self, parser, limit=0): 23 | lines = [] 24 | keys = [] 25 | pbar = tqdm(total=parser.get_size()) 26 | count = 0 27 | 28 | while limit == 0 or count <= limit: 29 | lines = parser.parse_bulk(self.BATCH_SIZE) 30 | count += len(lines) 31 | if len(lines) == 0: 32 | break 33 | 34 | try: 35 | lines_in_tuples = list(map(lambda line: tuple(line.values()), lines)) 36 | keys = lines[0].keys() 37 | self.cursor.executemany(self.build_insert(parser, keys), lines_in_tuples) 38 | pbar.update(len(lines)) 39 | except (IntegrityError, DataError) as e: 40 | self.log.error(str(e)) 41 | 42 | lines = [] 43 | 44 | self.context.commit() 45 | 46 | def run_script(self, filepath): 47 | for line in open(filepath): 48 | self.cursor.execute(line) 49 | self.log.info('Ran script', filepath) 50 | 51 | def truncate_table(self, table): 52 | self.cursor.execute('TRUNCATE TABLE ' + table) 53 | 54 | def close(self): 55 | self.context.commit() 56 | self.cursor.close() 57 | self.context.close() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Description 2 | This tool imports the brazilian companies open data to a MySQL server. 3 | 4 | Note: Python 3.6 or higher is required 5 | 6 | # Usage 7 | 8 | ## Download 9 | 10 | This may take a while, since it downloads and extracts all files. 11 | ``` 12 | sh download.sh 13 | ``` 14 | 15 | ## Import 16 | You can import the data in your existing MySQL server, but you can also start a new one using docker: 17 | ``` 18 | docker run --name mysql-cnpj -p 3306:3306 -h localhost -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=cnpj -d mysql 19 | ``` 20 | After starting your MySQL server, run the following, replacing with your credentials: 21 | ``` 22 | python mysql_import.py 23 | ``` 24 | Example: 25 | ``` 26 | python mysql_import.py localhost 3306 root my-secret-pw cnpj 27 | ``` 28 | 29 | # Schema 30 | The following table describes the defined schema: 31 | 32 | Table name | Description 33 | ---------- | ------------- 34 | empresa | Basic company data. 35 | estabelecimento | Detailed company data, each row represents a subsidiary company or the parent company itself. 36 | socio | Basic info about the company partners. 37 | optante_simples | References companies which use Simples Nacional tax regime. 38 | cnae | List of all company activities, references `cnae_principal` and `cnae_secundaria` in `estabelecimento`. 39 | motivo_situacao_cadastral | List of detailed company status info, references `motivo_situacao_cadastral` in `estabelecimento`. 40 | municipio | List of all brazilian cities, references `endereco_codigo_municipio` in `estabelecimento`. 41 | natureza_juridica | List of all company types, references `codigo_natureza_juridica` in `empresa`. 42 | pais | List of countries. 43 | qualificacao_socio | List of partner types, references `codigo_qualificacao` in `socio`. 44 | 45 | # Query examples 46 | Get all data from a specific company: 47 | ``` 48 | SELECT * FROM 49 | estabelecimento e 50 | JOIN 51 | empresa em ON em.id = e.id_empresa 52 | WHERE 53 | e.cnpj = '00000000000191' 54 | ``` 55 | -------------------------------------------------------------------------------- /tools/log.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import sys 3 | 4 | from datetime import datetime 5 | from logging.handlers import RotatingFileHandler 6 | 7 | class Log: 8 | LOG_FILE_FORMAT = '%(asctime)s — %(levelname)s — %(message)s' 9 | LOG_FILE_MAX_SIZE = 10000000 10 | LOG_CONSOLE_FORMAT = '(%(levelname)s): %(message)s' 11 | LOG_FILE_DIR = 'logs/' 12 | 13 | def __init__(self): 14 | log_name = 'log' + datetime.now().strftime("%Y%m%d_%H%M%S") 15 | self.logger = logging.getLogger(log_name) 16 | self.logger.setLevel(logging.DEBUG) 17 | 18 | self.setup_file_handler(log_name) 19 | self.setup_console_handler() 20 | 21 | def setup_file_handler(self, log_name): 22 | file_handler = RotatingFileHandler( 23 | self.LOG_FILE_DIR + log_name + '.log', 24 | maxBytes=self.LOG_FILE_MAX_SIZE, 25 | backupCount=1 26 | ) 27 | file_handler.setFormatter(logging.Formatter(self.LOG_FILE_FORMAT)) 28 | self.logger.addHandler(file_handler) 29 | 30 | def setup_console_handler(self): 31 | console_handler = logging.StreamHandler(sys.stdout) 32 | console_handler.setFormatter(logging.Formatter(self.LOG_CONSOLE_FORMAT)) 33 | self.logger.addHandler(console_handler) 34 | 35 | def debug(self, *messages): 36 | self._log(logging.DEBUG, messages) 37 | 38 | def info(self, *messages): 39 | self._log(logging.INFO, messages) 40 | 41 | def error(self, *messages): 42 | self._log(logging.ERROR, messages) 43 | 44 | def warn(self, *messages): 45 | self._log(logging.WARN, messages) 46 | 47 | def _log(self, level, messages): 48 | messages = list(map(lambda arg: str(arg), messages)) 49 | message = ' '.join(messages) 50 | 51 | if level == logging.DEBUG: 52 | self.logger.debug(message) 53 | elif level == logging.INFO: 54 | self.logger.info(message) 55 | elif level == logging.ERROR: 56 | self.logger.error(message) 57 | elif level == logging.WARN: 58 | self.logger.warn(message) -------------------------------------------------------------------------------- /schema/mysql/create-tables.sql: -------------------------------------------------------------------------------- 1 | SET default_storage_engine=myisam; 2 | CREATE TABLE IF NOT EXISTS empresa (id char(8), razao_social varchar(150), codigo_natureza_juridica char(4), qualificacao_responsavel char(2), capital_social decimal(20,2), porte tinyint(2), PRIMARY KEY (id)); 3 | CREATE TABLE IF NOT EXISTS socio (id_empresa char(8), tipo_pessoa tinyint(1), nome varchar(150), cpf_cnpj varchar(14), codigo_qualificacao char(2), data date, cpf_representante_legal varchar(11), nome_representante_legal varchar(150), codigo_qualificacao_representante_legal char(2), INDEX(id_empresa)); 4 | CREATE TABLE IF NOT EXISTS estabelecimento (id_empresa char(8), subsidiaria char(4), codigo_verificador char(2), cnpj char(14), matriz_filial tinyint(1), fantasia varchar(55), situacao_cadastral char(2), data_situacao_cadastral date, motivo_situacao_cadastral char(2), data_abertura date, cnae_principal char(7), cnae_secundaria char(7), endereco_tipo_logradouro varchar(20), endereco_logradouro varchar(60), endereco_numero varchar(6), endereco_complemento varchar(156), endereco_bairro varchar(50), endereco_cep char(8), endereco_uf char(2), endereco_codigo_municipio char(4), telefone1_ddd char(2), telefone1_numero char(8), telefone2_ddd char(2), telefone2_numero char(8), fax_ddd char(2), fax_numero char(8), email varchar(115), INDEX(id_empresa), INDEX(cnpj)); 5 | CREATE TABLE IF NOT EXISTS optante_simples (id_empresa char(8), simples char(1), simples_inicio date, simples_fim date, simei char(1), simei_inicio date, simei_fim date, INDEX(id_empresa)); 6 | CREATE TABLE IF NOT EXISTS cnae (cnae char(7), descricao varchar(150), PRIMARY KEY (cnae)); 7 | CREATE TABLE IF NOT EXISTS municipio (codigo char(4), nome varchar(150), PRIMARY KEY (codigo)); 8 | CREATE TABLE IF NOT EXISTS natureza_juridica (codigo char(4), descricao varchar(150), PRIMARY KEY (codigo)); 9 | CREATE TABLE IF NOT EXISTS qualificacao_socio (codigo char(2), descricao varchar(150), PRIMARY KEY (codigo)); 10 | CREATE TABLE IF NOT EXISTS pais (codigo char(3), descricao varchar(60), PRIMARY KEY (codigo)); 11 | CREATE TABLE IF NOT EXISTS motivo_situacao_cadastral (codigo char(2), descricao varchar(100), PRIMARY KEY (codigo)); -------------------------------------------------------------------------------- /data/motivo_situacao_cadastral.csv: -------------------------------------------------------------------------------- 1 | 1;EXTINÇÃO POR ENCERRAMENTO LIQUIDAÇÃO VOLUNTÁRIA 2 | 2;INCORPORAÇÃO 3 | 3;FUSÃO 4 | 4;CISÃO TOTAL 5 | 5;ENCERRAMENTO DA FALÊNCIA 6 | 6;ENCERRAMENTO DA LIQUIDAÇÃO 7 | 7;ELEVAÇÃO A MATRIZ 8 | 8;TRANSPASSE 9 | 9;NÃO INÍCIO DE ATIVIDADE 10 | 10;EXTINÇÃO PELO ENCERRAMENTO DA LIQUIDAÇÃO JUDICIAL 11 | 11;ANULAÇÃO POR MULTICIPLIDADE 12 | 12;ANULAÇÃO ONLINE DE OFICIO 13 | 13;OMISSA CONTUMAZ 14 | 14;OMISSA NÃO LOCALIZADA 15 | 15;INEXISTENTE DE FATO 16 | 16;ANULAÇÃO POR VÍCIOS 17 | 17;BAIXA INICIADA E AINDA NÃO DEFERIDA 18 | 18;INTERRUPÇÃO TEMPORÁRIA DAS ATIVIDADES 19 | 19;OMISSO DE DIRPJ ATÉ 5 EXERCÍCIOS 20 | 20;EM CONDIÇÃO DE INAPTIDÃO 21 | 21;PEDIDO DE BAIXA INDEFERIDA 22 | 22;RESTABELECIMENTO COM CERTIDÃO POSITIVA COM EFEITO DE NEGATIVA 23 | 23;COM PENDÊNCIA FISCAL 24 | 24;POR EMISSÃO CERTIDÃO NEGATIVA 25 | 25;CERTIDÃO POSITIVA COM EFEITO DE NEGATIVA 26 | 26;IRREGULARIDADE DE PAGAMENTO 27 | 27;IRREGULARIDADE DE RECOLHIMENTO E EXIGIBILIDADE SUSPENSA 28 | 28;TRANSFERÊNCIA FILIAL CONDIÇÃO MATRIZ 29 | 29;AGUARDANDO CONF. DE DIRPJ/DIPJ 30 | 30;ANR - AGUARDANDO CONF. DE DIRPJ/DIPJ 31 | 31;EXTINÇÃO DA FILIAL 32 | 32;INEXISTENTE DE FATO ADE/COSAR 33 | 33;TRANSFERÊNCIA DO ÓRGÃO LOCAL A CONDIÇÃO DE FILIAL DO ÓRGÃO REGIONAL 34 | 34;ANULAÇÃO DE INSCRIÇÃO INDEVIDA 35 | 35;EMPRESA ESTRANGEIRA AGUARDANDO DOCUMENTAÇÃO 36 | 36;PRÁTICA IRREGULAR DE OPERAÇÃO DE COMERCIO EXTERIOR 37 | 37;BAIXA DE PRODUTOR RURAL 38 | 38;BAIXA DEFERIDA PELA RFB AGUARDANDO ANALISE DO CONVENENTE 39 | 39;BAIXA DEFERIDA PELA RFB E INDEFERIDA PELO CONVENENTE 40 | 40;BAIXA INDEFERIDA PELA RFB E AGUARDANDO ANALISE DO CONVENENTE 41 | 41;BAIXA INDEFERIDA PELA RFB E DEFERIDA PELO CONVENENTE 42 | 42;BAIXA INDEFERIDA PELA RFB E SEFIN, AGUARDANDO ANALISE SEFAZ 43 | 43;BAIXA DEFERIDA PELA RFB, AGUARDANDO ANALISE DA SEFAZ E INDEFERIDA PELA SEFIN 44 | 44;BAIXA DEFERIDA PELA RFB E SEFAZ, AGUARDANDO ANALISE SEFIN 45 | 45;BAIXA DEFERIDA PELA RFB, AGUARDANDO ANALISE DA SEFIN E INDEFERIDA PELA SEFAZ 46 | 46;BAIXA DEFERIDA PELA RFB E SEFAZ E INDEFERIDA PELA SEFIN 47 | 47;BAIXA DEFERIDA PELA RFB E SEFIN E INDEFERIDA PELA SEFAZ 48 | 48;BAIXA INDEFERIDA PELA RFB, AGUARDANDO ANALISE SEFAZ E DEFERIDA PELA SEFIN 49 | 49;BAIXA INDEFERIDA PELA RFB, AGUARDANDO ANALISE DA SEFAZ E INDEFERIDA PELA SEFIN 50 | 50;BAIXA INDEFERIDA PELA RFB, DEFERIDA PELA SEFAZ E AGUARDANDO ANALISE DA SEFIN 51 | 51;BAIXA INDEFERIDA PELA RFB E SEFAZ, AGUARDANDO ANALISE DA SEFIN 52 | 52;BAIXA INDEFERIDA PELA RFB, DEFERIDA PELA SEFAZ E INDEFERIDA PELA SEFIN 53 | 53;BAIXA INDEFERIDA PELA RFB E SEFAZ E DEFERIDA PELA SEFIN 54 | 54;BAIXA - TRATAMENTO DIFERENCIADO DADO AS ME E EPP (LEI COMPLEMENTAR NUMERO 123/2006) 55 | 55;DEFERIDO PELO CONVENENTE, AGUARDANDO ANALISE DA RFB 56 | 60;ARTIGO 30, VI, DA IN 748/2007 57 | 61;INDICIO INTERPOS. FRAUDULENTA 58 | 62;FALTA DE PLURALIDADE DE SOCIOS 59 | 63;OMISSÃO DE DECLARAÇÕES 60 | 64;LOCALIZAÇÃO DESCONHECIDA 61 | 66;INAPTIDÃO 62 | 67;REGISTRO CANCELADO 63 | 70;ANULAÇÃO POR NÃO CONFIRMADO ATO DE REGISTRO DO MEI NA JUNTA COMERCIAL 64 | 71;INAPTIDÃO (LEI 11.941/2009 ART.54) 65 | 72;DETERMINAÇÃO JUDICIAL 66 | 73;OMISSÃO CONTUMAZ 67 | 74;INCONSISTÊNCIA CADASTRAL 68 | 80;BAIXA REGISTRADA NA JUNTA, INDEFERIDA NA RFB 69 | -------------------------------------------------------------------------------- /tests/test_parser.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from parser.parsers import * 3 | from parser.csv_reader import CsvReader 4 | 5 | class TestParser(unittest.TestCase): 6 | 7 | def test_parse_cnpj(self): 8 | cnpj_parser = CnpjCsvParser(CsvReader('tests/test-files/EMPRECSV')) 9 | 10 | self.assertEqual({ 11 | 'id': '11111111', 12 | 'razao_social': 'LOREM IPSUM', 13 | 'codigo_natureza_juridica': '2135', 14 | 'qualificacao_responsavel': '50', 15 | 'capital_social': 1000.00, 16 | 'porte': 5 17 | }, cnpj_parser.parse_line()) 18 | 19 | self.assertEqual({ 20 | 'id': '11111111', 21 | 'razao_social': 'LOREM IPSUM', 22 | 'codigo_natureza_juridica': '2135', 23 | 'qualificacao_responsavel': '50', 24 | 'capital_social': None, 25 | 'porte': None 26 | }, cnpj_parser.parse_line()) 27 | cnpj_parser.close() 28 | 29 | def test_parse_cnpj_bulk(self): 30 | cnpj_parser = CnpjCsvParser(CsvReader('tests/test-files/EMPRECSV')) 31 | self.assertEqual(1, len(cnpj_parser.parse_bulk(2))) 32 | cnpj_parser.close() 33 | 34 | cnpj_parser = CnpjCsvParser(CsvReader('tests/test-files/EMPRECSVBULK')) 35 | self.assertEqual(2, len(cnpj_parser.parse_bulk(2))) 36 | self.assertEqual(0, len(cnpj_parser.parse_bulk(2))) 37 | cnpj_parser.close() 38 | 39 | def test_parse_socio(self): 40 | socio_parser = SocioCsvParser(CsvReader('tests/test-files/SOCIOCSV')) 41 | 42 | self.assertEqual({ 43 | 'id_empresa': '00000000', 44 | 'tipo_pessoa': 2, 45 | 'nome': 'JOHN DOE', 46 | 'cpf_cnpj': '***111111**', 47 | 'codigo_qualificacao': '30', 48 | 'data': '2010-11-05', 49 | 'cpf_representante_legal': '***111111**', 50 | 'nome_representante_legal': 'JOHN DOE 2', 51 | 'codigo_qualificacao_representante_legal': '15', 52 | }, socio_parser.parse_line()) 53 | socio_parser.close() 54 | 55 | def test_parse_estabele(self): 56 | estabele_parser = EstabeleCsvParser(CsvReader('tests/test-files/ESTABELE')) 57 | 58 | self.assertEqual({ 59 | 'id_empresa': '00000000', 60 | 'subsidiaria': '0001', 61 | 'codigo_verificador': '41', 62 | 'cnpj': '00000000000141', 63 | 'matriz_filial': 1, 64 | 'fantasia': 'FANTASIA', 65 | 'situacao_cadastral': '2', 66 | 'data_situacao_cadastral': '2005-11-03', 67 | 'motivo_situacao_cadastral': '1', 68 | 'data_abertura': '1994-05-30', 69 | 'cnae_principal': '4712100', 70 | 'cnae_secundaria': '4712101', 71 | 'endereco_tipo_logradouro': 'RUA', 72 | 'endereco_logradouro': 'ROBERTO DE CAMPOS BICUDO', 73 | 'endereco_numero': '44', 74 | 'endereco_complemento': 'FRENTE', 75 | 'endereco_bairro': 'CATIAPOA', 76 | 'endereco_cep': '11370470', 77 | 'endereco_uf': 'SP', 78 | 'endereco_codigo_municipio': '7121', 79 | 'telefone1_ddd': '13', 80 | 'telefone1_numero': '11111111', 81 | 'telefone2_ddd': '13', 82 | 'telefone2_numero': '11111111', 83 | 'fax_ddd': '13', 84 | 'fax_numero': '11111111', 85 | 'email': 'test@hotmail.com' 86 | }, estabele_parser.parse_line()) 87 | estabele_parser.close() 88 | 89 | def test_parse_optante_simples(self): 90 | optante_simples_parser = OptanteSimplesCsvParser(CsvReader('tests/test-files/SIMPLES.CSV')) 91 | 92 | self.assertEqual({ 93 | 'id_empresa': '00000000', 94 | 'simples': 'N', 95 | 'simples_inicio': '2007-07-01', 96 | 'simples_fim': '2018-02-01', 97 | 'simei': 'N', 98 | 'simei_inicio': '2010-01-01', 99 | 'simei_fim': '2018-02-01' 100 | }, optante_simples_parser.parse_line()) 101 | 102 | self.assertEqual({ 103 | 'id_empresa': '00000000', 104 | 'simples': 'N', 105 | 'simples_inicio': None, 106 | 'simples_fim': None, 107 | 'simei': 'N', 108 | 'simei_inicio': None, 109 | 'simei_fim': None 110 | }, optante_simples_parser.parse_line()) 111 | optante_simples_parser.close() 112 | 113 | def test_parse_cnae(self): 114 | cnae_parser = CnaeCsvParser(CsvReader('tests/test-files/CNAECSV')) 115 | 116 | self.assertEqual({ 117 | 'cnae': '0111301', 118 | 'descricao': 'Cultivo de arroz' 119 | }, cnae_parser.parse_line()) 120 | cnae_parser.close() 121 | 122 | 123 | def test_parse_minicipio(self): 124 | municipio_parser = MunicipioCsvParser(CsvReader('tests/test-files/MUNICCSV')) 125 | 126 | self.assertEqual({ 127 | 'codigo': '0001', 128 | 'nome': 'GUAJARA-MIRIM' 129 | }, municipio_parser.parse_line()) 130 | municipio_parser.close() 131 | 132 | def test_parse_natureza_juridica(self): 133 | natureza_juridica_parser = NaturezaJuridicaCsvParser(CsvReader('tests/test-files/NATJUCSV')) 134 | 135 | self.assertEqual({ 136 | 'codigo': '0000', 137 | 'descricao': 'NATUREZA JURIDICA NAO INFORMADA' 138 | }, natureza_juridica_parser.parse_line()) 139 | natureza_juridica_parser.close() 140 | 141 | 142 | def test_parse_qual_socio(self): 143 | qual_socio_parser = QualSocioCsvParser(CsvReader('tests/test-files/QUALSCSV')) 144 | 145 | self.assertEqual({ 146 | 'codigo': '00', 147 | 'descricao': 'NAO INFORMADA' 148 | }, qual_socio_parser.parse_line()) 149 | qual_socio_parser.close() 150 | 151 | 152 | def test_parse_pais(self): 153 | pais_parser = PaisCsvParser(CsvReader('tests/test-files/PAISCSV')) 154 | 155 | self.assertEqual({ 156 | 'codigo': '000', 157 | 'descricao': 'COLIS POSTAUX' 158 | }, pais_parser.parse_line()) 159 | pais_parser.close() -------------------------------------------------------------------------------- /parser/parsers.py: -------------------------------------------------------------------------------- 1 | from parser.utils import * 2 | import glob 3 | import abc 4 | from parser.csv_reader import CsvReader 5 | 6 | class Parser: 7 | def __init__(self, reader): 8 | self.reader = reader 9 | self.size = self.reader.count_lines() 10 | self.reader.open() 11 | 12 | def read_line(self): 13 | return self.reader.read() 14 | 15 | def get_size(self): 16 | return self.size 17 | 18 | def get_name(self): 19 | return self.reader.filename 20 | 21 | @abc.abstractmethod 22 | def parse_line(self): 23 | pass 24 | 25 | def parse_bulk(self, size): 26 | count = 0 27 | lines = [] 28 | while count < size: 29 | line = self.parse_line() 30 | if line is None: 31 | break 32 | lines.append(line) 33 | count += 1 34 | 35 | return lines 36 | 37 | def close(self): 38 | self.reader.close() 39 | 40 | 41 | class CnpjCsvParser(Parser): 42 | TABLE = 'empresa' 43 | FILE_PATTERN = '*EMPRECSV' 44 | 45 | def __init__(self, reader): 46 | Parser.__init__(self, reader) 47 | self.parsed_ids = set() 48 | 49 | def parse_line(self): 50 | row = self.read_line() 51 | return { 52 | 'id': row[0], 53 | 'razao_social': row[1], 54 | 'codigo_natureza_juridica': row[2], 55 | 'qualificacao_responsavel': row[3], 56 | 'capital_social': parse_float(row[4]), 57 | 'porte': parse_int(row[5]) 58 | } if row else None 59 | 60 | def parse_bulk(self, size): 61 | count = 0 62 | lines = [] 63 | while count < size: 64 | line = self.parse_line() 65 | if line is None: 66 | break 67 | 68 | if line['id'] in self.parsed_ids: 69 | continue 70 | 71 | lines.append(line) 72 | self.parsed_ids.add(line['id']) 73 | count += 1 74 | 75 | return lines 76 | 77 | 78 | class SocioCsvParser(Parser): 79 | TABLE = 'socio' 80 | FILE_PATTERN = '*SOCIOCSV' 81 | 82 | def parse_line(self): 83 | row = self.read_line() 84 | return { 85 | 'id_empresa': row[0], 86 | 'tipo_pessoa': parse_int(row[1]), 87 | 'nome': row[2], 88 | 'cpf_cnpj': row[3], 89 | 'codigo_qualificacao': row[4], 90 | 'data': parse_date(row[5]), 91 | 'cpf_representante_legal': row[7], 92 | 'nome_representante_legal': row[8], 93 | 'codigo_qualificacao_representante_legal': row[9], 94 | } if row else None 95 | 96 | class EstabeleCsvParser(Parser): 97 | TABLE = 'estabelecimento' 98 | FILE_PATTERN = '*ESTABELE' 99 | 100 | def parse_line(self): 101 | row = self.read_line() 102 | return { 103 | 'id_empresa': row[0], 104 | 'subsidiaria': row[1], 105 | 'codigo_verificador': row[2], 106 | 'cnpj': row[0] + row[1] + row[2], 107 | 'matriz_filial': parse_int(row[3]), 108 | 'fantasia': row[4], 109 | 'situacao_cadastral': row[5], 110 | 'data_situacao_cadastral': parse_date(row[6]), 111 | 'motivo_situacao_cadastral': row[7], 112 | 'data_abertura': parse_date(row[10]), 113 | 'cnae_principal': parse_cnae(row[11]), 114 | 'cnae_secundaria': parse_cnae(row[12]), 115 | 'endereco_tipo_logradouro': row[13], 116 | 'endereco_logradouro': row[14], 117 | 'endereco_numero': row[15], 118 | 'endereco_complemento': row[16], 119 | 'endereco_bairro': row[17], 120 | 'endereco_cep': row[18], 121 | 'endereco_uf': row[19], 122 | 'endereco_codigo_municipio': row[20], 123 | 'telefone1_ddd': row[21][-2:], 124 | 'telefone1_numero': row[22], 125 | 'telefone2_ddd': row[23][-2:], 126 | 'telefone2_numero': row[24], 127 | 'fax_ddd': row[25][-2:], 128 | 'fax_numero': row[26], 129 | 'email': row[27] 130 | } if row else None 131 | 132 | class OptanteSimplesCsvParser(Parser): 133 | TABLE = 'optante_simples' 134 | FILE_PATTERN = '*SIMPLES.CSV*' 135 | 136 | def parse_line(self): 137 | row = self.read_line() 138 | return { 139 | 'id_empresa': row[0], 140 | 'simples': row[1], 141 | 'simples_inicio': parse_valid_date_or_none(row[2]), 142 | 'simples_fim': parse_valid_date_or_none(row[3]), 143 | 'simei': row[4], 144 | 'simei_inicio': parse_valid_date_or_none(row[5]), 145 | 'simei_fim': parse_valid_date_or_none(row[6]) 146 | } if row else None 147 | 148 | class CnaeCsvParser(Parser): 149 | TABLE = 'cnae' 150 | FILE_PATTERN = '*CNAECSV' 151 | 152 | def parse_line(self): 153 | row = self.read_line() 154 | return { 155 | 'cnae': row[0], 156 | 'descricao': row[1] 157 | } if row else None 158 | 159 | class MunicipioCsvParser(Parser): 160 | TABLE = 'municipio' 161 | FILE_PATTERN = '*MUNICCSV' 162 | 163 | def parse_line(self): 164 | row = self.read_line() 165 | return { 166 | 'codigo': row[0], 167 | 'nome': row[1] 168 | } if row else None 169 | 170 | class NaturezaJuridicaCsvParser(Parser): 171 | TABLE = 'natureza_juridica' 172 | FILE_PATTERN = '*NATJUCSV' 173 | 174 | def parse_line(self): 175 | row = self.read_line() 176 | return { 177 | 'codigo': row[0], 178 | 'descricao': row[1] 179 | } if row else None 180 | 181 | class QualSocioCsvParser(Parser): 182 | TABLE = 'qualificacao_socio' 183 | FILE_PATTERN = '*QUALSCSV' 184 | 185 | def parse_line(self): 186 | row = self.read_line() 187 | return { 188 | 'codigo': row[0], 189 | 'descricao': row[1] 190 | } if row else None 191 | 192 | class PaisCsvParser(Parser): 193 | TABLE = 'pais' 194 | FILE_PATTERN = '*PAISCSV' 195 | 196 | def parse_line(self): 197 | row = self.read_line() 198 | return { 199 | 'codigo': row[0], 200 | 'descricao': row[1] 201 | } if row else None 202 | 203 | class MotivoSituacaoCadastralCsvParser(Parser): 204 | TABLE = 'motivo_situacao_cadastral' 205 | 206 | def parse_line(self): 207 | row = self.read_line() 208 | return { 209 | 'codigo': row[0], 210 | 'descricao': row[1] 211 | } if row else None 212 | 213 | def generate_parsers_from_files(directory, log): 214 | parsers = [ 215 | CnpjCsvParser, 216 | SocioCsvParser, 217 | EstabeleCsvParser, 218 | OptanteSimplesCsvParser, 219 | CnaeCsvParser, 220 | MunicipioCsvParser, 221 | NaturezaJuridicaCsvParser, 222 | QualSocioCsvParser, 223 | PaisCsvParser 224 | ] 225 | parser_instances = [MotivoSituacaoCadastralCsvParser(CsvReader('data/motivo_situacao_cadastral.csv', log))] 226 | 227 | for parser in parsers: 228 | files_from_pattern = glob.glob(directory + '/' + parser.FILE_PATTERN) 229 | parser_instances += map(lambda filepath: parser(CsvReader(filepath)), files_from_pattern) 230 | 231 | return parser_instances 232 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | 474 | Copyright (C) 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 489 | USA 490 | 491 | Also add information on how to contact you by electronic and paper mail. 492 | 493 | You should also get your employer (if you work as a programmer) or your 494 | school, if any, to sign a "copyright disclaimer" for the library, if 495 | necessary. Here is a sample; alter the names: 496 | 497 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 498 | library `Frob' (a library for tweaking knobs) written by James Random 499 | Hacker. 500 | 501 | , 1 April 1990 502 | Ty Coon, President of Vice 503 | 504 | That's all there is to it! 505 | --------------------------------------------------------------------------------