From 4ec43770db073320cdd2b77d0368945127d2a5ac Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Tue, 26 Sep 2017 12:08:38 +0200 Subject: [PATCH] basic processing of upload options from email --- rowers/uploads.py | 82 +++++++++++++++++++++++++++++++++++++++++------ temp.txt | 1 + 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/rowers/uploads.py b/rowers/uploads.py index 0776a3ba..90f66483 100644 --- a/rowers/uploads.py +++ b/rowers/uploads.py @@ -15,6 +15,7 @@ import yaml import argparse import yamllint from subprocess import call +import re try: from cStringIO import StringIO @@ -26,17 +27,80 @@ from rowers.utils import ( str2bool,range_to_color_hex,absolute ) +def cleanbody(body): + p = re.compile('.*---\n([\s\S]*)...') + m = p.match(body) + if m != None: + body = m.group(1) + + return body + +def getsyncoptions(uploadoptions,values): + try: + value = values.lower() + values = [values] + except AttributeError: + pass + + for v in values: + try: + v = v.lower() + + if v in ['c2','concept2','logbook']: + uploadoptions['upload_to_C2'] = True + if v in ['tp','trainingpeaks']: + uploadoptions['upload_totp'] = True + if v in ['strava']: + uploadoptions['upload_to_Strava'] = True + if v in ['st','sporttracks']: + uploadoptions['upload_to_SportTracks'] = True + if v in ['rk','runkeeper']: + uploadoptions['upload_to_RunKeeper'] = True + if v in ['ua','underarmour','mapmyfitness']: + uploadoptions['upload_to_MapMyFitness'] = True + except AttributeError: + pass + + return uploadoptions + +def getplotoptions(uploadoptions,value): + try: + v = value.lower() + if v in ['pieplot','timeplot','distanceplot']: + uploadoptions['make_plot'] = True + uploadoptions['plottype'] = v + elif 'pie' in v: + uploadoptions['make_plot'] = True + uploadoptions['plottype'] = 'pieplot' + elif 'distance' in v: + uploadoptions['make_plot'] = True + uploadoptions['plottype'] = 'distanceplot' + elif 'time' in v: + uploadoptions['make_plot'] = True + uploadoptions['plottype'] = 'timeplot' + except TypeError: + pass + + return uploadoptions + def upload_options(body): uploadoptions = {} - # ok, a temp solution - write to temp file - with open('temp.txt','w') as f: - f.write(body) - with open('temp.txt','r') as f: - try: - yml = (yaml.load(f)) - print yml - except yaml.YAMLError as exc: - call(["yamllint","-f","parsable",'temp.txt']) + body = cleanbody(body) + try: + yml = (yaml.load(body)) + for key, value in yml.iteritems(): + if key == 'sync' or key == 'synchronization': + uploadoptions = getsyncoptions(uploadoptions,value) + if key == 'chart' or key == 'static' or key == 'plot': + uploadoptions = getplotoptions(uploadoptions,value) + except yaml.YAMLError as exc: + pm = exc.problem_mark + strpm = str(pm) + pbm = "Your email has an issue on line {} at position {}. The error is: ".format( + pm.line+1, + pm.column+1, + )+strpm + return {'error':pbm} return uploadoptions diff --git a/temp.txt b/temp.txt index 6a80554b..5fdc2a13 100644 --- a/temp.txt +++ b/temp.txt @@ -1,4 +1,5 @@ --- aap: noot mies: jet +jet ... \ No newline at end of file