Skip to content
Snippets Groups Projects
Commit 9dd8fc27 authored by Mike Lynch's avatar Mike Lynch
Browse files

A lot of work on making this more production-ready

parent a18de9e0
Branches
Tags
No related merge requests found
......@@ -2,6 +2,7 @@
const datacrate = require('./lib/catalog.js');
const calcyte = require('calcyte');
const fs = require('fs-extra');
const path = require('path');
const ArgumentParser = require('argparse').ArgumentParser;
......@@ -16,12 +17,16 @@ const ORGANISATION = {
const OWNER = 'Kathryn.T.Braye@student.uts.edu.au';
const APPROVER = 'Sharyn.Wise@uts.edu.au';
const CITATION = 'Citation';
const ZIP_PATH = 'https://test/'
async function makedatacrate(doi, datapub, dest) {
async function makecatalog(doi, datapub, dest, zip) {
await fs.ensureDir(dest);
const catalog = await datacrate.datapub2catalog({
id: doi,
bagged: true,
zip: zip,
datapub: datapub,
organisation: ORGANISATION,
owner: OWNER,
......@@ -29,11 +34,35 @@ async function makedatacrate(doi, datapub, dest) {
});
const catfile = path.join(dest, 'CATALOG.json');
await fs.writeFile(catfile, JSON.stringify(catalog, null, 2));
return catalog;
}
async function convertdatapub(pid, jsonfile, dest) {
async function makedatacrate(catalog, dir, citation, zip) {
const index = new calcyte.Index();
index.init(catalog, dir, true);
console.log("Writing DataCrate");
index.make_index_html(citation, zip);
}
// const catalog_json = path.join(dir, sails.config.datapubs.datacrate.catalog_json);
// sails.log.info(`Building CATALOG.json with jsonld_h`);
// sails.log.silly(`catalog = ${JSON.stringify(catalog)}`);
// sails.log.info(`Writing CATALOG.json to ${catalog_json}`);
// fs.writeFileSync(catalog_json, JSON.stringify(catalog, null, 2));
// const index = new Index();
// index.init(catalog, dir, false);
// sails.log.info(`Writing CATALOG.html`);
// index.make_index_html("text_citation", "zip_path"); //writeFileSync
// return Observable.of({});
async function convertdatapub(pid, jsonfile, dest, zip) {
const datapub = await fs.readJSON(jsonfile);
await makedatacrate(pid, datapub, dest);
const citation = datapub['citation_doi'];
const catalogfile = await makecatalog(pid, datapub, dest, zip);
await makedatacrate(catalogfile, dest, citation, zip);
}
......@@ -58,6 +87,14 @@ parser.addArgument(
required: true
});
parser.addArgument(
['-z', '--zip'],
{
help: "The URL of the zipped datacrate when it is published",
defaultValue: ZIP_PATH
}
);
parser.addArgument(
['-d', '--directory'],
......@@ -69,7 +106,8 @@ parser.addArgument(
var args = parser.parseArgs();
convertdatapub(args['persistentid'], args['input'], args['directory']);
convertdatapub(args['persistentid'], args['input'], args['directory'], args['zip']);
......@@ -40,7 +40,8 @@ const RELATED_TYPES = {
'services': 'CreativeWork' // I'm not sure about this?
};
const ROOT_PATH = 'data/';
const ROOT_PATH_BAGGED = 'data/';
const ROOT_PATH_WORKING = './';
const FUNDING_FIELDS = [ 'foaf:fundedBy_foaf:Agent', 'foaf:fundedBy_vivo:Grant' ];
......@@ -124,10 +125,12 @@ async function datapub2catalog(options) {
const id = options['id'];
const datapub = options['datapub'];
const bagged = options['bagged'];
const zip = options['zip'];
const org = options['organisation'];
const owner = options['owner'];
const approver = options['approver'];
const dates = dates_default(options['dates'])
const dates = dates_default(options['dates']);
const prefixes = options['prefixes'] || DEFAULT_IRI_PREFS;
const organisation = {
......@@ -146,7 +149,7 @@ async function datapub2catalog(options) {
var people = make_creators(datapub, organisation);
const files = make_files(datapub);
const dataset = make_dataset(id, datapub, organisation, dates, files, people);
const dataset = make_dataset(id, datapub, bagged, organisation, dates, files, people);
const [ history, more_people ] = make_history(
dataset, people, owner, approver, dates
);
......@@ -195,10 +198,31 @@ async function datapub2catalog(options) {
})
}
if( zip ) {
if( !dataset["distribution"] ) {
dataset["distribution"] = []
}
dataset["distribution"].push(
{
"@id": zip
}
);
graph.push(
{
"@id": zip,
"contentUrl": zip,
"@type": "DataDownload",
"encodingFormat": "zip"
});
context["DataDownload"] = "https://schema.org/DataDownload";
context["distribution"] = "https://schema.org/distribution";
}
const catalog = {
'@context': context,
'@graph': graph.filter((e) => e)
};
const jsonld = new calcyte.jsonld();
jsonld.init(catalog);
jsonld.trim_context();
......@@ -227,11 +251,13 @@ function link_id(item) {
// TODO: isBasedOn the data record
function make_dataset(id, datapub, organisation, dates, files, creators) {
// always does data/ as the root path
function make_dataset(id, datapub, bagged, organisation, dates, files, creators) {
const ds = {
'@id': id,
'@type': 'Dataset',
'path': ROOT_PATH,
'path': bagged ? ROOT_PATH_BAGGED : ROOT_PATH_WORKING,
'name': datapub['title'],
'description': datapub['description'],
'dateCreated': dates['dateCreated'],
......@@ -287,7 +313,7 @@ function make_history(dataset, people, owner, approver, dates) {
const history = [
{
'@id': dataset['@id'] + '_history1',
'@id': 'history1',
'@type': 'CreateAction',
'name': 'Create',
'description': 'Data record created',
......@@ -296,7 +322,7 @@ function make_history(dataset, people, owner, approver, dates) {
'agent': link_id(owner_item)
},
{
'@id': dataset['@id'] + '_history2',
'@id': 'history2',
'@type': 'UpdateAction',
'name': 'Publish',
'endTime': dates['datePublished'],
......
This diff is collapsed.
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment