-
Notifications
You must be signed in to change notification settings - Fork 4
/
core.clj
213 lines (171 loc) · 5.83 KB
/
core.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
(ns tools.io.core
(:require [clojure.java.io :as io]
[clojure.string :as str])
(:import [java.io File Closeable Reader Writer]
[java.util.zip GZIPInputStream GZIPOutputStream])
(:gen-class))
(defonce file-preds (atom {}))
(defn register-file-pred!
"Extend the dispatch function of the multimethods with your own
implementation.
Example:
(register-file-pred!
:my-filetype
(fn [filename] (clojure.string/starts-with? (str filename) \"myprotocol://\")))
(defmethod mk-input-stream :my-filetype
[filename & [options]]
;; ...
{:stream ...}
)
The code above is sufficient to get all reading function for free on your
custom protocol, given that mk-input-stream works correctly."
[file-type file-pred]
(swap! file-preds assoc file-type file-pred))
(defn unregister-file-pred!
[file-type]
(swap! file-preds dissoc file-type))
(defn get-file-type
"Dispatch function for files IO multimethods."
([filename]
(or (some (fn [[file-type file-pred]]
(when (file-pred filename)
file-type)) @file-preds)
(throw (Exception. (str "unsupported protocol for " filename ". perhaps you forgot to require the proper extension (eg.: tools.io.gs)")))))
([filename _ & _]
(get-file-type filename)))
(defn- mk-file-protocol-checker
[protocols]
{:pre [(set? protocols)]}
(fn [filename]
(when-not (instance? java.io.Reader filename)
(let [filename (str filename)]
(or (not (str/includes? filename "://"))
(-> filename
(str/split #"://" 2)
first
str/lower-case
protocols))))))
(defmulti mk-input-stream
"Returns an input stream with any implementation."
get-file-type)
(defmulti mk-output-stream
"Returns an output stream with any implementation."
get-file-type)
(defn gzipped?
"Tests if a filename ends with .gz or .gzip"
[filename]
(re-find #"(?i)\.gz(?:ip)?$" (str filename)))
(defn input-stream
"Returns an input-stream, with support of gzip compression."
([filename] (input-stream filename nil))
([filename options]
(let [is (mk-input-stream filename options)]
(if (gzipped? filename)
(update is :stream #(GZIPInputStream. %))
is))))
(defn output-stream
"Returns an output-stream, with support of gzip compression."
([filename] (output-stream filename nil))
([filename options]
(let [os (mk-output-stream filename options)]
(if (gzipped? filename)
(update os :stream #(GZIPOutputStream. %))
os))))
(defn file-reader
"Return a file reader"
([filename] (file-reader filename nil))
([filename options]
(let [file (input-stream filename options)]
(update file :stream #(io/reader % :encoding (:encoding options "UTF-8"))))))
(defn file-writer
"Return a file writer"
([filename] (file-writer filename nil))
([filename options]
(let [file (output-stream filename options)]
(update file :stream #(io/writer % :encoding (:encoding options "UTF-8"))))))
(defn close!
"Close a stream and optionally call its close-fn function if it's present."
[{:keys [^Closeable stream close-fn] :as file}]
(.close stream)
(when close-fn (close-fn file)))
(defmulti list-files
"Returns a seq of filenames with provided path as prefix."
get-file-type)
(defmulti delete-file
"Deletes a file with any implementation."
get-file-type)
(defmulti exists?
"Returns `true` if filename exists"
get-file-type)
;;Shamefully copied from clojure.java.io/do-copy because we hardly can reuse the do-copy multi-fn
(defn copy
[^Reader input ^Writer output opts]
(let [^"[C" buffer (make-array Character/TYPE (:buffer-size opts 4096))]
(loop []
(let [size (.read input buffer)]
(when (pos? size)
(.write output buffer 0 size)
(recur))))))
;; Default Hooks
;; =============
;; On-Disk Files
;; -------------
(register-file-pred!
:base (mk-file-protocol-checker #{"file" ""}))
(defmethod mk-input-stream :base
[filename & [options]]
{:stream (io/input-stream filename :encoding (:encoding options "UTF-8"))})
(defmethod mk-output-stream :base
[filename & [options]]
{:stream (io/output-stream filename :append (:append options false))})
(defmethod list-files :base
[path & [_options]]
(let [^File f (io/file path)]
(cond
(.isDirectory f)
(->> (file-seq f)
(filter #(.isFile ^File %))
(map #(.getPath ^File %)))
;; trailing slash but not a directory: nil
(str/ends-with? path File/separator)
nil
:else
(when-let [parent (.getParentFile f)]
(let [prefix (.getName f)]
(->> (.listFiles parent)
(filter (fn [^File file]
(and (.isFile file)
(str/starts-with? (.getName file) prefix))))
(map (fn [^File f] (.getPath f)))))))))
(defmethod delete-file :base
[path & [options]]
(io/delete-file path (:silently options false)))
(defmethod exists? :base
[filename & [_options]]
(when filename
(try
(.exists (io/as-file filename))
(catch Exception _ false))))
;; HTTP & HTTPS
;; ------------
;; Basic implementation.
(register-file-pred!
:http (mk-file-protocol-checker #{"http" "https"}))
(defmethod mk-input-stream :http
[filename & [options]]
{:stream (io/input-stream filename :encoding (:encoding options "UTF-8"))})
(defmethod exists? :http
[filename & [options]]
(when filename
(try
(with-open [stream (io/input-stream filename :encoding (:encoding options "UTF-8"))]
(pos? (.available stream)))
(catch Exception _ false))))
;; STDIN
;; -----
(register-file-pred!
:stdin (fn [filename]
(= *in* filename)))
(defmethod mk-input-stream :stdin
[filename & [_options]]
{:stream filename :close-fn (fn noop [_])})