From fc7f147e3fc7facb86e2e1ab6d2885f38f573235 Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Tue, 28 Aug 2018 08:09:30 -0700 Subject: [PATCH] check that output path exists before doing HTTP endpoint access; reformat to keep line length down --- appfigures.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/appfigures.py b/appfigures.py index d844293..38db1db 100755 --- a/appfigures.py +++ b/appfigures.py @@ -60,6 +60,7 @@ def getUpdatesDaily(t): def plotData(filename, ylabel, timespan, x, y): # allow this to run on a headless server import matplotlib + matplotlib.use("Agg") import matplotlib.pyplot as plt @@ -83,18 +84,35 @@ def plotData(filename, ylabel, timespan, x, y): plt.savefig(filename, dpi=150) +def checkOutputPath(path): + from os import access, F_OK + + return access(path, F_OK) + + def main(): from json import loads from os.path import join from sys import argv - getPath = lambda n: join(argv[1], n) + outputPath = argv[1] + assert checkOutputPath(outputPath) + getPath = lambda n: join(outputPath, n) data = getData(loads(openURL("sales/dates/-120/0").read())) - plotData(getPath("downloads_daily.png"), "Downloads", "Daily", *getDownloadsDaily(data)) + plotData( + getPath("downloads_daily.png"), "Downloads", "Daily", *getDownloadsDaily(data) + ) plotData(getPath("updates_daily.png"), "Updates", "Daily", *getUpdatesDaily(data)) - plotData(getPath("downloads_weekly.png"), "Downloads", "Weekly", *getDownloadsWeekly(data)) - plotData(getPath("updates_weekly.png"), "Updates", "Weekly", *getUpdatesWeekly(data)) + plotData( + getPath("downloads_weekly.png"), + "Downloads", + "Weekly", + *getDownloadsWeekly(data) + ) + plotData( + getPath("updates_weekly.png"), "Updates", "Weekly", *getUpdatesWeekly(data) + ) main()