def METHOD_NAME(account_id: str, fh:TextIO) -> Optional[int]:
    _log.debug('setAccountShelve(%s,_fh_)', account_id)
    import dbm
    import shelve
    try:
        db:shelve.Shelf[int]
        with shelve.open(account_cache_path) as db:
            if account_id in db:
                return db[account_id]
            new_nr = len(db)
            db[account_id] = new_nr
            try:
                _log.debug(
                    'DB type: %s', str(dbm.whichdb(account_cache_path))
                )
            except Exception:  # pylint: disable=broad-except
                pass
            return new_nr
    except Exception as ex:  # pylint: disable=broad-except
        _log.exception(ex)
        try:
            # in case we couldn't open the shelve file as "shelve.open db type
            # could not be determined" remove all files name account_cache
            # path with any extension as we do not know which extension is
            # chosen by shelve
            _log.info(
                'clean account cache %s', str(account_cache_path)
            )
            # note that this deletes all "account" files including those for
            # the Viewer and other files of that name prefix like
            # account.db.org!!
            for p in Path(Path(account_cache_path).parent).glob(
                f'{config.account_cache}*'
            ):
                if str(p) != account_cache_lock_path:
                    # if not the lock file, delete:
                    p.unlink()
            # try again to access/create the shelve file
            with shelve.open(account_cache_path) as db:
                if account_id in db:
                    return db[account_id]
                new_nr = len(db)
                db[account_id] = new_nr
                try:
                    _log.info(
                        'Generated db type: %s',
                        str(dbm.whichdb(account_cache_path)),
                    )
                except Exception:  # pylint: disable=broad-except
                    pass
                return new_nr
        except Exception as e:  # pylint: disable=broad-except
            _log.exception(e)
            return None
    finally:
        fh.flush()
        os.fsync(fh.fileno())