Skip to content
Snippets Groups Projects
Commit 7d153446 authored by Emmanuel Raviart's avatar Emmanuel Raviart
Browse files

Remove API for parameters & variables (moved to UI).

parent aa6a1e03
No related branches found
No related tags found
No related merge requests found
COUNTRY_JSON_DIR=../openfisca_france_json
COUNTRY_PACKAGE=openfisca_france
\ No newline at end of file
......@@ -4,7 +4,6 @@ from pydantic import BaseSettings
class Settings(BaseSettings):
country_json_dir: str
country_package: str
class Config:
......
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from .routers import parameters, simulations, variables
from .routers import simulations
app = FastAPI()
......@@ -12,6 +12,4 @@ app.add_middleware(
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(parameters.router)
app.include_router(simulations.router)
app.include_router(variables.router)
import json
import os
from fastapi import Depends
from . import config
parameters = None
def get_parameter(name: str):
parameter = parameters
for id in name.split("."):
children = parameter.get("children")
if children is None:
return None
parameter = children.get(id)
if parameter is None:
return None
return parameter
def get_parameter_with_ancestors(name: str):
ancestors = []
parameter = parameters
for id in name.split("."):
if parameter["name"]:
ancestors.append(parameter)
children = parameter.get("children")
if children is None:
return None, ancestors
parameter = children.get(id)
if parameter is None:
return None, ancestors
return parameter, ancestors
def get_parameters(settings: config.Settings = Depends(config.get_settings)):
global parameters
if parameters is None:
with open(
os.path.join(settings.country_json_dir, "parameters.json"), encoding="utf-8"
) as parameters_file:
parameters = json.load(parameters_file)
return parameters
def parameter_without_children(parameter):
parameter = parameter.copy()
parameter.pop("children", None)
return parameter
from fastapi import APIRouter, Depends
from ..parameters import (
get_parameter_with_ancestors,
get_parameters,
parameter_without_children,
)
router = APIRouter(
prefix="/parameters",
tags=["parameters"],
)
@router.get("/")
async def list_parameters(parameters=Depends(get_parameters)):
return parameters
@router.get("/{name}")
async def get_parameter(name: str, parameters=Depends(get_parameters)):
parameter, ancestors = get_parameter_with_ancestors(name)
if parameter is None:
# TODO: Return not found.
return None
# Note: Remove children from ancestors, because we don't want to send
# the full tree.
return dict(
ancestors=[parameter_without_children(ancestor) for ancestor in ancestors],
parameter=parameter,
)
from fastapi import APIRouter, Depends
from ..parameters import get_parameters, parameter_without_children
from ..variables import (
get_variables,
iter_variable_input_variables,
iter_variable_parameters_with_ancestors,
)
router = APIRouter(
prefix="/variables",
tags=["variables"],
)
@router.get("/")
async def list_variables(variables=Depends(get_variables)):
return variables
@router.get("/{name}")
async def get_variable(name: str, variables=Depends(get_variables)):
return variables.get(name)
@router.get("/{name}/inputs/{date}")
async def get_input_variables(name: str, date: str, variables=Depends(get_variables)):
variable = variables.get(name)
if variable is None:
return None
return [
input_variable
for input_variable in iter_variable_input_variables(variable, date)
]
@router.get("/{name}/parameters/{date}")
async def get_parameters(
name: str,
date: str,
parameters=Depends(get_parameters),
variables=Depends(get_variables),
):
variable = variables.get(name)
if variable is None:
return None
# Note: Remove children from ancestors, because we don't want to send
# the full tree.
return [
dict(
ancestors=[parameter_without_children(ancestor) for ancestor in ancestors],
parameter=parameter,
)
for parameter, ancestors in iter_variable_parameters_with_ancestors(
variable, date
)
]
import json
import os
from fastapi import Depends
from . import config
from .parameters import get_parameter_with_ancestors
variables = None
def get_variables(settings: config.Settings = Depends(config.get_settings)):
global variables
if variables is None:
with open(
os.path.join(settings.country_json_dir, "variables.json"), encoding="utf-8"
) as variables_file:
variables = json.load(variables_file)
return variables
def iter_variable_input_variables(variable, date, encountered_variables_name=None):
if encountered_variables_name is None:
encountered_variables_name = set()
name = variable["name"]
if name in encountered_variables_name:
return
encountered_variables_name.add(name)
formulas = variable.get("formulas")
if formulas is None:
# Variable is an input variable.
yield variable
return
dates = sorted(formulas.keys(), reverse=True)
for best_date in dates:
if best_date <= date:
break
else:
# No candidate date less than or equal to date found.
return
formula = formulas[best_date]
if formula is None:
return
referred_variables_name = formula.get("variables")
if referred_variables_name is None:
return
for referred_variable_name in referred_variables_name:
referred_variable = variables[referred_variable_name]
yield from iter_variable_input_variables(
referred_variable, date, encountered_variables_name
)
def iter_variable_parameters_with_ancestors(
variable, date, encountered_parameters_name=None, encountered_variables_name=None
):
if encountered_parameters_name is None:
encountered_parameters_name = set()
if encountered_variables_name is None:
encountered_variables_name = set()
name = variable["name"]
if name in encountered_variables_name:
return
encountered_variables_name.add(name)
formulas = variable.get("formulas")
if formulas is None:
return
dates = sorted(formulas.keys(), reverse=True)
for best_date in dates:
if best_date <= date:
break
else:
# No candidate date less than or equal to date found.
return
formula = formulas[best_date]
if formula is None:
return
referred_variables_name = formula.get("variables")
if referred_variables_name is not None:
for referred_variable_name in referred_variables_name:
referred_variable = variables[referred_variable_name]
yield from iter_variable_parameters_with_ancestors(
referred_variable,
date,
encountered_parameters_name,
encountered_variables_name,
)
referred_parameters_name = formula.get("parameters")
if referred_parameters_name is not None:
for referred_parameter_name in referred_parameters_name:
if referred_parameter_name in encountered_parameters_name:
continue
encountered_parameters_name.add(referred_parameter_name)
(
referred_parameter,
referred_parameter_ancestors,
) = get_parameter_with_ancestors(referred_parameter_name)
if referred_parameter is None:
continue
yield referred_parameter, referred_parameter_ancestors
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment