# Path: SAGA/database/core.py
# class DownloadableBase(ABC):
# class FileObject(DownloadableBase):
# class CsvTable(FileObject):
# class FastCsvTable(FileObject):
# class EcsvTable(FileObject):
# class GoogleSheets(FastCsvTable):
# class FitsTableGeneric(FileObject):
# class FitsTable(FileObject):
# class NumpyBinary(FileObject):
# class DataObject(object):
#     def download_as_file(self, file_path, overwrite=False, compress=False):
#     def __init__(self, path=None, **kwargs):
#     def _gz_fallback(file_path):
#     def read(self):
#     def write(self, table, **kwargs):
#     def download_as_file(self, file_path, overwrite=False, compress=False):
#     def isfile(self):
#     def __init__(self, key, gid, **kwargs):
#     def read(self):
#     def write(self, table, **kwargs):
#     def read(self):
#     def write(self, table, **kwargs):
#     def read(self):
#     def write(self, table, **kwargs):
#     def __init__(self, remote, local=None, cache_in_memory=False, use_local_first=False):
#     def local(self):
#     def local(self, value):
#     def read(self, reload=False, **kwargs):
#     def write(self, table, dest=None, overwrite=False, **kwargs):
#     def download(self, local_file_path=None, overwrite=False, compress=False, set_as_local=True):
#     def _copy_table(table):
#     def clear_cache(self):
#     def retrive_cache(self):
#     def store_cache(self, table):
#     def path(self):
#     def isfile(self):


import gzip
import os
import time
import requests
from io import BytesIO
from urllib.parse import urlencode
from astropy.table import Table
from .core import DownloadableBase, FastCsvTable, makedirs_if_needed


class HyperledaQuery(FastCsvTable):
    _read_default_kwargs = dict(format="ascii.fast_csv", comment="#")
    _default_fields = "".join(
        """pgc,objname,objtype,al1950,de1950,al2000,de2000,l2,b2,sgl,sgb,f_astrom,
    type,bar,ring,multiple,compactness,t,e_t,agnclass,logd25,e_logd25,logr25,
    e_logr25,pa,brief,e_brief,ut,e_ut,bt,e_bt,vt,e_vt,it,e_it,kt,e_kt,m21,
    e_m21,mfir,ube,bve,vmaxg,e_vmaxg,vmaxs,e_vmaxs,vdis,e_vdis,vrad,e_vrad,
    vopt,e_vopt,v,e_v,ag,ai,incl,a21,logdc,btc,itc,ubtc,bvtc,bri25,vrot,
    e_vrot,mg2,e_mg2,m21c,hic,vlg,vgsr,vvir,v3k,modz,e_modz,mod0,e_mod0,
    modbest,e_modbest,mabs,e_mabs""".split()
    )
    _default_conditions = ""

    def __init__(self, conditions=None, fields=None, **kwargs):
        path = "http://leda.univ-lyon1.fr/fG.cgi?n=meandata&c=o&of=1,leda,simbad&nra=l&nakd=1&" + urlencode(
            {
                "d": self._default_fields if fields is None else fields,
                "sql": self._default_conditions if conditions is None else conditions,
                "ob": "pgc",
                "a": "csv[,]",
            }
        )
        super().__init__(path, **kwargs)

    def write(self, table, **kwargs):
        raise AttributeError("`HyperledaQuery` do not have write method.")



# Path: SAGA/database/core.py
# class DownloadableBase(ABC):
# class FileObject(DownloadableBase):
# class CsvTable(FileObject):
# class FastCsvTable(FileObject):
# class EcsvTable(FileObject):
# class GoogleSheets(FastCsvTable):
# class FitsTableGeneric(FileObject):
# class FitsTable(FileObject):
# class NumpyBinary(FileObject):
# class DataObject(object):
#     def download_as_file(self, file_path, overwrite=False, compress=False):
#     def __init__(self, path=None, **kwargs):
#     def _gz_fallback(file_path):
#     def read(self):
#     def write(self, table, **kwargs):
#     def download_as_file(self, file_path, overwrite=False, compress=False):
#     def isfile(self):
#     def __init__(self, key, gid, **kwargs):
#     def read(self):
#     def write(self, table, **kwargs):
#     def read(self):
#     def write(self, table, **kwargs):
#     def read(self):
#     def write(self, table, **kwargs):
#     def __init__(self, remote, local=None, cache_in_memory=False, use_local_first=False):
#     def local(self):
#     def local(self, value):
#     def read(self, reload=False, **kwargs):
#     def write(self, table, dest=None, overwrite=False, **kwargs):
#     def download(self, local_file_path=None, overwrite=False, compress=False, set_as_local=True):
#     def _copy_table(table):
#     def clear_cache(self):
#     def retrive_cache(self):
#     def store_cache(self, table):
#     def path(self):
#     def isfile(self):


# Path: SAGA/database/external_masterlist.py
import gzip
import os
import time
import requests
from io import BytesIO
from urllib.parse import urlencode
from astropy.table import Table
from .core import DownloadableBase, FastCsvTable, makedirs_if_needed



class HyperledaQuery(FastCsvTable):
    _read_default_kwargs = dict(format="ascii.fast_csv", comment="#")
    _default_fields = "".join(
        """pgc,objname,objtype,al1950,de1950,al2000,de2000,l2,b2,sgl,sgb,f_astrom,
    type,bar,ring,multiple,compactness,t,e_t,agnclass,logd25,e_logd25,logr25,
    e_logr25,pa,brief,e_brief,ut,e_ut,bt,e_bt,vt,e_vt,it,e_it,kt,e_kt,m21,
    e_m21,mfir,ube,bve,vmaxg,e_vmaxg,vmaxs,e_vmaxs,vdis,e_vdis,vrad,e_vrad,
    vopt,e_vopt,v,e_v,ag,ai,incl,a21,logdc,btc,itc,ubtc,bvtc,bri25,vrot,
    e_vrot,mg2,e_mg2,m21c,hic,vlg,vgsr,vvir,v3k,modz,e_modz,mod0,e_mod0,
    modbest,e_modbest,mabs,e_mabs""".split()
    )
    _default_conditions = ""

    def __init__(self, conditions=None, fields=None, **kwargs):
        path = "http://leda.univ-lyon1.fr/fG.cgi?n=meandata&c=o&of=1,leda,simbad&nra=l&nakd=1&" + urlencode(
            {
                "d": self._default_fields if fields is None else fields,
                "sql": self._default_conditions if conditions is None else conditions,
                "ob": "pgc",
                "a": "csv[,]",
            }
        )
        super().__init__(path, **kwargs)

    def write(self, table, **kwargs):
        raise AttributeError("`HyperledaQuery` do not have write method.")



class EddQuery(DownloadableBase):