1# Copyright 2015-2017 ARM Limited 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# 15 16"""IPythonConf provides abstraction for the varying configurations in 17different versions of ipython/jupyter packages. 18""" 19import urllib 20import os 21import shutil 22from distutils.version import StrictVersion as V 23 24D3_PLOTTER_URL = "https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" 25D3_TIP_URL = "http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js" 26DYGRAPH_COMBINED_URL = "http://cdnjs.cloudflare.com/ajax/libs/dygraph/1.1.1/dygraph-combined.js" 27DYGRAPH_SYNC_URL = "http://dygraphs.com/extras/synchronizer.js" 28UNDERSCORE_URL = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js" 29 30IPLOT_RESOURCES = { 31 "ILinePlot": [ 32 DYGRAPH_COMBINED_URL, 33 "js/ILinePlot.js", 34 DYGRAPH_SYNC_URL, 35 UNDERSCORE_URL], 36 "EventPlot": [ 37 D3_PLOTTER_URL, 38 D3_TIP_URL, 39 "js/EventPlot.js", 40 "css/EventPlot_help.jpg"]} 41 42"""The location of the IPython webserver in IPython version 4.0+""" 43IPYTHON_V4_BASE = "/nbextensions" 44"""The webserver base directory for IPython version 4.0+""" 45IPYTHON_V3_BASE = "/static" 46"""The webserver base directory for IPython version < 4.0""" 47PLOTTER_SCRIPTS = "plotter_scripts" 48"""The installation directory of plotter JS files in the 49IPython webserver""" 50 51def install_http_resource(url, to_path): 52 """Install a HTTP Resource (eg. javascript) to 53 a destination on the disk 54 55 :param url: HTTP URL 56 :type url: str 57 58 :param to_path: Destination path on the disk 59 :type to_path: str 60 """ 61 try: 62 urllib.urlretrieve(url, filename=to_path) 63 except IOError: 64 raise ImportError("Could not receive Web Resource {}" 65 .format(to_path)) 66 67 68def install_local_resource(from_path, to_path): 69 """Move a local resource to the desired 70 a destination. 71 72 :param from_path: Path relative to this file 73 :type from_path: str 74 75 :param to_path: Destination path on the disk 76 :type to_path: str 77 """ 78 base_dir = os.path.dirname(__file__) 79 from_path = os.path.join(base_dir, from_path) 80 shutil.copy(from_path, to_path) 81 82 83def install_resource(from_path, to_path): 84 """Install a resource to a location on the disk 85 86 :param from_path: URL or relative path 87 :type from_path: str 88 89 :param to_path: Destination path on the disk 90 :type to_path: str 91 """ 92 93 if from_path.startswith("http"): 94 if not os.path.isfile(to_path): 95 install_http_resource(from_path, to_path) 96 else: 97 install_local_resource(from_path, to_path) 98 99 100def iplot_install(module_name): 101 """Install the resources for the module to the Ipython 102 profile directory 103 104 :param module_name: Name of the module 105 :type module_name: str 106 107 :return: A list than can be consumed by requirejs or 108 any relative resource dependency resolver 109 """ 110 111 resources = IPLOT_RESOURCES[module_name] 112 for resource in resources: 113 resource_name = os.path.basename(resource) 114 resource_dest_dir = os.path.join( 115 get_scripts_path(), 116 module_name) 117 118 # Ensure if the directory exists 119 if not os.path.isdir(resource_dest_dir): 120 os.mkdir(resource_dest_dir) 121 resource_dest_path = os.path.join(resource_dest_dir, resource_name) 122 install_resource(resource, resource_dest_path) 123 124 125def get_ipython(): 126 """Return an IPython instance. Returns None 127 if IPython is not installed""" 128 129 try: 130 import IPython 131 return IPython.get_ipython() 132 except ImportError: 133 return None 134 135def check_ipython(): 136 """A boolean function to check if IPython 137 is available""" 138 139 try: 140 import IPython 141 except ImportError: 142 return False 143 144 return True 145 146def get_profile_name(): 147 """Get the name of the profile of the current IPython 148 notebook. This is only relevant to V <= 4.0.0""" 149 150 ipy = get_ipython() 151 if not ipy: 152 raise ImportError("Cannot Find IPython Profile") 153 154 return ipy.profile 155 156def get_ipython_dir(profile=None): 157 """Returns the base directory of the IPython server 158 159 :param profile: The name of the IPython profile 160 :type profile: str 161 """ 162 163 if not check_ipython(): 164 raise ImportError("Cannot Find IPython Environment") 165 166 import IPython 167 # IPython 4.0+ changes the position of files in the profile 168 # directory 169 if V(IPython.__version__) >= V('4.0.0'): 170 from jupyter_core.paths import jupyter_data_dir 171 return os.path.join( 172 jupyter_data_dir(), 173 IPYTHON_V4_BASE.strip("/")) 174 else: 175 if not profile: 176 profile = get_profile_name() 177 return os.path.join( 178 IPython.utils.path.locate_profile( 179 profile), 180 IPYTHON_V3_BASE.strip("/")) 181 182def add_web_base(path): 183 """Add the base of the IPython dependency URLs 184 185 :param path: The path to be augmented with the 186 webserver base 187 :type path: str 188 """ 189 190 import IPython 191 if V(IPython.__version__) >= V('4.0.0'): 192 return os.path.join(IPYTHON_V4_BASE, path) 193 else: 194 return os.path.join(IPYTHON_V3_BASE, path) 195 196def get_scripts_path(profile=None): 197 """Directory where plotter scripts are installed 198 199 :param profile: The name of the IPython profile 200 :type profile: str 201 """ 202 203 dir_name = os.path.join(get_ipython_dir(profile), PLOTTER_SCRIPTS) 204 if not os.path.isdir(dir_name): 205 os.makedirs(dir_name) 206 return dir_name 207