Retrieve expiration date for articles in implementation

This commit is contained in:
Denis Merigoux 2020-04-22 11:51:17 +02:00
parent f5d1933fdc
commit f01f112e28
10 changed files with 86 additions and 25 deletions

View File

@ -6,7 +6,7 @@ CATALA_LANG?=fr
CATALA=dune exec ../../src/catala.exe -- --debug --language=$(CATALA_LANG)
CATALA_EXE=../../_build/default/src/main.exe
CATALA_EXE=../../_build/default/src/catala.exe
%.tex: %.catala $(CATALA_EXE)
$(CATALA) Makefile $<

View File

@ -8,7 +8,8 @@
@@Chapitre 1er : Liste des prestations@@++++
@Article L511-1@
@Article L511-1|LEGIARTI000038834530@
Les prestations familiales comprennent :
1°) la prestation d'accueil du jeune enfant ;
2°) les allocations familiales ;

View File

@ -110,7 +110,7 @@ let program_item_to_latex (i : A.program_item) (language : C.language_option) :
(match precedence with 0 -> "" | 1 -> "" | 2 -> "sub" | 3 -> "sub" | _ -> "subsub")
(pre_latexify title)
| A.LawText t -> pre_latexify t
| A.LawArticle a -> P.sprintf "\\paragraph{%s}" (pre_latexify a)
| A.LawArticle a -> P.sprintf "\\paragraph{%s}" (pre_latexify (Pos.unmark a.law_article_name))
| A.CodeBlock (_, c) ->
P.sprintf
"\\begin{minted}[label={\\hspace*{\\fill}\\texttt{%s}},firstnumber=%d]{%s}%s\\end{minted}"

View File

@ -169,9 +169,11 @@ type code_block = code_item Pos.marked list
type source_repr = string Pos.marked
type law_article = { law_article_name : string Pos.marked; law_article_id : string option }
type program_item =
| LawHeading of string * int
| LawArticle of string
| LawArticle of law_article
| LawText of string
| CodeBlock of code_block * source_repr
| MetadataBlock of code_block * source_repr

View File

@ -359,7 +359,7 @@ let rec lex_law_en lexbuf =
new_line lexbuf
done;
update lexbuf;
LAW_ARTICLE title
LAW_ARTICLE (title, None)
| Plus (Compl ('@' | '/' | '\n')) ->
update lexbuf;
LAW_TEXT (Sedlexing.Utf8.lexeme buf)

View File

@ -354,10 +354,14 @@ let rec lex_law_fr lexbuf =
update lexbuf;
LAW_HEADING (law_title, precedence)
| "@", Plus (Compl '@'), "@" ->
let extract_article_title = R.regexp "@([^@]+)@" in
let title =
R.get_substring (R.exec ~rex:extract_article_title (Sedlexing.Utf8.lexeme buf)) 1
let extract_article_title =
R.regexp "\\@(([^\\|]+)\\|(LEGIARTI[0-9]{12})|[^\\@]+)\\@"
in
let get_substring =
R.get_substring (R.exec ~rex:extract_article_title (Sedlexing.Utf8.lexeme buf))
in
let title = try get_substring 2 with Not_found -> get_substring 1 in
let article_id = try Some (get_substring 3) with Not_found -> None in
let get_new_lines = R.regexp "\n" in
let new_lines_count =
try Array.length (R.extract ~rex:get_new_lines (Sedlexing.Utf8.lexeme buf))
@ -367,7 +371,7 @@ let rec lex_law_fr lexbuf =
new_line lexbuf
done;
update lexbuf;
LAW_ARTICLE title
LAW_ARTICLE (title, article_id)
| Plus (Compl ('@' | '/' | '\n')) ->
update lexbuf;
LAW_TEXT (Sedlexing.Utf8.lexeme buf)

View File

@ -22,7 +22,7 @@
%}
%token EOF
%token<string> LAW_ARTICLE
%token<string * string option> LAW_ARTICLE
%token<string * int> LAW_HEADING
%token<string * int option> LAW_INCLUDE
%token<string> LAW_TEXT
@ -465,7 +465,12 @@ metadata_block:
}
source_file_item:
| title = LAW_ARTICLE { LawArticle title }
| title = LAW_ARTICLE {
let (title, id) = title in LawArticle {
law_article_name = (title, mk_position $sloc);
law_article_id = id;
}
}
| heading = LAW_HEADING { let (title, precedence) = heading in LawHeading (title, precedence) }
| text = LAW_TEXT { LawText text }
| BEGIN_METADATA code = metadata_block {

View File

@ -122,7 +122,6 @@ let get_article_expiration_date (json : Yojson.Basic.t) : Unix.tm =
|> Yojson.Basic.Util.member "articleVersions"
|> Yojson.Basic.Util.to_list
|> List.find (fun version ->
Catala.Cli.debug_print (Yojson.Basic.to_string (Yojson.Basic.Util.member "id" version));
Yojson.Basic.to_string (Yojson.Basic.Util.member "id" version) = "\"" ^ article_id ^ "\"")
|> Yojson.Basic.Util.member "dateFin"
|> Yojson.Basic.Util.to_int |> api_timestamp_to_localtime

View File

@ -1,6 +1,6 @@
(library
(name legifrance_catala)
(libraries catala cmdliner cohttp lwt cohttp-lwt-unix yojson))
(libraries catala cmdliner cohttp lwt cohttp-lwt-unix yojson re))
(documentation
(package catala))

