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 Main runner of the Toonloop application.
27 """
28 __version__ = "1.1.1"
29
30 import sys
31 import os
32 import pygame
33 import optparse
34
35 from twisted.internet import reactor
36 from twisted.internet import error
37
38
39 from rats import render
40 from toon import core
41 from toon import optgroup
42
44 """
45 Starts the application, reading the command-line arguments.
46 """
47 parser = optparse.OptionParser(usage="%prog", version='Toonloop ' + str(__version__))
48 parser.add_option("-d", "--device", dest="device", type="int", \
49 help="Specifies V4L2 device to grab image from. Expects an integer such as 0, 1 or 2.", default=0)
50 parser.add_option("-v", "--verbose", dest="verbose", action="store_true", \
51 help="Sets the output to verbose.")
52
53 parser.add_option("-o", "--option", action="append", nargs=2, \
54 help="Sets any toonloop option by name. See list-options for a list of possible options. Example : -o project_name example")
55 parser.add_option("-l", "--list-options", \
56 dest="list_options", action="store_true", \
57 help="Prints the list of options and exits.")
58
59 parser.add_option("-f", "--fps", type="int", \
60 help="Sets the rendering frame rate. Default is 30 FPS.", default=30)
61 parser.add_option("-F", "--fullscreen", action="store_true", \
62 help="Displays in fullscreen mode at startup.")
63 parser.add_option("-t", "--intervalometer-rate-seconds", type="float", \
64 help="Sets intervalometer interval in seconds.", default=30.0)
65 parser.add_option("-c", "--config-file", type="string", \
66 help="Path to config file.", default=os.path.expanduser("~/.toonloop.json"))
67 parser.add_option("-H", "--toonloop-home", type="string", \
68 help="Path to saved files.")
69 parser.add_option("-i", "--intervalometer-on", \
70 dest="intervalometer_on", action="store_true", \
71 help="Starts the intervalometer at startup.")
72 parser.add_option("-x", "--options-group",
73 action="append", nargs=3,
74 help="Sets option from an option group.")
75 parser.add_option("-e", "--intervalometer-enabled", \
76 dest="intervalometer_enabled", action="store_true", \
77 help="Enables/disables the use of the intervalometer.", default=True)
78 parser.add_option("-w", "--image-width", type="int", \
79 help="Width of the images grabbed from the camera. Using this flag also sets the height, with a 4:3 ratio.")
80 (options, args) = parser.parse_args()
81 pygame.init()
82 config = core.Configuration()
83 config_dict = config.__dict__
84 if options.config_file:
85 config_dict["config_file"] = options.config_file
86
87
88 config.load()
89 if options.toonloop_home:
90 config_dict['toonloop_home'] = options.toonloop_home
91 if options.fullscreen:
92 config_dict["display_fullscreen"] = options.fullscreen
93 if options.image_width:
94 config_dict['image_width'] = options.image_width
95 config_dict['image_height'] = options.image_width * 3 / 4
96
97 config_dict['video_device'] = options.device
98 config_dict['intervalometer_rate_seconds'] = options.intervalometer_rate_seconds
99 config_dict['intervalometer_on'] = options.intervalometer_on == True
100 config_dict['intervalometer_enabled'] = options.intervalometer_enabled
101 config_dict['verbose'] = options.verbose == True
102
103 if options.list_options:
104 print("""Use Toonloop options with -o flag :
105 toonloop -o [name] [value]""")
106 print("Toonloop options and their current values :")
107 config.print_values()
108 else:
109 print("Toonloop - Version " + str(__version__))
110 print("Copyright 2008 Alexandre Quessy & Tristan Matthews")
111 print("Released under the GNU General Public License")
112 print("Using video device %d" % options.device)
113 print("Press h for usage and instructions.")
114 print("Press i for informations and statistics.")
115
116 if config_dict['verbose']:
117 print("Started in verbose mode.")
118 if options.option:
119 if config.verbose:
120 print('options: %s' % (options.option))
121 for k, v in options.option:
122 try:
123 kind = config.set(k, v)
124 if config.verbose:
125 print("OPTION \"%s\" : %s %s" % (k, v, kind))
126 except KeyError, e:
127 print("Error. No such Toonloop option : %s" % (e.message))
128 sys.exit(1)
129
130 except Exception, e:
131 print(sys.exc_info())
132 raise core.ToonloopError('Error with Toonloop option :', e.message)
133 try:
134 toonloop = core.Toonloop(config)
135 if options.verbose:
136 toonloop.print_help()
137 except core.ToonloopError, e:
138 print(str(e.message))
139 print("Exiting toonloop with error")
140 sys.exit(1)
141 else:
142 print("Congratulations ! Toonloop started gracefully.")
143 pygame_timer = render.Renderer(toonloop, False)
144 pygame_timer.desired_fps = options.fps
145
146
147 if options.list_options:
148 toonloop.print_optgroups()
149 sys.exit(0)
150 if options.options_group is not None:
151 for group, key, value in options.options_group:
152 try:
153 toonloop.set_option_in_group(group, key, value)
154 except ToonloopError, e:
155 print(e.message)
156 except optgroup.OptionsError, e:
157 print(e.message)
158 try:
159 reactor.run()
160 except KeyboardInterrupt:
161 pass
162 print("Exiting toonloop")
163 try:
164 try:
165 reactor.stop()
166 except error.ReactorNotRunning, e:
167 pass
168 except AttributeError, e:
169 pass
170 sys.exit(0)
171