Skip to content

Commit

Permalink
Shift PlotUploader class to separate file.
Browse files Browse the repository at this point in the history
  • Loading branch information
Karan Desai committed Jun 17, 2016
1 parent c276de0 commit d4576a0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
41 changes: 2 additions & 39 deletions tardis/tests/tests_slow/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import os
import glob
import shutil
import tempfile
import yaml
import numpy as np
import pytest
from pytest_html import extras
from astropy import units as u

from tardis.tests.tests_slow.report import DokuReport
import tardis
from tardis.tests.tests_slow.plot_helpers import PlotUploader


def pytest_configure(config):
Expand Down Expand Up @@ -56,46 +54,11 @@ def pytest_runtest_makereport(item, call):

@pytest.fixture(scope="function")
def plot_object(request):
class PlotUploader(object):
def __init__(self):
self._plots = list()
self.plot_links = list()

def add(self, plot, name):
"""
Accept a `matplotlib.pyplot.figure` and add it to self._plots.
"""
self._plots.append((plot, name))

def upload(self):
"""
Upload the content in self._plots to dokuwiki.
"""
dokuwiki_url = request.config.dokureport.dokuwiki_url
for plot, name in self._plots:
plot_file = tempfile.NamedTemporaryFile(suffix=".png")
plot.savefig(plot_file.name)

request.config.dokureport.doku_conn.medias.add(
"plots:{0}_{1}.png".format(tardis.__githash__[0:7], name),
plot_file.name
)
self.plot_links.append(extras.url(
"{0}lib/exe/fetch.php?media=plots:{1}_{2}.png".format(
dokuwiki_url, tardis.__githash__[0:7], name
), name)
)
plot_file.close()

def get_extras(self):
return self.plot_links
plot_obj = PlotUploader()
setattr(request.node, "plot_obj", plot_obj)

def fin():
# Adding plots in this manner is temporary.
plot_obj.add()
plot_obj.upload()
plot_obj.upload(request)
request.addfinalizer(fin)
return plot_obj

Expand Down
38 changes: 38 additions & 0 deletions tardis/tests/tests_slow/plot_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import tempfile
from pytest_html import extras
import tardis


class PlotUploader(object):
def __init__(self):
self._plots = list()
self.plot_links = list()

def add(self, plot, name):
"""
Accept a `matplotlib.pyplot.figure` and add it to self._plots.
"""
self._plots.append((plot, name))

def upload(self, request):
"""
Upload the content in self._plots to dokuwiki.
"""
dokuwiki_url = request.config.dokureport.dokuwiki_url
for plot, name in self._plots:
plot_file = tempfile.NamedTemporaryFile(suffix=".png")
plot.savefig(plot_file.name)

request.config.dokureport.doku_conn.medias.add(
"plots:{0}_{1}.png".format(tardis.__githash__[0:7], name),
plot_file.name
)
self.plot_links.append(extras.url(
"{0}lib/exe/fetch.php?media=plots:{1}_{2}.png".format(
dokuwiki_url, tardis.__githash__[0:7], name
), name)
)
plot_file.close()

def get_extras(self):
return self.plot_links

0 comments on commit d4576a0

Please sign in to comment.