Skip to content

Commit

Permalink
runtime: switch to env-config
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin committed Oct 29, 2016
1 parent 76cc30f commit 17ba84d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 82 deletions.
52 changes: 7 additions & 45 deletions src/runtime/dirac/runtime/prefs.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns dirac.runtime.prefs
(:require [environ.core :refer [env]]
[clojure.string :as string])
(:require [clojure.string :as string]
[dirac.lib.utils :as utils])
(:import (java.io File)))

(defn attempt-to-read-runtime-tag-from-project-settings []
Expand All @@ -26,47 +26,9 @@
(or (attempt-to-read-runtime-tag-from-project-settings)
(attempt-to-read-runtime-tag-from-project-folder)))

(def ^:dynamic *agent-host* (env :dirac-agent-host))
(def ^:dynamic *agent-port* (env :dirac-agent-port))
(def ^:dynamic *agent-verbose* (env :dirac-agent-verbose))
(def ^:dynamic *agent-auto-reconnect* (env :dirac-agent-auto-reconnect))
(def ^:dynamic *agent-response-timeout* (env :dirac-agent-response-timeout))
(def ^:dynamic *weasel-verbose* (env :dirac-weasel-verbose))
(def ^:dynamic *weasel-auto-reconnect* (env :dirac-weasel-auto-reconnect))
(def ^:dynamic *weasel-pre-eval-delay* (env :dirac-weasel-pre-eval-delay))
(def ^:dynamic *install-check-total-time-limit* (env :dirac-install-check-total-time-limit))
(def ^:dynamic *install-check-next-trial-waiting-time* (env :dirac-install-check-next-trial-waiting-time))
(def ^:dynamic *install-check-eval-time-limit* (env :dirac-install-check-eval-time-limit))
(def ^:dynamic *context-availability-total-time-limit* (env :dirac-context-availability-total-time-limit))
(def ^:dynamic *context-availability-next-trial-waiting-time* (env :dirac-context-availability-next-trial-waiting-time))
(def ^:dynamic *eval-time-limit* (env :dirac-eval-time-limit))
(def ^:dynamic *runtime-tag* (or (env :dirac-runtime-tag) (attempt-to-determine-runtime-tag)))
(def ^:dynamic *silence-use-of-undeclared-var-warnings* (env :dirac-silence-use-of-undeclared-var-warnings))
(def ^:dynamic *silence-no-such-namespace-warnings* (env :dirac-silence-no-such-namespace-warnings))
(def env-config-prefix "dirac-runtime")

