mirror of
https://github.com/QuantumPackage/qp2.git
synced 2025-01-03 01:55:59 +01:00
Implemented #322
This commit is contained in:
parent
fdd6392d57
commit
a29c67a738
@ -129,15 +129,23 @@ let set str s =
|
||||
|
||||
|
||||
(** Creates the temporary file for interactive editing *)
|
||||
let create_temp_file ezfio_filename fields =
|
||||
let temp_filename = Filename.temp_file "qp_edit_" ".rst" in
|
||||
let create_temp_file ?filename ezfio_filename fields =
|
||||
let temp_filename =
|
||||
match filename with
|
||||
| None -> Filename.temp_file "qp_edit_" ".rst"
|
||||
| Some f -> f
|
||||
in
|
||||
let () =
|
||||
match filename with
|
||||
| None -> at_exit (fun () -> Sys.remove temp_filename)
|
||||
| _ -> ()
|
||||
in
|
||||
begin
|
||||
let oc = open_out temp_filename in
|
||||
(file_header ezfio_filename) :: (List.map get fields)
|
||||
|> String.concat "\n"
|
||||
|> Printf.fprintf oc "%s";
|
||||
close_out oc;
|
||||
at_exit (fun () -> Sys.remove temp_filename);
|
||||
temp_filename
|
||||
end
|
||||
|
||||
@ -145,12 +153,12 @@ let create_temp_file ezfio_filename fields =
|
||||
|
||||
|
||||
|
||||
let run check_only ?ndet ?state ezfio_filename =
|
||||
let run check_only ?ndet ?state ?read ?write ezfio_filename =
|
||||
|
||||
(* Set check_only if the arguments are not empty *)
|
||||
let check_only =
|
||||
match ndet, state with
|
||||
| None, None -> check_only
|
||||
match ndet, state, read with
|
||||
| None, None, None -> check_only
|
||||
| _ -> true
|
||||
in
|
||||
|
||||
@ -230,33 +238,41 @@ let run check_only ?ndet ?state ezfio_filename =
|
||||
|
||||
(* Create the temp file *)
|
||||
let temp_filename =
|
||||
create_temp_file ezfio_filename tasks
|
||||
match read, write with
|
||||
| None, None -> create_temp_file ezfio_filename tasks
|
||||
| Some filename, None -> filename
|
||||
| None, filename -> create_temp_file ?filename ezfio_filename tasks
|
||||
| x, y -> failwith "read and write options are incompatible"
|
||||
in
|
||||
|
||||
(* Open the temp file with external editor *)
|
||||
let editor =
|
||||
try Sys.getenv "EDITOR"
|
||||
with Not_found -> "vi"
|
||||
|
||||
let () =
|
||||
match check_only with
|
||||
| true -> ()
|
||||
| false ->
|
||||
begin
|
||||
(* Open the temp file with external editor *)
|
||||
let editor =
|
||||
try Sys.getenv "EDITOR"
|
||||
with Not_found -> "vi"
|
||||
in
|
||||
Printf.sprintf "%s %s" editor temp_filename
|
||||
|> Sys.command |> ignore
|
||||
end
|
||||
in
|
||||
|
||||
match check_only with
|
||||
| true -> ()
|
||||
| false ->
|
||||
Printf.sprintf "%s %s" editor temp_filename
|
||||
|> Sys.command |> ignore
|
||||
;
|
||||
|
||||
(* Re-read the temp file *)
|
||||
let temp_string =
|
||||
let ic = open_in temp_filename in
|
||||
let result =
|
||||
input_lines ic
|
||||
|> String.concat "\n"
|
||||
if write = None then
|
||||
(* Re-read the temp file *)
|
||||
let temp_string =
|
||||
let ic = open_in temp_filename in
|
||||
let result =
|
||||
input_lines ic
|
||||
|> String.concat "\n"
|
||||
in
|
||||
close_in ic;
|
||||
result
|
||||
in
|
||||
close_in ic;
|
||||
result
|
||||
in
|
||||
List.iter (fun x -> set temp_string x) tasks
|
||||
List.iter (fun x -> set temp_string x) tasks
|
||||
|
||||
|
||||
|
||||
@ -312,6 +328,16 @@ let () =
|
||||
doc="Checks the input data";
|
||||
arg=Without_arg; }};
|
||||
|
||||
{{
|
||||
short='w'; long="write"; opt=Optional;
|
||||
doc="Writes the qp_edit file to a file\"";
|
||||
arg=With_arg "<string>"; }};
|
||||
|
||||
{{
|
||||
short='r'; long="read"; opt=Optional;
|
||||
doc="Reads the file and applies it to the EZFIO\"";
|
||||
arg=With_arg "<string>"; }};
|
||||
|
||||
{{ short='n'; long="ndet"; opt=Optional;
|
||||
doc="Truncates the wavefunction to the target number of determinants";
|
||||
arg=With_arg "<int>"; }};
|
||||
@ -328,6 +354,12 @@ let () =
|
||||
end;
|
||||
|
||||
(* Handle options *)
|
||||
let write =
|
||||
Command_line.get "write"
|
||||
in
|
||||
let read =
|
||||
Command_line.get "read"
|
||||
in
|
||||
let ndet =
|
||||
match Command_line.get "ndet" with
|
||||
| None -> None
|
||||
@ -353,7 +385,7 @@ let () =
|
||||
(* Run the program *)
|
||||
try
|
||||
if (not c) then create_backup ezfio_filename;
|
||||
run c ?ndet ?state ezfio_filename
|
||||
run c ?ndet ?state ?read ?write ezfio_filename
|
||||
with
|
||||
| Failure exc
|
||||
| Invalid_argument exc ->
|
||||
|
Loading…
Reference in New Issue
Block a user