Skip to content

Commit

Permalink
add cessate tool
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisaaronland committed Sep 30, 2021
1 parent 44a3777 commit 793bb85
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 6 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ cli:
go build -mod vendor -o bin/exportify cmd/exportify/main.go
go build -mod vendor -o bin/create cmd/create/main.go
go build -mod vendor -o bin/deprecate cmd/deprecate/main.go
go build -mod vendor -o bin/cessate cmd/cessate/main.go
go build -mod vendor -o bin/ensure-properties cmd/ensure-properties/main.go
go build -mod vendor -o bin/deprecate-and-supersede cmd/deprecate-and-supersede/main.go
go build -mod vendor -o bin/merge-feature-collection cmd/merge-feature-collection/main.go
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,40 @@ $> ./bin/assign-parent \
-id 1477855939 -id 1477855941 -id 1477855943 -id 1477855945 -id 1477855947 -id 1477855949 1477855955
```

### cessate

"Cessate" one or more Who's On First IDs (assign an `edtf:cessation` property and assign `mz:is_current=0`).

```
> ./bin/cessate -h
"Cessate" one or more Who's On First IDs.
Usage:
./bin/cessate [options]
For example:
./bin/cessate -s . -i 1234
./bin/cessate -reader-uri fs:///usr/local/data/whosonfirst-data-admin-ca/data -id 1234 -id 5678
Valid options are:
-date string
A valid EDTF date. If empty then the current date will be used
-exporter-uri string
A valid whosonfirst/go-whosonfirst-export URI. (default "whosonfirst://")
-i string
A valid Who's On First ID.
-id value
One or more Who's On First IDs. If left empty the value of the -i flag will be used.
-reader-uri string
A valid whosonfirst/go-reader URI. If empty the value of the -s flag will be used in combination with the fs:// scheme.
-s string
A valid path to the root directory of the Who's On First data repository. If empty (and -reader-uri or -writer-uri are empty) the current working directory will be used and appended with a 'data' subdirectory.
-superseded-by value
Zero or more Who's On First IDs that the records being deprecated are superseded by.
-writer-uri string
A valid whosonfirst/go-writer URI. If empty the value of the -s flag will be used in combination with the fs:// scheme.
```

### create

Create a new Who's On First record.
Expand Down
189 changes: 189 additions & 0 deletions cmd/cessate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package main

import (
"context"
"flag"
"fmt"
"github.com/sfomuseum/go-edtf"
"github.com/sfomuseum/go-edtf/parser"
"github.com/sfomuseum/go-flags/multi"
"github.com/tidwall/gjson"
"github.com/whosonfirst/go-reader"
"github.com/whosonfirst/go-whosonfirst-export/v2"
"github.com/whosonfirst/go-whosonfirst-exportify"
wof_reader "github.com/whosonfirst/go-whosonfirst-reader"
"github.com/whosonfirst/go-writer"
"log"
"os"
"path/filepath"
"time"
)

func main() {

source := flag.String("s", "", "A valid path to the root directory of the Who's On First data repository. If empty (and -reader-uri or -writer-uri are empty) the current working directory will be used and appended with a 'data' subdirectory.")
id := flag.String("i", "", "A valid Who's On First ID.")

date := flag.String("date", "", "A valid EDTF date. If empty then the current date will be used")

reader_uri := flag.String("reader-uri", "", "A valid whosonfirst/go-reader URI. If empty the value of the -s flag will be used in combination with the fs:// scheme.")
writer_uri := flag.String("writer-uri", "", "A valid whosonfirst/go-writer URI. If empty the value of the -s flag will be used in combination with the fs:// scheme.")

exporter_uri := flag.String("exporter-uri", "whosonfirst://", "A valid whosonfirst/go-whosonfirst-export URI.")

var ids multi.MultiInt64
flag.Var(&ids, "id", "One or more Who's On First IDs. If left empty the value of the -i flag will be used.")

var superseded_by multi.MultiInt64
flag.Var(&superseded_by, "superseded-by", "Zero or more Who's On First IDs that the records being deprecated are superseded by.")

flag.Usage = func() {

fmt.Fprintf(os.Stderr, "\"Cessate\" one or more Who's On First IDs.\n\n")
fmt.Fprintf(os.Stderr, "Usage:\n\t %s [options]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "For example:\n")
fmt.Fprintf(os.Stderr, "\t%s -s . -i 1234\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\t%s -reader-uri fs:///usr/local/data/whosonfirst-data-admin-ca/data -id 1234 -id 5678\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Valid options are:\n")
flag.PrintDefaults()
}

flag.Parse()

if *reader_uri == "" || *writer_uri == "" {

var path string

if *source == "" {

cwd, err := os.Getwd()

if err != nil {
log.Fatalf("Failed to determine current working directory, %v", err)
}

path = cwd

} else {

abs_source, err := filepath.Abs(*source)

if err != nil {
log.Fatalf("Failed to derive absolute path for '%s', %v", *source, err)
}

path = abs_source
}

abs_path := fmt.Sprintf("fs://%s/data", path)

if *reader_uri == "" {
*reader_uri = abs_path
}

if *writer_uri == "" {
*writer_uri = abs_path
}
}

if *id != "" {

err := ids.Set(*id)

if err != nil {
log.Fatalf("Failed to assign '%d' (-id) flag, %v", *id, err)
}
}

ctx := context.Background()

ex, err := export.NewExporter(ctx, *exporter_uri)

if err != nil {
log.Fatalf("Failed create exporter for '%s', %v", *exporter_uri, err)
}

r, err := reader.NewReader(ctx, *reader_uri)

if err != nil {
log.Fatalf("Failed to create reader for '%s', %v", *reader_uri, err)
}

wr, err := writer.NewWriter(ctx, *writer_uri)

if err != nil {
log.Fatalf("Failed to create writer for '%s', %v", *writer_uri, err)
}

if *date == "" {
now := time.Now()
*date = now.Format("2006-01-02")
}

edtf_dt, err := parser.ParseString(*date)

if err != nil {
log.Fatalf("Failed to parse date string, %v", err)
}

for _, id := range ids {

err := cessateId(ctx, r, wr, ex, id, edtf_dt, superseded_by)

if err != nil {
log.Fatalf("Failed to cessate record for '%d', %v", id, err)
}
}
}

func cessateId(ctx context.Context, r reader.Reader, wr writer.Writer, ex export.Exporter, id int64, dt *edtf.EDTFDate, superseded_by multi.MultiInt64) error {

body, err := wof_reader.LoadBytesFromID(ctx, r, id)

if err != nil {
return err
}

to_update := map[string]interface{}{
"properties.edtf:cessation": dt.EDTF,
"properties.mz:is_current": 0,
}

if len(superseded_by) > 0 {

tmp := make(map[int64]bool)

for _, id := range superseded_by {
tmp[id] = true
}

rsp := gjson.GetBytes(body, "properties.wof:superseded_by")

for _, r := range rsp.Array() {
id := r.Int()
tmp[id] = true
}

new_list := make([]int64, 0)

for id, _ := range tmp {

switch id {
case 0, -1:
continue
default:
new_list = append(new_list, id)
}
}

to_update["properties.wof:superseded_by"] = new_list
}

new_body, err := export.AssignProperties(ctx, body, to_update)

if err != nil {
return err
}

return exportify.ExportWithWriter(ctx, ex, wr, new_body)
}
10 changes: 5 additions & 5 deletions cmd/deprecate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"github.com/sfomuseum/go-flags/multi"
"github.com/tidwall/gjson"
"github.com/whosonfirst/go-reader"
"github.com/whosonfirst/go-whosonfirst-export/v2"
"github.com/whosonfirst/go-whosonfirst-exportify"
Expand All @@ -13,7 +14,6 @@ import (
"log"
"os"
"path/filepath"
"github.com/tidwall/gjson"
"time"
)

Expand All @@ -32,7 +32,7 @@ func main() {

var superseded_by multi.MultiInt64
flag.Var(&superseded_by, "superseded-by", "Zero or more Who's On First IDs that the records being deprecated are superseded by.")

flag.Usage = func() {

fmt.Fprintf(os.Stderr, "Deprecate one or more Who's On First IDs.\n\n")
Expand Down Expand Up @@ -143,10 +143,10 @@ func deprecateId(ctx context.Context, r reader.Reader, wr writer.Writer, ex expo
for _, id := range superseded_by {
tmp[id] = true
}

rsp := gjson.GetBytes(body, "properties.wof:superseded_by")

for _, r := range rsp.Array(){
for _, r := range rsp.Array() {
id := r.Int()
tmp[id] = true
}
Expand All @@ -165,7 +165,7 @@ func deprecateId(ctx context.Context, r reader.Reader, wr writer.Writer, ex expo

to_update["properties.wof:superseded_by"] = new_list
}

new_body, err := export.AssignProperties(ctx, body, to_update)

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/ensure-properties/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/sfomuseum/go-flags/multi"
"github.com/whosonfirst/go-whosonfirst-export/v2"
"github.com/whosonfirst/go-whosonfirst-exportify"
_ "github.com/whosonfirst/go-whosonfirst-iterate-reader"
"github.com/whosonfirst/go-whosonfirst-iterate/emitter"
_ "github.com/whosonfirst/go-whosonfirst-iterate-reader"
"github.com/whosonfirst/go-whosonfirst-iterate/iterator"
"github.com/whosonfirst/go-whosonfirst-uri"
wof_writer "github.com/whosonfirst/go-whosonfirst-writer"
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.16
require (
github.com/aaronland/go-json-query v0.1.0
github.com/paulmach/orb v0.2.2
github.com/sfomuseum/go-edtf v0.2.4
github.com/sfomuseum/go-flags v0.8.2
github.com/tidwall/gjson v1.9.1
github.com/tidwall/sjson v1.2.2
Expand Down
1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ github.com/psanford/sqlite3vfs
# github.com/psanford/sqlite3vfshttp v0.0.0-20210810192816-7ed3ddba507d
github.com/psanford/sqlite3vfshttp
# github.com/sfomuseum/go-edtf v0.2.4
## explicit
github.com/sfomuseum/go-edtf
github.com/sfomuseum/go-edtf/calendar
github.com/sfomuseum/go-edtf/common
Expand Down

0 comments on commit 793bb85

Please sign in to comment.