示例#1
0
def print_info(options):
    f = options.dumpfile
    try:
        read_signature(f)
        version = read_version(f)
    except FileFormatError:
        sys.stderr.write("Invalid file format\n")
        sys.exit(1)
    else:
        sys.stdout.write('version: {}\n'.format(version))
    f.seek(0)

    dbs = {}

    def skip_db(dbnum):
        dbs.setdefault(dbnum, defaultdict(int))
        return False

    def skip_key_type(dbnum, key_type):
        dbs[dbnum][key_type] += 1
        return True

    list(parse_rdb_stream(f, skip_db=skip_db, skip_key_type=skip_key_type))

    headline = '{:>3} | {:>6} | '.format('db', 'keys')
    headline += ' | '.join('{:>6}'.format(t) for t in TYPES)
    sys.stdout.write(headline)
    sys.stdout.write('\n' + ('-' * len(headline)) + '\n')
    for db, keys in sorted(dbs.items()):
        sys.stdout.write('{:>3} | {:>6} | '.format(db, sum(keys.values())))
        sys.stdout.write(' | '.join('{:>6}'.format(keys.get(t, ''))
                                    for t in TYPES))
        sys.stdout.write('\n')
示例#2
0
def print_keys(options):
    parser = parse_rdb_stream(options.dumpfile,
                              skip_db=skip_db(options),
                              skip_key_type=skip_key_type(options),
                              skip_key=skip_key(options),
                              )
    sys.stdout.write(options.headline.format(db='db', type='type', key='key',
                     info_encoding='encoding', expire='expire'))
    sys.stdout.write('\n')
    for item in parser:
        if isinstance(item.key, bytes):
            item = item._replace(key=item.key.decode(options.encoding))
        sys.stdout.write(options.line_fmt.format(i=item))
        sys.stdout.write('\n')