import anydbm
import pprint

from .compat import json
from .cmd_base import Command, opt_depfile

class DumpDB(Command):
    """dump dependency DB"""
    doc_purpose = 'dump dependency DB'
    doc_usage = ''
    doc_description = None

    cmd_options = (opt_depfile,)

    def execute(self, opt_values, pos_args):
        dep_file = opt_values['dep_file']
        data = anydbm.open(dep_file)
        for key, value_str in data.iteritems():
            value_dict = json.loads(value_str)
            value_fmt = pprint.pformat(value_dict, indent=4, width=100)
            print "{key} -> {value}".format(key=key, value=value_fmt)

import anydbm
from whichdb import whichdb
import pprint

from .compat import json
from .exceptions import InvalidCommand
from .cmd_base import Command, opt_depfile


def dbm_iter(db):
    # try dictionary interface - ok in python2 and dumbdb
    try:
        return db.iteritems()
    except: # pragma: no cover
        pass

    # try firstkey/nextkey - ok for py3 dbm.gnu
    try: # pragma: no cover
        db.firstkey
        def iter_gdbm(db):
            k = db.firstkey()
            while k != None:
                yield k, db[k]
                k = db.nextkey(k)
        return iter_gdbm(db)
    except: # pragma: no cover
        raise InvalidCommand("It seems your DB backend doesn't support "
                             "iterating through all elements")


class DumpDB(Command):
    """dump dependency DB"""
    doc_purpose = 'dump dependency DB'
    doc_usage = ''
    doc_description = None

    cmd_options = (opt_depfile,)

    def execute(self, opt_values, pos_args):
        dep_file = opt_values['dep_file']
        db_type = whichdb(dep_file)
        print "DBM type is '%s'" % db_type
        if db_type == 'dbm.ndbm': # pragma: no cover
            raise InvalidCommand('ndbm does not support iteration of elements')
        data = anydbm.open(dep_file)
        for key, value_str in dbm_iter(data):
            value_dict = json.loads(value_str.decode('utf-8'))
            value_fmt = pprint.pformat(value_dict, indent=4, width=100)
            print "{key} -> {value}".format(key=key, value=value_fmt)

fix python3