|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from ..arquivos import ArquivoDigital |
| 4 | +from . import blocos |
| 5 | +from . import registros |
| 6 | +from .blocos import Bloco0 |
| 7 | +from .blocos import Bloco5 |
| 8 | +from .blocos import Bloco9 |
| 9 | +from .registros import Registro0000 |
| 10 | +from .registros import Registro9999 |
| 11 | + |
| 12 | + |
| 13 | +class ArquivoDigital(ArquivoDigital): |
| 14 | + registro_abertura = Registro0000 |
| 15 | + registro_fechamento = Registro9999 |
| 16 | + registros = registros |
| 17 | + blocos = blocos |
| 18 | + |
| 19 | + def __init__(self): |
| 20 | + super(ArquivoDigital, self).__init__() |
| 21 | + self._blocos['0'] = Bloco0() |
| 22 | + self._blocos['5'] = Bloco5() |
| 23 | + self._blocos['9'] = Bloco9() |
| 24 | + |
| 25 | + utf = (u"0001|Texto em caracteres UTF-8: (dígrafo BR)'ção',(dígrafo " |
| 26 | + u"espanhol-enhe)'ñ',(trema)'Ü',(ordinais)'ªº',(ligamento s+z a" |
| 27 | + u"lemão)'ß'.") |
| 28 | + self.read_registro(utf) |
| 29 | + self.read_registro('|9900|0000|1') |
| 30 | + self.read_registro('|9900|0010|1') |
| 31 | + self.read_registro('|9900|5020|') |
| 32 | + |
| 33 | + def read_registro(self, line): |
| 34 | + |
| 35 | + # caso o usuario insira linha sem pip |
| 36 | + pipe = '|' if line[0] != '|' else '' |
| 37 | + line = pipe + line |
| 38 | + reg_id = line.split('|')[1] |
| 39 | + |
| 40 | + try: |
| 41 | + registro_class = \ |
| 42 | + getattr(self.__class__.registros, 'Registro' + reg_id) |
| 43 | + except AttributeError: |
| 44 | + raise RuntimeError(u"Arquivo inválido para FCI") |
| 45 | + |
| 46 | + registro = registro_class(line) |
| 47 | + if registro.__class__ == self.__class__.registro_abertura: |
| 48 | + self._registro_abertura = registro |
| 49 | + elif registro.__class__ == self.__class__.registro_fechamento: |
| 50 | + self._registro_fechamento = registro |
| 51 | + elif registro.__class__ == \ |
| 52 | + self.__class__.blocos.Bloco0.registro_abertura: |
| 53 | + self.blocos.Bloco0.abertura = registro |
| 54 | + else: |
| 55 | + bloco_id = reg_id[0] |
| 56 | + bloco = self._blocos[bloco_id] |
| 57 | + bloco.add(registro) |
| 58 | + |
| 59 | + # Contabiliza os registros 5020 |
| 60 | + if reg_id == '5020': |
| 61 | + registros_9 = self._blocos['9'].registros[3] |
| 62 | + registros_9.valores[3] = \ |
| 63 | + str((len(self._blocos['5'].registros)) - 2) |
| 64 | + |
| 65 | + def write_to(self, buffer): |
| 66 | + |
| 67 | + linha_abertura = self._registro_abertura.as_line()[1:] |
| 68 | + buffer.write(linha_abertura + u'\r\n') |
| 69 | + reg_count = 2 |
| 70 | + for key in self._blocos.keys(): |
| 71 | + bloco = self._blocos[key] |
| 72 | + reg_count += len(bloco.registros) |
| 73 | + for r in bloco.registros: |
| 74 | + a = r.as_line() |
| 75 | + a = a[1:] |
| 76 | + buffer.write(a + u'\r\n') |
| 77 | + |
| 78 | + self._registro_fechamento[2] = reg_count |
| 79 | + linha_fechamento = self._registro_fechamento.as_line()[1:] |
| 80 | + buffer.write(linha_fechamento + u'\r\n') |
| 81 | + |
| 82 | + def readfile(self, filename): |
| 83 | + |
| 84 | + with open(filename) as file: |
| 85 | + for line in [line.rstrip('\r\n') for line in file]: |
| 86 | + if (line[:4] != '9900' and line[:4] != '0001' and line[:4] != '0990'): |
| 87 | + self.read_registro(line.decode('utf-8-sig')) |
0 commit comments