Changeset 7 in code for trunk/cli/pull.ml
- Timestamp:
- Jun 18, 2022, 8:45:45 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/cli/pull.ml
r3 r7 50 50 List.fold_left (fun a x -> Printf.sprintf "%s %s" a (Msgpck.to_string x)) "peers: " ps 51 51 52 let parse_index _is_selected fn url dir p = 53 let open Logarion.Header_pack in 54 match Msgpck.to_list p.texts with 55 | [] -> Printf.printf "%s => %s, has empty index\n" p.info.title dir; false 56 | texts -> 57 let numof_texts = string_of_int @@ List.length texts in 58 let text_num_len = String.length numof_texts in 59 Printf.printf "%*d/%s %s => %s\r" text_num_len 0 numof_texts p.info.title dir; 60 let of_pck i x = 61 Printf.printf "\r%*d/%s %!" text_num_len (i+1) numof_texts; 62 match x with 63 | Msgpck.List (id::time::title::_authors::_topics) -> 64 (match Logarion.Header_pack.to_id id with 65 | "" -> Printf.eprintf "Invalid id for%s " (Msgpck.to_string title) 66 | id -> 67 let t = match time with Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i | x -> Msgpck.to_uint32 x in 68 if newer t id dir then fn url dir id) 69 | _ -> prerr_endline ("Invalid record structure") in 70 List.iteri of_pck texts; 71 print_newline (); 72 true 52 type filter_t = { authors: Logarion.Person.Set.t; topics: Logarion.String_set.t } 53 54 let print_pull_start width total title dir = 55 Printf.printf "%*d/%s %s => %s %!" width 0 total title dir 56 57 let print_pull width total i = 58 Printf.printf "\r%*d/%s %!" width (i+1) total 59 60 let printers total title dir = 61 let width = String.length total in 62 print_pull_start width total title dir; 63 print_pull width total 73 64 74 65 let fname dir text = Filename.concat dir (Logarion.Text.short_id text ^ ".txt") 66 75 67 let pull_text url dir id = 76 68 let u = Filename.concat url ((Logarion.Id.short id) ^ ".txt") in 77 69 match curl_pull u with 78 70 | Error msg -> Printf.eprintf "Failed getting %s: %s" u msg 79 | Ok txt -> 80 let txt = Buffer.contents txt in 71 | Ok txt -> let txt = Buffer.contents txt in 81 72 match Logarion.Text.of_string txt with 82 73 | Error s -> prerr_endline s … … 85 76 output_string file txt; close_out file 86 77 87 let pull_index url _authors _topics = 78 let per_text url dir filter print i id time title authors topics = match id with 79 | "" -> Printf.eprintf "\nInvalid id for %s\n" title 80 | id -> let open Logarion in 81 print i; 82 if newer time id dir 83 && (String_set.empty = filter.topics 84 || String_set.exists (fun t -> List.mem t topics) filter.topics) 85 && (Person.Set.empty = filter.authors 86 || Person.Set.exists (fun t -> List.mem (Person.to_string t) authors) filter.authors) 87 then pull_text url dir id 88 89 let pull_index url authors_opt topics_opt = 88 90 let index_url = url ^ "/index.pck" in 89 91 match curl_pull index_url with … … 95 97 let dir = Filename.concat Logarion.Peers.text_dir pk.info.id in 96 98 Logarion.File_store.with_dir dir; 97 let file = open_out_gen [Open_creat; Open_trunc; Open_wronly] 0o640 (Filename.concat dir "index.pck") in 99 let file = open_out_gen [Open_creat; Open_trunc; Open_wronly] 0o640 100 (Filename.concat dir "index.pck") in 98 101 output_string file ( Logarion.Header_pack.string { 99 102 pk with info = { pk.info with locations = url::pk.info.locations }}); 100 103 close_out file; 101 (* let predicates = A.predicate A.authored authors_opt*) 102 (* @ A.predicate A.topics topics_opt in*) 103 let is_selected text = List.fold_left (fun a e -> a && e text) true [](*predicates*) in 104 try parse_index is_selected pull_text url dir pk with 105 Invalid_argument msg -> Printf.eprintf "Failed to parse: %s\n%!" msg; false 104 let filter = let open Logarion in { 105 authors = (match authors_opt with Some s -> Person.Set.of_string s | None -> Person.Set.empty); 106 topics =( match topics_opt with Some s -> String_set.of_string s | None -> String_set.empty); 107 } in 108 let print = printers (string_of_int @@ Logarion.Header_pack.numof_texts pk) pk.info.title dir in 109 try Logarion.Header_pack.iteri (per_text url dir filter print) pk; print_newline (); true 110 with Invalid_argument msg -> Printf.eprintf "\nFailed to parse %s: %s\n%!" url msg; false 106 111 107 112 let pull_list auths topics =
Note:
See TracChangeset
for help on using the changeset viewer.