From b89723ac36f7eb7636b3869f0a4e348c647a9d91 Mon Sep 17 00:00:00 2001 From: Steve Robinson Date: Mon, 4 Dec 2023 16:37:34 +0000 Subject: [PATCH 1/2] Add a command line argument to specify the base uri --- src/csv2rdf/csvw.clj | 4 ++-- src/csv2rdf/main.clj | 11 +++++++---- src/csv2rdf/metadata.clj | 7 ++----- src/csv2rdf/tabular/processing.clj | 11 ++++++----- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/csv2rdf/csvw.clj b/src/csv2rdf/csvw.clj index 9ee503cf..4e564612 100644 --- a/src/csv2rdf/csvw.clj +++ b/src/csv2rdf/csvw.clj @@ -58,9 +58,9 @@ like `:minimal`, but it also includes RDF data from the CSVW metadata json file." ([tabular-source metadata-source] (csv->rdf tabular-source metadata-source {})) - ([tabular-source metadata-source {:keys [mode] :as options}] + ([tabular-source metadata-source {:keys [mode base-uri] :as options}] (let [mode (or mode :standard) - {:keys [tables] :as metadata} (processing/get-metadata tabular-source metadata-source) + {:keys [tables] :as metadata} (processing/get-metadata tabular-source metadata-source base-uri) table-group-dialect (:dialect metadata) output-tables (remove properties/suppress-output? tables) {:keys [statements] :as ctx} (table-group-context mode metadata) diff --git a/src/csv2rdf/main.clj b/src/csv2rdf/main.clj index eacf65b3..89fee299 100644 --- a/src/csv2rdf/main.clj +++ b/src/csv2rdf/main.clj @@ -15,6 +15,7 @@ [["-t" "--tabular TABULAR" "Location of the tabular file"] ["-u" "--user-metadata METADATA" "Location of the metadata file"] ["-o" "--output-file OUTPUT" "Output file to write to"] + ["-b" "--base-uri URI" "The base uri to use"] ["-m" "--mode MODE" "CSVW mode to run" :validate [#(contains? #{:minimal :standard :annotated} %)] :default :standard @@ -51,9 +52,10 @@ :summary summary})) options))) -(defn- write-output [writer {:keys [rdf-format tabular-source metadata-source mode]}] +(defn- write-output [writer {:keys [rdf-format tabular-source metadata-source mode base-uri]}] (let [dest (gio/rdf-writer writer :format rdf-format :prefixes nil)] - (csvw/csv->rdf->destination tabular-source metadata-source dest {:mode mode}))) + (csvw/csv->rdf->destination tabular-source metadata-source dest {:mode mode + :base-uri base-uri}))) (defmulti display-error "Displays an exception in the UI" @@ -71,11 +73,12 @@ (defn- inner-main [args] (let [options (parse-cli-options args) - {:keys [mode tabular user-metadata output-file]} options + {:keys [mode tabular user-metadata output-file base-uri]} options opts {:tabular-source (some-> tabular parse-source) :metadata-source (some-> user-metadata parse-source) :rdf-format (or (some-> output-file formats/->rdf-format) RDFFormat/TURTLE) - :mode mode} + :mode mode + :base-uri (some-> base-uri (URI.))} output-file (some-> output-file io/file)] (if output-file (with-open [w (io/writer output-file)] diff --git a/src/csv2rdf/metadata.clj b/src/csv2rdf/metadata.clj index 3cd0379c..f075c9c5 100644 --- a/src/csv2rdf/metadata.clj +++ b/src/csv2rdf/metadata.clj @@ -18,12 +18,9 @@ :else (make-error context "Expected top-level of metadata document to describe a table or table group")))) -(defn parse-table-group-from-source [source] +(defn parse-table-group-from-source [source base-uri] (let [json (source/get-json source)] - (parse-metadata-json (source/->uri source) json))) - -(s/fdef parse-table-group-from-source - :args (s/cat :source (s/and ::source/uriable ::source/json-source))) + (parse-metadata-json (or base-uri (source/->uri source)) json))) (defn ^{:tabular-spec "5.1"} overriding-metadata "Construct an 'overriding metadata' document if the user provides both tabular and diff --git a/src/csv2rdf/tabular/processing.clj b/src/csv2rdf/tabular/processing.clj index fe378766..e31e72c8 100644 --- a/src/csv2rdf/tabular/processing.clj +++ b/src/csv2rdf/tabular/processing.clj @@ -17,23 +17,24 @@ (table/validate-compatible validating? user-table table-metadata) (table/compatibility-merge user-table table-metadata))) -(defn- from-metadata-source [metadata-source] - (let [{:keys [tables] :as user-table-group} (meta/parse-table-group-from-source metadata-source) +(defn- from-metadata-source [metadata-source base-uri] + (let [{:keys [tables] :as user-table-group} (meta/parse-table-group-from-source metadata-source base-uri) validating? false merged-tables (mapv (fn [table] (validate-merge-table validating? table)) tables) merged-table-group (assoc user-table-group :tables merged-tables)] (set-table-group-parents merged-table-group))) (defn ^{:tabular-spec "6.1"} get-metadata + "Retrieves and resolves the metadata given either a tabular data source or metadata source. If user metadata is provided, each referenced table definition is validated against the corresponding tabular data file." - [tabular-source metadata-source] + [tabular-source metadata-source base-uri] (cond (and (some? tabular-source) (some? metadata-source)) - (from-metadata-source (meta/overriding-metadata tabular-source metadata-source)) + (from-metadata-source (meta/overriding-metadata tabular-source metadata-source) base-uri) (some? metadata-source) - (from-metadata-source metadata-source) + (from-metadata-source metadata-source base-uri) (some? tabular-source) (from-tabular-source tabular-source) From 4ff340a455c285c363c75a4c388366b518052e20 Mon Sep 17 00:00:00 2001 From: Steve Robinson Date: Mon, 4 Dec 2023 16:39:27 +0000 Subject: [PATCH 2/2] Remove unused binding --- src/csv2rdf/csvw.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/csv2rdf/csvw.clj b/src/csv2rdf/csvw.clj index 4e564612..5d8f0422 100644 --- a/src/csv2rdf/csvw.clj +++ b/src/csv2rdf/csvw.clj @@ -58,7 +58,7 @@ like `:minimal`, but it also includes RDF data from the CSVW metadata json file." ([tabular-source metadata-source] (csv->rdf tabular-source metadata-source {})) - ([tabular-source metadata-source {:keys [mode base-uri] :as options}] + ([tabular-source metadata-source {:keys [mode base-uri]}] (let [mode (or mode :standard) {:keys [tables] :as metadata} (processing/get-metadata tabular-source metadata-source base-uri) table-group-dialect (:dialect metadata)