On 11/18/2015 5:19 PM, Christopher Walborn wrote:
I'm looking for a way to read configuration files. The configuration file
format can be anything provided it's easily human readable/writable. I found
the ApacheConf solution on RosettaCode and may use that, but am wondering if
there is a conf format that's more commonly used for Racket projects.
There's no serious need for portability, longevity or anything else. I'm just
using it to provide some settings and paths to local utilities in shell scripts
which are little more than wrappers around other utilities.
Thanks,
Christopher
I second Sam's suggestion to read/write files with Racket.
I personally don't like the require/dynamic-require approach that Neil
suggested because the config file then is evaluated only at load time [I
don't know a way to "unload" a module, though maybe it is possible with
custodians], and it is hard to limit execution of arbitrary Racket code.
I work a lot on server applications where other people may have to edit
the configuration file, and I often need ability to reload (parts of)
the configuration while the process is running.
I use a simple dotted pair format:
( name1 . setting1 )
( name2 . setting2 )
:
and I read/eval it using something like:
(define (load-config config-path)
(let [
(eval-ns (make-base-namespace))
(params (make-hash))
]
(let [
(settings (file->list config-path))
]
(for [(name:value settings)]
(let* [
; extract the 'name' symbol
(n (car name:value))
; evaluate the 'value' expression
(v (cdr name:value))
(v (eval (quasiquote (,@v)) eval-ns))
]
(hash-set! params n v)
))
)
params
))
My full version includes file error handling and ways to sanity check
the settings, but this is the basic idea. For more control, the
namespace could be recreated (or copied) each time, or you can start
with an empty namespace and add only those features you want to allow in
the file. Or use a sandbox with a custom language. So far I have not
found any of these to be necessary.
YMMV,
George
--
You received this message because you are subscribed to the Google Groups "Racket
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.