1 (* Copyright (C) 2004, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
26 (** Configuration repository for HELM applications.
31 * path ::= component ( '.' component )*
32 * component ::= ( lowercase_alpha | num | '_' )+
33 * # with the only exception that sequences of '_' longer than 1 aren't valid
36 * Suggested usage <application>.<setting>:
37 * e.g. gTopLevel.prooffile, http_getter.port, ...
39 * ++ Configuration file example ++
41 * gTopLevel.prooffile = "/home/zack/prooffile"
42 * http_getter.port = "58080"
44 * ++ Environment variable override ++
46 * each key has an associated environment variable name. At runtime (i.e. when
47 * "get" requests are performed) a variable with this name will be looked for,
48 * if it's defined it will override the value present (or absent) in the
50 * Environment variables are _not_ considered when saving the configuration to
51 * a configuration file (via "save_to" function below) .
53 * Mapping between keys and environment variables is as follows:
54 * - the whole key is uppercased
55 * - each "." is converted to "__"
56 * E.g.: my.foo_ish.application -> MY__FOO_ISH__APPLICATION
58 * ++ Variable interpolation ++
60 * Interpolation is supported with the following syntax:
63 * foo.baz = $(foo.bar)/baz
66 (** raised when a looked up key can't be found
67 * @param key looked up key *)
68 exception Key_not_found of string
70 (** raised when a cyclic definitions is found, e.g. after
71 * Helm_registry.set "a" "$b"
72 * Helm_registry.set "b" "$a"
73 * @param msg brief description of the definition cycle *)
74 exception Cyclic_definition of string
76 (** raised when a looked up key doesn't have the required type
77 * @param expected_type
80 exception Type_error of string * string * string
82 (** raised when a malformed key is encountered
83 * @param key malformed key *)
84 exception Malformed_key of string
86 (** raised when an error is encountered while parsing a configuration file
87 * @param fname file name
88 * @param line line number
89 * @param col column number
90 * @param msg error description
92 exception Parse_error of string * int * int * string
94 (** raised when a given <key,value> pair fails validity test(s)
95 * @param pair <key,value> pair
96 * @param descr description of the failed test *)
97 exception Invalid_value of (string * string) * string
99 (** {2 Generic untyped interface}
100 * Using the functions below this module could be used as a repository of
103 (** lookup key in registry with environment variable override *)
104 val get: string -> string
105 val set: key:string -> value:string -> unit
107 (** remove a key from the current environment, next get over this key will
108 * raise Key_not_found until the key will be redefined *)
109 val unset: string -> unit
111 (** {2 Typed interface}
112 * Three basic types are supported: strings, int and strings list. Strings
113 * correspond literally to what is written inside double quotes; int to the
114 * parsing of an integer number from ; strings list to the splitting at blanks
115 * of it (heading and trailing blanks are removed before splitting) *)
117 val get_string: string -> string (* alias for bare "get" above *)
118 val get_int: string -> int
119 val get_float: string -> float
120 val get_bool: string -> bool
121 val get_string_list: string -> string list
123 (* alias for bare "set" above *)
124 val set_string: key:string -> value:string -> unit
125 val set_int: key:string -> value:int -> unit
126 val set_float: key:string -> value:float -> unit
127 val set_bool: key:string -> value:bool -> unit
128 val set_string_list: key:string -> value:string list -> unit
130 (** {3 Optional values interface}
131 * Functions below took as first argument respectively a "getter" and a "setter"
132 * function. A getter is one of the get_* function above, a setter is one of the
133 * set_* function above. Returned value is a get (set) function typed as the
134 * given getter (setter) whith optional values. None is returned for missing
135 * keys and None can be assigned to a key removing it from the registry.
139 * match Helm_registry.get_opt Helm_registry.get_int "foo.bar" with
145 (string -> 'a) (* getter *) ->
148 (key:string -> value:'a -> unit) (* setter *) ->
149 key:string -> value:'a option -> unit
150 val get_opt_default: (* as get_opt with an additional default value *)
151 (string -> 'a) -> 'a -> string -> 'a
154 * Each key may have zero or more associated validators, that are predicates
155 * "this value is valid for this key". Each time a value is set, all validators
156 * associated to the corresponding key are executed, if at least one of them
157 * fails, Invalid_value exception will be raised *)
161 (** register a new validator for a given key
162 * @param key key to which validator applies
163 * @param validator a function applying to a value returning true if that
164 * value is valid, false otherwise
165 * @param descr validator description, for the final user when a validation
167 * @return validator_id should be used to remove the validator later on *)
169 key:string -> validator:(string -> bool) -> descr:string ->
171 (* val remove_validator: validator_id -> unit *)
173 (** {2 Persistent configuration}
174 * Validators aren't saved. load_from/save_to sequences don't preserve comments
177 (** @param fname file to which save current configuration
178 * If xmllint is available then it will be used for pretty printing fname,
179 * otherwise fname will be in the usual pxp ugly format *)
180 val save_to: string -> unit
182 (** @param fname file from which load new configuration. If it's an absolute
183 * file name "path" argument is ignored.
184 * Otherwise given file name is looked up in each directory member of the
185 * given path. Each matching file is loaded overriding previous settings. If
186 * no path is given a default path composed of just the current working
189 val load_from: ?path:string list -> string -> unit
192 (* val dump: unit -> unit *)