def gerpgm(self): try: bookin = file(self.book).readlines() lengthin = str(calc_length(bookin)['lrecl']) col = Columns() bkout = col.columns(self.book, fmt='cbl', signal=self.signal) bookout = '' for b in bkout: bookout += b lengthout = str(calc_length(bookout)['lrecl']) regout = 'WRK-ARQOUTZD-REGISTRO' bookin = Homogenize(bookin) if int(word(bookin[0], 1)) == 1: regin = word(bookin[0], 2).replace('.', '') bookout = change({regin: regout}, bookout) lvl01bookin = '' lvl01bookout = '' else: regin = 'WRK-ARQINPPD-REGISTRO' lvl01bookin = '{:>9} {}.\n'.format('01', regin) lvl01bookout = '{:>9} {}.\n'.format('01', regout) formatout = '' for line in bookin: if 'PIC' in line: if word(line, 2) == 'FILLER': continue fld = word(line, 2) formatout += '{:>15} {:31} OF {}\n'.format( 'MOVE', fld, regin) formatout += '{:>15} {:31} OF {}\n'.format( 'TO', fld, regout) dicProg = { '@PGMID': self.programId, '@DATE': date.today().strftime('%d %b %Y').upper(), '@BOOKIN': os.path.basename(self.book).split('.')[0].upper(), '@SINAL': 'COM' if self.signal else 'SEM', '@BOOKOUT\n': bookout, '@REGIN': regin, '@REGOUT': regout, '@LVL01BKIN\n': lvl01bookin, '@LVL01BKOUT\n': lvl01bookout, '@LENGTHIN': lengthin, '@LENGTHOUT': lengthout, '@FORMATOUT\n': formatout } prog = insAster72(change(dicProg, file('legrpdzd.cbl').read())) progName = os.path.join(self.path, '{}.cbl'.format(self.programId)) progWrite = open(progName, 'w') progWrite.write(prog) progWrite.close() return True, None except: return (False, traceback.format_exc(sys.exc_info))
def gerjob(self): try: basename = os.path.basename(self.book).split('.')[0].upper() col = Columns() bookout = col.columns(self.book, fmt='cbl', signal=False) book_zonado = os.path.join(self.path, '{}_ZD.cpy'.format(basename)) with open(book_zonado, 'w') as bkzd: bkzd.writelines(bookout) lengthout = str(calc_length(bookout)['lrecl']) formatout = '' start = 1 bookin = file(self.book).readlines() bookin = Homogenize(bookin, cbl=True) for line in bookin: if 'PIC' not in line: continue splt_pic = line.split('PIC')[1] repl_pic = splt_pic.replace(' USAGE ', '').replace( 'COMP-3', '').replace('COMP', '').rstrip() ap = ' OUTREC FIELDS=(' if start == 1 else ' ' length = int( str( calc_length(line.replace(splt_pic, repl_pic))['lrecl'])) lenpd = calc_length(line)['lrecl'] pd2zd = ('PD,TO=ZD,LENGTH={:03},'.format(length) if 'COMP-3' in splt_pic else 'BI,TO=ZD,LENGTH={:03},'.format(length) if 'COMP' in splt_pic else '') formatout += '{}{:03},{:03},{}\n'.format( ap, start, lenpd, pd2zd) start += lenpd formatout = formatout[:-2] + ')\n' dicjob = { '@JOBNAME': '{:8}'.format(self.jobname), '@BOOK': basename, '@SORTIN': self.sortin, '@SORTOUT': self.sortout, '@LRECL': lengthout, '@OUTREC\n': formatout } job = change(dicjob, file('jobpk2zd.template').read()) jobName = os.path.join(self.path, '{}.jcl'.format(self.jobname)) with open(jobName, 'w') as jobWrite: jobWrite.write(job) return True, None except: return (False, traceback.format_exc(sys.exc_info))
def gericetool(self): try: header = '' start = 1 bookin = Homogenize(file(self.book).readlines(), cbl=True) for line in bookin: match = CobolPatterns.row_pattern.match(line.strip()) if not match: continue match = match.groupdict() if not match['pic']: continue col = match['name'].replace(self.prefix, '') pic_str = match['pic'] if pic_str[0] == 'S': pic_str = pic_str[1:] pic = pic_str[0] length = calc_length(line)['lrecl'] if col == 'FILLER': start += length continue usage = match['usage'] if match['usage'] else '' hdr = ('ZD' + self.decimals(pic_str) if 'COMP' not in usage and pic == '9' else 'PD' + self.decimals(pic_str) if 'COMP-3' in usage else 'BI' if 'COMP' in usage else 'CH') header += """ HEADER('{:30} ON({},{},{}) -\n""".format( col + "')", start, length, hdr) start += length dicjob = { '@TITULO': self.titulo, '@ENTRADA': self.entrada, '@PROGRAM': '{:8}'.format(self.progname), '@HEADER\n': header } job = change(dicjob, file('icetool.template').read()) jobName = os.path.join(self.path, '{}.jcl'.format(self.progname)) with open(jobName, 'w') as jobWrite: jobWrite.write(job) return True, None except: return (False, traceback.format_exc(sys.exc_info))
def gericetool(self): try: header = '' start = 1 bookin = file(self.book).readlines() bookin = Homogenize(bookin, cbl=True) for line in bookin: if not isPic(line): continue col = l672(line).split()[1].replace(self.prefix, '') splt_pic = line.split('PIC')[1].strip() pic = '9' if splt_pic[0] == 'S' else splt_pic[0] length = calc_length(line)['lrecl'] if col == 'FILLER': start += length continue hdr = ('ZD' + self.decimals(splt_pic) if 'COMP' not in splt_pic and pic == '9' else 'PD' + self.decimals(splt_pic) if 'COMP-3' in splt_pic else 'BI' if 'COMP' in splt_pic else 'CH') header += """ HEADER('{:30} ON({},{},{}) -\n""".format( col + "')", start, length, hdr) start += length dicjob = { '@TITULO': self.titulo, '@ENTRADA': self.entrada, '@PROGRAM': '{:8}'.format(self.progname), '@HEADER\n': header } job = change(dicjob, file('icetool.template').read()) jobName = os.path.join(self.path, '{}.jcl'.format(self.progname)) with open(jobName, 'w') as jobWrite: jobWrite.write(job) return True, None except: return (False, traceback.format_exc(sys.exc_info))
def calc_length(copy): if isinstance(copy, list): book = copy else: if isinstance(copy, str): book = copy.splitlines() else: book = [] lines = Homogenize(book) havecopy = filter(isCopy, lines) if havecopy: bkm = ''.join(havecopy[0].split('COPY')[1].replace('.', '').split()) msg = 'COPY {} deve ser expandido.'.format(bkm) return {'retorno': False, 'msg': msg, 'lrecl': 0} lrecl = 0 redefines = False occurs = False occurs_length = 0 level_redefines = 0 level_occurs = 0 for line in lines: match = CobolPatterns.row_pattern.match(line.strip()) if not match: continue match = match.groupdict() if not match['level']: continue level = int(match['level']) if redefines: if level > level_redefines: continue redefines = False level_redefines = 0 if match['redefines']: level_redefines = level redefines = True continue if occurs: if level > level_occurs: if match['pic']: occurs_length += FieldLength(match['pic'], match['usage']) continue lrecl += occurs_length * occurs occurs = False level_occurs = 0 if match['occurs']: level_occurs = level occurs = match['occurs'] if match['pic']: if occurs: occurs_length += FieldLength(match['pic'], match['usage']) else: lrecl += FieldLength(match['pic'], match['usage']) return {'retorno': True, 'msg': None, 'lrecl': lrecl}
def columns(self, file, fmt='json', signal=True): self.fmt = fmt basename = os.path.basename(file).split('.')[0] file = open(file).readlines() lines = Homogenize(file) addColumns = [] filler = 0 redefines = False lvlAnt = 0 for line in lines: match = CobolPatterns.row_pattern.match(line.strip()) if not match: continue match = match.groupdict() if not match['level']: continue line = line[:-1] if self.fmt != 'json': line = ' ' * 6 + line level = int(match['level']) if redefines: if level > lvlAnt: continue redefines = False if match['redefines']: lvlAnt = level redefines = True continue if not match['pic']: if self.fmt != 'json': addColumns.append(line + '.\n') continue dataname = match['name'].replace('-', '_').lower() if dataname == 'filler': filler += 1 dataname = '{}_{}_{:02}'.format(basename.lower(), dataname, filler) if self.fmt == 'json': type, length, decimals, sign = self.attr.attribute_json( match['pic'], match['usage'], match['occurs'], signal) jCol = ( '{}"field": "{}", "type": "{}", "length": {}, "decimals": {}, "sign": ' '{}{}\n').format('{', dataname, type, length, decimals, sign, '}') addColumns.append(jCol) else: lCol = line.replace(line.split('PIC')[1].strip(), match['pic']) sign = '.\n' if match['pic'][0] == 'S': if signal: sign = ' SIGN TRAILING SEPARATE.\n' else: lCol = re.sub('PIC\s+S', 'PIC ', lCol) if len(lCol) > 73 - len(sign): addColumns.append(lCol + '\n') addColumns.append('{:>72}'.format(sign)) else: addColumns.append(lCol + sign) else: addColumns.append(lCol + sign) return addColumns
def columns(self, file, fmt='json', signal=True): self.fmt = fmt basename = os.path.basename(file).split('.')[0] file = open(file).readlines() lines = Homogenize(file) addColumns = [] filler = 0 redefines = False lvlAnt = 0 for lin in lines: line = lin[:-1] if self.fmt != 'json': line = ' ' * 6 + line wrd, wrds = words(line) if not wrds[0].isdigit(): continue level = int(wrds[0]) if redefines: if level > lvlAnt: continue redefines = False if 'REDEFINES' in wrds: lvlAnt = level redefines = True continue if 'PIC' not in wrds: if self.fmt != 'json': addColumns.append(line + '.\n') continue dataname = wrds[1].lower().replace('-', '_') if dataname == 'filler': filler += 1 dataname = '{}_{}_{:02}'.format(basename.lower(), dataname, filler) picture = line.split('PIC')[1].split() pic = picture[0] usage = picture[1] if len( picture) > 1 and picture[1] != 'VALUE' else None occurs = picture[2:] if len( picture) > 2 and picture[2] == 'OCCURS' else None if self.fmt == 'json': type, length, decimals, sign = self.attr.attribute_json( pic, usage, occurs, signal) jCol = ( '{}"field": "{}", "type": "{}", "length": "{}", "decimals": "{}", "sign": ' '"{}"{}\n').format('{', dataname, type, length, decimals, sign, '}') addColumns.append(jCol) else: splt_pic = line.split('PIC')[1] repl_pic = splt_pic.replace(' USAGE ', '').replace( 'COMP-3', '').replace('COMP', '').rstrip() lCol = line.replace(splt_pic, repl_pic) sign = '.\n' if pic[0] == 'S': if signal: sign = ' SIGN TRAILING SEPARATE.\n' else: lCol = re.sub('PIC\s+S', 'PIC ', lCol) if len(lCol) > 73 - len(sign): addColumns.append(lCol + '\n') addColumns.append('{:>72}'.format(sign)) else: addColumns.append(lCol + sign) else: addColumns.append(lCol + sign) return addColumns
def calc_length(copy): if isinstance(copy, list): book = copy else: if isinstance(copy, str): book = copy.splitlines() else: book = [] lines = Homogenize(book) havecopy = filter(isCopy, lines) if havecopy: bkm = ''.join(havecopy[0].split('COPY')[1].replace('.', '').split()) msg = 'COPY {} deve ser expandido.'.format(bkm) return {'retorno': False, 'msg': msg, 'lrecl': 0} lrecl = 0 redefines = False occurs = False locc = 0 lvlred = 0 lvlocc = 0 for line in lines: line = line[:-1] wrd, wrds = words(line) if not wrds[0].isdigit(): continue level = int(wrds[0]) if redefines: if level > lvlred: continue redefines = False lvlred = 0 if 'REDEFINES' in wrds: lvlred = level redefines = True continue if occurs: if level > lvlocc: if 'PIC' in wrds: locc += lenfield(wrds[wrds.index('PIC') + 1:]) continue lrecl += locc * occurs occurs = False lvlocc = 0 if 'OCCURS' in wrds: lvlocc = level occurs = (int(nextWord('OCCURS', line)) if 'TO' not in wrds else int(nextWord('TO', line))) if 'PIC' in wrds: if occurs: locc += lenfield(wrds[wrds.index('PIC') + 1:]) else: lrecl += lenfield(wrds[wrds.index('PIC') + 1:]) return {'retorno': True, 'msg': None, 'lrecl': lrecl}