# Path: openelex/base/cache.py
# class StateCache(object):
# 
#     def __init__(self, state):
#         self.state = state.lower()
#         self.path = os.path.join(PROJECT_ROOT, 'us', self.state, 'cache')
#         try:
#             os.makedirs(self.path)
#         except OSError:
#             pass
# 
#     @property
#     def abspath(self):
#         return os.path.abspath(self.path)
# 
#     def list_dir(self, datefilter='', full_path=False):
#         if full_path:
#             filtered = [os.path.join(PROJECT_ROOT, self.path, f)
#                         for f in os.listdir(self.path)
#                         if datefilter.strip() in f]
#         else:
#             files = os.listdir(self.path)
#             filtered = [path for path in files if datefilter.strip() in path]
#         filtered.sort()
#         return filtered
# 
#     def clear(self, datefilter=''):
#         files = self.list_dir(datefilter)
# 
#         for f in files:
#             try:
#                 os.remove(os.path.join(self.path, f))
#             except OSError as e:
#                 if e.errno != errno.EISDIR:
#                     raise
# 
#                 shutil.rmtree(os.path.join(self.path, f))
# 
#         remaining = self.list_dir()
#         print("%s files deleted" % len(files))
#         print("%s files still in cache" % len(remaining))
# 
# Path: openelex/tasks/utils.py
# def default_state_options(f):
#     """Decorator that adds the default options for a state command"""
#     d1 = click.option('--state', '-s', required=True,
#         help='Two-letter state-abbreviation, e.g. NY')
#     d2 = click.option('--datefilter', '-d',
#         help='A year specified as YYYY, e.g. 2012', default='')
#     return d1(d2(f))
# 
# def print_files(files):
#     for f in files:
#         print(f)
#     print("%s files found" % len(files))


import click
from openelex.base.cache import StateCache
from .utils import default_state_options, print_files
from __future__ import print_function



@click.command(name="cache.files", help="List files in state cache diretory")
@default_state_options
def files(state, datefilter=''):
    """List files in state cache diretory

    State is required. Optionally provide a date 
    filter to limit results.

    NOTE: Cache must be populated in order to load data.
    """
    cache = StateCache(state)
    files = cache.list_dir(datefilter)
    if files:
        print_files(files)
    else:
        msg = "No files found"
        if datefilter:
            msg += " using date filter: %s" % datefilter
        print(msg) 


@click.command(name='cache.clear', help="Delete files in state cache diretory")

# Path: openelex/base/cache.py
# class StateCache(object):
# 
#     def __init__(self, state):
#         self.state = state.lower()
#         self.path = os.path.join(PROJECT_ROOT, 'us', self.state, 'cache')
#         try:
#             os.makedirs(self.path)
#         except OSError:
#             pass
# 
#     @property
#     def abspath(self):
#         return os.path.abspath(self.path)
# 
#     def list_dir(self, datefilter='', full_path=False):
#         if full_path:
#             filtered = [os.path.join(PROJECT_ROOT, self.path, f)
#                         for f in os.listdir(self.path)
#                         if datefilter.strip() in f]
#         else:
#             files = os.listdir(self.path)
#             filtered = [path for path in files if datefilter.strip() in path]
#         filtered.sort()
#         return filtered
# 
#     def clear(self, datefilter=''):
#         files = self.list_dir(datefilter)
# 
#         for f in files:
#             try:
#                 os.remove(os.path.join(self.path, f))
#             except OSError as e:
#                 if e.errno != errno.EISDIR:
#                     raise
# 
#                 shutil.rmtree(os.path.join(self.path, f))
# 
#         remaining = self.list_dir()
#         print("%s files deleted" % len(files))
#         print("%s files still in cache" % len(remaining))
# 
# Path: openelex/tasks/utils.py
# def default_state_options(f):
#     """Decorator that adds the default options for a state command"""
#     d1 = click.option('--state', '-s', required=True,
#         help='Two-letter state-abbreviation, e.g. NY')
#     d2 = click.option('--datefilter', '-d',
#         help='A year specified as YYYY, e.g. 2012', default='')
#     return d1(d2(f))
# 
# def print_files(files):
#     for f in files:
#         print(f)
#     print("%s files found" % len(files))


# Path: openelex/tasks/cache.py
import click
from openelex.base.cache import StateCache
from .utils import default_state_options, print_files

from __future__ import print_function



@click.command(name="cache.files", help="List files in state cache diretory")
@default_state_options
def files(state, datefilter=''):
    """List files in state cache diretory

    State is required. Optionally provide a date 
    filter to limit results.

    NOTE: Cache must be populated in order to load data.
    """
    cache = StateCache(state)
    files = cache.list_dir(datefilter)
    if files:
        print_files(files)
    else:
        msg = "No files found"
        if datefilter:
            msg += " using date filter: %s" % datefilter
        print(msg) 


@click.command(name='cache.clear', help="Delete files in state cache diretory")

@default_state_options