1
2
3
4 """The main file parse the command line and run the program in the desired way.
5
6 There's three possible runing options:
7 - -c or --cmdl: Run the program using the command line interface (not
8 really suitable)
9 - -g or --gui: Run the program using the graphic user interface.
10 - -p or --profile: Simulate the predictions computation for a sample
11 string and print the profiling statistics for the session. This is
12 useful to see what part of the program took the largest part of the
13 program's execution time.
14
15 Examples of profiling use::
16 tipy -p '' -l 10
17 -> Simulate the input and predction of ten consecutive inputs of the
18 'hello world' string (default string).
19
20 tipy -p 'the seasons wither'
21 -> Simulate the input and predction of the 'the seasons wither'
22 string.
23
24 @note: You must specify your own test string after -p or --profile, if
25 none are specified a default one will be used.
26 @note: The option -l or -loop indicate how many times the profiling
27 operation have to be carried out.
28
29 @todo 0.1.0:
30 Add memoize classes and decorators to improve performance.
31 """
32
33 from PyQt5 import QtWidgets
34 from tipy.clbk import Callback
35 from tipy.drvr import Driver
36 from tipy.gui import MainWindow
37 from tipy.lg import lg
38 from sys import argv, exit
39 try:
40 from StringIO import StringIO
41 except ImportError:
42 from io import StringIO
43 from getopt import getopt, GetoptError
44 from cProfile import Profile, runctx
45 from pstats import Stats
46 from os import path, environ
47
48
49 CONFIG_FILE = ''
50
51
53 """Look for the best directory where a config file can be."""
54 global CONFIG_FILE
55 locations = [
56 path.join(path.expanduser("~"), '.config/tipy'),
57 path.join(path.dirname(path.realpath(__file__)), 'cfg'),
58 "/usr/share/tipy",
59 environ.get("TIPY_CONFIG_PATH"),
60 ]
61 for loc in locations:
62 try:
63 if path.isfile(path.join(loc, "tipy.ini")):
64 CONFIG_FILE = path.join(loc, "tipy.ini")
65 lg.info('Using config file "%s/tipy.ini"' % loc)
66 break
67 except IOError:
68 pass
69 except TypeError:
70 pass
71
72
74 """Initialize the Driver instance and start a cmdl REP loop."""
75 for i in range(profileLoops):
76 callback = Callback()
77 driver = Driver(callback, CONFIG_FILE)
78 for c in string:
79 callback.update(c)
80 suggestions = driver.predict()
81 for p in suggestions:
82 print(p)
83 print('-- Context: %s<|>' % (driver.callback.left))
84
85
87 """Initialize the Driver instance and start a cmdl REP loop."""
88 callback = Callback()
89 driver = Driver(callback, CONFIG_FILE)
90 while True:
91 buffer = input('> ')
92 callback.update(buffer)
93 suggestions = driver.predict()
94 for p in suggestions:
95 print(p)
96 print('-- Context: %s<|>' % (driver.callback.left))
97
98
100 """Initialize the Driver instance and run the """
101 callback = Callback()
102 driver = Driver(callback, CONFIG_FILE)
103 app = QtWidgets.QApplication(argv)
104 mySW = MainWindow(driver)
105 mySW.show()
106 exit(app.exec_())
107
108
110 """Print the usage message."""
111 print('USAGE: python3 main.py [-h -c -g -p <string> -l <loops>]\n')
112 print('OPTIONS:')
113 print('\t-h or --help: print the help')
114 print('\t-c or --cmdl: use the command line version')
115 print('\t-g or --gui: use the graphic user interface version')
116 print('\t-p or --profile: use the profile module to evaluate program '
117 'performance.')
118 print('\t\t<string> is the string to use for prediction computation.')
119 print('\t-l or --loops: run the profiling operation multiple times.')
120 print('\t\t<loops> is the number of loops.')
121
122
124 """The main function of the program.
125
126 Take care of the command line options and run the program (gui or command
127 line) or print the help.
128 """
129 try:
130 opts, args = getopt(
131 argv[1:], "hcgp:l:",
132 ['help', 'cmdl', 'gui', 'profile=', 'loop='])
133 except GetoptError as err:
134 print(str(err))
135 usage()
136 exit(2)
137 mode = 'gui'
138 profileString = 'hello world'
139 profileLoops = 1
140 for o, a in opts:
141 if o in ("-h", "--help"):
142 usage()
143 exit()
144 elif o in ("-c", "--cmdl"):
145 mode = 'cmdl'
146 elif o in ("-g", "--gui"):
147 mode = 'gui'
148 elif o in ("-p", "--profile"):
149 mode = 'profile'
150 if a:
151 profileString = a
152 elif o in ("-l", "--loop"):
153 if a:
154 profileLoops = int(a)
155 else:
156 assert False, "ERROR: Unrecognized option"
157 set_config_file()
158 if mode == 'cmdl':
159 run_cmdl()
160 elif mode == 'profile':
161 pr = Profile()
162 pr.enable()
163 runctx(
164 'run_profile(profileString, profileLoops)',
165 {'profileString': profileString, 'run_profile': run_profile,
166 'profileLoops': profileLoops}, locals())
167 pr.disable()
168 s = StringIO()
169 ps = Stats(pr, stream=s).sort_stats('time')
170 ps.print_stats()
171 print(s.getvalue())
172 else:
173 run_gui()
174
175
176 if __name__ == "__main__":
177 main()
178