Changeset 7 in code


Ignore:
Timestamp:
Jun 18, 2022, 8:45:45 PM (3 years ago)
Author:
fox
Message:

Implement author & topic filter for txt pull

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/cli/pull.ml

    r3 r7  
    5050                List.fold_left (fun a x -> Printf.sprintf "%s %s" a (Msgpck.to_string x)) "peers: " ps
    5151
    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
     52type filter_t = { authors: Logarion.Person.Set.t; topics: Logarion.String_set.t }
     53
     54let print_pull_start width total title dir =
     55        Printf.printf "%*d/%s %s => %s %!" width 0 total title dir
     56
     57let print_pull width total i =
     58        Printf.printf "\r%*d/%s %!" width (i+1) total
     59
     60let printers total title dir =
     61        let width = String.length total in
     62        print_pull_start width total title dir;
     63        print_pull width total
    7364
    7465let fname dir text = Filename.concat dir (Logarion.Text.short_id text ^ ".txt")
     66
    7567let pull_text url dir id =
    7668        let u = Filename.concat url ((Logarion.Id.short id) ^ ".txt") in
    7769        match curl_pull u with
    7870        | 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
    8172                match Logarion.Text.of_string txt with
    8273                | Error s -> prerr_endline s
     
    8576                        output_string file txt; close_out file
    8677
    87 let pull_index url _authors _topics =
     78let 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
     89let pull_index url authors_opt topics_opt =
    8890        let index_url = url ^ "/index.pck" in
    8991        match curl_pull index_url with
     
    9597                        let dir = Filename.concat Logarion.Peers.text_dir pk.info.id in
    9698                        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
    98101                        output_string file ( Logarion.Header_pack.string {
    99102                                        pk with info = { pk.info with locations = url::pk.info.locations }});
    100103                        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
    106111
    107112let pull_list auths topics =
  • trunk/lib/header_pack.ml

    r3 r7  
    6969        | _ -> prerr_endline ("Invalid record pattern"); false
    7070
     71let numof_texts pack = List.length (Msgpck.to_list pack.texts)
     72
     73let iteri fn pack =
     74        let of_pck i = function Msgpck.List (id::time::title::authors::topics::[]) ->
     75                let t = match time with Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i
     76                        | x -> Msgpck.to_uint32 x in
     77                let id = to_id id in
     78                let title = Msgpck.to_string title in
     79                let topics = to_str_list topics in
     80                let authors = to_str_list authors in
     81                fn i id t title authors topics
     82        | _  -> prerr_endline ("\n\nInvalid record structure\n\n")
     83        in List.iteri of_pck (Msgpck.to_list pack.texts);
    7184
    7285(*let pack_filename ?(filename="index.pck") archive =*)
Note: See TracChangeset for help on using the changeset viewer.