# Path: openelex/base/datasource.py
# MAPPING_FIELDNAMES = [
#     'election',
#     'raw_url',
#     'generated_filename',
#     'pre_processed_url',
#     'ocd_id',
#     'name',
# ]
# 
# 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 load_module(state, mod_list=[]):
#     """Dynamically load modules for states
# 
#     USAGE
# 
#        mod = load_module(state, ['datasource', 'loader'])
#        mod.datasource.Datasource()
# 
#     """
#     return __import__('openelex.us.%s' % state.lower(), fromlist=mod_list)


import csv
import inspect
import sys
import click
from pprint import pprint
from openelex.base.datasource import MAPPING_FIELDNAMES
from .utils import default_state_options, load_module

def csv_results(results):
    fieldname_set = set()
    # Since mappings can have extra fields needed  by a particular state,
    # iterate through the items and record all seen fields.
    for r in results:
        for f in list(r.keys()):
            fieldname_set.add(f)
    # Put known fieldnames first in CSV header output
    fieldnames = [] 
    fieldnames.extend(MAPPING_FIELDNAMES)
    fieldnames.extend(fieldname_set.difference(set(MAPPING_FIELDNAMES)))
    writer = csv.DictWriter(sys.stdout, fieldnames=fieldnames)
    writer.writeheader()
    for r in results:
        writer.writerow(r)

@click.command(name='datasource.target_urls', help="List source data urls for a state")
@default_state_options
def target_urls(state, datefilter=''):
    """
    List source data urls for a state.

    State is required. Optionally provide 'datefilter' to limit  results.
    """
    func_name = inspect.stack()[0][3]
    results = handle_task(func_name, state, datefilter)
    pprint_results(func_name, results)

@click.command(name='datasource.mappings', help="List metadata mappings for a state")

# Path: openelex/base/datasource.py
# MAPPING_FIELDNAMES = [
#     'election',
#     'raw_url',
#     'generated_filename',
#     'pre_processed_url',
#     'ocd_id',
#     'name',
# ]
# 
# 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 load_module(state, mod_list=[]):
#     """Dynamically load modules for states
# 
#     USAGE
# 
#        mod = load_module(state, ['datasource', 'loader'])
#        mod.datasource.Datasource()
# 
#     """
#     return __import__('openelex.us.%s' % state.lower(), fromlist=mod_list)


# Path: openelex/tasks/datasource.py
import csv
import inspect
import sys
import click
from pprint import pprint
from openelex.base.datasource import MAPPING_FIELDNAMES
from .utils import default_state_options, load_module


def csv_results(results):
    fieldname_set = set()
    # Since mappings can have extra fields needed  by a particular state,
    # iterate through the items and record all seen fields.
    for r in results:
        for f in list(r.keys()):
            fieldname_set.add(f)
    # Put known fieldnames first in CSV header output
    fieldnames = [] 
    fieldnames.extend(MAPPING_FIELDNAMES)
    fieldnames.extend(fieldname_set.difference(set(MAPPING_FIELDNAMES)))
    writer = csv.DictWriter(sys.stdout, fieldnames=fieldnames)
    writer.writeheader()
    for r in results:
        writer.writerow(r)

@click.command(name='datasource.target_urls', help="List source data urls for a state")
@default_state_options
def target_urls(state, datefilter=''):
    """
    List source data urls for a state.

    State is required. Optionally provide 'datefilter' to limit  results.
    """
    func_name = inspect.stack()[0][3]
    results = handle_task(func_name, state, datefilter)
    pprint_results(func_name, results)

@click.command(name='datasource.mappings', help="List metadata mappings for a state")

@default_state_options