'Parse store.args.datadir, create store.datadirs.'

def _init_datadirs(store):

    if (store.args.datadir == []):
        store.datadirs = []
        return
    datadirs = {}
    for row in store.selectall('\n                                                SELECT    datadir_id,    dirname,    blkfile_number,    blkfile_offset,\n                                                                            chain_id\n                                                        FROM    datadir'):
        (id, dir, num, offs, chain_id) = row
        datadirs[dir] = {'id': id, 'dirname': dir, 'blkfile_number': int(num), 'blkfile_offset': int(offs), 'chain_id': (None if (chain_id is None) else int(chain_id)), 'loader': None}
    if (store.args.datadir is None):
        store.datadirs = datadirs.values()
        return
    def lookup_chain_id(name):
        row = store.selectrow('SELECT    chain_id    FROM    chain    WHERE    chain_name    =    ?', (name,))
        return (None if (row is None) else int(row[0]))
    store.datadirs = []
    for dircfg in store.args.datadir:
        loader = None
        conf = None
        if isinstance(dircfg, dict):
            dirname = dircfg.get('dirname')
            if (dirname is None):
                raise ValueError(('Missing    dirname    in    datadir    configuration:    ' + str(dircfg)))
            if (dirname in datadirs):
                d = datadirs[dirname]
                d['loader'] = dircfg.get('loader')
                d['conf'] = dircfg.get('conf')
                if ((d['chain_id'] is None) and ('chain' in dircfg)):
                    d['chain_id'] = lookup_chain_id(dircfg['chain'])
                store.datadirs.append(d)
                continue
            loader = dircfg.get('loader')
            conf = dircfg.get('conf')
            chain_id = dircfg.get('chain_id')
            if (chain_id is None):
                chain_name = dircfg.get('chain')
                chain_id = lookup_chain_id(chain_name)
                if ((chain_id is None) and (chain_name is not None)):
                    chain_id = store.new_id('chain')
                    code3 = dircfg.get('code3')
                    if (code3 is None):
                        code3 = ('000' if (chain_id > 999) else ('%03d' % (chain_id,)))
                    addr_vers = dircfg.get('address_version')
                    if (addr_vers is None):
                        addr_vers = '\x00'
                    elif isinstance(addr_vers, unicode):
                        addr_vers = addr_vers.encode('latin_1')
                    script_addr_vers = dircfg.get('script_addr_vers')
                    if (script_addr_vers is None):
                        script_addr_vers = '\x05'
                    elif isinstance(script_addr_vers, unicode):
                        script_addr_vers = script_addr_vers.encode('latin_1')
                    decimals = dircfg.get('decimals')
                    if (decimals is not None):
                        decimals = int(decimals)
                    store.sql('\n                                                                                                                INSERT    INTO    chain    (\n                                                                                                                                chain_id,    chain_name,    chain_code3,\n                                                                                                                                chain_address_version,    chain_script_addr_vers,    chain_policy,\n                                                                                                                                chain_decimals\n                                                                                                                )    VALUES    (?,    ?,    ?,    ?,    ?,    ?,    ?)', (chain_id, chain_name, code3, store.binin(addr_vers), store.binin(script_addr_vers), dircfg.get('policy', chain_name), decimals))
                    store.commit()
                    store.log.warning('Assigned    chain_id    %d    to    %s', chain_id, chain_name)
        elif (dircfg in datadirs):
            store.datadirs.append(datadirs[dircfg])
            continue
        else:
            dirname = dircfg
            chain_id = None
        d = {'id': store.new_id('datadir'), 'dirname': dirname, 'blkfile_number': 1, 'blkfile_offset': 0, 'chain_id': chain_id, 'loader': loader, 'conf': conf}
        store.datadirs.append(d)