(defmacro static-pref [key kind]
(let [sym (symbol (str "*" (name key) "*"))]
(case kind
:str `(if ~sym {~key (str ~sym)})
:boolean `(if ~sym {~key (boolean (#{"1" "true" "yes"} (string/lower-case ~sym)))})
:int `(if ~sym {~key (Integer/parseInt ~sym)}))))

(defmacro gen-static-prefs []
(merge {}
(static-pref :agent-host :str)
(static-pref :agent-port :int)
(static-pref :agent-verbose :boolean)
(static-pref :agent-auto-reconnect :boolean)
(static-pref :agent-response-timeout :int)
(static-pref :weasel-verbose :boolean)
(static-pref :weasel-auto-reconnect :boolean)
(static-pref :weasel-pre-eval-delay :int)
(static-pref :install-check-total-time-limit :int)
(static-pref :install-check-next-trial-waiting-time :int)
(static-pref :install-check-eval-time-limit :int)
(static-pref :context-availability-total-time-limit :int)
(static-pref :context-availability-next-trial-waiting-time :int)
(static-pref :eval-time-limit :int)
(static-pref :runtime-tag :str)
(static-pref :silence-use-of-undeclared-var-warnings :boolean)
(static-pref :silence-no-such-namespace-warnings :boolean)))
(defmacro gen-static-config []
(let [env-config (utils/read-env-config env-config-prefix)
runtime-tag-config {:runtime-tag (attempt-to-determine-runtime-tag)}]
(utils/deep-merge-ignoring-nils {} runtime-tag-config env-config)))
9 changes: 6 additions & 3 deletions src/runtime/dirac/runtime/prefs.cljs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
(ns dirac.runtime.prefs
(:require-macros [dirac.runtime.prefs :refer [gen-static-prefs]]))
(:require-macros [dirac.runtime.prefs :refer [gen-static-config]]))

(def known-features [:repl])
(def default-features [:repl])
(def feature-groups {:all known-features
:default default-features})
(def reset-styles "color:inherit;background-color:none;font-weight:normal;text-decoration:none;font-style:normal")

; you can override individual config keys via ENV variables, for example:
; DIRAC_RUNTIME/AGENT_PORT=1234 or DIRAC_RUNTIME/NREPL_CONFIG/WEASEL_REPL/RANGE=20
;
; see https://github.com/binaryage/env-config
(def default-prefs
{; you can specify a list/vector of features from known-features or a keyword from feature-groups
:features-to-install :default
Expand Down Expand Up @@ -65,8 +69,7 @@
:silence-use-of-undeclared-var-warnings true
:silence-no-such-namespace-warnings true})


(def static-prefs (gen-static-prefs)) ; this config is comming from environment and system properties
(def static-prefs (gen-static-config)) ; this config is comming from environment and system properties

(def current-prefs (atom (merge default-prefs static-prefs)))

Expand Down
35 changes: 1 addition & 34 deletions src/runtime/dirac/runtime/repl.clj
Original file line number Diff line number Diff line change
@@ -1,37 +1,4 @@
(ns dirac.runtime.repl
(:require [environ.core :refer [env]]))

(def ^:dynamic *agent-host* (env :dirac-agent-host))
(def ^:dynamic *agent-port* (env :dirac-agent-port))
(def ^:dynamic *agent-verbose* (env :dirac-agent-verbose))
(def ^:dynamic *agent-auto-reconnect* (env :dirac-agent-auto-reconnect))
(def ^:dynamic *agent-response-timeout* (env :dirac-agent-response-timeout))
(def ^:dynamic *weasel-verbose* (env :dirac-weasel-verbose))
(def ^:dynamic *weasel-auto-reconnect* (env :dirac-weasel-auto-reconnect))
(def ^:dynamic *weasel-pre-eval-delay* (env :dirac-weasel-pre-eval-delay))
(def ^:dynamic *install-check-total-time-limit* (env :dirac-install-check-total-time-limit))
(def ^:dynamic *install-check-next-trial-waiting-time* (env :dirac-install-check-next-trial-waiting-time))
(def ^:dynamic *install-check-eval-time-limit* (env :dirac-install-check-eval-time-limit))
(def ^:dynamic *context-availability-total-time-limit* (env :dirac-context-availability-total-time-limit))
(def ^:dynamic *context-availability-next-trial-waiting-time* (env :context-availability-next-trial-waiting-time))
(def ^:dynamic *eval-time-limit* (env :dirac-eval-time-limit))

(defmacro gen-config []
(merge {}
(if *agent-host* [:agent-host (str *agent-host*)])
(if *agent-port* [:agent-port (Integer/parseInt *agent-port*)])
(if *agent-verbose* [:agent-verbose (boolean *agent-verbose*)])
(if *agent-auto-reconnect* [:agent-auto-reconnect (boolean *agent-auto-reconnect*)])
(if *agent-response-timeout* [:agent-response-timeout (Integer/parseInt *agent-response-timeout*)])
(if *weasel-verbose* [:weasel-verbose (boolean *weasel-verbose*)])
(if *weasel-auto-reconnect* [:weasel-auto-reconnect (boolean *weasel-auto-reconnect*)])
(if *weasel-pre-eval-delay* [:weasel-pre-eval-delay (Integer/parseInt *weasel-pre-eval-delay*)])
(if *install-check-total-time-limit* [:install-check-total-time-limit (Integer/parseInt *install-check-total-time-limit*)])
(if *install-check-next-trial-waiting-time* [:install-check-next-trial-waiting-time (Integer/parseInt *install-check-next-trial-waiting-time*)])
(if *install-check-eval-time-limit* [:install-check-eval-time-limit (Integer/parseInt *install-check-eval-time-limit*)])
(if *context-availability-total-time-limit* [:context-availability-total-time-limit (Integer/parseInt *context-availability-total-time-limit*)])
(if *context-availability-next-trial-waiting-time* [:context-availability-next-trial-waiting-time (Integer/parseInt *context-availability-next-trial-waiting-time*)])
(if *eval-time-limit* [:eval-time-limit (Integer/parseInt *eval-time-limit*)])))
(ns dirac.runtime.repl)

(defmacro with-safe-printing [& body]
`(binding [cljs.core/*print-level* (dirac.runtime.prefs/pref :safe-print-level)
Expand Down

0 comments on commit 17ba84d

Please sign in to comment.