Skip to content

Commit bfdd1a3

Browse files
committed
Added codemeta2cff tool
1 parent 53e7180 commit bfdd1a3

17 files changed

+356
-580
lines changed

CITATION.cff

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
cff-version: 1.1.0
3+
message: "If you use this software, please cite it as below."
4+
authors:
5+
- family-names: Doiel
6+
given-names: Robert
7+
orcid: https://orcid.org/0000-0003-0900-6903
8+
9+
date-released: 2021-07-15

INSTALL.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ The basic recipe
4242
+ Test
4343

4444

45-
### Mac OS X
45+
### Mac OS
4646

4747
1. Download the zip file
4848
2. Unzip the zip file
@@ -127,9 +127,7 @@ downloading the zip file.
127127
Compiling from source
128128
---------------------
129129

130-
_datatools_ is "go gettable" if you have gotten xlsx v1.0.5 package from [github.com/tealeg/xlsx](https://github.com/tealeg/xlsx). The datatools package does not support versions v2.x and greater of xlsx. Below are the steps
131-
I use today with "go get" command to download the dependant packages
132-
as well as _datatools_'s source code.
130+
_datatools_ is "go gettable" if you have previously gotten xlsx v1.0.5 package from [github.com/tealeg/xlsx](https://github.com/tealeg/xlsx). The datatools package does not support versions v2.x and greater of xlsx. Below are the steps I use today with "go get" command to download the dependant packages as well as _datatools_'s source code.
133131

134132
Setting up the right version of xlsx for datatools
135133

Makefile

Lines changed: 78 additions & 311 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Excel Workbooks and plain text files or content.
3131
+ [xlsx2csv](docs/xlsx2csv/) - a tool for converting Excel Workbooks sheets to CSV files
3232
+ [xlsx2json](docs/xlsx2json/) - a tool for converting Excel Workbooks to JSON files
3333
+ [yaml2json](docs/yaml2json/) - a tool for converting YAML files to JSON
34+
+ [codemeta2cff](docs/codemeta2cff) - a tool to convert a codemeta.json file into a CITATION.cff file.
3435

3536

3637
Compiled versions are provided for Linux (amd64), Mac OS X (amd64),

RELEASES.html

Lines changed: 0 additions & 97 deletions
This file was deleted.

RELEASES.md

Lines changed: 0 additions & 43 deletions
This file was deleted.

cmd/codemeta2cff/codemeta2cff.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// codemeta2cff.go converts a codemeta.json file to CITATION.cff.
2+
//
3+
// Author: R. S. Doiel <[email protected]>
4+
//
5+
// Copyright (c) 2021, Caltech
6+
// All rights not granted herein are expressly reserved by Caltech.
7+
//
8+
// Redistribution and use in source and binary forms, with or without
9+
// modification, are permitted provided that the following conditions are met:
10+
//
11+
// 1. Redistributions of source code must retain the above copyright notice,
12+
// this list of conditions and the following disclaimer.
13+
//
14+
// 2. Redistributions in binary form must reproduce the above copyright notice,
15+
// this list of conditions and the following disclaimer in the documentation
16+
// and/or other materials provided with the distribution.
17+
//
18+
// 3. Neither the name of the copyright holder nor the names of its contributors
19+
// may be used to endorse or promote products derived from this software without
20+
// specific prior written permission.
21+
//
22+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
// POSSIBILITY OF SUCH DAMAGE.
33+
//
34+
package main
35+
36+
import (
37+
"flag"
38+
"fmt"
39+
"os"
40+
"path"
41+
42+
// Caltech Library Package
43+
"github.com/caltechlibrary/datatools"
44+
)
45+
46+
func usage(appName string, exitCode int) {
47+
out := os.Stderr
48+
if exitCode == 0 {
49+
out = os.Stdout
50+
}
51+
fmt.Fprintf(out, `
52+
USAGE: %s
53+
54+
%s
55+
56+
Reads codemeta.json file and writes CITATION.cff.
57+
58+
OPTIONS
59+
60+
-h, -help display help
61+
62+
EXAMPLE
63+
64+
%s
65+
66+
datatools v%s
67+
`, appName, appName, appName, datatools.Version)
68+
os.Exit(exitCode)
69+
}
70+
71+
func main() {
72+
// command line name and options support
73+
appName := path.Base(os.Args[0])
74+
help, version := false, false
75+
args := []string{}
76+
// Setup to parse command line
77+
flag.BoolVar(&help, "h", false, "display help")
78+
flag.BoolVar(&help, "help", false, "display help")
79+
flag.BoolVar(&version, "version", false, "display version")
80+
flag.Parse()
81+
82+
args = flag.Args()
83+
84+
// Process options and run report
85+
if help {
86+
usage(appName, 0)
87+
}
88+
if version {
89+
fmt.Printf("datatools, %s v%s\n", appName, datatools.Version)
90+
os.Exit(0)
91+
}
92+
if len(args) != 0 {
93+
fmt.Printf("This command always reads codemeta.json creating/replacing CITATION.cff\n\n")
94+
usage(appName, 1)
95+
}
96+
err := datatools.CodemetaToCitationCff("codemeta.json", "CITATION.cff")
97+
if err != nil {
98+
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
99+
os.Exit(1)
100+
}
101+
}

codemeta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"codeRepository": "https://github.com/caltechlibrary/datatools",
77
"issueTracker": "https://github.com/caltechlibrary/datatools/issues",
88
"license": "https://data.caltech.edu/license",
9-
"version": "1.0.2",
9+
"version": "1.0.3",
1010
"author": [
1111
{
1212
"@type": "Person",

codemeta/codemeta.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package codemeta
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"strings"
7+
"time"
8+
9+
"github.com/caltechlibrary/doitools"
10+
)
11+
12+
type PersonOrOrganization struct {
13+
// ORCID is use for person id
14+
Id string `json:"@id"`
15+
Type string `json:"@type"`
16+
// Name is used by organizations
17+
Name string `json:"name,omitempty"`
18+
// Given/Family are used by individual persons
19+
GivenName string `json:"givenName,omitempty"`
20+
FamilyName string `json:"familyName,omitempty"`
21+
Affiliation string `json:"affiliation"`
22+
Email string `json:"email"`
23+
}
24+
25+
type Codemeta struct {
26+
// Terms used by Caltech Library codemeta.json
27+
// Id should be the DOI if available
28+
Context string `json:"@context"`
29+
Type string `json:"@type"`
30+
Name string `json:"name"`
31+
Description string `json:"description"`
32+
CodeRepository string `json:"codeRepository"`
33+
IssueTracker string `json:"issueTracker"`
34+
License string `json:"license"`
35+
Version string `json:"version"`
36+
Author []*PersonOrOrganization `json:"author"`
37+
Contributor []*PersonOrOrganization `json:"contributor,omitempty"`
38+
Editor []*PersonOrOrganization `json:"editor,omitempty"`
39+
DevelopmentStatus string `json:"developmentStatus"`
40+
DownloadURL string `json:"downloadUrl"`
41+
Keywords []string `json:"keywords"`
42+
Maintainer string `json:"maintainer,omitempty"`
43+
Funder []*PersonOrOrganization `json:"funder,omitempty"`
44+
CopyrightHolder []*PersonOrOrganization `json:"copyrightHolder,omitempty"`
45+
CopyrightYear string `json:"copyrightYear,omitempty"`
46+
Created string `json:"dateCreated,omitempty"`
47+
Updated string `json:"dateModified,omitempty"`
48+
Published string `json:"datePublished,omitempty"`
49+
Identifier string `json:"identifier,omitempty"` //FIXME: Is this where I can put the DOI
50+
51+
// Additional codemeta Terms are defined at https://codemeta.github.io/terms/
52+
}
53+
54+
func (person *PersonOrOrganization) ToJSON() ([]byte, error) {
55+
return json.MarshalIndent(person, "", "\t")
56+
}
57+
58+
func (cm *Codemeta) ToJSON() ([]byte, error) {
59+
return json.MarshalIndent(cm, "", "\t")
60+
}
61+
62+
func (person *PersonOrOrganization) ToCFF() ([]byte, error) {
63+
if (person.FamilyName == "") || (person.GivenName == "") || (strings.HasPrefix(person.Id, "https://orcid.org/") == false) {
64+
return []byte(""), fmt.Errorf("Missing family name, given name or ORCID")
65+
}
66+
return []byte(fmt.Sprintf(`
67+
- family-names: %s
68+
given-names: %s
69+
orcid: %s`, person.FamilyName, person.GivenName, person.Id)), nil
70+
}
71+
72+
// ToCff crosswalks a Codemeta data structure rendering
73+
// CITATION.cff document as an array of byte.
74+
// Based on documentation at https://citation-file-format.github.io/
75+
func (cm *Codemeta) ToCff() ([]byte, error) {
76+
src := []byte(`
77+
cff-version: 1.1.0
78+
message: "If you use this software, please cite it as below."
79+
authors:`)
80+
for _, person := range cm.Author {
81+
if text, err := person.ToCFF(); err == nil {
82+
src = append(src, text...)
83+
}
84+
}
85+
if strings.HasPrefix(cm.Identifier, "https://doi.org/") {
86+
if doi, err := doitools.NormalizeDOI(cm.Identifier); err == nil {
87+
src = append(src, []byte(fmt.Sprintf(`
88+
doi: %s`, doi))...)
89+
}
90+
}
91+
if cm.Published != "" {
92+
src = append(src, []byte(fmt.Sprintf(`
93+
date-released: %s`, cm.Published))...)
94+
} else {
95+
now := time.Now()
96+
dt := now.Format("2006-01-02")
97+
src = append(src, []byte(fmt.Sprintf(`
98+
date-released: %s`, dt))...)
99+
}
100+
return src, nil
101+
}

0 commit comments

Comments
 (0)