-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.clj
74 lines (61 loc) 路 1.93 KB
/
utility.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
(ns epsilon.utility
(:require [me.raynes.fs :as fs]
[taoensso.timbre :as log])
(:import [java.io File]
[clojure.lang ExceptionInfo]))
(defmulti is-ext?
"Check if a given path or file has the given extension."
(fn [path _] (class path)))
(defmethod is-ext? String
[path ext]
(let [ext (if (= \. (first ext)) ext (str "." ext))]
(= ext (fs/extension path))))
(defmethod is-ext? File
[path ext]
(is-ext? (.getAbsolutePath path) ext))
(defmethod is-ext? :default
[_ _]
(throw (ex-info "Unknown type. Only support String and File." {})))
(defn egx?
"Check if a given path or file is an EGX file."
[path]
(is-ext? path ".egx"))
(defn egl?
"Check if a given path or file is an EGL file."
[path]
(is-ext? path ".egl"))
(defn eol?
"Check if a given path or file is an EOL file."
[path]
(is-ext? path ".eol"))
(defn evl?
"Check if a given path or file is an EVL file."
[path]
(is-ext? path ".evl"))
(defn xml?
"Check if a given path or file is an XML file."
[path]
(is-ext? path ".xml"))
(defn replace-ext
"Replace the given file's extension with the given extension.
If the extension is not prefixed with a dot, add one automatically.
Returns the new path with no side-effect (no file created/modified/etc.)"
[target ^String ext]
(let [file-path (if (string? target) target (.getAbsolutePath target))
dot-index (.lastIndexOf file-path ".")
no-ext-file (subs file-path 0 dot-index)]
(if (= \. (first ext))
(str no-ext-file ext)
(str no-ext-file "." ext))))
(defn handle-exception
"Wrap the function f inside a try catch block that will handle exception by logging the data inside it."
[f]
(try
(f)
(catch Exception e
(if (= ExceptionInfo (class e))
(let [payload (:payload (.getData e))]
(if (seq? payload)
(doall (map #(log/error %) payload))
(log/error payload)))
(log/error e)))))