def run_button(self, button, command_name): # Get command class for cn, Command in utils.get_commands().items(): if command_name == cn: break # Get option fields options = {} for field, widget in self.optionfields[command_name].items(): field_dict = [f for f in Command.fields if f['name'] == field][0] if field_dict['input'] == command.FieldInputs.boolean: options[field] = widget.get_active() elif field_dict['input'] == command.FieldInputs.text: options[field] = widget.get_text() # Create output window output_window = self.glade.get_object('output') output_window.show_all() output_window.present() self.log.debug(str(options)) # Process the simfiles simfile_list = self.glade.get_object('simfiles') try: command_instance = Command(options) except Exception: self.error_to_output_window() return for item in simfile_list: try: command_instance.run(Simfile(item[-1])) except Exception: self.error_to_output_window() command_instance.done() self.log.info('')
def __init__(self): # Set up Glade self.glade = gtk.Builder() self.glade.add_from_file(SynctoolsGUI.gladefile) self.glade.connect_signals(self) # Set up logging self.log = logging.getLogger('synctools') self.log.setLevel(logging.DEBUG) self.log.addHandler(GtkTextViewHandler( self.glade.get_object('output_textview') )) # Populate command combo box notebook = self.glade.get_object('command_notebook') self.optionfields = {} # Create a page for each command for cn, Command in utils.get_commands().items(): # Save fields for future access self.optionfields[cn] = current_fields = {} # Each tab is a Table with the first column used for labels and the # second column for inputs. There's an extra row at the top for # the description and one at the bottom for the "Run" button. page = gtk.Table(rows=len(Command.fields)+2, columns=2) for f, field in enumerate(Command.fields): page.attach(gtk.Label(field['title']), 0, 1, f + 1, f + 2) if field['input'] == command.FieldInputs.text: # Add text field field_widget = gtk.Entry() field_widget.set_text(str(field['default'])) elif field['input'] == command.FieldInputs.boolean: # Add checkbox / check button field_widget = gtk.CheckButton() if field['default']: field_widget.set_active(True) page.attach(field_widget, 1, 2, f + 1, f + 2) current_fields[field['name']] = field_widget # Add description page.attach(gtk.Label(Command.description), 0, 2, 0, 1) # Add "Run" button run_button = gtk.Button(Command.title) run_button.connect('clicked', self.run_button, cn) page.attach(run_button, 0, 2, f + 2, f + 3, 0, 0, 0, 5) notebook.append_page(page, gtk.Label(Command.title)) # Allow selection of multiple simfiles selection = self.glade.get_object('simfile_tree').get_selection() selection.set_mode(gtk.SELECTION_MULTIPLE) # Parse any file arguments self.add_simfiles(sys.argv[1:]) # Add version number to about window self.glade.get_object('about').set_comments('v' + __version__) # Set up main window self.window = self.glade.get_object('synctools') self.window.drag_dest_set( gtk.DEST_DEFAULT_MOTION | gtk.DEST_DEFAULT_HIGHLIGHT | gtk.DEST_DEFAULT_DROP, [('text/uri-list', 0, SynctoolsGUI.file_uri_target)], gtk.gdk.ACTION_COPY ) self.window.show_all() gtk.main()
def main(): # Set up logging log = logging.getLogger('synctools') log.setLevel(logging.INFO) log.addHandler(logging.StreamHandler()) # Set up argument parsing parser = argparse.ArgumentParser() parser.add_argument('command', metavar='cmd', help='the command to run') parser.add_argument('paths', metavar='path', nargs='+', help='paths to simfiles and/or simfile directories') parser.add_argument('-v', '--version', action='version', version=__version__) parser.add_argument('-l', '--ls', action='store_true', help='list installed commands and exit') parser.add_argument('-d', '--defaults', action='store_true', help="don't prompt for input; just use default values") # argparse doesn't know how to handle print-and-exit options outside of # --help and --version, so this has to be done before the arguments are # parsed. if len(sys.argv) >= 2 and sys.argv[1] in ('-l', '--ls'): for command_name, Command in utils.get_commands().items(): print '{name}: {description}'.format( name=command_name, description=Command.description ) sys.exit() args = parser.parse_args() # Make sure input files exist for path in args.paths: if not os.path.exists(path): parser.error('%r: no such file or directory' % path) # Determine the command to run commands = utils.get_commands() keys = commands.keys() # Map lowercase keys to the original CamelCase versions keys_ci = dict(zip((k.lower() for k in keys), keys)) command_normalized = keys_ci.get(args.command.lower(), None) if not command_normalized: parser.error('invalid command %r' % args.command) Command = commands[command_normalized] # Get options from command line options = {} for field in Command.fields: while True: if args.defaults: # Use the default value value = None else: # Determine default value to show in brackets if field['input'] == command.FieldInputs.boolean: default_string = 'Y/n' if field['default'] else 'y/N' else: default_string = field['default'] # Request user input value = raw_input('{title} [{default}]: '.format( title=field['title'], default=default_string)) if not value: value = field['default'] try: options[field['name']] = field['type'](value) break except Exception: print traceback.format_exc().splitlines()[-1] command_instance = Command(options) # Find simfiles simfiles = [utils.find_simfiles(arg) for arg in args.paths] for simfile in itertools.chain(*simfiles): command_instance.run(Simfile(simfile)) command_instance.done()