Ejemplo n.º 1
0
import load
import csv, struct, sys

def parse_data(struct_fmt, lines):
    try:
      return [ struct.unpack(struct_fmt, i) for i in lines ]
    except struct.error:
        sys.stderr.write('Record layout vs. record size mismatch\n')
        size = sum([ int(i) for i in struct_fmt.split('s')[:-1] ])
        return [ struct.unpack(struct_fmt, i.ljust(size)[:size]) 
          for i in lines ]

def main(args):  
    copybook = load.csv_(args.copybook.readlines(), strip_=True)[1:]
    field_lengths = [ int(i[2]) for i in copybook ]
    struct_fmt = 's'.join([ str(i) for i in field_lengths ]) + 's'
    if args.struct:
        print struct_fmt
    else:
        for record in parse_data(struct_fmt, load.lines(args.datafile)):
            print record

if __name__ == '__main__':
    from cmd_line_args import Args
    args = Args(USAGE, __version__)
    args.allow_stdin()
    args.add_files('datafile', 'copybook')
    args.parser.add_argument('-s', '--struct', action='store_true',
        help='show structure format')
    main(args.parse())
Ejemplo n.º 2
0
def parse_data(struct_fmt, lines):
    try:
        return [struct.unpack(struct_fmt, i) for i in lines]
    except struct.error:
        sys.stderr.write('Record layout vs. record size mismatch\n')
        size = sum([int(i) for i in struct_fmt.split('s')[:-1]])
        return [struct.unpack(struct_fmt, i.ljust(size)[:size]) for i in lines]


def main(args):
    copybook = load.csv_(args.copybook.readlines(), strip_=True)[1:]
    field_lengths = [int(i[2]) for i in copybook]
    struct_fmt = 's'.join([str(i) for i in field_lengths]) + 's'
    if args.struct:
        print struct_fmt
    else:
        for record in parse_data(struct_fmt, load.lines(args.datafile)):
            print record


if __name__ == '__main__':
    from cmd_line_args import Args
    args = Args(USAGE, __version__)
    args.allow_stdin()
    args.add_files('datafile', 'copybook')
    args.parser.add_argument('-s',
                             '--struct',
                             action='store_true',
                             help='show structure format')
    main(args.parse())
Ejemplo n.º 3
0
                line = ', '.join([str(i) for i in field[self.NAME:]])
            elif field[self.OCCURS] != '1':
                line = '{0[2]} OCCURS {0[0]!r} TIMES:'.format(field)
                levels.append(level)
            if line:
                sys.stdout.write(tabs + line + '\n')

    def parse(self, lines):
        lines = [i.strip() for i in lines]
        lines = [i for i in lines if i]
        lines = [i for i in lines if i[0] != '*']
        field = Field()
        self.fields = [field.parse(i, j) for i, j in enumerate(lines)]
        self.set2legal_db_names()
        self.occurs_n_times()

    def camel_case(self, name):
        return ''.join([i.title() for i in name.split('_')])


def main(args):
    Copybook().parse(args.copybook.readlines())


if __name__ == '__main__':
    from cmd_line_args import Args
    args = Args(USAGE, __version__)
    args.allow_stdin()
    args.add_files('copybook')
    main(args.parse())
Ejemplo n.º 4
0
          
    def remove_filler(self, record_num, record):
        pass

   
def main(args):
    fields = load.csv_(args.copybook, strip_="right", prune=True)
    datetime_output_fmt = FormatDateTimeOutput(
        date_fmt = '%Y-%m-%d', time_fmt = '%H:%M:%S.%f')
    data = Data(fields, args, datetime_output_fmt)
    record = True
    record_num = 1
    while record:
        line = args.datafile.readline()
        if args.debug and line:
            sys.stdout.write('%s\n' % DBL_HORIZ_LINE)
            sys.stdout.write('RECORD NUMBER: %d\n' % record_num)
            sys.stdout.write('%s%s%s' % (HORIZ_LINE, line, HORIZ_LINE))
        record = data.parse_record(record_num, line, args.debug)
        if record:
            print record
        record_num += 1      

if __name__ == '__main__':
    from cmd_line_args import Args
    args = Args(USAGE, __version__)
    args.allow_stdin()
    args.add_files('copybook', 'datafile')
    args.add_options('debug')
    main(args.parse())
Ejemplo n.º 5
0
                line = '{0[2]} OCCURS {0[0]!r} TIMES:'.format(field)
                levels.append(level)
            if line:
                sys.stdout.write(tabs + line + '\n')

    def parse(self, lines):
        lines = [ i.strip() for i in lines ]
        lines = [ i for i in lines if i ]
        lines = [ i for i in lines if i[0] != '*' ]
        field = Field()
        self.fields = [ field.parse(i, j) for i, j in enumerate(lines) ]
        self.set2legal_db_names()
        self.occurs_n_times()


    def camel_case(self, name):
        return ''.join([ i.title() for i in name.split('_') ])


def main(args):
    Copybook().parse(args.copybook.readlines())

if __name__ == '__main__':
    from cmd_line_args import Args
    args = Args(USAGE, __version__)
    args.allow_stdin()
    args.add_files('copybook')
    main(args.parse())