# Path: tasks/meta.py
# def current_session():
#     '''
#     Returns the session relevant to the currently operating :class:`Task`, if
#     any.  Outside the context of a :class:`Task`, this can still be used for
#     manual session management.
#     '''
#     return _current_session.get()
# 
# Path: tasks/uk/census/wrapper.py
# class CensusPostcodeAreas(InterpolationTask):
#     def table_timespan(self):
#         return get_timespan('2011')
# 
#     def columns(self):
#         cols = OrderedDict()
#         input_ = self.input()
#         cols['pa_id'] = input_['target_geom_columns']['pa_id']
#         cols.update(input_['source_data_columns'])
#         return cols
# 
#     def requires(self):
#         deps = {
#             'source_geom_columns': OutputAreaColumns(),
#             'source_geom': OutputAreas(),
#             'source_data_columns': CensusColumns(),
#             'source_data': CensusOutputAreas(),
#             'target_geom_columns': PostcodeAreasColumns(),
#             'target_geom': PostcodeAreas(),
#             'target_data_columns': CensusColumns(),
#         }
# 
#         return deps
# 
#     def get_interpolation_parameters(self):
#         params = {
#             'source_data_geoid': 'geographycode',
#             'source_geom_geoid': 'oa_sa',
#             'target_data_geoid': 'pa_id',
#             'target_geom_geoid': 'pa_id',
#             'source_geom_geomfield': 'the_geom',
#             'target_geom_geomfield': 'the_geom',
#         }
# 
#         return params
# 
# class CensusPostcodeDistricts(CensusPostcodeEntitiesFromOAs):
#     def requires(self):
#         deps = {
#             'source_geom_columns': OutputAreaColumns(),
#             'source_geom': OutputAreas(),
#             'source_data_columns': CensusColumns(),
#             'source_data': CensusOutputAreas(),
#             'target_geom_columns': PostcodeDistrictsColumns(),
#             'target_geom': PostcodeDistricts(),
#             'target_data_columns': CensusColumns(),
#         }
# 
#         return deps
# 
# class CensusPostcodeSectors(CensusPostcodeEntitiesFromOAs):
#     def requires(self):
#         deps = {
#             'source_geom_columns': OutputAreaColumns(),
#             'source_geom': OutputAreas(),
#             'source_data_columns': CensusColumns(),
#             'source_data': CensusOutputAreas(),
#             'target_geom_columns': PostcodeSectorsColumns(),
#             'target_geom': PostcodeSectors(),
#             'target_data_columns': CensusColumns(),
#         }
# 
#         return deps
# 
# Path: lib/logger.py
# def get_logger(name):
#     '''
#     Obtain a logger outputing to stderr with specified name. Defaults to INFO
#     log level.
#     '''
#     logger = logging.getLogger(name)
#     if LUIGI_LOG_ARG in sys.argv:
#         index = sys.argv.index(LUIGI_LOG_ARG) + 1
#         if len(sys.argv) > index:
#             logger.setLevel(sys.argv[index])
# 
#     return logger


import os
import json
import csv
from luigi import Task, WrapperTask, Parameter
from luigi.local_target import LocalTarget
from tasks.meta import current_session
from tasks.uk.census.wrapper import CensusPostcodeAreas, CensusPostcodeDistricts, CensusPostcodeSectors
from lib.logger import get_logger


LOGGER = get_logger(__name__)

GEOGRAPHY_LEVELS = {
    'GEO_PA': 'uk.datashare.pa_geo',  # Postcode Areas
    'GEO_PD': 'uk.odl.pd_geo',  # Postcode Districts
    'GEO_PS': 'uk.odl.ps_geo',  # Postcode Sectors
}


class Measurements2CSV(Task):
    geography = Parameter()
    file_name = Parameter()

    def __init__(self, *args, **kwargs):
        super(Measurements2CSV, self).__init__(*args, **kwargs)

    def requires(self):
        requirements = {}
        if self.geography == 'GEO_PA':
            requirements['data'] = CensusPostcodeAreas()

