Package toon :: Module runner
[hide private]
[frames] | no frames]

Source Code for Module toon.runner

  1  #!/usr/bin/env python 
  2  # 
  3  # Toonloop for Python 
  4  # 
  5  # Copyright 2008 Alexandre Quessy & Tristan Matthews 
  6  # <alexandre@quessy.net> & <le.businessman@gmail.com> 
  7  # http://www.toonloop.com 
  8  # 
  9  # Original idea by Alexandre Quessy 
 10  # http://alexandre.quessy.net 
 11  # 
 12  # Toonloop is free software: you can redistribute it and/or modify 
 13  # it under the terms of the GNU General Public License as published by 
 14  # the Free Software Foundation, either version 3 of the License, or 
 15  # (at your option) any later version. 
 16  # 
 17  # Toonloop is distributed in the hope that it will be useful, 
 18  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 20  # GNU General Public License for more details. 
 21  # 
 22  # You should have received a copy of the gnu general public license 
 23  # along with Toonloop.  If not, see <http://www.gnu.org/licenses/>. 
 24  # 
 25  """ 
 26  Main runner of the Toonloop application. 
 27  """ 
 28  __version__ = "1.1.1" # MUST ALSO CHANGE IT IN setup.py and desktop file 
 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  # the core:  
 39  from rats import render 
 40  from toon import core # Configuration, Toonloop, ToonloopError 
 41  from toon import optgroup 
 42   
43 -def run():
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 # most important, the configuration options: 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 # other args: 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.") # default=False 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.") # default=False 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() # all the config is in this object. 83 config_dict = config.__dict__ 84 if options.config_file: 85 config_dict["config_file"] = options.config_file 86 # XXX: Loading from the json state saving file might cause bugs that 87 # are hard to find. 88 config.load() # updates the config dict with values serialized before. 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 # fixed aspect ratio 96 # options that have default values: 97 config_dict['video_device'] = options.device #FIXME 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 #raise core.ToonloopError('No such Toonloop option :', e.message) 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) # not verbose ! options.verbose 144 pygame_timer.desired_fps = options.fps 145 # optgroups must be set once toonloop has been initialized. 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 # will exit on ctrl-c 162 print("Exiting toonloop") 163 try: 164 try: 165 reactor.stop() # just in case. 166 except error.ReactorNotRunning, e: 167 pass 168 except AttributeError, e: # error.ReactorNotRunning is deprecated in later twisted version. 169 pass 170 sys.exit(0)
171