# 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
    state_mod_name = "openelex.us.%s" % state
    err_msg = "%s module could not be imported. Does it exist?"
    try:
        state_mod = load_module(state, ['datasource'])
    except ImportError:
        sys.exit(err_msg % state_mod_name)

    try:
        datasrc = state_mod.datasource.Datasource()
    except AttributeError:
        state_mod_name += ".datasource.py"
        sys.exit(err_msg % state_mod_name)
    method = getattr(datasrc, task)
    return method(datefilter)

def pprint_results(func_name, results):
    for result in results:
        pprint(result)
    print("\n%s returned %s results" % (func_name, len(results)))

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)

# 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

    state_mod_name = "openelex.us.%s" % state
    err_msg = "%s module could not be imported. Does it exist?"
    try:
        state_mod = load_module(state, ['datasource'])
    except ImportError:
        sys.exit(err_msg % state_mod_name)

    try:
        datasrc = state_mod.datasource.Datasource()
    except AttributeError:
        state_mod_name += ".datasource.py"
        sys.exit(err_msg % state_mod_name)
    method = getattr(datasrc, task)
    return method(datefilter)

def pprint_results(func_name, results):
    for result in results:
        pprint(result)
    print("\n%s returned %s results" % (func_name, len(results)))

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)))