# Path: openelex/lib/text.py
# def election_slug(state, start_date, race_type, special=False, **kwargs):
#     """
#     Generate a standardized election identifier string.
# 
#     Args:
#         state: Lowercase state postal abbreviation.  For example, "md".
#         start_date: Start date of election, in the form YYYY-MM-DD. Required.
#         race_type: Race type, for example "general" or "primary".  Required.
#         special: Boolean indicating whether the election is a special election.
#                 Default is False.
# 
#     Returns:
#         String formatted like: ``{state_abbrev}-YYYY-MM-DD-(special)-{race_type}``
# 
#         For example, "ar-2012-05-22-primary".
# 
#     """
#     bits = [
#         state.lower(),
#         start_date,
#     ]
# 
#     if special:
#         bits.append('special')
# 
#     bits.append(race_type.lower())
# 
#     return "-".join(bits)
# 
# def slugify(text, substitute='_'):
#     """Slugify text, with option of character subsitution for whitespace.
# 
#     USAGE
# 
#         >>> slugify("Testing.  1! 2! 3?")
#         'testing_1_2_3'
# 
#         >>> slugify("Testing.  1! 2! 3?", substitute='-')
#         'testing-1-2-3'
# 
#     """
#     # remove punctuation and lower case
#     text = re.sub(r'[^\w\s]', '', text).lower()
#     # Collapse whitespace and convert to substitution string
#     return re.sub(r'\s+', ' ', text).replace(' ', substitute)
# 
# Path: openelex/base/state.py
# class StateBase(object):
#     """Base class with common functionality for working with state modules.
# 
#     Intended to be subclassed by other base modules or
#     state specific modules
#     """
# 
#     def __init__(self, state=''):
#         if not state:
#             self.state = self.__module__.split('.')[-2]
#         else:
#             self.state = state
#         self.cache = StateCache(self.state)
#         # Create mappings directory if it doesn't exist
#         self.mappings_dir = os.path.join(PROJECT_ROOT, 'us', self.state, 'mappings')
#         try:
#             os.makedirs(self.mappings_dir)
#         except OSError:
#             pass
#         # Create ocd mappings csv if it doesn't exist
#         open(os.path.join(self.mappings_dir, self.state + '.csv'), 'a').close()


from future import standard_library
from builtins import next
from os.path import join, splitext
from openelex.api import elections as elec_api
from openelex.lib.text import election_slug, slugify
from .state import StateBase
import re
import urllib.parse
import unicodecsv
            an election's data file.

        """
        # TODO(geoffhing@gmail.com) Delegate to
        # openelex.lib.standardized_filename()
        reporting_level = kwargs.get('reporting_level')
        jurisdiction = kwargs.get('jurisdiction')
        office = kwargs.get('office')
        office_district = kwargs.get('office_district')
        extension = kwargs.get('extension')
        if extension is None:
            extension =  self._filename_extension(election['direct_links'][0])

        if bits is None:
            bits = []

        bits.extend([
            election['start_date'].replace('-', ''),
            self.state,
        ])

        if election['special']:
            bits.append('special')

        bits.append(election['race_type'].replace('-', '_'))

        if jurisdiction:
            bits.append(slugify(jurisdiction))

        if office:

# Path: openelex/lib/text.py
# def election_slug(state, start_date, race_type, special=False, **kwargs):
#     """
#     Generate a standardized election identifier string.
# 
#     Args:
#         state: Lowercase state postal abbreviation.  For example, "md".
#         start_date: Start date of election, in the form YYYY-MM-DD. Required.
#         race_type: Race type, for example "general" or "primary".  Required.
#         special: Boolean indicating whether the election is a special election.
#                 Default is False.
# 
#     Returns:
#         String formatted like: ``{state_abbrev}-YYYY-MM-DD-(special)-{race_type}``
# 
#         For example, "ar-2012-05-22-primary".
# 
#     """
#     bits = [
#         state.lower(),
#         start_date,
#     ]
# 
#     if special:
#         bits.append('special')
# 
#     bits.append(race_type.lower())
# 
#     return "-".join(bits)
# 
# def slugify(text, substitute='_'):
#     """Slugify text, with option of character subsitution for whitespace.
# 
#     USAGE
# 
#         >>> slugify("Testing.  1! 2! 3?")
#         'testing_1_2_3'
# 
#         >>> slugify("Testing.  1! 2! 3?", substitute='-')
#         'testing-1-2-3'
# 
#     """
#     # remove punctuation and lower case
#     text = re.sub(r'[^\w\s]', '', text).lower()
#     # Collapse whitespace and convert to substitution string
#     return re.sub(r'\s+', ' ', text).replace(' ', substitute)
# 
# Path: openelex/base/state.py
# class StateBase(object):
#     """Base class with common functionality for working with state modules.
# 
#     Intended to be subclassed by other base modules or
#     state specific modules
#     """
# 
#     def __init__(self, state=''):
#         if not state:
#             self.state = self.__module__.split('.')[-2]
#         else:
#             self.state = state
#         self.cache = StateCache(self.state)
#         # Create mappings directory if it doesn't exist
#         self.mappings_dir = os.path.join(PROJECT_ROOT, 'us', self.state, 'mappings')
#         try:
#             os.makedirs(self.mappings_dir)
#         except OSError:
#             pass
#         # Create ocd mappings csv if it doesn't exist
#         open(os.path.join(self.mappings_dir, self.state + '.csv'), 'a').close()


# Path: openelex/base/datasource.py
from future import standard_library
from builtins import next
from os.path import join, splitext
from openelex.api import elections as elec_api
from openelex.lib.text import election_slug, slugify
from .state import StateBase
import re
import urllib.parse
import unicodecsv

            an election's data file.

        """
        # TODO(geoffhing@gmail.com) Delegate to
        # openelex.lib.standardized_filename()
        reporting_level = kwargs.get('reporting_level')
        jurisdiction = kwargs.get('jurisdiction')
        office = kwargs.get('office')
        office_district = kwargs.get('office_district')
        extension = kwargs.get('extension')
        if extension is None:
            extension =  self._filename_extension(election['direct_links'][0])

        if bits is None:
            bits = []

        bits.extend([
            election['start_date'].replace('-', ''),
            self.state,
        ])

        if election['special']:
            bits.append('special')

        bits.append(election['race_type'].replace('-', '_'))

        if jurisdiction:
            bits.append(slugify(jurisdiction))

        if office:

            bits.append(slugify(office))