# Path: tasks/ca/statcan/data.py
# class AllCensusTopics(BaseParams, WrapperTask):
#     def requires(self):
#         topic_range = list(range(1, 11))   # 1-10
# 
#         for resolution in (GEO_CT, GEO_PR, GEO_CD, GEO_CSD, GEO_CMA, GEO_DA, GEO_FSA):
#             for count in topic_range:
#                 topic = 't{:03d}'.format(count)
#                 yield Census(resolution=resolution, survey=SURVEY_CEN, topic=topic)
# 
# class AllNHSTopics(BaseParams, WrapperTask):
#     def requires(self):
#         topic_range = list(range(1, 30))   # 1-29
# 
#         for resolution in (GEO_CT, GEO_PR, GEO_CD, GEO_CSD, GEO_CMA, GEO_DA, GEO_FSA):
#             for count in topic_range:
#                 topic = 't{:03d}'.format(count)
#                 yield NHS(resolution=resolution, survey=SURVEY_NHS, topic=topic)
# 
# 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: 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.ca.statcan.data import AllCensusTopics, AllNHSTopics
from tasks.meta import current_session
from lib.logger import get_logger
        dir_path = os.path.dirname(os.path.realpath(__file__))
        with (open('{}/{}'.format(dir_path, 'measurements.json'))) as f:
            return json.load(f)

    def run(self):
        session = current_session()
        measurements = self._get_config_data()
        for measure in measurements:
            measure['geom_id'] = GEOGRAPHY_LEVELS[self.geography]
        json_metadata = json.dumps(measurements)
        result = session.execute(self._get_meta_query(json_metadata))
        if result:
            join_data = {}
            join_data['numer'] = {}
            colnames = ['geom_id as geoid']
            for data in result.fetchall():
                join_data['numer'][data['numer_table']] = {'table': 'observatory.{}'.format(data['numer_table']),
                                                           'join_column': data['numer_join_col']}
                # All come from the same geometry tables so we use, by now, just one geometry
                # TODO Make it possible to have multiple geometry tables
                join_data['geom'] = {'table': 'observatory.{}'.format(data['geom_table']), 'join_column': data['geom_join_col']}
                colnames.append(data['numer_col'])
            measurement_result = session.execute(self._get_measurements_query(join_data, colnames))
            if measurement_result:
                measurements = measurement_result.fetchall()
                self._generate_csv_file(colnames, measurements)
            else:
                LOGGER.error('No results for the queried measurements')

        else:

# Path: tasks/ca/statcan/data.py
# class AllCensusTopics(BaseParams, WrapperTask):
#     def requires(self):
#         topic_range = list(range(1, 11))   # 1-10
# 
#         for resolution in (GEO_CT, GEO_PR, GEO_CD, GEO_CSD, GEO_CMA, GEO_DA, GEO_FSA):
#             for count in topic_range:
#                 topic = 't{:03d}'.format(count)
#                 yield Census(resolution=resolution, survey=SURVEY_CEN, topic=topic)
# 
# class AllNHSTopics(BaseParams, WrapperTask):
#     def requires(self):
#         topic_range = list(range(1, 30))   # 1-29
# 
#         for resolution in (GEO_CT, GEO_PR, GEO_CD, GEO_CSD, GEO_CMA, GEO_DA, GEO_FSA):
#             for count in topic_range:
#                 topic = 't{:03d}'.format(count)
#                 yield NHS(resolution=resolution, survey=SURVEY_NHS, topic=topic)
# 
# 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: 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/ca/csv.py
import os
import json
import csv
from luigi import Task, WrapperTask, Parameter
from luigi.local_target import LocalTarget
from tasks.ca.statcan.data import AllCensusTopics, AllNHSTopics
from tasks.meta import current_session
from lib.logger import get_logger

        dir_path = os.path.dirname(os.path.realpath(__file__))
        with (open('{}/{}'.format(dir_path, 'measurements.json'))) as f:
            return json.load(f)

    def run(self):
        session = current_session()
        measurements = self._get_config_data()
        for measure in measurements:
            measure['geom_id'] = GEOGRAPHY_LEVELS[self.geography]
        json_metadata = json.dumps(measurements)
        result = session.execute(self._get_meta_query(json_metadata))
        if result:
            join_data = {}
            join_data['numer'] = {}
            colnames = ['geom_id as geoid']
            for data in result.fetchall():
                join_data['numer'][data['numer_table']] = {'table': 'observatory.{}'.format(data['numer_table']),
                                                           'join_column': data['numer_join_col']}
                # All come from the same geometry tables so we use, by now, just one geometry
                # TODO Make it possible to have multiple geometry tables
                join_data['geom'] = {'table': 'observatory.{}'.format(data['geom_table']), 'join_column': data['geom_join_col']}
                colnames.append(data['numer_col'])
            measurement_result = session.execute(self._get_measurements_query(join_data, colnames))
            if measurement_result:
                measurements = measurement_result.fetchall()
                self._generate_csv_file(colnames, measurements)
            else:
                LOGGER.error('No results for the queried measurements')

        else:

            LOGGER.error('No results for the defined measurements in the JSON file')