# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

from astropy.table import Table

from . import conf
from ..utils import commons

__all__ = ['get_field_info', 'photoobj_defs', 'specobj_defs', 'crossid_defs']

# Default photometric and spectroscopic quantities to retrieve.
photoobj_defs = ['ra', 'dec', 'objid', 'run', 'rerun', 'camcol', 'field']
specobj_defs = ['z', 'plate', 'mjd', 'fiberID', 'specobjid', 'run2d',
                'instrument']
crossid_defs = ['ra', 'dec', 'psfMag_u', 'psfMagerr_u', 'psfMag_g',
                'psfMagerr_g', 'psfMag_r', 'psfMagerr_r', 'psfMag_i',
                'psfMagerr_i', 'psfMag_z', 'psfMagerr_z']


_cached_table_fields = {}
def get_field_info(tablename, sqlurl, timeout=conf.timeout):
    key = (tablename, sqlurl)

    if key in _cached_table_fields:
        info = _cached_table_fields[key]
    else:
        request_payload = {'cmd': "select * from dbo.fDocColumns('PhotoObj')", 'format': 'csv'}
        qryres = commons.send_request(sqlurl, request_payload, timeout, request_type='GET')
        info = Table.read(qryres.text, format='ascii.csv', header_start=1, data_start=2, guess=False)
        _cached_table_fields[key] = info
    return info

@@ -896,47 +896,14 @@
 key 
-in _cached_table_fields:%0A        info =
+not in
  _ca
@@ -923,22 +923,8 @@
 elds
-%5Bkey%5D%0A    else
 :%0A  
@@ -991,19 +991,32 @@
 ns('
-PhotoObj')%22
+%7B0%7D')%22.format(tablename)
 , 'f
@@ -1024,19 +1024,20 @@
 rmat': '
-csv
+json
 '%7D%0A     
@@ -1135,155 +1135,530 @@
     
-info = Table.read(qryres.text, format='ascii.csv', header_start=1, data_start=2, guess=False)%0A        _cached_table_fields%5Bkey%5D = info%0A    return info
+# we're compelled to use JSON because CSV responses are broken in SDSS -%0A        # sometimes there are improperly nested %22 characters.%0A        _cached_table_fields%5Bkey%5D = _columns_json_to_table(qryres.json())%0A    return _cached_table_fields%5Bkey%5D%0A%0Adef _columns_json_to_table(jsonobj):%0A    rows = jsonobj%5B0%5D%5B'Rows'%5D%0A    columns = dict(%5B(nm, %5B%5D) for nm in rows%5B0%5D.keys()%5D)%0A%0A    for row in rows:%0A        for k, v in row.items():%0A            columns%5Bk%5D.append(v)%0A%0A    return Table(columns)%0A%0A# below here are builtin cached queries
 %0A

fix machinery for looking up field names