Skip to content

Commit

Permalink
Add basic subtree
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisBrunner committed Nov 18, 2018
1 parent 00d095c commit dabd74e
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 64 deletions.
8 changes: 3 additions & 5 deletions cmd/regenea/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ func getCheckCommand() cli.Command {
Name: "check",
Usage: "check that the given genea file is valid (if no file is specified, stdin is used)",
Flags: []cli.Flag{
cli.StringFlag{
Name: "in",
Usage: "input file to transform (default to stdin)",
},
inField,
subsetField,
},
Action: doCheckCommand,
}
Expand All @@ -24,6 +22,6 @@ func doCheckCommand(ctxt *cli.Context) error {
infile = ctxt.Args()[0]
}

_, _, err := helperRead(infile, "genea")
_, _, err := helperRead(infile, "genea", ctxt.String("subset"))
return err
}
25 changes: 17 additions & 8 deletions cmd/regenea/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@ import (
"os"

"github.com/LouisBrunner/regenea/genea"

"gopkg.in/urfave/cli.v1"
)

const (
formGenea = "genea"
)

func helperRead(infile, format string) (*genea.Tree, genea.Version, error) {
var inField = cli.StringFlag{
Name: "in",
Usage: "input file to transform (default to stdin)",
}

var subsetField = cli.StringFlag{
Name: "subset",
Usage: "use a subset of the tree including the given person, their ascendants, descendants and close family (partners, siblings, cousins, uncles and aunts)",
}

func helperRead(infile, format, subset string) (*genea.Tree, genea.Version, error) {
var err error
in := os.Stdin

Expand All @@ -26,17 +38,14 @@ func helperRead(infile, format string) (*genea.Tree, genea.Version, error) {
}
}

var tree *genea.Tree
var version genea.Version
switch format {
case formGenea:
tree, version, err = genea.Parse(in)
if err != nil {
return nil, 0, err
tree, version, err := genea.Parse(in)
if err == nil && subset != "" {
tree, err = tree.Subtree(subset)
}
return tree, version, err
default:
return nil, 0, fmt.Errorf("unknown format: `%s`", format)
}

return tree, version, err
}
2 changes: 2 additions & 0 deletions cmd/regenea/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"gopkg.in/urfave/cli.v1"
)

// TODO: add display/search for person

func getCLI() *cli.App {
app := cli.NewApp()
app.Name = "regenea"
Expand Down
16 changes: 11 additions & 5 deletions cmd/regenea/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ func getReportCommand() cli.Command {
Name: "report",
Usage: "display different stats about your tree (if no file is specified, stdin is used)",
Flags: []cli.Flag{
cli.StringFlag{
Name: "in",
Usage: "input file to transform (default to stdin)",
inField,
subsetField,
cli.BoolFlag{
Name: "pretty",
Usage: "prettify the output",
},
},
Action: doReportCommand,
Expand All @@ -29,12 +31,16 @@ func doReportCommand(ctxt *cli.Context) error {
infile = ctxt.Args()[0]
}

tree, _, err := helperRead(infile, "genea")
tree, _, err := helperRead(infile, "genea", ctxt.String("subset"))
if err != nil {
return err
}

return report.Process(tree, os.Stdout, func(v interface{}) ([]byte, error) {
return json.MarshalIndent(v, "", " ")
if ctxt.Bool("pretty") {
return json.MarshalIndent(v, "", " ")
} else {
return json.Marshal(v)
}
})
}
8 changes: 3 additions & 5 deletions cmd/regenea/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ func getTransformCommand() cli.Command {
Name: "transform",
Usage: "perform a transformation between 2 formats or 2 versions of the same format (supported: genea)",
Flags: []cli.Flag{
cli.StringFlag{
Name: "in",
Usage: "input file to transform (default to stdin)",
},
inField,
cli.StringFlag{
Name: "inform",
Usage: "format of the input file",
Expand All @@ -42,6 +39,7 @@ func getTransformCommand() cli.Command {
Name: "pretty",
Usage: "prettify the output",
},
subsetField,
},
Action: doTransformCommand,
}
Expand All @@ -62,7 +60,7 @@ func doTransformCommand(ctxt *cli.Context) error {
}

inform := ctxt.String("inform")
tree, inversion, err := helperRead(ctxt.String("in"), inform)
tree, inversion, err := helperRead(ctxt.String("in"), inform, ctxt.String("subset"))
if err != nil {
return err
}
Expand Down
41 changes: 0 additions & 41 deletions genea/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,44 +56,3 @@ func (e *EventCore) FormatDate() string {
func FormatDate(tm time.Time) string {
return tm.Format(json.DateFormat)
}

func (tree *Tree) IterateOverUnionErr(do func(union *Union) error) error {
for _, person := range tree.People {
if person.Family != nil {
person.Family.flags.serialized = false
}
for _, partner := range person.Partners {
partner.flags.serialized = false
}
}

for _, person := range tree.People {
if person.Family != nil {
if !person.Family.flags.serialized {
err := do(person.Family)
if err != nil {
return err
}
person.Family.flags.serialized = true
}
}
for _, partner := range person.Partners {
if !partner.flags.serialized {
err := do(partner)
if err != nil {
return err
}
partner.flags.serialized = true
}
}
}

return nil
}

func (tree *Tree) IterateOverUnion(do func(union *Union)) {
tree.IterateOverUnionErr(func(union *Union) error {
do(union)
return nil
})
}
3 changes: 3 additions & 0 deletions report/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"github.com/LouisBrunner/regenea/report/procs"
)

// TODO: add "lived through <historic event>" processor
// TODO: add chronological timeline

func Process(tree *genea.Tree, out io.Writer, marshaller func(v interface{}) ([]byte, error)) error {
processors := []procs.Processor{
&procs.Counter{},
Expand Down
6 changes: 6 additions & 0 deletions report/procs/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ func (p *Counter) Finish() {

func (p *Counter) Output() (string, StringMap) {
totalpf := float32(p.personCount)
if p.personCount == 0 {
totalpf = 1
}
totaluf := float32(p.unionCount)
if p.unionCount == 0 {
totaluf = 1
}
unknownSexUnion := p.unionCount - (p.sameSexCount + p.diffSexCount)

return categoryGeneral, StringMap{
Expand Down

0 comments on commit dabd74e

Please sign in to comment.