From 3eb01f8b0611e18d293283f761259961752c3c5c Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 9 Jul 2024 11:43:25 -0500 Subject: Rename script: Give file a more relevant name. Reviewed-by: Munyoki Kilyungi --- README.md | 2 +- json-dump.scm | 73 --------------------------------------------------------- json-to-ttl.scm | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 74 deletions(-) delete mode 100755 json-dump.scm create mode 100755 json-to-ttl.scm diff --git a/README.md b/README.md index 9e68ff9..92d5e31 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ or for a container mkdir ./tmp guix shell -C --network --share=/run/mysqld/ --manifest=manifest.scm export GUILE_LOAD_PATH=.:$GUILE_LOAD_PATH -guile json-dump.scm etc/sample.json tmp/ +guile json-to-ttl.scm etc/sample.json tmp/ ``` That reads the `etc/sample.json` file included in this repository and converts it to an RDF representation that is stored in a file `./tmp/sampledata.ttl`. diff --git a/json-dump.scm b/json-dump.scm deleted file mode 100755 index 0a054c5..0000000 --- a/json-dump.scm +++ /dev/null @@ -1,73 +0,0 @@ -#! /usr/bin/env guile -!# - -(use-modules (json) - (ice-9 ftw) - (ice-9 match) - (transform triples)) - - - -(define %directory - (list-ref (command-line) 2)) - -(define %data-directory - (list-ref (command-line) 1)) - - - -(define (json-metadata->rdf path) - "Given a PATH that contains a json file, fetch the metadata embedded -inside it." - (if (access? path F_OK) - (let* ((data (assoc-ref (call-with-input-file - path - (lambda (port) - (json->scm port))) - "metadata")) - (name (or (assoc-ref data "name") - (assoc-ref data "displayName")))) - (match data - (((key . value) ...) - (map - (lambda (a b) - (format - #f "gn:sampledata_~a gn:sampledata:~A ~a ." - name a (format #f "~s" - (cond ((boolean? b) - (if b "True" "False")) - (else b))))) - key value)))))) - -(define (run-proc-on-files path proc) - (define (enter? name stat result) - (not (member (basename name) '(".git" ".svn" "CVS")))) - (define (leaf name stat result) - (proc name)) - (define (down name stat result) result) - (define (up name stat result) result) - (define (skip name stat result) result) - - ;; Ignore unreadable files/directories but warn the user. - (define (error name stat errno result) - (format (current-error-port) "warning: ~a: ~a~%" - name (strerror errno)) - result) - (file-system-fold enter? leaf down up skip error 0 path)) - -(define (rdf path) - (with-output-to-file - (string-append %directory "/sampledata.ttl") - (lambda () - (prefix "gn:" "") - (newline) - (run-proc-on-files - %data-directory - (lambda (file) - (when (string-suffix? "json" file) - (map (lambda (line) - (display line) - (newline)) - (json-metadata->rdf file)))))))) - -(rdf %data-directory) diff --git a/json-to-ttl.scm b/json-to-ttl.scm new file mode 100755 index 0000000..0a054c5 --- /dev/null +++ b/json-to-ttl.scm @@ -0,0 +1,73 @@ +#! /usr/bin/env guile +!# + +(use-modules (json) + (ice-9 ftw) + (ice-9 match) + (transform triples)) + + + +(define %directory + (list-ref (command-line) 2)) + +(define %data-directory + (list-ref (command-line) 1)) + + + +(define (json-metadata->rdf path) + "Given a PATH that contains a json file, fetch the metadata embedded +inside it." + (if (access? path F_OK) + (let* ((data (assoc-ref (call-with-input-file + path + (lambda (port) + (json->scm port))) + "metadata")) + (name (or (assoc-ref data "name") + (assoc-ref data "displayName")))) + (match data + (((key . value) ...) + (map + (lambda (a b) + (format + #f "gn:sampledata_~a gn:sampledata:~A ~a ." + name a (format #f "~s" + (cond ((boolean? b) + (if b "True" "False")) + (else b))))) + key value)))))) + +(define (run-proc-on-files path proc) + (define (enter? name stat result) + (not (member (basename name) '(".git" ".svn" "CVS")))) + (define (leaf name stat result) + (proc name)) + (define (down name stat result) result) + (define (up name stat result) result) + (define (skip name stat result) result) + + ;; Ignore unreadable files/directories but warn the user. + (define (error name stat errno result) + (format (current-error-port) "warning: ~a: ~a~%" + name (strerror errno)) + result) + (file-system-fold enter? leaf down up skip error 0 path)) + +(define (rdf path) + (with-output-to-file + (string-append %directory "/sampledata.ttl") + (lambda () + (prefix "gn:" "") + (newline) + (run-proc-on-files + %data-directory + (lambda (file) + (when (string-suffix? "json" file) + (map (lambda (line) + (display line) + (newline)) + (json-metadata->rdf file)))))))) + +(rdf %data-directory) -- cgit v1.2.3