1

improvements to ui

This commit is contained in:
Alex Yatskov 2017-06-25 18:06:41 -07:00
parent d39df0f3eb
commit 6f1d9cd495
3 changed files with 42 additions and 40 deletions

View File

@ -26,6 +26,7 @@ import (
"archive/zip"
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
@ -232,33 +233,31 @@ func hasString(needle string, haystack []string) bool {
return false
}
func detectFormat(path string) string {
func detectFormat(path string) (string, error) {
base := filepath.Base(path)
fmt.Print(base)
switch base {
case "JMdict", "JMdict.xml", "JMdict_e", "JMdict_e.xml":
return "edict", nil
case "JMnedict", "JMnedict.xml":
return "enamdict", nil
case "kanjidic2", "kanjidic2.xml":
return "kanjidic", nil
case "CATALOGS":
return "epwing", nil
}
info, err := os.Stat(path)
if err != nil {
return ""
return "", err
}
if info.IsDir() {
_, err := os.Stat(filepath.Join(path, "CATALOGS"))
if err == nil {
return "epwing"
}
} else {
base := filepath.Base(path)
switch base {
case "JMdict":
case "JMdict.xml":
case "JMdict_e":
case "JMdict_e.xml":
return "edict"
case "JMnedict":
case "JMnedict.xml":
return "enamdict"
case "kanjidic2":
case "kanjidic2.xml":
return "kanjidic"
return "epwing", nil
}
}
return ""
return "", errors.New("unrecognized dictionary format")
}

33
gui.go
View File

@ -56,27 +56,21 @@ func gui() error {
pathTargetBox.Append(pathTargetEntry, true)
pathTargetBox.Append(pathTargetButton, false)
formatCombo := ui.NewCombobox()
formatCombo.Append("EPWING")
formatCombo.Append("EDICT")
formatCombo.Append("ENAMDICT")
formatCombo.Append("KANJIDIC")
formatCombo.SetSelected(0)
titleEntry := ui.NewEntry()
languageEntry := ui.NewEntry()
outputEntry := ui.NewEntry()
importButton := ui.NewButton("Import dictionary...")
mainBox := ui.NewVerticalBox()
mainBox.Append(ui.NewLabel("Path to dictionary source (CATALOGS file for EPWING):"), false)
mainBox.Append(ui.NewLabel("Path to dictionary source (CATALOGS file for EPWING)"), false)
mainBox.Append(pathSourceBox, false)
mainBox.Append(ui.NewLabel("Path to dictionary target ZIP file"), false)
mainBox.Append(pathTargetBox, false)
mainBox.Append(ui.NewLabel("Dictionary title (leave blank for default):"), false)
mainBox.Append(ui.NewLabel("Dictionary display title (blank for default)"), false)
mainBox.Append(titleEntry, false)
mainBox.Append(ui.NewLabel("Dictionary format:"), false)
mainBox.Append(formatCombo, false)
mainBox.Append(ui.NewLabel("Application output:"), false)
mainBox.Append(ui.NewLabel("Dictionary glossary language (blank for English)"), false)
mainBox.Append(languageEntry, false)
mainBox.Append(ui.NewLabel("Application output"), false)
mainBox.Append(outputEntry, false)
mainBox.Append(ui.NewVerticalBox(), true)
mainBox.Append(importButton, false)
@ -109,31 +103,38 @@ func gui() error {
inputPath := pathSourceEntry.Text()
if len(inputPath) == 0 {
ui.MsgBoxError(window, "Error", "You must specify a dictionary source path.")
ui.MsgBoxError(window, "Error", "You must specify a dictionary source path")
importButton.Enable()
return
}
outputPath := pathTargetEntry.Text()
if len(outputPath) == 0 {
ui.MsgBoxError(window, "Error", "You must specify a dictionary target path.")
ui.MsgBoxError(window, "Error", "You must specify a dictionary target path")
importButton.Enable()
return
}
format, err := detectFormat(inputPath)
if err != nil {
ui.MsgBoxError(window, "Error", "Unable to detect dictionary format")
importButton.Enable()
return
}
format := []string{"epwing", "edict", "enamdict", "kanjidic"}[formatCombo.Selected()]
if format == "epwing" {
inputPath = filepath.Dir(inputPath)
}
title := titleEntry.Text()
language := languageEntry.Text()
go func() {
defer ui.QueueMain(func() {
importButton.Enable()
})
if err := exportDb(inputPath, outputPath, format, "english", title, DEFAULT_STRIDE, false); err != nil {
if err := exportDb(inputPath, outputPath, format, language, title, DEFAULT_STRIDE, false); err != nil {
log.Print(err)
}
}()

12
main.go
View File

@ -30,6 +30,7 @@ import (
"log"
"os"
"path"
"strings"
)
const (
@ -53,13 +54,13 @@ func exportDb(inputPath, outputPath, format, language, title string, stride int,
"epwing": epwingExportDb,
}
handler, ok := handlers[format]
handler, ok := handlers[strings.ToLower(format)]
if !ok {
return errors.New("unrecognized dictionray format")
return errors.New("unrecognized dictionary format")
}
log.Printf("converting '%s' to '%s' in '%s' format...", inputPath, outputPath, format)
return handler(inputPath, outputPath, language, title, stride, pretty)
return handler(inputPath, outputPath, strings.ToLower(language), title, stride, pretty)
}
func makeTmpDir() (string, error) {
@ -97,8 +98,9 @@ func main() {
}
if *format == "" {
if *format = detectFormat(inputPath); *format == "" {
log.Fatal("failed to detect dictionary format")
var err error
if *format, err = detectFormat(inputPath); err != nil {
log.Fatal(err)
}
}