Mercurial > lasercutter
view src/clojure/contrib/properties.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line source
1 ; Copyright (c) Stuart Halloway & Contributors, April 2009. All rights reserved.2 ; The use and distribution terms for this software are covered by the3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)4 ; which can be found in the file epl-v10.html at the root of this distribution.5 ; By using this software in any fashion, you are agreeing to be bound by6 ; the terms of this license.7 ; You must not remove this notice, or any other, from this software.9 ;; DEPRECATED in 1.2. Moved to c.c.java-utils11 (ns ^{:deprecated "1.2"}12 clojure.contrib.properties13 (:use [clojure.contrib.string :only (as-str)]14 [clojure.contrib.io :only (file)])15 (:import (java.util Properties)16 (java.io FileInputStream FileOutputStream)))18 (defn get-system-property19 "Get a system property."20 ([stringable]21 (System/getProperty (as-str stringable)))22 ([stringable default]23 (System/getProperty (as-str stringable) default)))25 (defn set-system-properties26 "Set some system properties. Nil clears a property."27 [settings]28 (doseq [[name val] settings]29 (if val30 (System/setProperty (as-str name) (as-str val))31 (System/clearProperty (as-str name)))))33 (defmacro with-system-properties34 "setting => property-name value36 Sets the system properties to the supplied values, executes the body, and37 sets the properties back to their original values. Values of nil are38 translated to a clearing of the property."39 [settings & body]40 `(let [settings# ~settings41 current# (reduce (fn [coll# k#]42 (assoc coll# k# (get-system-property k#)))43 {}44 (keys settings#))]45 (set-system-properties settings#)46 (try47 ~@body48 (finally49 (set-system-properties current#)))))52 ; Not there is no corresponding props->map. Just destructure!53 (defn ^Properties as-properties54 "Convert any seq of pairs to a java.utils.Properties instance.55 Uses as-str to convert both keys and values into strings."56 {:tag Properties}57 [m]58 (let [p (Properties.)]59 (doseq [[k v] m]60 (.setProperty p (as-str k) (as-str v)))61 p))63 (defn read-properties64 "Read properties from file-able."65 [file-able]66 (with-open [f (java.io.FileInputStream. (file file-able))]67 (doto (Properties.)68 (.load f))))70 (defn write-properties71 "Write properties to file-able."72 {:tag Properties}73 ([m file-able] (write-properties m file-able nil))74 ([m file-able comments]75 (with-open [^FileOutputStream f (FileOutputStream. (file file-able))]76 (doto (as-properties m)77 (.store f ^String comments)))))