Exemplo n.º 1
0
def read_garmin_file(fname, msg_q=None, options=None, s3_files=None):
    """ read single garmin file """
    from garmin_app.garmin_cache import pool
    from garmin_app.garmin_cache import GarminCache
    from garmin_app.garmin_parse import GarminParse
    from garmin_app.garmin_report import GarminReport
    from garmin_app.garmin_corrections import list_of_corrected_laps
    if options is None:
        options = {'cache_dir': CACHEDIR, 'do_update': False}
    tcx_job = pool.submit(convert_fit_to_tcx, fname)
    gpx_job = pool.submit(convert_gmn_to_gpx, fname)
    cache_dir = options['cache_dir']

    corr_list_ = list_of_corrected_laps(json_path='%s/run' % cache_dir)

    pickle_file_ = '%s/run/garmin.pkl.gz' % cache_dir
    cache_dir_ = '%s/run/cache' % cache_dir

    avro_file = '%s/%s.avro' % (cache_dir, os.path.basename(fname))
    if not os.path.exists(avro_file):
        s3_cache_files = get_list_of_keys('garmin-scripts-cache-ddboline')
        s3_key = os.path.basename(avro_file)
        if s3_key in s3_cache_files:
            download_from_s3('garmin-scripts-cache-ddboline', s3_key,
                             avro_file)

    cache_ = GarminCache(cache_directory=cache_dir_, corr_list=corr_list_)

    _temp_file = None
    if not options['do_update']:
        _temp_file = cache_.read_cached_gfile(gfbname=os.path.basename(fname))
    if _temp_file:
        _gfile = _temp_file
    else:
        _gfile = GarminParse(fname, corr_list=corr_list_)
        _gfile.read_file()
    if not _gfile:
        return False
    if options['do_update'] or not os.path.exists(avro_file):
        cache_.write_cached_gfile(garminfile=_gfile)
    _report = GarminReport(cache_obj=cache_, msg_q=msg_q, gfile=_gfile)
    print(_report.file_report_txt())
    _report.file_report_html(options=options)
    for fn0, fn1 in ((tcx_job.result(), '/tmp/temp.tcx'), (gpx_job.result(),
                                                           '/tmp/temp.gpx')):
        if fn0 and os.path.exists(fn0):
            os.rename(fn0, fn1)
    return True
Exemplo n.º 2
0
def test_garmin_corrections():
    test_json = {
        u'2011-07-04T08:58:27Z': {
            0: 3.1068559611866697
        },
        u'2012-03-13T18:21:11Z': {
            0: 0.362
        },
        u'DUMMY': {
            0: 0.0
        }
    }
    save_corrections(test_json, json_path='json_test', json_file='test.json')
    tmp = list_of_corrected_laps(json_path='json_test', json_file='test.json')
    assert garmin_corrections.get_list_of_corrected_laps() == tmp
    cleanup_pickle()
Exemplo n.º 3
0
def add_correction(correction_str, json_path=None, options=None):
    """ add correction to json file """
    from garmin_app.garmin_corrections import (list_of_corrected_laps,
                                               save_corrections)
    from garmin_app.garmin_corrections_sql import (read_corrections_table,
                                                   write_corrections_table)
    l_corr = list_of_corrected_laps(json_path=json_path)
    l_corr.update(
        read_corrections_table(do_tunnel=options.get('do_tunnel', False)))
    ent = correction_str.split()
    timestr = ent[0]
    try:
        parse(timestr)
    except ValueError:
        return {}
    lapdict = {}
    for idx, line in enumerate(ent[1:]):
        arg = line.split(',')
        tmp_ = []
        if len(arg) == 0:
            continue
        if len(arg) > 0:
            try:
                lapdist = float(arg[0])
            except ValueError:
                continue
            tmp_.append(lapdist)
        if len(arg) > 1:
            try:
                laptime = float(arg[1])
            except ValueError:
                laptime = None
            tmp_.append(laptime)
        if len(tmp_) == 1:
            lapdict[idx] = tmp_[0]
        elif len(tmp_) > 1:
            lapdict[idx] = tmp_
    l_corr[timestr] = lapdict
    save_corrections(l_corr)
    save_corrections(l_corr, json_path=json_path)
    if os.path.exists('%s/public_html/garmin/files' % HOMEDIR):
        save_corrections(l_corr,
                         json_path='%s/public_html/garmin/files' % HOMEDIR)
    write_corrections_table(l_corr, do_tunnel=options.get('do_tunnel', False))
    return l_corr
