39
39
40
40
import os
41
41
42
+
42
43
from chirptext import confirm , TextReport , Timer
43
44
from chirptext .cli import CLIApp , setup_logging
44
45
45
46
from jamdict import Jamdict
46
47
from jamdict import config
48
+ from jamdict import version_info
47
49
48
50
# -------------------------------------------------------------------------------
49
51
# Configuration
53
55
JMD_XML = config .get_file ('JMDICT_XML' )
54
56
KD2_XML = config .get_file ('KD2_XML' )
55
57
JMD_DB = config .get_file ('JAMDICT_DB' )
56
- setup_logging ('logging.json' , 'logs' )
58
+
59
+ if os .path .isfile ('logging.json' ):
60
+ setup_logging ('logging.json' , 'logs' )
61
+ else :
62
+ setup_logging (os .path .join (config .home_dir (), 'logging.json' ), 'logs' )
57
63
58
64
59
65
# -------------------------------------------------------------------------------
@@ -99,39 +105,41 @@ def import_data(cli, args):
99
105
print ("Database paths were not provided. Process aborted." )
100
106
101
107
102
- def dump_result (results ):
108
+ def dump_result (results , report = None ):
109
+ if report is None :
110
+ report = TextReport ()
103
111
if results .entries :
104
- print ("=" * 40 )
105
- print ("Found entries" )
106
- print ("=" * 40 )
112
+ report . print ("=" * 40 )
113
+ report . print ("Found entries" )
114
+ report . print ("=" * 40 )
107
115
for e in results .entries :
108
116
kj = ', ' .join ([k .text for k in e .kanji_forms ])
109
117
kn = ', ' .join ([k .text for k in e .kana_forms ])
110
- print ("Entry: {} | Kj: {} | Kn: {}" .format (e .idseq , kj , kn ))
111
- print ("-" * 20 )
118
+ report . print ("Entry: {} | Kj: {} | Kn: {}" .format (e .idseq , kj , kn ))
119
+ report . print ("-" * 20 )
112
120
for idx , s in enumerate (e .senses ):
113
- print ("{idx}. {s}" .format (idx = idx + 1 , s = s ))
114
- print ('' )
121
+ report . print ("{idx}. {s}" .format (idx = idx + 1 , s = s ))
122
+ report . print ('' )
115
123
else :
116
- print ("No dictionary entry was found." )
124
+ report . print ("No dictionary entry was found." )
117
125
if results .chars :
118
- print ("=" * 40 )
119
- print ("Found characters" )
120
- print ("=" * 40 )
126
+ report . print ("=" * 40 )
127
+ report . print ("Found characters" )
128
+ report . print ("=" * 40 )
121
129
for c in results .chars :
122
- print ("Char: {} | Strokes: {}" .format (c , c .stroke_count ))
123
- print ("-" * 20 )
130
+ report . print ("Char: {} | Strokes: {}" .format (c , c .stroke_count ))
131
+ report . print ("-" * 20 )
124
132
for rmg in c .rm_groups :
125
- print ("Readings:" , ", " .join ([r .value for r in rmg .readings ]))
126
- print ("Meanings:" , ", " .join ([m .value for m in rmg .meanings if not m .m_lang or m .m_lang == 'en' ]))
133
+ report . print ("Readings:" , ", " .join ([r .value for r in rmg .readings ]))
134
+ report . print ("Meanings:" , ", " .join ([m .value for m in rmg .meanings if not m .m_lang or m .m_lang == 'en' ]))
127
135
else :
128
- print ("No character was found." )
136
+ report . print ("No character was found." )
129
137
130
138
131
139
def lookup (cli , args ):
132
140
'''Lookup words by kanji/kana'''
133
141
jam = get_jam (cli , args )
134
- results = jam .lookup (args .query )
142
+ results = jam .lookup (args .query , strict_lookup = args . strict )
135
143
if args .format == 'json' :
136
144
print (results .to_json ())
137
145
else :
@@ -148,11 +156,15 @@ def file_status(file_path):
148
156
149
157
def show_info (cli , args ):
150
158
''' Show jamdict configuration (data folder, configuration file location, etc.) '''
151
- print ("Configuration location: {}" .format (config ._get_config_manager ().locate_config ()))
152
- print ("-" * 40 )
153
- print ("Jamdict DB location : {} - {}" .format (args .jdb , file_status (args .jdb )))
154
- print ("JMDict XML file : {} - {}" .format (args .jmdxml , file_status (args .jmdxml )))
155
- print ("KanjiDic2 XML file : {} - {}" .format (args .kd2xml , file_status (args .kd2xml )))
159
+ output = TextReport (args .output ) if 'output' in args else TextReport ()
160
+ output .header ("Jamdict | {} - Version: {}" .format (version_info .__description__ , version_info .__version__ ), level = 'h0' )
161
+ output .header ("Basic configuration" )
162
+ output .print ("JAMDICT_HOME: {}" .format (config .home_dir ()))
163
+ output .print ("Configuration location: {}" .format (config ._get_config_manager ().locate_config ()))
164
+ output .header ("Data files" )
165
+ output .print ("Jamdict DB location: {} - {}" .format (args .jdb , file_status (args .jdb )))
166
+ output .print ("JMDict XML file : {} - {}" .format (args .jmdxml , file_status (args .jmdxml )))
167
+ output .print ("KanjiDic2 XML file : {} - {}" .format (args .kd2xml , file_status (args .kd2xml )))
156
168
157
169
158
170
# -------------------------------------------------------------------------------
@@ -178,13 +190,15 @@ def main():
178
190
179
191
# show info
180
192
info_task = app .add_task ('info' , func = show_info )
193
+ info_task .add_argument ('-o' , '--output' , help = 'Write information to a text file' )
181
194
add_data_config (info_task )
182
195
183
196
# look up task
184
197
lookup_task = app .add_task ('lookup' , func = lookup )
185
198
lookup_task .add_argument ('query' , help = 'kanji/kana' )
186
199
lookup_task .add_argument ('-f' , '--format' , help = 'json or text' )
187
200
lookup_task .add_argument ('--compact' , action = 'store_true' )
201
+ lookup_task .add_argument ('-s' , '--strict' , action = 'store_true' )
188
202
lookup_task .set_defaults (func = lookup )
189
203
add_data_config (lookup_task )
190
204
0 commit comments