def read_zkey(zdb, sort):
    """
    Reads z-index database file and returns the first or last key depending on sort order
    Arguments:
        zdb -- the z-index database file name
        sort -- the sort order
    """
    try:
        log_info_mssg("Connecting to " + zdb)
        db_exists = os.path.isfile(zdb)
        if db_exists == False:
            log_sig_err(zdb + " does not exist", sigevent_url)
            return "Error"
        else:
            con = sqlite3.connect(zdb, timeout=60) # 1 minute timeout
            cur = con.cursor()
            
            # Check for existing key
            cur.execute("SELECT key_str FROM ZINDEX ORDER BY key_str "+sort+" LIMIT 1;")
            try:        
                key = cur.fetchone()[0]
                log_info_mssg("Retrieved key " + key)
            except:
                return "Error"
            if con:
                con.close()              
            return key
        
    except sqlite3.Error, e:
        if con:
            con.rollback()
        mssg = "%s:" % e.args[0]
        log_sig_err(mssg, sigevent_url)
dataset/ETHPy150Open nasa-gibs/onearth/src/layer_config/bin/oe_configure_layer.py/read_zkey/original