let debug_print s =
if debug then prerr_endline ("Helm_registry debugging: " ^ s)
+ (** <helpers> *)
+
+let list_uniq l =
+ let rec aux last_element = function
+ | [] -> []
+ | hd :: tl ->
+ (match last_element with
+ | Some elt when elt = hd -> aux last_element tl
+ | _ -> hd :: aux (Some hd) tl)
+ in
+ aux None l
+
+let starts_with prefix =
+(*
+ let rex = Str.regexp (Str.quote prefix) in
+ fun s -> Str.string_match rex s 0
+*)
+ let prefix_len = String.length prefix in
+ fun s ->
+ try
+ String.sub s 0 prefix_len = prefix
+ with Invalid_argument _ -> false
+
+let hashtbl_keys tbl = Hashtbl.fold (fun k _ acc -> k :: acc) tbl []
+let hashtbl_pairs tbl = Hashtbl.fold (fun k v acc -> (k,v) :: acc) tbl []
+
+ (** </helpers> *)
+
exception Malformed_key of string
exception Key_not_found of string
exception Cyclic_definition of string
Netencoding.Html.decode ~in_enc ~out_enc ~entity_base:`Xml ())
let key_is_valid key =
-(* if not (Pcre.pmatch ~rex:valid_key_rex key) then *)
if not (Str.string_match valid_key_rex key 0) then
raise (Malformed_key key)
let unset = Hashtbl.remove registry
let env_var_of_key key =
-(* Pcre.replace ~rex:dot_rex ~templ:"__" (String.uppercase key) *)
Str.global_replace dot_rex "__" (String.uppercase key)
let get key =
let set = set' registry
+let has key = Hashtbl.mem registry key
+
let mk_get_set type_name (from_string: string -> 'a) (to_string: 'a -> string) =
let getter key =
let value = get key in
match value with
| None -> unset key
| Some value -> setter ~key ~value
-
-(*
-let save_to =
- let dtd = new dtd default_config.warner `Enc_utf8 in
- let rec create_key node sections key value =
- match sections with
- | [] -> create_element_node ~valcheck:false default_spec dtd
-*)
-
-let save_to fname =
- debug_print ("Saving configuration to " ^ fname);
- let oc = open_out fname in
- output_string oc "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
- output_string oc "<helm_registry>\n";
- try
- Hashtbl.iter
- (fun key value ->
- fprintf oc " <value key=\"%s\">%s</value>\n" key (escape value))
- registry;
- output_string oc "</helm_registry>";
- close_out oc
- with e ->
- close_out oc;
- raise e
+let get_opt_default getter default key =
+ match get_opt getter key with
+ | None -> default
+ | Some v -> v
let add_validator ~key ~validator ~descr =
let id = get_next_validator_id () in
Hashtbl.add validators key (validator, descr);
id
+open Pxp_dtd
open Pxp_document
open Pxp_types
open Pxp_yacc
+let save_to =
+ let dtd = new dtd PxpHelmConf.pxp_config.warner `Enc_utf8 in
+ let dot_RE = Str.regexp "\\." in
+ let create_key_node key value = (* create a <key name="foo">value</key> *)
+ let element =
+ create_element_node ~valcheck:false PxpHelmConf.pxp_spec dtd
+ "key" ["name", key]
+ in
+ let data = create_data_node PxpHelmConf.pxp_spec dtd value in
+ element#append_node data;
+ element
+ in
+ let is_section name =
+ fun node ->
+ match node#node_type with
+ | T_element "section" ->
+ (try node#attribute "name" = Value name with Not_found -> false)
+ | _ -> false
+ in
+ let add_key_node root sections key value =
+ let rec aux node = function
+ | [] ->
+ let key_node = create_key_node key value in
+ node#append_node key_node
+ | section :: tl ->
+ let next_node =
+ try
+ find ~deeply:false (is_section section) node
+ with Not_found ->
+ let section_node =
+ create_element_node ~valcheck:false PxpHelmConf.pxp_spec dtd
+ "section" ["name", section]
+ in
+ node#append_node section_node;
+ section_node
+ in
+ aux next_node tl
+ in
+ aux root sections
+ in
+ fun fname ->
+ let xml_root =
+ create_element_node ~valcheck:false PxpHelmConf.pxp_spec dtd "helm_registry" []
+ in
+ Hashtbl.iter
+ (fun key value ->
+ let sections, key =
+ let hd, tl =
+ match List.rev (Str.split dot_RE key) with
+ | hd :: tl -> hd, tl
+ | _ -> assert false
+ in
+ List.rev tl, hd
+ in
+ add_key_node xml_root sections key value)
+ registry;
+ let outchan = (* let's write xml output to fname *)
+ if Unix.system "xmllint --version &> /dev/null" = Unix.WEXITED 0 then
+ (* xmllint available, use it! *)
+ Unix.open_process_out (sprintf
+ "xmllint --format --encode utf8 -o '%s' -" fname)
+ else
+ (* xmllint not available, write pxp ugly output directly to fname *)
+ open_out fname
+ in
+ xml_root#write (`Out_channel outchan) `Enc_utf8;
+ close_out outchan
+
let load_from_absolute =
- let config = default_config in
+ let config = PxpHelmConf.pxp_config in
let entry = `Entry_document [ `Extend_dtd_fully; `Parse_xml_decl ] in
let fold_key key_stack key =
match key_stack with
fun fname ->
debug_print ("Loading configuration from " ^ fname);
let document =
- parse_wfdocument_entity config (from_file fname) default_spec
+ parse_wfdocument_entity config (from_file fname) PxpHelmConf.pxp_spec
in
let rec aux key_stack node =
node#iter_nodes (fun n ->
end else
load_from_absolute fname
+let fold ?prefix f init =
+ match prefix with
+ | None -> Hashtbl.fold (fun k v acc -> f acc k v) registry init
+ | Some s ->
+ let key_matches = starts_with (s ^ ".") in
+ let rec fold_filter acc = function
+ | [] -> acc
+ | (k,v) :: tl when key_matches k -> fold_filter (f acc k v) tl
+ | _ :: tl -> fold_filter acc tl
+ in
+ fold_filter init (hashtbl_pairs registry)
+
+let iter ?prefix f = fold ?prefix (fun _ k v -> f k v) ()
+let to_list ?prefix () = fold ?prefix (fun acc k v -> (k, v) :: acc) []
+
+let ls prefix =
+ let prefix = prefix ^ "." in
+ let prefix_len = String.length prefix in
+ let key_matches = starts_with prefix in
+ let matching_keys = (* collect matching keys' _postfixes_ *)
+ fold
+ (fun acc key _ ->
+ if key_matches key then
+ String.sub key prefix_len (String.length key - prefix_len) :: acc
+ else
+ acc)
+ []
+ in
+ let (sections, keys) =
+ List.fold_left
+ (fun (sections, keys) postfix ->
+ match Str.split dot_rex postfix with
+ | [key] -> (sections, key :: keys)
+ | hd_key :: _ -> (* length > 1 => nested section found *)
+ (hd_key :: sections, keys)
+ | _ -> assert false)
+ ([], []) matching_keys
+ in
+ (list_uniq (List.sort Pervasives.compare sections), keys)
+
(* DEBUGGING ONLY *)
let dump () = Hashtbl.iter (fun k v -> printf "%s = %s\n" k v) registry