Exemplo n.º 4
0
def do_summary(directory_, msg_q=None, options=None):
    """ produce summary report """
    from garmin_app.garmin_cache import (GarminCache,
                                         read_pickle_object_in_file,
                                         write_pickle_object_to_file)
    from garmin_app.garmin_corrections import list_of_corrected_laps
    from garmin_app.garmin_corrections_sql import write_corrections_table
    from garmin_app.garmin_report import GarminReport
    if options is None:
        options = {'cache_dir': CACHEDIR}
    cache_dir = options['cache_dir']
    corr_list_ = list_of_corrected_laps(json_path='%s/run' % cache_dir)
    pickle_file_ = '%s/run/garmin.pkl.gz' % cache_dir
    cache_dir_ = '%s/run/cache' % cache_dir
    cache_ = GarminCache(cache_directory=cache_dir_,
                         corr_list=corr_list_,
                         use_sql=True,
                         do_tunnel=options.get('do_tunnel', False),
                         check_md5=options.get('do_check', False))
    if 'build' in options and options['build']:
        summary_list_ = cache_.get_cache_summary_list(directory='%s/run' %
                                                      cache_dir,
                                                      options=options)
        cache_ = GarminCache(pickle_file=pickle_file_,
                             cache_directory=cache_dir_,
                             corr_list=corr_list_,
                             use_sql=False,
                             check_md5=True,
                             cache_read_fn=read_pickle_object_in_file,
                             cache_write_fn=write_pickle_object_to_file,
                             do_tunnel=options.get('do_tunnel', False))
        cache_.cache_write_fn(cache_.cache_summary_file_dict)
        write_corrections_table(corr_list_,
                                do_tunnel=options.get('do_tunnel', False))
        return summary_list_
    summary_list_ = cache_.get_cache_summary_list(directory=directory_,
                                                  options=options)
    if not summary_list_:
        return False
    _report = GarminReport(cache_obj=cache_, msg_q=msg_q)
    print(_report.summary_report(summary_list_.values(), options=options))
    return True
Exemplo n.º 5
0
def test_garmin_corrections_sql():
    cor0 = list_of_corrected_laps()
    with OpenPostgreSQLsshTunnel(port=5433, do_tunnel=True) as pport:
        postgre_str = '%s:%d/%s' % (POSTGRESTRING, pport,
                                    'test_garmin_summary')
        gc_ = GarminCorrectionsSQL(sql_string=postgre_str)
        gc_.create_table()
        gc_.delete_table()
        gc_.create_table()
        gc_.write_sql_table(cor0)
        cor1 = gc_.read_sql_table()

        for key, val in cor0.items():
            if key == 'DUMMY':
                continue
            for lap in val:
                print(key, lap, cor0[key][lap], cor1[key][lap])
                if isinstance(val[lap], list):
                    assert abs(cor0[key][lap][0] - cor1[key][lap][0]) < 1e-6
                    assert abs(cor0[key][lap][1] - cor1[key][lap][1]) < 1e-6
                else:
                    assert abs(cor0[key][lap] - cor1[key][lap]) < 1e-6
        gc_.delete_table()
Exemplo n.º 6
0
 def test_list_of_corrected_laps(self):
     """ test list_of_corrected_laps """
     test_json = {
         u'2011-07-04T08:58:27Z': {
             0: 3.1068559611866697
         },
         u'2012-03-13T18:21:11Z': {
             0: 0.362
         },
         u'DUMMY': {
             0: 0.0
         }
     }
     save_corrections(test_json,
                      json_path='json_test',
                      json_file='test.json')
     tmp = list_of_corrected_laps(json_path='json_test',
                                  json_file='test.json')
     md5_ = get_md5('json_test/test.json')
     self.assertEqual(tmp, test_json)
     self.assertIn(md5_, [
         'a6ff6819f6c47a0af8334fae36c49b2d',
         '11bfd6af403c3fa8b38203493d0e1c4e'
     ])
