Skip to content

Commit

Permalink
Merge pull request #23 from TaxIPP-Life/til_init
Browse files Browse the repository at this point in the history
Update til_init.py
  • Loading branch information
benjello authored Mar 26, 2018
2 parents 4940f3e + 4dfe087 commit 5b62e97
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 106 deletions.
8 changes: 0 additions & 8 deletions til_france/scripts/config_example_til.txt

This file was deleted.

185 changes: 87 additions & 98 deletions til_france/scripts/til_init.py
Original file line number Diff line number Diff line change
@@ -1,89 +1,86 @@
#! /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'

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))
os.mkdir(config_dir)

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(
Expand All @@ -94,129 +91,112 @@ 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')
return


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


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()
Expand All @@ -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"
Expand All @@ -240,8 +231,6 @@ def data_prompt():


def main():
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

parser = argparse.ArgumentParser()

parser.add_argument(
Expand All @@ -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,
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions til_france/scripts/til_init/config_example_til.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5b62e97

Please sign in to comment.