aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKapileshwar Singh <kapileshwar.singh@arm.com>2015-08-25 16:57:39 +0100
committerJavi Merino <javi.merino@arm.com>2015-09-01 16:25:07 +0100
commit53de9b352977814c37608788cc1df83fc6485371 (patch)
tree0dd16a2f772674ce21d951c740a84a14df4746a8
parent557bfd9d7e1f5f8b24250bda48f05e1c2c259114 (diff)
downloadtrappy-53de9b352977814c37608788cc1df83fc6485371.tar.gz
plotter: IPython v4 Compatibility
IPython 4.0 changes the location of the web server and the way IPython 4.0 profiles are handled. This changes allows plotter to be compatible with both IPython 4.0+ and the earlier versions Signed-off-by: Kapileshwar Singh <kapileshwar.singh@arm.com>
-rw-r--r--trappy/plotter/AttrConf.py34
-rw-r--r--trappy/plotter/EventPlot.py23
-rw-r--r--trappy/plotter/ILinePlot.py4
-rw-r--r--trappy/plotter/ILinePlotGen.py16
-rw-r--r--trappy/plotter/IPythonConf.py180
-rw-r--r--trappy/plotter/Utils.py68
-rw-r--r--trappy/plotter/__init__.py4
-rw-r--r--trappy/plotter/js/EventPlot.js4
-rw-r--r--trappy/plotter/js/ILinePlot.js4
9 files changed, 210 insertions, 127 deletions
diff --git a/trappy/plotter/AttrConf.py b/trappy/plotter/AttrConf.py
index 7116112..46839e7 100644
--- a/trappy/plotter/AttrConf.py
+++ b/trappy/plotter/AttrConf.py
@@ -95,37 +95,3 @@ HTML_HEIGHT = 400
DEFAULT_SYNC_ZOOM = False
EVENT_PLOT_STRIDE = False
-IPLOT_RESOURCES = {
- "ILinePlot": [
- "http://cdnjs.cloudflare.com/ajax/libs/dygraph/1.1.1/dygraph-combined.js",
- "js/ILinePlot.js",
- "http://dygraphs.com/extras/synchronizer.js"],
- "EventPlot": [
- "http://d3js.org/d3.v3.min.js",
- "http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js",
- "js/EventPlot.js"]}
-
-try:
- import IPython
- import os
- ip = IPython.get_ipython()
- if not ip:
- PLOTTER_IPYTHON = False
- else:
- PLOTTER_IPYTHON = True
- PLOTTER_IPYTHON_PROFILE_DIR = ip.config.ProfileDir["location"]
- PLOTTER_STATIC_DATA_DIR = os.path.join(
- PLOTTER_IPYTHON_PROFILE_DIR,
- "static", "plotter_data")
- PLOTTER_SCRIPTS_DIR = "plotter_scripts"
- PLOTTER_SCRIPTS_PATH = os.path.join(
- PLOTTER_IPYTHON_PROFILE_DIR,
- "static",
- PLOTTER_SCRIPTS_DIR)
-
- if not os.path.isdir(PLOTTER_STATIC_DATA_DIR):
- os.mkdir(PLOTTER_STATIC_DATA_DIR)
- if not os.path.isdir(PLOTTER_SCRIPTS_PATH):
- os.mkdir(PLOTTER_SCRIPTS_PATH)
-except:
- PLOTTER_IPYTHON = False
diff --git a/trappy/plotter/EventPlot.py b/trappy/plotter/EventPlot.py
index 4943ff4..25b30ec 100644
--- a/trappy/plotter/EventPlot.py
+++ b/trappy/plotter/EventPlot.py
@@ -29,15 +29,15 @@ import json
import os
from IPython.display import display, HTML
from trappy.plotter.AbstractDataPlotter import AbstractDataPlotter
+from trappy.plotter import IPythonConf
-if not AttrConf.PLOTTER_IPYTHON:
+if not IPythonConf.check_ipython():
raise ImportError("Ipython Environment not Found")
# pylint: disable=R0201
# pylint: disable=R0921
# Initialize Resources
-from trappy.plotter import Utils
-Utils.iplot_install("EventPlot")
+IPythonConf.iplot_install("EventPlot")
class EventPlot(AbstractDataPlotter):
@@ -96,7 +96,7 @@ class EventPlot(AbstractDataPlotter):
graph["stride"] = AttrConf.EVENT_PLOT_STRIDE
json_file = os.path.join(
- AttrConf.PLOTTER_STATIC_DATA_DIR,
+ IPythonConf.get_data_path(),
self._fig_name +
".json")
@@ -139,21 +139,24 @@ class EventPlot(AbstractDataPlotter):
paths: {
- "EventPlot": "/static/plotter_scripts/EventPlot/EventPlot",
- "d3-tip": "/static/plotter_scripts/EventPlot/d3.tip.v0.6.3",
- "d3": "/static/plotter_scripts/EventPlot/d3.v3.min"
+ "EventPlot": '""" + IPythonConf.add_web_base("plotter_scripts/EventPlot/EventPlot") + """',
+ "d3-tip": '""" + IPythonConf.add_web_base("plotter_scripts/EventPlot/d3.tip.v0.6.3") + """',
+ "d3-plotter": '""" + IPythonConf.add_web_base("plotter_scripts/EventPlot/d3.v3.min") + """'
},
shim: {
- "d3-tip": ["d3"],
+ "d3-plotter" : {
+ "exports" : "d3"
+ },
+ "d3-tip": ["d3-plotter"],
"EventPlot": {
- "deps": ["d3-tip", "d3" ],
+ "deps": ["d3-tip", "d3-plotter" ],
"exports": "EventPlot"
}
}
});
req(["require", "EventPlot"], function() {
- EventPlot.generate('""" + self._fig_name + """');
+ EventPlot.generate('""" + self._fig_name + """', '""" + IPythonConf.add_web_base("") + """');
});
</script>
"""
diff --git a/trappy/plotter/ILinePlot.py b/trappy/plotter/ILinePlot.py
index ab0bcdf..380d255 100644
--- a/trappy/plotter/ILinePlot.py
+++ b/trappy/plotter/ILinePlot.py
@@ -19,13 +19,15 @@ customizing Line Plots with a pandas dataframe input
import matplotlib.pyplot as plt
from trappy.plotter import AttrConf
+from trappy.plotter import Utils
from trappy.plotter.Constraint import ConstraintManager
from trappy.plotter.ILinePlotGen import ILinePlotGen
from trappy.plotter.AbstractDataPlotter import AbstractDataPlotter
from trappy.plotter.ColorMap import ColorMap
+from trappy.plotter import IPythonConf
import pandas as pd
-if not AttrConf.PLOTTER_IPYTHON:
+if not IPythonConf.check_ipython():
raise ImportError("Ipython Environment not Found")
class ILinePlot(AbstractDataPlotter):
diff --git a/trappy/plotter/ILinePlotGen.py b/trappy/plotter/ILinePlotGen.py
index ae889f6..f17c782 100644
--- a/trappy/plotter/ILinePlotGen.py
+++ b/trappy/plotter/ILinePlotGen.py
@@ -23,14 +23,14 @@ import uuid
import json
import os
from IPython.display import display, HTML
+from trappy.plotter import IPythonConf
-if not AttrConf.PLOTTER_IPYTHON:
+if not IPythonConf.check_ipython():
raise ImportError("No Ipython Environment found")
# Install resources
-from trappy.plotter import Utils
-Utils.iplot_install("ILinePlot")
+IPythonConf.iplot_install("ILinePlot")
class ILinePlotGen(object):
@@ -50,9 +50,9 @@ class ILinePlotGen(object):
var ilp_req = require.config( {
paths: {
- "dygraph-sync": "/static/plotter_scripts/ILinePlot/synchronizer",
- "dygraph": "/static/plotter_scripts/ILinePlot/dygraph-combined",
- "ILinePlot": "/static/plotter_scripts/ILinePlot/ILinePlot",
+ "dygraph-sync": '""" + IPythonConf.add_web_base("plotter_scripts/ILinePlot/synchronizer") + """',
+ "dygraph": '""" + IPythonConf.add_web_base("plotter_scripts/ILinePlot/dygraph-combined") + """',
+ "ILinePlot": '""" + IPythonConf.add_web_base("plotter_scripts/ILinePlot/ILinePlot") + """',
},
shim: {
@@ -65,7 +65,7 @@ class ILinePlotGen(object):
}
});
ilp_req(["require", "ILinePlot"], function() {
- ILinePlot.generate('""" + fig_name + """');
+ ILinePlot.generate('""" + fig_name + """', '""" + IPythonConf.add_web_base("") + """');
});
</script>
"""
@@ -183,7 +183,7 @@ width: {0}px; height: auto;"; id="{1}"></div></td>'.format(width,
if "ylim" in self._attr:
fig_params["valueRange"] = self._attr["ylim"]
- json_file = os.path.join(AttrConf.PLOTTER_STATIC_DATA_DIR, fig_name + ".json")
+ json_file = os.path.join(IPythonConf.get_data_path(), fig_name + ".json")
fh = open(json_file, "w")
json.dump(fig_params, fh)
fh.close()
diff --git a/trappy/plotter/IPythonConf.py b/trappy/plotter/IPythonConf.py
new file mode 100644
index 0000000..35117cf
--- /dev/null
+++ b/trappy/plotter/IPythonConf.py
@@ -0,0 +1,180 @@
+# Copyright 2015-2015 ARM Limited
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""IPythonConf provides abstraction for the varying configurations in
+different versions of ipython/jupyter packages.
+"""
+import urllib
+import os
+import shutil
+from distutils.version import StrictVersion as V
+
+IPLOT_RESOURCES = {
+ "ILinePlot": [
+ "http://cdnjs.cloudflare.com/ajax/libs/dygraph/1.1.1/dygraph-combined.js",
+ "js/ILinePlot.js",
+ "http://dygraphs.com/extras/synchronizer.js"],
+ "EventPlot": [
+ "http://d3js.org/d3.v3.min.js",
+ "http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js",
+ "js/EventPlot.js"]}
+
+PROFILE_DIR_IPYTHON_V4 = os.path.expanduser("~/.local/share/jupyter")
+IPYTHON_V4_BASE = "/nbextensions"
+IPYTHON_V3_BASE = "/static"
+PLOTTER_SCRIPTS = "plotter_scripts"
+PLOTTER_DATA = "plotter_data"
+
+def install_http_resource(url, to_path):
+ """Install a HTTP Resource (eg. javascript) to
+ a destination on the disk
+
+ Args:
+ url (str): HTTP URL
+ to_path (str): Destintation path on the disk
+ """
+ urllib.urlretrieve(url, filename=to_path)
+
+
+def install_local_resource(from_path, to_path):
+ """Move a local resource to the desired
+ a destination.
+
+ Args:
+ from_path (str): Path relative to this file
+ to_path (str): Destintation path on the disk
+ """
+ base_dir = os.path.dirname(__file__)
+ from_path = os.path.join(base_dir, from_path)
+ shutil.copy(from_path, to_path)
+
+
+def install_resource(from_path, to_path):
+ """Install a resource to a location on the disk
+
+ Args:
+ from_path (str): URL or relative path
+ to_path (str): Destintation path on the disk
+ """
+
+ if from_path.startswith("http"):
+ if not os.path.isfile(to_path):
+ install_http_resource(from_path, to_path)
+ else:
+ install_local_resource(from_path, to_path)
+
+
+def iplot_install(module_name):
+ """Install the resources for the module to the Ipython
+ profile directory
+
+ Args:
+ module_name (str): Name of the module
+
+ Returns:
+ A list than can be consumed by requirejs or
+ any relative resource dependency resolver
+ """
+
+ resources = IPLOT_RESOURCES[module_name]
+ for resource in resources:
+ resource_name = os.path.basename(resource)
+ resource_dest_dir = os.path.join(
+ get_scripts_path(),
+ module_name)
+
+ # Ensure if the directory exists
+ if not os.path.isdir(resource_dest_dir):
+ os.mkdir(resource_dest_dir)
+ resource_dest_path = os.path.join(resource_dest_dir, resource_name)
+ install_resource(resource, resource_dest_path)
+
+
+def get_ipython():
+ """Return an IPython instance. Returns None
+ if IPython is not installed"""
+
+ try:
+ import IPython
+ return IPython.get_ipython()
+ except ImportError:
+ return None
+
+def check_ipython():
+ """A boolean function to check if IPython
+ is available"""
+
+ try:
+ import IPython
+ except ImportError:
+ return False
+
+ return True
+
+def get_profile_name():
+ """Get the name of the profile of the current IPython
+ notebook. This is only relevant to V <= 4.0.0"""
+
+ ipy = get_ipython()
+ if not ipy:
+ raise ImportError("Cannot Find IPython Profile")
+
+ return ipy.profile
+
+def get_ipython_dir(profile=None):
+ """Returns the base directory of the IPython server"""
+
+ if not check_ipython():
+ raise ImportError("Cannot Find IPython Environment")
+
+ import IPython
+ # IPython 4.0+ changes the position of files in the profile
+ # directory
+ if V(IPython.__version__) >= V('4.0.0'):
+ return os.path.join(
+ PROFILE_DIR_IPYTHON_V4,
+ IPYTHON_V4_BASE.strip("/"))
+ else:
+ if not profile:
+ profile = get_profile_name()
+ return os.path.join(
+ IPython.utils.path.locate_profile(
+ profile),
+ IPYTHON_V3_BASE.strip("/"))
+
+def add_web_base(path):
+ """Add the base of the IPython dependency URLs"""
+
+ import IPython
+ if V(IPython.__version__) >= V('4.0.0'):
+ return os.path.join(IPYTHON_V4_BASE, path)
+ else:
+ return os.path.join(IPYTHON_V3_BASE, path)
+
+def get_scripts_path(profile=None):
+ """Directory where plotter scripts are installed"""
+
+ dir_name = os.path.join(get_ipython_dir(profile), PLOTTER_SCRIPTS)
+ if not os.path.isdir(dir_name):
+ os.makedirs(dir_name)
+ return dir_name
+
+def get_data_path(profile=None):
+ """Directory where Plotter Data is stored"""
+
+ dir_name = os.path.join(get_ipython_dir(profile), PLOTTER_DATA)
+ if not os.path.isdir(dir_name):
+ os.makedirs(dir_name)
+ return dir_name
diff --git a/trappy/plotter/Utils.py b/trappy/plotter/Utils.py
index 01a8bcc..0c1257e 100644
--- a/trappy/plotter/Utils.py
+++ b/trappy/plotter/Utils.py
@@ -16,10 +16,6 @@
"""Utils module has generic utils that will be used across
objects
"""
-import urllib
-import os
-import shutil
-from trappy.plotter import AttrConf
import collections
@@ -52,70 +48,6 @@ def decolonize(val):
return val.strip(":")
-def install_http_resource(url, to_path):
- """Install a HTTP Resource (eg. javascript) to
- a destination on the disk
-
- Args:
- url (str): HTTP URL
- to_path (str): Destintation path on the disk
- """
- urllib.urlretrieve(url, filename=to_path)
-
-
-def install_local_resource(from_path, to_path):
- """Move a local resource to the desired
- a destination.
-
- Args:
- from_path (str): Path relative to this file
- to_path (str): Destintation path on the disk
- """
- base_dir = os.path.dirname(__file__)
- from_path = os.path.join(base_dir, from_path)
- shutil.copy(from_path, to_path)
-
-
-def install_resource(from_path, to_path):
- """Install a resource to a location on the disk
-
- Args:
- from_path (str): URL or relative path
- to_path (str): Destintation path on the disk
- """
-
- if from_path.startswith("http"):
- if not os.path.isfile(to_path):
- install_http_resource(from_path, to_path)
- else:
- install_local_resource(from_path, to_path)
-
-
-def iplot_install(module_name):
- """Install the resources for the module to the Ipython
- profile directory
-
- Args:
- module_name (str): Name of the module
-
- Returns:
- A list than can be consumed by requirejs or
- any relative resource dependency resolver
- """
-
- resources = AttrConf.IPLOT_RESOURCES[module_name]
- for resource in resources:
- resource_name = os.path.basename(resource)
- resource_dest_dir = os.path.join(
- AttrConf.PLOTTER_SCRIPTS_PATH,
- module_name)
-
- # Ensure if the directory exists
- if not os.path.isdir(resource_dest_dir):
- os.mkdir(resource_dest_dir)
- resource_dest_path = os.path.join(resource_dest_dir, resource_name)
- install_resource(resource, resource_dest_path)
-
def get_trace_event_data(run):
"""
Args:
diff --git a/trappy/plotter/__init__.py b/trappy/plotter/__init__.py
index 84232f2..1560697 100644
--- a/trappy/plotter/__init__.py
+++ b/trappy/plotter/__init__.py
@@ -25,7 +25,7 @@ except ImportError:
pass
import Utils
import trappy
-
+import IPythonConf
def register_forwarding_arg(arg_name):
"""Allows the user to register args to
@@ -46,7 +46,7 @@ def unregister_forwarding_arg(arg_name):
def plot_trace(trace_dir):
"""Creates a kernelshark like plot of the trace file"""
- if not AttrConf.PLOTTER_IPYTHON:
+ if not IPythonConf.check_ipython():
raise RuntimeError("plot_trace needs ipython environment")
run = trappy.Run(trace_dir)
diff --git a/trappy/plotter/js/EventPlot.js b/trappy/plotter/js/EventPlot.js
index ae41b6c..6e58c1a 100644
--- a/trappy/plotter/js/EventPlot.js
+++ b/trappy/plotter/js/EventPlot.js
@@ -39,13 +39,13 @@ var EventPlot = (function () {
return left;
}
- var generate = function (div_name) {
+ var generate = function (div_name, base) {
var margin, brush, x, ext, yMain, chart, main,
mainAxis,
itemRects, items, colourAxis, tip, lanes;
- var json_file = "/static/plotter_data/" + div_name +
+ var json_file = base + "plotter_data/" + div_name +
".json"
$.getJSON(json_file, function (d) {
diff --git a/trappy/plotter/js/ILinePlot.js b/trappy/plotter/js/ILinePlot.js
index 39da1d2..e52c704 100644
--- a/trappy/plotter/js/ILinePlot.js
+++ b/trappy/plotter/js/ILinePlot.js
@@ -149,8 +149,8 @@ var ILinePlot = ( function() {
}
};
- var generate = function(div_name) {
- var json_file = "/static/plotter_data/" + div_name + ".json";
+ var generate = function(div_name, base) {
+ var json_file = base + "plotter_data/" + div_name + ".json";
$.getJSON( json_file, function( data ) {
create_graph(data);
purge();