# Path: tasks/meta.py
# def current_session():
#     '''
#     Returns the session relevant to the currently operating :class:`Task`, if
#     any.  Outside the context of a :class:`Task`, this can still be used for
#     manual session management.
#     '''
#     return _current_session.get()
# 
# Path: tasks/uk/census/wrapper.py
# class CensusPostcodeAreas(InterpolationTask):
#     def table_timespan(self):
#         return get_timespan('2011')
# 
#     def columns(self):
#         cols = OrderedDict()
#         input_ = self.input()
#         cols['pa_id'] = input_['target_geom_columns']['pa_id']
#         cols.update(input_['source_data_columns'])
#         return cols
# 
#     def requires(self):
#         deps = {
#             'source_geom_columns': OutputAreaColumns(),
#             'source_geom': OutputAreas(),
#             'source_data_columns': CensusColumns(),
#             'source_data': CensusOutputAreas(),
#             'target_geom_columns': PostcodeAreasColumns(),
#             'target_geom': PostcodeAreas(),
#             'target_data_columns': CensusColumns(),
#         }
# 
#         return deps
# 
#     def get_interpolation_parameters(self):
#         params = {
#             'source_data_geoid': 'geographycode',
#             'source_geom_geoid': 'oa_sa',
#             'target_data_geoid': 'pa_id',
#             'target_geom_geoid': 'pa_id',
#             'source_geom_geomfield': 'the_geom',
#             'target_geom_geomfield': 'the_geom',
#         }
# 
#         return params
# 
# class CensusPostcodeDistricts(CensusPostcodeEntitiesFromOAs):
#     def requires(self):
#         deps = {
#             'source_geom_columns': OutputAreaColumns(),
#             'source_geom': OutputAreas(),
#             'source_data_columns': CensusColumns(),
#             'source_data': CensusOutputAreas(),
#             'target_geom_columns': PostcodeDistrictsColumns(),
#             'target_geom': PostcodeDistricts(),
#             'target_data_columns': CensusColumns(),
#         }
# 
#         return deps
# 
# class CensusPostcodeSectors(CensusPostcodeEntitiesFromOAs):
#     def requires(self):
#         deps = {
#             'source_geom_columns': OutputAreaColumns(),
#             'source_geom': OutputAreas(),
#             'source_data_columns': CensusColumns(),
#             'source_data': CensusOutputAreas(),
#             'target_geom_columns': PostcodeSectorsColumns(),
#             'target_geom': PostcodeSectors(),
#             'target_data_columns': CensusColumns(),
#         }
# 
#         return deps
# 
# Path: lib/logger.py
# def get_logger(name):
#     '''
#     Obtain a logger outputing to stderr with specified name. Defaults to INFO
#     log level.
#     '''
#     logger = logging.getLogger(name)
#     if LUIGI_LOG_ARG in sys.argv:
#         index = sys.argv.index(LUIGI_LOG_ARG) + 1
#         if len(sys.argv) > index:
#             logger.setLevel(sys.argv[index])
# 
#     return logger


# Path: tasks/geographica/uk/csv.py
import os
import json
import csv
from luigi import Task, WrapperTask, Parameter
from luigi.local_target import LocalTarget
from tasks.meta import current_session
from tasks.uk.census.wrapper import CensusPostcodeAreas, CensusPostcodeDistricts, CensusPostcodeSectors
from lib.logger import get_logger



LOGGER = get_logger(__name__)

GEOGRAPHY_LEVELS = {
    'GEO_PA': 'uk.datashare.pa_geo',  # Postcode Areas
    'GEO_PD': 'uk.odl.pd_geo',  # Postcode Districts
    'GEO_PS': 'uk.odl.ps_geo',  # Postcode Sectors
}


class Measurements2CSV(Task):
    geography = Parameter()
    file_name = Parameter()

    def __init__(self, *args, **kwargs):
        super(Measurements2CSV, self).__init__(*args, **kwargs)

    def requires(self):
        requirements = {}
        if self.geography == 'GEO_PA':
            requirements['data'] = CensusPostcodeAreas()

        elif self.geography == 'GEO_PD':