# Path: ugali/utils/logger.py
# class SpecialFormatter(logging.Formatter):
#     FORMATS = {'DEFAULT'       : "%(message)s",
#                logging.WARNING : "WARNING: %(message)s",
#                logging.ERROR   : "ERROR: %(message)s"}
#     def format(self, record):
# def file_found(filename,force):


import os, sys
import re
import io
import subprocess
import numpy as np
    import http.client as httpcl
    import httplib as httpcl
        import healpy as hp
        import ugali.utils.projector
        import despydb.desdbi
    import ugali.utils.parser
from ugali.utils.logger import logger
from ugali.utils.shell import mkdir
    from ugali.utils.config import Config
    ###     logger.info("Found %i rows."%len(data))
    ###     if not len(header) or not len(data): return None
    ###     array = np.rec.array(data,names=header)
    ###     return array

    def query(self,dbase,task,query):
        logger.info("Running query...")
        cmd = "easyaccess -l %s" % (query)
        logger.info(cmd)
        return subprocess.call(cmd,shell=True)
        #ret = subprocess.check_output(cmd,shell=True,stderr=subprocess.STDOUT) 
        #if 'ERROR:' in ret:
        #    raise subprocess.CalledProcessError(1,cmd,ret)
        #return ret

    def download(self, pixel, outdir=None, force=False):
        if outdir is None: outdir = './'
        else:              mkdir(outdir)
        sqldir = mkdir(os.path.join(outdir,'sql'))
        self._setup_desdbi()

        basename = self.basename + "_%04d"%pixel['name']
        sqlname = os.path.join(sqldir,basename+'.sql')
        taskname = basename
        outfile = os.path.join(outdir,basename+".fits")
        # ADW: There should be a 'force' option here
        if os.path.exists(outfile) and not force:
            logger.warning("Found %s; skipping..."%(outfile))
            return


# Path: ugali/utils/logger.py
# class SpecialFormatter(logging.Formatter):
#     FORMATS = {'DEFAULT'       : "%(message)s",
#                logging.WARNING : "WARNING: %(message)s",
#                logging.ERROR   : "ERROR: %(message)s"}
#     def format(self, record):
# def file_found(filename,force):


# Path: ugali/preprocess/database.py
import os, sys
import re
import io
import subprocess
import numpy as np
    import http.client as httpcl
    import httplib as httpcl
        import healpy as hp
        import ugali.utils.projector
        import despydb.desdbi
    import ugali.utils.parser
from ugali.utils.logger import logger
from ugali.utils.shell import mkdir
    from ugali.utils.config import Config

    ###     logger.info("Found %i rows."%len(data))
    ###     if not len(header) or not len(data): return None
    ###     array = np.rec.array(data,names=header)
    ###     return array

    def query(self,dbase,task,query):
        logger.info("Running query...")
        cmd = "easyaccess -l %s" % (query)
        logger.info(cmd)
        return subprocess.call(cmd,shell=True)
        #ret = subprocess.check_output(cmd,shell=True,stderr=subprocess.STDOUT) 
        #if 'ERROR:' in ret:
        #    raise subprocess.CalledProcessError(1,cmd,ret)
        #return ret

    def download(self, pixel, outdir=None, force=False):
        if outdir is None: outdir = './'
        else:              mkdir(outdir)
        sqldir = mkdir(os.path.join(outdir,'sql'))
        self._setup_desdbi()

        basename = self.basename + "_%04d"%pixel['name']
        sqlname = os.path.join(sqldir,basename+'.sql')
        taskname = basename
        outfile = os.path.join(outdir,basename+".fits")
        # ADW: There should be a 'force' option here
        if os.path.exists(outfile) and not force:
            logger.warning("Found %s; skipping..."%(outfile))
            return


        logger.info("\nDownloading pixel: %(name)i (ra=%(ra_min)g:%(ra_max)g,dec=%(dec_min)g:%(dec_max)g)"%(pixel))