|
11 | 11 | [naga.data :as data] |
12 | 12 | [naga.storage.memory.core]) |
13 | 13 | (:import [clojure.lang ExceptionInfo] |
| 14 | + [java.net URI] |
14 | 15 | [java.io File])) |
15 | 16 |
|
16 | | -(def stores (map name (keys @store/registered-stores))) |
| 17 | +(def stores (set (map name (keys @store/registered-stores)))) |
17 | 18 |
|
18 | 19 | (def valid-output? #(.exists (.getParentFile (.getAbsoluteFile (File. %))))) |
19 | 20 | (def valid-input? #(.exists (File. %))) |
20 | 21 |
|
| 22 | +(def uri #(try (URI. %) (catch Exception _))) |
| 23 | + |
21 | 24 | (def cli-options |
22 | 25 | [[nil "--storage STRING" "Select store type" |
23 | | - :validate [(set stores) "Must be a registered storage type."]] |
| 26 | + :validate [stores "Must be a registered storage type."]] |
| 27 | + [nil "--uri STRING" "URI for storage" |
| 28 | + :parse-fn uri |
| 29 | + :validate [identity "Invalid storage URL"]] |
| 30 | + [nil "--init STRING" "Initialization data" |
| 31 | + :validate [valid-input? "Invalid initialization data file"]] |
24 | 32 | [nil "--json STRING" "Filename for input json" |
25 | 33 | :validate [valid-input? "Input file does not exist."]] |
26 | 34 | [nil "--out STRING" "Filename for output json" |
|
43 | 51 | (str "Store types: " (vec stores)) |
44 | 52 | ""])) |
45 | 53 |
|
| 54 | +(defn storage-configuration |
| 55 | + "Reads storage parameters, and builds an appropriate configuration structure" |
| 56 | + [storage uri init] |
| 57 | + (let [store-from-uri (fn [u] |
| 58 | + ;; may want this to be more complex in future |
| 59 | + (let [s (.getScheme u)] |
| 60 | + (stores s))) |
| 61 | + store-type (and storage (store-from-uri uri))] |
| 62 | + (when (and uri (nil? store-type)) (exit "Unable to determine storage type for: " uri)) |
| 63 | + {:type (or store-type :memory) |
| 64 | + :uri uri |
| 65 | + :init init})) |
| 66 | + |
46 | 67 | (defn run-all |
47 | 68 | "Runs a program, and returns the data processed, the results, and the stats. |
48 | 69 | Takes an input stream. Returns a map of: |
|
101 | 122 |
|
102 | 123 | (defn json-program |
103 | 124 | "Runs a program over data in a JSON file" |
104 | | - [in-stream json-file out-file storage] |
| 125 | + [in-stream json-file out-file storage uri] |
105 | 126 | (when-not out-file |
106 | 127 | (exit 2 "No output json file specified")) |
107 | | - (let [; TODO: handle storage URIs to determine type and connection |
108 | | - ; fresh-store (instantiate-storage storage) |
109 | | - fresh-store (store/get-storage-handle {:type :memory}) |
| 128 | + (let [store-config (storage-configuration storage uri) |
| 129 | + fresh-store (store/get-storage-handle store-config) |
110 | 130 | {:keys [rules axioms]} (pabu/read-stream in-stream) |
111 | 131 |
|
112 | 132 | basic-store (store/assert-data fresh-store axioms) |
|
125 | 145 |
|
126 | 146 | (defn -main [& args] |
127 | 147 | (try |
128 | | - (let [{{:keys [halp json out storage]} :options, |
| 148 | + (let [{{:keys [halp json out storage uri]} :options, |
129 | 149 | arguments :arguments :as opts} (parse-opts args cli-options)] |
130 | 150 |
|
131 | 151 | (when halp (exit 1 (usage opts))) |
132 | 152 | (with-open [in-stream (if-let [filename (first arguments)] |
133 | 153 | (io/input-stream filename) |
134 | 154 | *in*)] |
135 | 155 | (if json |
136 | | - (json-program in-stream json out storage) |
| 156 | + (json-program in-stream json out storage uri) |
137 | 157 | (logic-program in-stream)))) |
138 | 158 | (catch ExceptionInfo e |
139 | 159 | (binding [*out* *err*] |
|
0 commit comments