merge

Simple tool to quickly merge datasets for statistical analysis
git clone git://git.wrycode.com/wrycode/archive/merge.git
Log | Files | Refs | README | LICENSE

commit 65f98f26d64a261d4a5e0dc71ea3c343f81d1e2d
parent 1984521360477e8efe3e11854bdf308b480a89a4
Author: Nick Econopouly <wry@mm.st>
Date:   Sun, 22 Mar 2020 16:12:41 -0400

Add dataset merging to main method

Diffstat:
Mimport.go | 4++--
Mmain.go | 80++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
2 files changed, 61 insertions(+), 23 deletions(-)

diff --git a/import.go b/import.go @@ -68,7 +68,7 @@ func (d *Dataset) removeUnusedTerms() { // return datasets // } -func exportDataset(d *Dataset, name string) { +func exportDataset(d *Dataset, filepath string) { f := excelize.NewFile() sheetName := f.GetSheetMap()[1] @@ -90,7 +90,7 @@ func exportDataset(d *Dataset, name string) { } } - err := f.SaveAs("merged.xlsx") + err := f.SaveAs(filepath) if err != nil { log.Fatal("Unable to save file", err) } diff --git a/main.go b/main.go @@ -4,8 +4,8 @@ import ( "fmt" "os" "log" - // "path/filepath" - // "strings" + "path/filepath" + "strings" "bytes" "C" "unsafe" @@ -66,7 +66,7 @@ func rebuildDatasetListBox(list *gtk.ListBox, filenames *[]string, window *gtk.W removeButton, err := gtk.ButtonNewWithLabel("Remove") if err != nil { - log.Fatal("Unable to create removeButton", err) + log.Fatal("Unable to create removeButton", err) } trashImage, err := gtk.ImageNewFromFile("trash.png") @@ -194,6 +194,7 @@ func main() { window := windowSetup() + // main box container mainBox, err := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 5) if err != nil { @@ -207,12 +208,12 @@ func main() { mainBox.PackStart(datasetListBox,false, false, 0) // holds the dataset filenames - var selections = &[]string{"poop", "butt"} + selections := &[]string{} - // refresh the list of datasets for the first time + // refresh the (empty) list of datasets for the first time rebuildDatasetListBox(datasetListBox, selections, window) - addButton, err := gtk.ButtonNewWithLabel("Add Dataset") + addButton, err := gtk.ButtonNewWithLabel("Add Dataset(s)") if err != nil { log.Fatal("unable to create addButton") } @@ -240,28 +241,62 @@ func main() { window.ShowAll() }) - mergeButton, err := gtk.ButtonNewWithLabel("Merge Datasets!") + mergeButton, err := gtk.ButtonNewWithLabel("Merge Datasets") if err != nil { log.Fatal("Unable to create mergeButton", err) } - // refreshButton, err := gtk.ButtonNewWithLabel("refresh") - // if err != nil { - // log.Fatal("faicl") - // } - - // _, err = refreshButton.Connect("clicked", func() { - // rebuildDatasetListBox(datasetListBox, selections, window) - // window.ShowAll() - // }) mainBox.PackStart(addButton, false, false, 0) - // mainBox.PackStart(refreshButton, false, false, 0) + + saveDialog, err := gtk.FileChooserNativeDialogNew("save",window,gtk.FILE_CHOOSER_ACTION_SAVE,"save","cancel") mainBox.PackStart(mergeButton, false, false, 0) - // _, err = mergeButton.Connect("clicked", func() { - // // export dataset - // exportDataset(&dataset, "merged.xlsx") - // }) + _, err = mergeButton.Connect("clicked", func() { + if checkFiles(selections) { + _ = saveDialog.Run() + + outputFile := saveDialog.GetFilename() + + // supported file extensions and their associated function for pulling the raw data + fileFormat := map[string]func(string) [][]string{ + ".xlsx": pullExcel, + ".csv": pullCSV, + ".ods": pullODS, + } + + + // map of basenames to the [][]string data + raws := make(map[string][][]string) + + datasets := make(map[string]*Dataset) + + for _, path := range *selections { + basename := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)) + ext := strings.ToLower(filepath.Ext(path)) + + if _, ok := fileFormat[ext]; ok { + raws[basename] = fileFormat[ext](path) + } + for name, data := range raws { + datasets[name] = ImportDataset(name, data) + } + + } + // merge them into a single Dataset + var dataset Dataset + dataset.height = 1 // row of terms, even though it's empty + dataset.data = make(map[string][]string) + for _, d := range datasets { + mergeDatasets(&dataset,d) + } + + // export dataset + fmt.Println(outputFile) + exportDataset(&dataset, outputFile) + + } + + }) window.Add(mainBox) @@ -269,6 +304,9 @@ func main() { gtk.Main() } +func checkFiles(selections *[]string) bool { + return true +} func newHeadline(s string) *gtk.Label { if len(s) < 115 { var b bytes.Buffer