-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.clj
105 lines (94 loc) · 2.99 KB
/
util.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
(ns wormbase.util
(:require
[clojure.edn :as edn]
[clojure.java.io :as io]
[clojure.string :as str]
[clojure.walk :as w]
[aero.core :as aero]
[java-time.api :as jt]
[wormbase.ids.core :as wic])
(:import
(java.io File PushbackReader)
(java.util Date)))
(defn read-edn [readable]
(let [edn-read (partial edn/read {:readers *data-readers*})]
(-> readable
io/reader
(PushbackReader.)
(edn-read))))
(defn read-app-config
([]
(read-app-config "config.edn"))
([resource-filename]
(aero/read-config (io/resource resource-filename))))
(defn elide-db-internals
"Remove datomic internal attribute/value pairs from a seq of maps."
[db data]
(w/postwalk (fn presenter [m]
(cond
(and (map? m) (:db/ident m)) (:db/ident m)
(and (map? m)
(= (count m) 1)
(wic/attr-schema-unique? db (-> m first key))) (-> m first val)
(map? m) (dissoc m :db/id :db/txInstant)
:else m))
data))
(defn elide-importer-info
[data]
(reduce-kv (fn masker [m k v]
(if (= (namespace k) "importer")
(dissoc m k v)
(assoc m k v)))
(empty data)
data))
(defn days-ago [n]
(-> (jt/instant)
(jt/minus (jt/days n))
(jt/java-date)))
(defn format-java-date [dt & {:keys [tz fmt]
:or {tz (jt/zone-id)
fmt :iso-date-time}}]
{:pre [(instance? Date dt)]}
(jt/format fmt (-> dt
(jt/instant)
(jt/zoned-date-time tz))))
(defn sort-events-by
"Sort a sequence of mappings representing events in temporal order."
[k events & {:keys [most-recent-first]
:or {most-recent-first false}}]
(let [cmp (if most-recent-first
#(compare %2 %1)
#(compare %1 %2))]
(sort-by k cmp events)))
(defn now
"Return a java date converted from the current time in timezone `tz`."
([tz]
(-> (jt/instant)
(jt/zoned-date-time (jt/zone-id tz))
(jt/java-date)))
([]
(now "UTC")))
(defn datomic-internal-namespaces []
(-> (read-app-config) :datomic :internal-namespaces set))
(defn discard-empty-valued-entries
"Discard nil or blank values from a mapping."
[data]
(reduce-kv (fn [m k v]
(if (or (nil? v)
(and (string? v) (str/blank? v))
;; handle references like [:species/latin-name "C..."]
(and (vector? v) (nil? (second v))))
(dissoc m k)
m))
data
data))
(defn list-resource-files
"Returns a seq of .edn files under dir"
[dir & {:keys [ext]
:or {ext ".edn"}}]
(->> (io/file dir)
file-seq
(filter (fn [^java.io.File f]
(and (.isFile f)
(str/ends-with? (.getName f) ext))))
(map #(.getPath ^File %))))