1
yomichan-import/main.go

118 lines
3.4 KiB
Go
Raw Normal View History

2016-07-27 03:24:33 +00:00
/*
* Copyright (c) 2016 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* 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"
2016-08-03 16:12:31 +00:00
"flag"
2016-07-27 03:24:33 +00:00
"fmt"
2016-12-19 01:31:27 +00:00
"io/ioutil"
2016-07-27 03:24:33 +00:00
"log"
"os"
"path"
2017-06-26 01:06:41 +00:00
"strings"
2016-07-27 03:24:33 +00:00
)
2017-04-10 01:15:19 +00:00
const (
2017-06-11 06:31:40 +00:00
DEFAULT_STRIDE = 10000
DEFAULT_PORT = 9876
DEFAULT_LANGUAGE = "english"
2017-04-10 01:15:19 +00:00
)
2016-07-27 03:24:33 +00:00
func usage() {
2017-06-26 00:22:17 +00:00
fmt.Fprintf(os.Stderr, "Usage: %s [options] input-path output-path\n", path.Base(os.Args[0]))
2016-12-29 19:28:36 +00:00
fmt.Fprint(os.Stderr, "https://foosoft.net/projects/yomichan-import/\n\n")
fmt.Fprint(os.Stderr, "Parameters:\n")
2016-08-03 16:12:31 +00:00
flag.PrintDefaults()
2016-07-27 03:24:33 +00:00
}
2017-06-26 00:22:17 +00:00
func exportDb(inputPath, outputPath, format, language, title string, stride int, pretty bool) error {
handlers := map[string]func(string, string, string, string, int, bool) error{
2016-12-18 19:46:47 +00:00
"edict": jmdictExportDb,
"enamdict": jmnedictExportDb,
"epwing": epwingExportDb,
2017-08-19 19:17:31 +00:00
"kanjidic": kanjidicExportDb,
"rikai": rikaiExportDb,
2016-07-29 04:39:35 +00:00
}
2017-06-26 01:06:41 +00:00
handler, ok := handlers[strings.ToLower(format)]
2016-07-29 04:39:35 +00:00
if !ok {
2017-06-26 01:06:41 +00:00
return errors.New("unrecognized dictionary format")
2016-07-29 04:39:35 +00:00
}
2017-06-26 00:22:17 +00:00
log.Printf("converting '%s' to '%s' in '%s' format...", inputPath, outputPath, format)
2017-06-26 01:17:00 +00:00
if err := handler(inputPath, outputPath, strings.ToLower(language), title, stride, pretty); err != nil {
log.Printf("conversion process failed: %s", err.Error())
return err
}
2016-12-19 01:31:27 +00:00
2017-06-26 01:17:00 +00:00
log.Print("conversion process complete")
return nil
2016-07-27 03:24:33 +00:00
}
2017-04-10 01:15:19 +00:00
func makeTmpDir() (string, error) {
return ioutil.TempDir("", "yomichan_tmp_")
}
2016-07-27 03:24:33 +00:00
func main() {
2016-12-19 01:31:27 +00:00
var (
2017-08-19 19:18:24 +00:00
format = flag.String("format", "", "dictionary format [edict|enamdict|epwing|kanjidic|rikai]")
2017-06-26 00:22:17 +00:00
language = flag.String("language", DEFAULT_LANGUAGE, "dictionary language (if supported)")
title = flag.String("title", "", "dictionary title")
stride = flag.Int("stride", DEFAULT_STRIDE, "dictionary bank stride")
pretty = flag.Bool("pretty", false, "output prettified dictionary JSON")
2016-12-19 01:31:27 +00:00
)
2016-08-03 16:12:31 +00:00
flag.Usage = usage
flag.Parse()
2017-06-26 00:22:17 +00:00
if flag.NArg() != 2 {
2017-04-10 00:53:44 +00:00
if err := gui(); err == nil {
return
} else {
usage()
os.Exit(2)
}
}
2017-06-26 00:22:17 +00:00
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)
2016-08-03 16:12:31 +00:00
}
2016-07-27 03:24:33 +00:00
2016-12-29 01:45:33 +00:00
if *format == "" {
2016-12-19 01:31:27 +00:00
var err error
2017-06-26 01:06:41 +00:00
if *format, err = detectFormat(inputPath); err != nil {
log.Fatal(err)
2016-12-19 01:31:27 +00:00
}
}
2017-06-26 00:22:17 +00:00
if err := exportDb(inputPath, outputPath, *format, *language, *title, *stride, *pretty); err != nil {
log.Fatal(err)
2016-12-19 01:31:27 +00:00
}
2016-07-27 03:24:33 +00:00
}