-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
99 lines (77 loc) · 2.77 KB
/
Copy pathmain.py
File metadata and controls
99 lines (77 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
from kivy.app import App
from kivy.uix.label import Label
from midori import Dictionary
LIMIT = 100
def to_background():
from jnius import cast
from jnius import autoclass
PythonActivity = autoclass('org.renpy.android.PythonActivity')
currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
currentActivity.moveTaskToBack(True)
class MultiLineLabel(Label):
pass
def get_label_text(parsed):
make_clickable = lambda w: "[ref=" + w + ']' + w + "[/ref]"
mystr = ''
for i, item in enumerate(parsed):
words = map(make_clickable, list(item.words))
mystr += str(i) + ': ' + ', '.join(words)
if item.pos != '':
mystr += ' [' + ','.join(item.pos) + ']'
if item.tags != '':
mystr += ' (' + ' | '.join(item.tags) + ')'
if item.comment != '':
mystr += ' [' + ','.join(item.comment) + ']'
mystr += '\n'
return mystr
def render_kanji(k):
make_clickable = lambda w: "[ref=" + w + ']' + w + "[/ref]"
large = lambda w: "[size=50]" + w + "[/size]"
mystr = large(make_clickable(k.uid)) + '\n'
words = map(make_clickable, list(k.meanings.words))
mystr += ', '.join(words)
on, kun = map(make_clickable, k.on), map(make_clickable, ['.'.join(i) for i in k.kun])
mystr += u'\n音: ' + ', '.join(on)
mystr += u'\n訓: ' + ', '.join(kun)
return mystr
def render_compound(c):
lst = [c.kanji, c.kana, c.meanings]
return '\n'.join(map(get_label_text, lst))
class MainApp(App):
def build(self):
self.dct = Dictionary('midori.db')
self.txt = self.root.ids['text']
self.redraw('', [], [])
def on_start(self):
from kivy.base import EventLoop
def bg(_, key, *args):
if key == 27: # 'Back' button
to_background()
return True
EventLoop.window.bind(on_keyboard=bg)
def tap_entry(self, txt):
compounds, kanjis = self.dct.lookup(txt)
self.redraw(txt, kanjis, compounds)
def search(self, btn_instance):
txt = self.txt.text
compounds, kanjis = self.dct.lookup(txt)
self.redraw(txt, kanjis, compounds)
def redraw(self, text, kanjis, compounds):
self.txt.text = text
grid = self.root.ids['grid']
grid.clear_widgets()
rendered_kanji = map(render_kanji, kanjis)
rendered_compounds = map(render_compound, compounds)
for txt in rendered_kanji[:LIMIT] + rendered_compounds[:LIMIT]:
box = MultiLineLabel(text=txt, markup=True)
grid.add_widget(box)
def on_pause(self):
return True
def quit(self, inst):
sys.exit(0)
if __name__ == "__main__":
a = MainApp()
a.run()