Beispiel #1
0
 def add_simfiles(self, paths):
     simfile_paths = []
     for path in paths:
         simfile_paths.extend(utils.find_simfiles(path))
     simfile_list = self.glade.get_object('simfiles')
     for path in simfile_paths:
         # Don't re-add simfiles that were already added
         already_added = False
         for existing_simfile in simfile_list:
             if path == existing_simfile[-1]:
                 already_added = True
                 break
         if already_added:
             break
         # Get metadata from the simfiles (without creating Simfile objects)
         metadata = {'TITLE': None, 'ARTIST': None, 'CREDIT': None}
         metadata_missing = len(metadata)
         with codecs.open(path, 'r', encoding='utf-8') as msdfile:
             for param in MSDParser(msdfile):
                 if param[0].upper() in metadata:
                     metadata[param[0].upper()] = ':'.join(param[1:])
                     metadata_missing -= 1
                     if not metadata_missing: break
                 # We're probably not going to find any useful
                 # parameters once we're down to the charts
                 elif param[0].upper() == 'NOTES':
                     break
         simfile_list.append([metadata['TITLE'], metadata['ARTIST'],
                               metadata['CREDIT'], path])
         while gtk.events_pending():
             gtk.main_iteration_do(False)
Beispiel #2
0
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()