Ejemplo n.º 1
0
def init_db():
    global config
    
    parser = optparse.OptionParser(usage='%prog -c CONFIGFILE [OPTIONS] --last-ride=[NUM]',
                                   description="Set the last (most recent) sync'd ride ID from Garmin Connect.")
    
    _setup_parser_common(parser)
    #parser.add_option('--user-id', dest='userid', metavar='ID', type='int', 
    #                  help='The Garmin Connect userid.')
    
    parser.add_option('--last-ride', dest='last_ride', metavar='ID', type='int', 
                      help='The last (most recent) ride that has been synchronized.')
        
    (options, args) = parser.parse_args()
    
    if not options.config_file:
        parser.error("No config file specified")
        parser.print_usage()
        sys.exit(2)
    
    if not options.last_ride:
        parser.error("Must specify the --last-ride option.")
        parser.print_help()
        sys.exit(2)
    
    init_config(options.config_file)
    logging.config.fileConfig(options.config_file)
    
    if options.database:
        config.set('main', 'database_path', options.database)
    
    gc_username = config.get('main', 'gc.username')
    gc_password = config.get('main', 'gc.password')
    gc_client = ConnectClient(gc_username, gc_password)
    
    activity = gc_client.get_activity(options.last_ride)
    userid = activity['userId'].encode('latin1') # It's a unicode string in response
    
    with closing(shelve.open(config.get('main', 'database_path'), 'c')) as db:
        db[userid] = options.last_ride
        logging.info("Updated last activity id for user {0} to {1}".format(userid, db[userid]))