diff --git a/til_france/scripts/config_example_til.txt b/til_france/scripts/config_example_til.txt deleted file mode 100644 index f98c3b7..0000000 --- a/til_france/scripts/config_example_til.txt +++ /dev/null @@ -1,8 +0,0 @@ -[raw_data] -patrimoine_data_directory = where/to/put/patrimoine/data -hsi_data_directory = where/to/put/hsi/data - -[til] -output_dir = where/to/put/til/output -input_dir = where/to/put/til/input -temp_dir = where/to/put/til/temp/files \ No newline at end of file diff --git a/til_france/scripts/til_init.py b/til_france/scripts/til_init.py index 866a7c5..6cf1163 100644 --- a/til_france/scripts/til_init.py +++ b/til_france/scripts/til_init.py @@ -1,41 +1,47 @@ #! /usr/bin/env python # -*- coding: utf-8 -*- - import argparse import logging import os import sys - from glob import glob from shutil import copyfile from subprocess import call - import openfisca_survey_manager import til_core - +import til_france from til_france.data.data.Patrimoine import test_build as patrimoine from til_france.data.data.handicap_sante_institutions import create_hsi_data as hsi from openfisca_survey_manager.scripts import build_collection - from build_demographic_model_data import main as build_demographic_model_data_ from population_and_dependance_builder import main as build_parameters_ - app_name = os.path.splitext(os.path.basename(__file__))[0] log = logging.getLogger(app_name) # utils def read_config_example(option): + """ + str -> str + Reads the file containing pre-filled configuration for the option + """ + filename = 'config_example_{}.txt'.format(option) - fichier = os.path.abspath(filename) + fichier = os.path.join( + os.path.dirname(til_france.__file__), + 'scripts', + 'til_init', + filename + ) - assert os.path.exists(fichier), "Could not find config example file" + assert os.path.exists(fichier), \ + "Could not find {} config example file".format(option) with open(fichier) as fichier: text = fichier.read() + '\n' @@ -43,13 +49,14 @@ def read_config_example(option): return(text) -def copy_init_file_to_config(attributes): - name = attributes['name'] - config_dir = attributes['config_dir'] - config_filename = attributes['config_filename'] - module = attributes['module'] +def copy_init_file_to_config(option): + """ + str * dict[str->str] -> None + Reads the file containing pre-filled configuration and writes a file with + the proper name to ~/.config + """ - log.info('Creating a {} configuration template...'.format(name)) + config_dir = get_config_dir(option) if not os.path.exists(config_dir): log.info('Creating a .config folder here: {}'.format(config_dir)) @@ -57,33 +64,23 @@ def copy_init_file_to_config(attributes): assert os.path.exists(config_dir), "Could not locate your config folder" - # locating the template config in module installation folder - module_path = os.path.dirname(module.__file__) - relative_path_of_init = '../' + config_filename - - init_template = os.path.join( - module_path, - relative_path_of_init - ) - destination = os.path.join( config_dir, - remove_templateini(config_filename) + get_config_filename(option) ) - copyfile(init_template, destination) + with open(destination, 'w') as f: + text = read_config_example(option) + f.write(text) return -def remove_templateini(s): - if s.endswith('_template.ini'): - remove = len('_template.ini') - s = s[:-remove] + '.ini' - return(s) - - def get_config_dir(option): + """ + str -> str + Returns the abs path to ~/.config/[option directory] + """ home = os.path.expanduser("~") name = 'til-core' if option == 'til' else 'openfisca-survey-manager' config_dir = os.path.join( @@ -94,115 +91,90 @@ def get_config_dir(option): def get_config_filename(option): - assert option in ['til', 'openfisca_survey_manager'] - - module = til_core if option == 'til' else openfisca_survey_manager - module_path = os.path.dirname(module.__file__) - - search = os.path.join( - module_path, - '../*.ini' - ) - - candidates = glob(search) - assert len(candidates) == 1, "Not clear what config template should be used" - - filename = os.path.basename(candidates[0]) - + """ + str -> str + Returns the name fo the config file for the option + """ + assert option in ['til', 'osm', 'hsi'] + filename = 'raw_data.ini' if option == 'hsi' else 'config.ini' return(filename) # main functions def configuration(option): - assert option in ['til', 'openfisca_survey_manager', 'hsi'], "Unrecognized option" - - last_words = ( - "NB: If your editor is not well set, this will start vim as a default editor.\n" + - "If you are lost, you can exit it by pressing escape then :q! then enter.\n" + - "Set your editor properly and re-run this script.\n" + - "Press ENTER to continue\n" - ) + """ + str -> None + Given an option, prompts the user to fill the appropriate config files from + a template + """ + assert option in ['til', 'osm', 'hsi'], "Unrecognized option" attributes_by_option = { 'til': { - 'raw_input': [ + 'raw_input': ( "You now will be taken to your text editor.\n" + "You must give appropriate directories for where raw data should be stored, " + "as well as appropriate directories for Til Core. \n" + - "Your config file should contain at least:\n\n", - last_words], + "Your config file should contain at least:\n\n"), 'module': til_core, 'name': 'Til Core', - 'config_dir': get_config_dir('til'), - 'config_filename': get_config_filename('til'), - 'config_example': read_config_example('til'), }, - 'openfisca_survey_manager': { - 'raw_input': [ + 'osm': { + 'raw_input': ( "You now will be taken to your text editor.\n" + "HSI data is first processed by openfisca survey manager (openfisca_survey_manager), " + "then by til_france. You must give directories where to store openfisca_survey_manager files\n" + - "Your config file should contain at least:\n\n", - "If it is already the case, just close your editor.\n" + - last_words - ], + "Your config file should contain at least:\n\n"), 'module': openfisca_survey_manager, 'name': 'Openfisca Survey Manager', - 'config_dir': get_config_dir('openfisca_survey_manager'), - 'config_filename': 'config_template.ini', - 'config_example': read_config_example('openfisca_survey_manager') }, 'hsi': { - 'raw_input': [ - "You now will be taken to your text editor (for the last time).\n" + + 'raw_input': ("You now will be taken to your text editor (for the last time).\n" + "You must give a directory where to get HSI raw data. " + - "Your config file should contain at least:\n\n", - "If it is already the case, just close your editor.\n" + - last_words - ], + "Your config file should contain at least:\n\n"), 'module': openfisca_survey_manager, 'name': 'HSI raw data', - 'config_dir': get_config_dir('openfisca_survey_manager'), - 'config_filename': 'raw_data_template.ini', - 'config_example': read_config_example('hsi') } } attributes = attributes_by_option[option] name = attributes['name'] - raw_input_1, raw_input_2 = attributes['raw_input'] - config_dir = attributes['config_dir'] - config_filename = attributes['config_filename'] - config_example = attributes['config_example'] + raw_input_text = attributes['raw_input'] + config_filename = get_config_filename(option) + config_example = read_config_example(option) + config_dir = get_config_dir(option) log.info('Starting {} configuration ...'.format(name)) - fichier_config = os.path.join( + config_file = os.path.join( config_dir, - remove_templateini(config_filename) + config_filename ) - if not os.path.isfile(fichier_config): - init_file_exit_value = copy_init_file_to_config(attributes) - assert init_file_exit_value == 0, "Unable to copy {} init template to your .config folder".format(name) + if not os.path.isfile(config_file): + log.info('Creating a {} configuration template...'.format(name)) + init_file_exit_value = copy_init_file_to_config(option) + assert init_file_exit_value is None, "Unable to copy {} init template to your .config folder".format(name) - assert os.path.isfile(fichier_config), "Config template was not created" + assert os.path.isfile(config_file), "Config template was not created" - raw_input(( - raw_input_1 + - config_example + '\n' + - raw_input_2 - )) + raw_input( + raw_input_text + config_example + ) editor = os.environ.get('EDITOR', 'vim') - call([editor, fichier_config], shell = True) + call([editor, config_file], shell = True) # from now on, assume we have a correct config file return def build_insee_data(): + """ + None -> None + Build INSEE data + """ log.info(u"Starting basic insee data processing ...") build_demographic_model_data_() log.info('... done') @@ -210,6 +182,10 @@ def build_insee_data(): def build_patrimoine_data(): + """ + None -> None + Build Patrimoine data /// NB: should not work! Patrimoine script is broken + """ log.info(u"Starting Patrimoine data processing ...") patrimoine() log.info('... done') @@ -217,6 +193,10 @@ def build_patrimoine_data(): def build_hsi_data(): + """ + None -> None + Build HSI data + """ log.info(u"Starting HSI data processing ...") sys.argv.extend(['-chsi', '-d', '-m']) build_collection.main() @@ -226,12 +206,23 @@ def build_hsi_data(): def build_parameters(): + """ + None -> None + Build Parameters file + """ + + # no idea what this is for sys.argv.extend(['-d']) + build_parameters_() return def data_prompt(): + """ + None -> None + Prompt the user to check if the data is where it should be + """ raw_input(( "Make sure the input folders you chose actually contain the input data " + "(Patrimoine and HSI) and press ENTER to proceed" @@ -240,8 +231,6 @@ def data_prompt(): def main(): - sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) - parser = argparse.ArgumentParser() parser.add_argument( @@ -260,7 +249,7 @@ def main(): to_do = [ lambda: configuration('til'), - lambda: configuration('openfisca_survey_manager'), + lambda: configuration('osm'), lambda: configuration('hsi'), data_prompt, build_insee_data, diff --git a/til_france/scripts/config_example_hsi.txt b/til_france/scripts/til_init/config_example_hsi.txt similarity index 100% rename from til_france/scripts/config_example_hsi.txt rename to til_france/scripts/til_init/config_example_hsi.txt diff --git a/til_france/scripts/config_example_osm.txt b/til_france/scripts/til_init/config_example_osm.txt similarity index 100% rename from til_france/scripts/config_example_osm.txt rename to til_france/scripts/til_init/config_example_osm.txt diff --git a/til_france/scripts/til_init/config_example_til.txt b/til_france/scripts/til_init/config_example_til.txt new file mode 100644 index 0000000..95a4afc --- /dev/null +++ b/til_france/scripts/til_init/config_example_til.txt @@ -0,0 +1,8 @@ +[raw_data] +patrimoine_data_directory = /where/patrimoine/data/is +hsi_data_directory = /where/hsi/data/is + +[til] +output_dir = where/til/output/goes +input_dir = where/til/input/comes/from +temp_dir = where/til/temp/files/go \ No newline at end of file