From 795af5caa1e0786fe540594d52f03ae34eaf3f21 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Fri, 1 Jan 2021 14:31:58 -0800 Subject: [PATCH] Refactor --- .gitignore | 4 +- common.go | 36 +++++- daijirin.go | 2 +- daijisen.go | 2 +- edict.go | 2 +- enamdict.go | 2 +- epwing.go | 5 +- frequency.go | 2 +- gakken.go | 2 +- kanjidic.go | 2 +- kotowaza.go | 2 +- koujien.go | 2 +- main.go | 118 ------------------ meikyou.go | 2 +- rikai.go | 2 +- wadai.go | 2 +- build_mingw.sh => yomichan-gtk/build_mingw.sh | 0 gui.go => yomichan-gtk/main.go | 36 ++++-- yomichan/main.go | 62 +++++++++ 19 files changed, 135 insertions(+), 150 deletions(-) delete mode 100644 main.go rename build_mingw.sh => yomichan-gtk/build_mingw.sh (100%) rename gui.go => yomichan-gtk/main.go (90%) create mode 100644 yomichan/main.go diff --git a/.gitignore b/.gitignore index cc034bb..ab69ae5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -yomichan-import* -!yomichan-import.bat +yomichan-gtk/yomichan-gtk* +yomichan/yomichan* diff --git a/common.go b/common.go index d263bdb..f6584a7 100644 --- a/common.go +++ b/common.go @@ -20,7 +20,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package main +package yomichan import ( "archive/zip" @@ -33,6 +33,14 @@ import ( "strings" ) +const ( + DefaultFormat = "" + DefaultLanguage = "" + DefaultPretty = false + DefaultStride = 10000 + DefaultTitle = "" +) + const databaseFormat = 3 type dbRecord []interface{} @@ -302,3 +310,29 @@ func detectFormat(path *string) (string, error) { return "", errors.New("unrecognized dictionary format") } + +func ExportDb(inputPath, outputPath, format, language, title string, stride int, pretty bool) error { + handlers := map[string]func(string, string, string, string, int, bool) error{ + "edict": jmdictExportDb, + "enamdict": jmnedictExportDb, + "epwing": epwingExportDb, + "kanjidic": kanjidicExportDb, + "rikai": rikaiExportDb, + "kanjifreq": frequencyKanjiExportDb, + "termfreq": frequencyTermsExportDb, + } + + var err error + if format == DefaultFormat { + if format, err = detectFormat(&inputPath); err != nil { + return err + } + } + + handler, ok := handlers[strings.ToLower(format)] + if !ok { + return errors.New("unrecognized dictionary format") + } + + return handler(inputPath, outputPath, strings.ToLower(language), title, stride, pretty) +} diff --git a/daijirin.go b/daijirin.go index a812a7a..b3a0fd9 100644 --- a/daijirin.go +++ b/daijirin.go @@ -20,7 +20,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package main +package yomichan import ( "regexp" diff --git a/daijisen.go b/daijisen.go index 2d79f9b..de91c1a 100644 --- a/daijisen.go +++ b/daijisen.go @@ -20,7 +20,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package main +package yomichan import ( "regexp" diff --git a/edict.go b/edict.go index c2f565b..2e3637f 100644 --- a/edict.go +++ b/edict.go @@ -20,7 +20,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package main +package yomichan import ( "os" diff --git a/enamdict.go b/enamdict.go index 7d6a4e1..e2aee50 100644 --- a/enamdict.go +++ b/enamdict.go @@ -20,7 +20,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package main +package yomichan import ( "os" diff --git a/epwing.go b/epwing.go index f7829fe..098ba18 100644 --- a/epwing.go +++ b/epwing.go @@ -20,11 +20,10 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package main +package yomichan import ( "fmt" - "log" "regexp" "strconv" "strings" @@ -69,8 +68,6 @@ func epwingExportDb(inputPath, outputPath, language, title string, stride int, p sequence int ) - log.Println("formatting dictionary data...") - for _, subbook := range book.Subbooks { if extractor, ok := epwingExtractors[subbook.Title]; ok { fontNarrow := extractor.getFontNarrow() diff --git a/frequency.go b/frequency.go index f812c57..8e0d41f 100644 --- a/frequency.go +++ b/frequency.go @@ -20,7 +20,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package main +package yomichan import ( "bufio" diff --git a/gakken.go b/gakken.go index 91b0929..7654584 100644 --- a/gakken.go +++ b/gakken.go @@ -20,7 +20,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package main +package yomichan import ( "regexp" diff --git a/kanjidic.go b/kanjidic.go index 6b37803..109f798 100644 --- a/kanjidic.go +++ b/kanjidic.go @@ -20,7 +20,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package main +package yomichan import ( "os" diff --git a/kotowaza.go b/kotowaza.go index c934d75..48929fb 100644 --- a/kotowaza.go +++ b/kotowaza.go @@ -20,7 +20,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package main +package yomichan import ( "regexp" diff --git a/koujien.go b/koujien.go index 0fb5673..c08d970 100644 --- a/koujien.go +++ b/koujien.go @@ -20,7 +20,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package main +package yomichan import ( "regexp" diff --git a/main.go b/main.go deleted file mode 100644 index 985d9f9..0000000 --- a/main.go +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright (c) 2016 Alex Yatskov - * Author: Alex Yatskov - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of - * the Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -package main - -import ( - "errors" - "flag" - "fmt" - "io/ioutil" - "log" - "os" - "path" - "strings" -) - -const ( - defaultStride = 10000 - defaultLanguage = "english" -) - -func usage() { - fmt.Fprintf(os.Stderr, "Usage: %s [options] input-path output-path\n", path.Base(os.Args[0])) - fmt.Fprint(os.Stderr, "https://foosoft.net/projects/yomichan-import/\n\n") - fmt.Fprint(os.Stderr, "Parameters:\n") - flag.PrintDefaults() -} - -func exportDb(inputPath, outputPath, format, language, title string, stride int, pretty bool) error { - handlers := map[string]func(string, string, string, string, int, bool) error{ - "edict": jmdictExportDb, - "enamdict": jmnedictExportDb, - "epwing": epwingExportDb, - "kanjidic": kanjidicExportDb, - "rikai": rikaiExportDb, - "kanjifreq": frequencyKanjiExportDb, - "termfreq": frequencyTermsExportDb, - } - - handler, ok := handlers[strings.ToLower(format)] - if !ok { - return errors.New("unrecognized dictionary format") - } - - log.Printf("converting '%s' to '%s' in '%s' format...", inputPath, outputPath, format) - if err := handler(inputPath, outputPath, strings.ToLower(language), title, stride, pretty); err != nil { - log.Printf("conversion process failed: %s", err.Error()) - return err - } - - log.Print("conversion process complete") - return nil -} - -func makeTmpDir() (string, error) { - return ioutil.TempDir("", "yomichan_tmp_") -} - -func main() { - var ( - format = flag.String("format", "", "dictionary format [edict|enamdict|epwing|kanjidic|rikai]") - language = flag.String("language", defaultLanguage, "dictionary language (if supported)") - title = flag.String("title", "", "dictionary title") - stride = flag.Int("stride", defaultStride, "dictionary bank stride") - pretty = flag.Bool("pretty", false, "output prettified dictionary JSON") - ) - - flag.Usage = usage - flag.Parse() - - if flag.NArg() != 2 { - if err := gui(); err == nil { - return - } - - usage() - os.Exit(2) - } - - var ( - inputPath = flag.Arg(0) - outputPath = flag.Arg(1) - ) - - if _, err := os.Stat(inputPath); err != nil { - log.Fatalf("dictionary path '%s' does not exist", inputPath) - } - - if *format == "" { - var err error - if *format, err = detectFormat(&inputPath); err != nil { - log.Fatal(err) - } - } - - if err := exportDb(inputPath, outputPath, *format, *language, *title, *stride, *pretty); err != nil { - log.Fatal(err) - } -} diff --git a/meikyou.go b/meikyou.go index d7f5258..fefffe0 100644 --- a/meikyou.go +++ b/meikyou.go @@ -20,7 +20,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package main +package yomichan import ( "regexp" diff --git a/rikai.go b/rikai.go index 9c3af74..f78346c 100644 --- a/rikai.go +++ b/rikai.go @@ -20,7 +20,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package main +package yomichan import ( "database/sql" diff --git a/wadai.go b/wadai.go index 7cd23c7..5066078 100644 --- a/wadai.go +++ b/wadai.go @@ -20,7 +20,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -package main +package yomichan import ( "regexp" diff --git a/build_mingw.sh b/yomichan-gtk/build_mingw.sh similarity index 100% rename from build_mingw.sh rename to yomichan-gtk/build_mingw.sh diff --git a/gui.go b/yomichan-gtk/main.go similarity index 90% rename from gui.go rename to yomichan-gtk/main.go index d2ac3fd..392e075 100644 --- a/gui.go +++ b/yomichan-gtk/main.go @@ -29,6 +29,8 @@ import ( "github.com/andlabs/ui" _ "github.com/andlabs/ui/winmanifest" + + yomichan "github.com/FooSoft/yomichan-import" ) type logger struct { @@ -58,7 +60,11 @@ func gui() error { pathTargetBox.Append(pathTargetButton, false) titleEntry := ui.NewEntry() + titleEntry.SetText(yomichan.DefaultTitle) + languageEntry := ui.NewEntry() + languageEntry.SetText(yomichan.DefaultLanguage) + outputEntry := ui.NewEntry() importButton := ui.NewButton("Import dictionary...") @@ -116,28 +122,26 @@ func gui() error { return } - format, err := detectFormat(&inputPath) - if err != nil { - ui.MsgBoxError(window, "Error", "Unable to detect dictionary format") - importButton.Enable() - return - } - - title := titleEntry.Text() - language := languageEntry.Text() - go func() { - var success bool + var err error defer ui.QueueMain(func() { importButton.Enable() - if success { + if err == nil { ui.MsgBox(window, "Success", "Conversion process complete") } else { ui.MsgBox(window, "Error", "Conversion process failed") } }) - success = exportDb(inputPath, outputPath, format, language, title, defaultStride, false) == nil + err = yomichan.ExportDb( + inputPath, + outputPath, + yomichan.DefaultFormat, + languageEntry.Text(), + titleEntry.Text(), + yomichan.DefaultStride, + yomichan.DefaultPretty, + ) }() }) @@ -149,3 +153,9 @@ func gui() error { window.Show() }) } + +func main() { + if err := gui(); err != nil { + log.Fatal(err) + } +} diff --git a/yomichan/main.go b/yomichan/main.go new file mode 100644 index 0000000..897275d --- /dev/null +++ b/yomichan/main.go @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package main + +import ( + "flag" + "fmt" + "log" + "os" + "path" + + yomichan "github.com/FooSoft/yomichan-import" +) + +func usage() { + fmt.Fprintf(os.Stderr, "Usage: %s [options] input-path output-path\n", path.Base(os.Args[0])) + fmt.Fprint(os.Stderr, "https://foosoft.net/projects/yomichan-import/\n\n") + fmt.Fprint(os.Stderr, "Parameters:\n") + flag.PrintDefaults() +} + +func main() { + var ( + format = flag.String("format", yomichan.DefaultFormat, "dictionary format [edict|enamdict|epwing|kanjidic|rikai]") + language = flag.String("language", yomichan.DefaultLanguage, "dictionary language (if supported)") + title = flag.String("title", yomichan.DefaultTitle, "dictionary title") + stride = flag.Int("stride", yomichan.DefaultStride, "dictionary bank stride") + pretty = flag.Bool("pretty", yomichan.DefaultPretty, "output prettified dictionary JSON") + ) + + flag.Usage = usage + flag.Parse() + + if flag.NArg() != 2 { + usage() + os.Exit(2) + } + + if err := yomichan.ExportDb(flag.Arg(0), flag.Arg(1), *format, *language, *title, *stride, *pretty); err != nil { + log.Fatal(err) + } +}