View File

@ -33,9 +33,17 @@ let client_secret =
& pos 2 (some string) None
& info [] ~docv:"CLIENT_SECRET" ~doc:"LegiFrance Oauth cliend secret")
let expiration_date =
Arg.(
required
& pos 3 (some string) None
& info [] ~docv:"EXPIRATION_DATE"
~doc:"Articles that expire before this date will yield a warning (format DD/MM/YYYY)")
let debug = Arg.(value & flag & info [ "d"; "debug" ] ~doc:"Prints debug information")
let catala_legifrance_t f = Term.(const f $ file $ debug $ client_id $ client_secret)
let catala_legifrance_t f =
Term.(const f $ file $ debug $ client_id $ client_secret $ expiration_date)
let info =
let doc = "LegiFrance interaction tool for Catala" in
@ -55,21 +63,63 @@ let info =
| Some v -> Build_info.V1.Version.to_string v )
~doc ~exits ~man
let driver (file : string) (debug : bool) (client_id : string) (client_secret : string) =
let parse_expiration_date (expiration_date : string) : Unix.tm =
try
let extract_article_title = Re.Pcre.regexp "([0-9]{2})\\/([0-9]{2})\\/([0-9]{4})" in
let get_substring =
Re.Pcre.get_substring (Re.Pcre.exec ~rex:extract_article_title expiration_date)
in
snd
(Unix.mktime
{
Unix.tm_mday = int_of_string (get_substring 1);
Unix.tm_mon = int_of_string (get_substring 2);
Unix.tm_year = int_of_string (get_substring 3) - 1900;
Unix.tm_sec = 0;
Unix.tm_min = 0;
Unix.tm_hour = 0;
Unix.tm_wday = 0;
Unix.tm_yday = 0;
Unix.tm_isdst = false;
})
with _ ->
Catala.Cli.error_print
(Printf.sprintf "Error while parsing expiration date argument (%s)" expiration_date);
exit 0
let print_tm (d : Unix.tm) : string =
Printf.sprintf "%02d/%02d/%d " d.Unix.tm_mday (1 + d.Unix.tm_mon) (1900 + d.Unix.tm_year)
let date_before (d1 : Unix.tm) (d2 : Unix.tm) : bool = fst (Unix.mktime d1) <= fst (Unix.mktime d2)
let driver (file : string) (debug : bool) (client_id : string) (client_secret : string)
(expiration_date : string) =
if debug then Catala.Cli.debug_flag := true;
let expiration_date = parse_expiration_date expiration_date in
let access_token = Api.get_token client_id client_secret in
Catala.Cli.debug_print (Printf.sprintf "The LegiFrance API access token is %s" access_token);
let article = Api.get_article_json access_token "LEGIARTI000038889038" in
let article_text = Api.get_article_text article in
let article_expiration_date = Api.get_article_expiration_date article in
Catala.Cli.debug_print
(Printf.sprintf "The content of the article (that expires on %02d/%02d/%d) is\n%s"
article_expiration_date.Unix.tm_mday article_expiration_date.Unix.tm_mon
(1900 + article_expiration_date.Unix.tm_year)
article_text);
(* LegiFrance is only supported for French texts *)
let _program = Catala.Parser_driver.parse_source_files [ file ] Catala.Cli.Fr in
(*TODO: introduce content id on Catala articles, and then retrive the text through the API *)
let program = Catala.Parser_driver.parse_source_files [ file ] Catala.Cli.Fr in
List.iter
(fun item ->
match item with
| Catala.Ast.LawArticle article_catala -> (
match article_catala.Catala.Ast.law_article_id with
| None -> ()
| Some article_id ->
let article = Api.get_article_json access_token article_id in
let article_expiration_date = Api.get_article_expiration_date article in
if date_before article_expiration_date expiration_date then assert false
else
Catala.Cli.debug_print
(Printf.sprintf "%s %s expires on %s"
(Catala.Pos.unmark article_catala.Catala.Ast.law_article_name)
(Catala.Pos.to_string
(Catala.Pos.get_position article_catala.Catala.Ast.law_article_name))
(print_tm article_expiration_date)
) )
| _ -> ())
program.program_items;
exit 0
let main () = Cmdliner.Term.exit @@ Cmdliner.Term.eval (catala_legifrance_t driver, info)