/
main.py
157 lines (122 loc) · 4.27 KB
/
main.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/python
# encoding: utf8
from os import environ
import sys
from clint.textui import colored, puts, indent
from api import login, lookup, availabledictionaries
import api
from commandparser import parse, Command
api.DEBUG = False
def main(args):
""" Log in to ordbogen.com and start an interactive shell.
"""
username = environ['ORDBOGEN_COM_USERNAME']
password = environ['ORDBOGEN_COM_PASSWORD']
success, msg = login(username, password)
if not success:
puts(msg)
return
puts("Ordbogen.com: You are logged in as {u}!\n".format(u=username))
_printavailabledictionaries()
_interactive(word=args)
def _interactive(word=None):
dicts = availabledictionaries()
dic = 'auto'
words = None
if word:
words = _lookup_and_print(word, dic)
_prompt()
# Main loop.
while True:
cmd, arg = parse(raw_input())
if cmd == Command.EXIT:
sys.exit(0)
elif cmd == Command.LIST_DICTS:
_printavailabledictionaries()
elif cmd == Command.SET_DICT:
dic = arg
puts("Dictionary set to {d}!".format(d=dicts[dic]))
elif cmd == Command.DETAILS:
_printdetailed(arg, words)
elif cmd == Command.LOOKUP:
words = _lookup_and_print(arg, dic)
_prompt()
def _lookup_and_print(word, dic):
""" Look up word and print found translations.
Number each translation so that it can be referenced by the user.
Return a list of `WordDetails` for each translation. Translations are
numbered (as described above) 1-indexed, so the 'real' index in to the list
is i - 1.
"""
# Use ordbogen.com api to look word up.
results = lookup(word, dic)
if not results:
puts("No results!")
return
# Delete line that user just wrote. For now we just put an empty line.
puts()
words = []
for language in results:
# Print language 'header'.
puts(language)
with indent(4):
for word in results[language]:
words.append(word)
_printword(word, "%i. " % len(words))
# Print detailed information if we only found 1 word.
if len(words) == 1:
_printdetailed(1, words)
return words
def _prompt():
puts("\nSearch for a word or view a translation in details.")
puts("Search: ", newline=False)
def _(arg):
""" Return an empty string if arg is None, else return arg.
"""
return arg if arg is not None else ''
def _printword(word, indicator=None):
""" Pretty print an instance of `TranslatedWord` from api.py
`indicator` must be None or a string, and will be printed just
before `word`.
"""
# Abusing automatic string concatenation.
txt = ("{indicator}{word} {class_} {inflection}"
"".format(indicator=_(indicator), word=colored.red(word.word),
class_=colored.blue(_(word.wordclass)),
inflection=colored.cyan(_(word.inflection))))
puts(txt)
def _printdetailed(input_, words):
""" Pretty print detailed translation from an instance of TranslatedWord.
"""
index = int(input_) - 1
if index < 0 or index >= len(words):
puts("You must choose a number in the range 1-" + str(len(words) + 1))
return
word = words[index]
# Print header
puts()
puts(word.language)
# Print details
with indent(4):
_printword(word)
for detail in word.details:
_printdetail(detail)
def _printdetail(detail):
""" Pretty print an instance of `WordDetails` from api.py
"""
txt = ("-{explanation}{combination} {word} - {example}"
"".format(explanation=colored.green(_(detail.explanation)),
combination=colored.cyan(_(detail.combination)),
word=colored.yellow(_(detail.word)),
example=colored.magenta(_(detail.example))))
puts(txt)
def _printavailabledictionaries():
print("The following dictionaries are available:")
dicts = availabledictionaries()
for dic in dicts:
puts("{short}: {long}".format(short=dic, long=dicts[dic]))
if __name__ == '__main__':
try:
main(" ".join(sys.argv[1:]))
except (KeyboardInterrupt, EOFError):
puts("Stopping program!")