]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/registry/helm_registry.mli
- no longer needs PXP
[helm.git] / helm / ocaml / registry / helm_registry.mli
1 (* Copyright (C) 2004, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (** Configuration repository for HELM applications.
27  *
28  * ++ Keys format ++
29  *
30  *  key ::= path
31  *  path ::= component ( '.' component )*
32  *  component ::= ( alpha | num | '_' )+
33  *  # with the only exception that sequences of '_' longer than 1 aren't valid
34  *  # components
35  *
36  *  Suggested usage <application>.<setting>:
37  *   e.g. gTopLevel.prooffile, http_getter.port, ...
38  *
39  * ++ Configuration file example ++
40  *
41  *  gTopLevel.prooffile = "/home/zack/prooffile"
42  *  http_getter.port = "58080"
43  *
44  * ++ Environment variable override ++
45  *
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
49  *  registry.
50  *  Environment variables are _not_ considered when saving the configuration to
51  *  a configuration file (via "save_to" function below) .
52  *
53  *  Mapping between keys and environment variables is as follows:
54  *  - each "." is converted to "__"
55  *  E.g.: my.Foo_iSH.Application -> my__Foo_iSH__Application
56  *
57  * ++ Variable interpolation ++
58  *
59  * Interpolation is supported with the following syntax:
60  *  
61  *  foo.bar = "quux"
62  *  foo.baz = $(foo.bar)/baz
63  *)
64
65   (** raised when a looked up key can't be found
66    * @param key looked up key *)
67 exception Key_not_found of string
68
69   (** raised when a cyclic definitions is found, e.g. after
70    * Helm_registry.set "a" "$b"
71    * Helm_registry.set "b" "$a"
72    * @param msg brief description of the definition cycle *)
73 exception Cyclic_definition of string
74
75   (** raised when a looked up key doesn't have the required type
76    * @param expected_type
77    * @param value
78    * @param msg *)
79 exception Type_error of string * string * string
80
81   (** raised when a malformed key is encountered
82    * @param key malformed key *)
83 exception Malformed_key of string
84
85   (** raised when an error is encountered while parsing a configuration file
86    * @param fname file name 
87    * @param line line number
88    * @param col column number
89    * @param msg error description
90    *)
91 exception Parse_error of string * int * int * string
92
93   (** raised when a given <key,value> pair fails validity test(s)
94    * @param pair <key,value> pair
95    * @param descr description of the failed test *)
96 exception Invalid_value of (string * string) * string
97
98 (** {2 Generic untyped interface}
99  * Using the functions below this module could be used as a repository of
100  * key/value pairs *)
101
102   (** lookup key in registry with environment variable override *)
103 val get: string -> string
104 val set: key:string -> value:string -> unit
105 val has: string -> bool
106
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
110
111   (** @param interpolate defaults to true *)
112 val fold:
113   ?prefix:string -> ?interpolate:bool ->
114     ('a -> string -> string -> 'a) -> 'a -> 'a
115
116   (** @param interpolate defaults to true *)
117 val iter:
118   ?prefix:string -> ?interpolate:bool -> 
119     (string -> string -> unit) -> unit
120
121   (** @param interpolate defaults to true *)
122 val to_list:
123   ?prefix:string -> ?interpolate:bool ->
124     unit -> (string * string) list
125
126   (** @param prefix key representing the section whose contents should be listed
127   * @return section list * key list *)
128 val ls: string -> string list * string list
129
130 (** {2 Typed interface}
131  * Three basic types are supported: strings, int and strings list. Strings
132  * correspond literally to what is written inside double quotes; int to the
133  * parsing of an integer number from ; strings list to the splitting at blanks
134  * of it (heading and trailing blanks are removed before splitting) *)
135
136 val get_string:       string -> string  (* alias for bare "get" above *)
137 val get_int:          string -> int
138 val get_float:        string -> float
139 val get_bool:         string -> bool
140 val get_string_list:  string -> string list
141
142   (* alias for bare "set" above *)
143 val set_string:       key:string -> value:string      -> unit
144 val set_int:          key:string -> value:int         -> unit
145 val set_float:        key:string -> value:float       -> unit
146 val set_bool:         key:string -> value:bool        -> unit
147 val set_string_list:  key:string -> value:string list -> unit
148
149 (** {3 Optional values interface}
150  * Functions below took as first argument respectively a "getter" and a "setter"
151  * function. A getter is one of the get_* function above, a setter is one of the
152  * set_* function above. Returned value is a get (set) function typed as the
153  * given getter (setter) whith optional values. None is returned for missing
154  * keys and None can be assigned to a key removing it from the registry.
155  *
156  * Sample  usage:
157  *
158  *  match Helm_registry.get_opt Helm_registry.get_int "foo.bar" with
159  *  | Some i -> ...
160  *  | None -> ...
161  *)
162
163 val get_opt:
164   (string -> 'a) (* getter *) ->
165     string -> 'a option
166 val set_opt:
167   (key:string -> value:'a -> unit) (* setter *) ->
168     key:string -> value:'a option -> unit
169 val get_opt_default:  (* as get_opt with an additional default value *)
170   (string -> 'a) -> 'a -> string -> 'a
171
172 (** {2 Validators}
173  * Each key may have zero or more associated validators, that are predicates
174  * "this value is valid for this key". Each time a value is set, all validators
175  * associated to the corresponding key are executed, if at least one of them
176  * fails, Invalid_value exception will be raised *)
177
178
179 (*
180 type validator_id
181   (** register a new validator for a given key
182    * @param key key to which validator applies
183    * @param validator a function applying to a value returning true if that
184    *  value is valid, false otherwise
185    * @param descr validator description, for the final user when a validation
186    *  attempt fails
187    * @return validator_id should be used to remove the validator later on *)
188 val add_validator:
189   key:string -> validator:(string -> bool) -> descr:string ->
190     validator_id
191 val remove_validator: validator_id -> unit
192 *)
193
194 (** {2 Persistent configuration}
195  * Validators aren't saved. load_from/save_to sequences don't preserve comments
196  *)
197
198   (** @param fname file to which save current configuration
199    * If xmllint is available then it will be used for pretty printing fname,
200    * otherwise fname will be in the usual pxp ugly format *)
201 val save_to: string -> unit
202
203   (** @param fname file from which load new configuration. If it's an absolute
204    * file name "path" argument is ignored.
205    * Otherwise given file name is looked up in each directory member of the
206    * given path. Each matching file is loaded overriding previous settings. If
207    * no path is given a default path composed of just the current working
208    * directory is used.
209    *)
210 val load_from: ?path:string list -> string -> unit
211
212 (** {2 OO interface} *)
213
214   (** @see load_from *)
215 class registry: ?path: string list -> string ->
216   object
217     method get: string -> string
218     method set: key:string -> value:string -> unit
219     method has: string -> bool
220     method unset: string -> unit
221     method fold:
222       ?prefix:string -> ?interpolate:bool ->
223         ('a -> string -> string -> 'a) -> 'a -> 'a
224     method iter:
225       ?prefix:string -> ?interpolate:bool -> (string -> string -> unit) -> unit
226     method to_list:
227       ?prefix:string -> ?interpolate:bool -> unit -> (string * string) list
228     method ls: string -> string list * string list
229     method get_string: string -> string
230     method get_int: string -> int
231     method get_float: string -> float
232     method get_bool: string -> bool
233     method get_string_list: string -> string list
234     method set_string: key:string -> value:string -> unit
235     method set_int: key:string -> value:int -> unit
236     method set_float: key:string -> value:float -> unit
237     method set_bool: key:string -> value:bool -> unit
238     method set_string_list: key:string -> value:string list -> unit
239     method get_opt:
240       (string -> 'a) (* getter *) ->
241         string -> 'a option
242     method set_opt:
243       (key:string -> value:'a -> unit) (* setter *) ->
244         key:string -> value:'a option -> unit
245     method get_opt_default:
246       (string -> 'a) (* getter *) ->
247         'a -> string -> 'a
248     method save_to: string -> unit
249   end
250