PKqK˳ԼeepyFixedFlatFile/__init__.py"""PyFixedFlatFile""" __version__ = '0.8' from pyFixedFlatFile.pyFixedFlatFile import PyFixedFlatFilePKJ$K#nnpyFixedFlatFile/exceptions.pyclass ParamsException(Exception): """Exception raised when tp, fmt and size values are wrongs""" pass PKׂqK "pyFixedFlatFile/pyFixedFlatFile.py"""module to build fixed flat files""" from pyFixedFlatFile.specs import Spec class PyFixedFlatFile: """implements the logics to build the flat files""" def __init__(self, *args, **kwargs): self.__spec = Spec() self.nl = '\r\n' if args['NL'] == 'dos' else '\n' @property def spec(self): return self.__spec @property def data(self): """return steps from specs definition""" return self.spec.data def fmt(self, spec, registro): result = "" ident = spec['ident'] size = spec['size'] if ident == 'constant': result = str(size) # o valor que está em size é o valor da constante else: if 'fmt' in spec: resp = spec['fmt'](registro[ident]) elif ident == 'id': resp = registro[ident] if len(resp) != size: raise Exception("The value of id parameter is not equal size! Id value: {}, size value {}. the size must be {}".format(resp, size, len(resp))) elif ident == 'filler': resp = ' ' # o campo será preenchido com espaços em branco else: if ident in registro: resp = registro[ident] else: if 'default' in spec: resp = spec['default'] else: raise Exception("attribute {} not specified".format(ident)) if 'tp' in spec and spec['tp'] == 'numeric': # Coloca zero(a) a esquerda result = '{:0>{size}}'.format(int(resp), size=size) else: # alinha os dados a esquerda e preenche com espaços em branco a direita result = '{:<{size}}'.format(resp, size=size) if len(result) != size: raise Exception("The length of value returned by function is not equal the size! Value return ed: {}, size value {}. the size must be {}".format(resp, size, len(resp))) return result def eq(self, id): """Setando o identicador da linha""" self.spec.eq(id) def generate(self, registro): s = "" if 'id' in registro and registro['id'] in self.data: reg_spec = self.data[registro['id']] for spec in reg_spec: s += self.fmt(spec, registro) else: raise Exception("Id is not in attributes specification!") # melhorar essas mensagens em inglês return s def generate_all(self, registros): s = "" for registro in registros: row_str = self.generate(registro) + "{}".format(self.nl) s += row_str return s def __getattr__(self, class_name): """implementation of builder pattern that turn possible write code like this: builder = PyFixedFlatFile() builder.eq("10") builder.id(2).\ cnpj(14, fmt=lambda v: "{:>14}".format(v)).\ inscricaoEstadual(14, default='').\ """ def builder(size, **kwargs): keys = {'ident': class_name, **kwargs} self.spec.builder(size, **keys) return self return builderPK#[3KEV4pyFixedFlatFile/specs.py"""Module to build a dict with the information of the attibutes defined in specification""" from pyFixedFlatFile.exceptions import ParamsException class Spec: """create __steps dict with the data defined in specification""" def __init__(self, *args, **kwargs): self.current_id = None # identifier of the line self._spec_data = {} @property def data(self): return self._spec_data def eq(self, id): self.current_id = str(id) def builder(self, size, **kwargs): """Gera os dicionário que contém como chave o identificador da linha e os seus valores são uma lista de dicionário com as informações dos atributos:""" """Builds the dict with the attributes of the specification. This dict will be used for generate method from PyFixedFlatFile generate linha of the flat file. """ if kwargs['ident'] != 'constant' and not isinstance(size, int): raise ParamsException("Size must be a int! Error in {} attribute.".format(kwargs['ident'])) if 'tp' in kwargs and kwargs['tp'] != 'numeric': raise ParamsException("tp value must be only 'numeric'! Error in {} attribute.".format(kwargs['ident'])) if 'fmt' in kwargs and not callable(kwargs['fmt']): raise ParamsException("fmt value must be only a callable! Error in {} attribute.".format(kwargs['ident'])) specs = {k: v for k, v in kwargs.items()} try: specs['size'] = int(size) if kwargs['ident'] != 'constant' else size # preciso colocar isso dentro de um trycexcept except Exception as e: raise Exception("size attribute must be int but its value has type {}".format(type(size))) if not self.current_id: raise Exception("Id of line not especified: You must use 'eq' method of PyFixedFlatFile!") if self.current_id not in self.data: self.data[self.current_id] = [] self.data[self.current_id].append(specs) return PK*J`::)pyFixedFlatFile-0.8.dist-info/LICENSE.txtThe MIT License (MIT) Copyright (c) 2017 Anderson Marques Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.PK!H}0RR#pyFixedFlatFile-0.8.dist-info/WHEEL1 0 RZtMDtPI{w<wUnbaKM*A1ѭ g\Ic c~PK!HCm9 #&pyFixedFlatFile-0.8.dist-info/METADATAZ[o7~ TyN pwf7qbݭJ133ˏiH>WɹHE͐Ddr‹WԅL|Rɚ%wye¼ӍUљ-ea'w,TyV͢XgwD?w6?Q-vqg?aGUÿ́Lt듵thB%IӮjJiwO_8H^t=Hjhө #L D N;j/hm㼔*Q2r)S] 0x1?* ؊O^?flSQ>UTRsA@>JU)Qb?'j-]MKUh6AAd:ugJd<I 2i?rQx&-XBQ!Y K$Q\c43Xf pY.mi2bxɣBne$A^%18HÒdǧPWnQ 40Z[YܪHǴ:q&Jc$&׸*WĊdLux:W`ohP+K|DJ |Q~ΉPpW{FVcc?opY1#Ґ3U>&v!>A[O_A_E ; jOU~& X:V%K!b2]gBYnIrPR 'B7;tJrDJQȬkNx{+;wwPM&;y]xZn^}u.d*ws?YIb=~/8hnޥ$Z#DRw"EGF.-&)‡ǰ$1a„V:C͒vA}eLb-|F^#\&LJ*ѕw G{7$L(!3wҼާ\\x\p\h\`\;cRL6F$dG  *Dh8&hh$!.w?oݐApk)Lw!z`HD_X4_ʈ@5Q p:i&M%Fgm P$lJ_B͋Q97s86V۬GʚwϜU*E+`ڎՏIe@zv.m>GSW%Kקv~ot7Xx}źB<+St~0r{N>ߋ\*FbXr}Qz9GIbĄ;]Adoɐ||}`S :h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