Exemplo n.º 7
0
def garmin_parse_arg_list(args, options=None, msg_q=None):
    """ parse command line arguments """
    from garmin_app.garmin_cache import GarminCache
    from garmin_app.garmin_cache_sql import write_postgresql_table
    from garmin_app.garmin_corrections import list_of_corrected_laps
    from garmin_app.garmin_corrections_sql import write_corrections_table

    if options is None:
        options = {'cache_dir': CACHEDIR}
    cache_dir = options['cache_dir']

    s3_files = get_list_of_keys()

    gdir = set()
    for arg in args:
        if arg == 'build':
            if msg_q is not None:
                return
            options['build'] = True
        elif arg == 'backup':
            if msg_q is not None:
                return
            fname = '%s/garmin_data_%s.tar.gz'\
                    % (cache_dir, datetime.date.today().strftime('%Y%m%d'))
            run_command('cd %s/run/ ; ' % cache_dir +
                        'tar zcvf %s gps_tracks/ ' % fname +
                        'garmin_corrections.json')
            if os.path.exists('%s/public_html/backup' % os.getenv('HOME')):
                run_command('cp %s %s/public_html/backup/garmin_data.tar.gz' %
                            (fname, os.getenv('HOME')))
            if os.path.exists('%s/public_html/garmin/tar' % os.getenv('HOME')):
                run_command('mv %s %s/public_html/garmin/tar' %
                            (fname, os.getenv('HOME')))

            pickle_file_ = '%s/run/garmin.pkl.gz' % cache_dir
            cache_dir_ = '%s/run/cache' % cache_dir
            corr_list_ = list_of_corrected_laps(json_path='%s/run' % cache_dir)

            write_corrections_table(corr_list_,
                                    do_tunnel=options.get('do_tunnel', False))

            cache_ = GarminCache(pickle_file=pickle_file_,
                                 cache_directory=cache_dir_,
                                 corr_list=corr_list_,
                                 check_md5=True)
            summary_list_ = cache_.cache_read_fn()
            # backup garmin.pkl.gz info to postgresql database
            write_postgresql_table(summary_list_,
                                   do_tunnel=options.get('do_tunnel', False))

            return
        elif arg == 'occur':
            options['occur'] = True
        elif os.path.isfile(arg):
            gdir.add(arg)
        elif arg != 'run' and os.path.isdir(arg):
            gdir.add(arg)
        elif arg != 'run' and os.path.isdir('%s/run/%s' % (cache_dir, arg)):
            gdir.add('%s/run/%s' % (cache_dir, arg))
        elif arg == 'correction':
            add_correction(' '.join(args[1:]),
                           json_path='%s/run' % cache_dir,
                           options=options)
            return
        elif arg in options:
            options[arg] = True
        elif 'do_%s' % arg in options:
            options['do_%s' % arg] = True
        else:
            spts = [x for x in SPORT_TYPES if arg in x]
            if len(spts) > 0:
                options['do_sport'] = spts[0]
            elif arg == 'bike':
                options['do_sport'] = 'biking'
            elif '-' in arg or arg in (
                    '%4d' % _ for _ in range(2008,
                                             datetime.date.today().year + 1)):
                gdir.update(find_gps_tracks(arg, cache_dir, s3_files=s3_files))
            elif '.gmn' in arg or 'T' in arg:
                files = glob.glob('%s/run/gps_tracks/%s' % (cache_dir, arg))
                gdir.update(files)
            else:
                print('unhandled argument:', arg)
    if not gdir:
        gdir.add('%s/run/gps_tracks' % cache_dir)

    gdir = sorted(gdir)

    if len(gdir) == 1 and os.path.isfile(gdir[0]):
        return read_garmin_file(gdir[0],
                                msg_q=msg_q,
                                options=options,
                                s3_files=s3_files)
    return do_summary(gdir, msg_q=msg_q, options=options)