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 1984521360477e8efe3e11854bdf308b480a89a4
parent fd73ee5bb8cb23497382a63b5063424573390f86
Author: Nick Econopouly <wry@mm.st>
Date:   Sun, 22 Mar 2020 14:50:58 -0400

Add simple dataset choosing and removal

Diffstat:
Mmain.go | 157+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
Atrash.png | 0
2 files changed, 90 insertions(+), 67 deletions(-)

diff --git a/main.go b/main.go @@ -4,9 +4,11 @@ import ( "fmt" "os" "log" - "path/filepath" - "strings" + // "path/filepath" + // "strings" "bytes" + "C" + "unsafe" "github.com/360EntSecGroup-Skylar/excelize" "github.com/knieriem/odf/ods" "encoding/csv" @@ -48,6 +50,39 @@ func windowSetup() *gtk.Window { return window } +func rebuildDatasetListBox(list *gtk.ListBox, filenames *[]string, window *gtk.Window) { + + // clear list + list.GetChildren().Foreach(func(item interface{}) { + list.Remove(item.(*gtk.Widget)) + }) + + for _, filename := range *filenames { + poop := filename + box, err := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 2) + text := newLabel(filename) + text.SetSelectable(true) + box.PackStart(text, false, false, 0) + + removeButton, err := gtk.ButtonNewWithLabel("Remove") + if err != nil { + log.Fatal("Unable to create removeButton", err) + } + + trashImage, err := gtk.ImageNewFromFile("trash.png") + removeButton.SetImage(trashImage) + + _, err = removeButton.Connect("clicked", func() { + *filenames = Remove(*filenames, poop) + rebuildDatasetListBox(list, filenames, window) + window.ShowAll() + }) + + box.PackEnd(removeButton,false, false, 0) + list.Insert(box, 0) + } +} + func pullExcel(path string) [][]string { var rows [][]string f, err := excelize.OpenFile(path) @@ -157,93 +192,81 @@ func mergeDatasets(base *Dataset, new *Dataset) { func main() { - // get set up with the current directory - wd, err := os.Getwd() - if err != nil { - log.Println(err) - } - - // supported file extensions and their associated function for pulling the raw data - fileFormat := map[string]func(string) [][]string{ - ".xlsx": pullExcel, - ".csv": pullCSV, - ".ods": pullODS, - } - - fmt.Println("Attempting to merge spreadsheet files in ", wd) - - // map of basenames to the [][]string data - raws := make(map[string][][]string) - - err = filepath.Walk(wd, func(path string, info os.FileInfo, err error) error { - if info.IsDir() { - return nil - } - basename := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)) - ext := strings.ToLower(filepath.Ext(path)) + window := windowSetup() - if basename == EXPORT_BASE_FILENAME { // ignore our output file - return nil - } - if _, ok := fileFormat[ext]; ok { - raws[basename] = fileFormat[ext](path) - } - return nil - }) + // main box container + mainBox, err := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 5) if err != nil { - panic(err) - } - - datasets := make(map[string]*Dataset) - for name, data := range raws { - datasets[name] = ImportDataset(name, data) + log.Fatal("Unable to create grid: ", err) } - // 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) - } + mainBox.PackStart(newHeadline("Add datasets:"), false, false, 0) + datasetListBox, err := gtk.ListBoxNew() + // datasetListBox.Insert(newLabel("testing datasetList"), 0) + mainBox.PackStart(datasetListBox,false, false, 0) - window := windowSetup() + // holds the dataset filenames + var selections = &[]string{"poop", "butt"} - window.Add(mainBox()) + // refresh the list of datasets for the first time + rebuildDatasetListBox(datasetListBox, selections, window) - window.ShowAll() - gtk.Main() -} - -func mainBox() *gtk.Box { - // store everything in a grid - topBox, err := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 5) + addButton, err := gtk.ButtonNewWithLabel("Add Dataset") if err != nil { - log.Fatal("Unable to create grid: ", err) + log.Fatal("unable to create addButton") } - // Add new datasets - topBox.PackStart(newHeadline("Add datasets:"), false, false, 0) - - datasetList, err := gtk.ListBoxNew() - datasetList.Insert(newLabel("testing datasetList"), 0) - datasetList.Insert(newLabel("testing datasetList poop"), 0) - - topBox.PackStart(datasetList,false, false, 0) + // native file chooser + addDatasetDialog, err := gtk.FileChooserNativeDialogNew("open",window,gtk.FILE_CHOOSER_ACTION_OPEN,"open","cancel") + addDatasetDialog.SetSelectMultiple(true) + + _, err = addButton.Connect("clicked", func() { + _ = addDatasetDialog.Run() + // runButton.SetSensitive(true) + // fmt.Println(saveDialog.GetFilename()) + list, err := addDatasetDialog.GetFilenames() + if err != nil { + log.Fatal("Error getting filenames") + } + + list.Foreach(func(ptr unsafe.Pointer) { + filename := C.GoString((*C.char)(ptr)) + fmt.Println(filename) + *selections = append(*selections, filename) + }) + rebuildDatasetListBox(datasetListBox, selections, window) + window.ShowAll() + }) mergeButton, err := gtk.ButtonNewWithLabel("Merge Datasets!") if err != nil { log.Fatal("Unable to create mergeButton", err) } - topBox.PackStart(mergeButton, false, false, 0) + + // 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) + + mainBox.PackStart(mergeButton, false, false, 0) // _, err = mergeButton.Connect("clicked", func() { // // export dataset // exportDataset(&dataset, "merged.xlsx") // }) + + window.Add(mainBox) - return topBox + window.ShowAll() + gtk.Main() } func newHeadline(s string) *gtk.Label { diff --git a/trash.png b/trash.png Binary files differ.