diff --git a/plotly/Test_plotlysetup.m b/plotly/Test_plotlysetup.m new file mode 100644 index 00000000..434efa49 --- /dev/null +++ b/plotly/Test_plotlysetup.m @@ -0,0 +1,80 @@ +classdef Test_plotlysetup < PlotlyTestCase + methods + function testPlotlysetupOffline(tc) + % finds plotly/ from an unrelated folder and writes no startup + % file; the URL cannot resolve, so the real bundle is never replaced + sandbox = Test_plotlysetup.enterSetupSandbox(); %#ok + if is_octave() + pathdefs = {}; + else + pathdefs = which('pathdef.m', '-all'); + end + before = Test_plotlysetup.fileContents(pathdefs); + + output = evalc(['try, plotlysetup_offline(', ... + '''https://plotly.invalid/plotly.min.js''); catch, end']); + + tc.verifyTrue(isempty(strfind(output, 'plotly:notFound')), output); + tc.verifyTrue(~isempty(strfind(output, 'Now downloading')), output); + tc.verifyTrue(isequal(Test_plotlysetup.fileContents(pathdefs), before), ... + 'a pathdef.m on the search path changed'); + tc.verifyNumElements(dir(fullfile(getenv('HOME'), '.octaverc')), 0); + end + + function testPrintPlotlyPathHint(tc) + if is_octave() + startupFile = '~/.octaverc'; + else + startupFile = 'startup.m (in the folder that userpath returns)'; + end + + output = evalc('printplotlypathhint(''/opt/it''''s here/plotly'')'); + + tc.verifyTrue(~isempty(strfind(output, ... + ['add this line to your ' startupFile ':'])), output); + tc.verifyTrue(~isempty(strfind(output, ... + 'addpath(genpath(''/opt/it''''s here/plotly''));')), output); + end + end + + methods (Static) + function sandbox = enterSetupSandbox() + %-run the setup scripts, which live in the repository root, + %-from an empty folder that is also HOME; the returned object + %-restores folder, HOME and the repository root's absence from + %-the path when it goes out of scope (restoring the whole path + %-with path() breaks the running test object in Octave)-% + oldFolder = pwd(); + oldHome = getenv('HOME'); + repoRoot = fileparts(fileparts(which('fig2plotly'))); + rootWasOnPath = ~isempty(strfind([pathsep path() pathsep], ... + [pathsep repoRoot pathsep])); + home = tempname(); + mkdir(home); + cd(home); + setenv('HOME', home); + addpath(repoRoot); + sandbox = onCleanup(@() Test_plotlysetup.leaveSetupSandbox( ... + oldFolder, oldHome, home, repoRoot, rootWasOnPath)); + end + + function leaveSetupSandbox(oldFolder, oldHome, home, repoRoot, rootWasOnPath) + if ~rootWasOnPath + rmpath(repoRoot); + end + cd(oldFolder); + setenv('HOME', oldHome); + if is_octave() + confirm_recursive_rmdir(false, 'local'); + end + try rmdir(home, 's'); catch; end + end + + function contents = fileContents(files) + contents = cell(size(files)); + for i = 1:numel(files) + contents{i} = fileread(files{i}); + end + end + end +end diff --git a/plotly/plotly_setup_aux/addplotlystartup.m b/plotly/plotly_setup_aux/addplotlystartup.m deleted file mode 100644 index dbacdebd..00000000 --- a/plotly/plotly_setup_aux/addplotlystartup.m +++ /dev/null @@ -1,59 +0,0 @@ -function [warnings] = addplotlystartup(startupPaths) - %[1]looks at startup.m files specified by the entries of startupPaths - %[2]appends the addplotly function to startup.m files (if not already - % present) - %[3]checks for other plotly addpath calls within any startup.m and - % outputs warning - - %output warnings - warnings = cell(size(startupPaths)); - - for locs = 1:size(startupPaths,1); - %addpath string for Plotly API - addString = ['addpath(genpath(fullfile(matlabroot,''toolbox'',' ... - '''plotly'')),''-end'');']; - %open current startup.m to read - currentStartupID = fopen(startupPaths{locs},'r'); - if currentStartupID == -1, error('plotly:startupRead', ... - ['\n\nShoot! It looks like something went wrong ' ... - 'reading the file: \n' startupPaths{locs} '\n\n' ... - 'Please contact your system admin or post a topic on ' ... - 'https://community.plotly.com/c/api/matlab/ for more ' ... - '\ninformation. In the mean time you can add the ' ... - 'Plotly API \nto your search path manually whenever ' ... - 'you need it! \n\n']); - end - %check for any instances of the addplotlyapi function - startupScan = textscan(currentStartupID, '%s', 'delimiter', ... - '\n', 'whitespace', ''); - startupLines = startupScan{1}; - Index = find(strcmp(startupLines,addString)); - otherPlotlyOccurrence = findstr(fileread(startupPaths{locs}), ... - 'plotly'); - %if addString is not in startup.m add it - if (~any(Index)) - %reopen current startup.m with new permission - currentStartupID = fopen(startupPaths{locs},'a+'); - if currentStartupID == -1 - error('plotly:startupWrite', ['\n\nShoot! It looks ' ... - 'like something went wrong writing to the ' ... - 'file: \n\n' startupPaths{locs} '\n\nPlease ' ... - 'contact your system admin or post a topic on ' ... - 'https://community.plotly.com/c/api/matlab/ ' ... - 'for more \ninformation. In the mean time you ' ... - 'can add the Plotly API \nto your search path ' ... - 'manually whenever you need it! \n']); - end - fprintf(currentStartupID,['\n' addString]); - end - if (length(Index) ~= length(otherPlotlyOccurrence)) - warnings{locs} = ['\n[WARNING]: \n\nWe found an addpath ' ... - 'specification for another version of Plotly at: ' ... - '\n\n' startupPaths{locs} '\n\nyou may be forcing ' ... - 'MATLAB to look for an older version of Plotly!\n\n']; - else - warnings{locs} = ''; - end - fclose(currentStartupID); - end -end diff --git a/plotly/plotly_setup_aux/plotlycleanup.m b/plotly/plotly_setup_aux/plotlycleanup.m deleted file mode 100644 index a8b34bf1..00000000 --- a/plotly/plotly_setup_aux/plotlycleanup.m +++ /dev/null @@ -1,96 +0,0 @@ -function removed = plotlycleanup - % cleans up any old Plotly API MATLAB library files and folders - - % initialize output - removed = {}; - - %----REMOVE WRAPPER FILES----% - REMOVEFILES = {'plotly.m'}; - - %----REMOVE WRAPPER FOLDERS----% - REMOVEFOLDERS = {'fig2plotly_aux'}; - - %----check for local Plotly instances----% - try - plotlyScriptDirs = which('plotly.m','-all'); - if isempty(plotlyScriptDirs); - error('plotly:missingScript', ['\n\nWe were unable to ' ... - 'locate plotly.m. Please Add this\nscript to your ' ... - 'MATLAB search path and try again.\n\n']); - end - catch exception %locating plotly error catch... - fprintf(['\n\n' exception.identifier exception.message '\n\n']); - return - end - - % plotly toolbox directory - plotlyToolboxDir = fullfile(matlabroot,'toolbox','plotly'); - - % find the location of all plotly/ directories - dircount = 1; - for d = 1:length(plotlyScriptDirs) - %parse filepath string at the Plotly directory - plotlyLoc = strfind(fileparts(plotlyScriptDirs{d}), ... - fullfile('MATLAB-api-master','plotly')); - plotlyToolboxLoc = strfind(fileparts(plotlyScriptDirs{d}), ... - plotlyToolboxDir); - if ~isempty(plotlyLoc) - plotlyDirs{dircount} = fullfile( ... - plotlyScriptDirs{d}(1:plotlyLoc-1), ... - 'MATLAB-api-master','plotly'); - dircount = dircount + 1; - elseif ~isempty(plotlyToolboxLoc) - plotlyDirs{dircount} = plotlyToolboxDir; - dircount = dircount + 1; - end - end - - for d = 1:length(plotlyDirs) - % add plotlydirs to searchpath - addpath(genpath(plotlyDirs{d})); - - % delete files from plotly directory - removefiles = fullfile(plotlyDirs{d}, REMOVEFILES); - - for f = 1:length(removefiles) - % remove removefiles filepath from searchpath - rmpath(fileparts(removefiles{f})); - - if exist(removefiles{f},'file') - delete(removefiles{f}); - % update removed list - removed = [removed removefiles{f}]; - end - - % add removefiles filepath back to searchpath - addpath(fileparts(removefiles{f})); - end - - % remove folders from plotly directory - removefolders = fullfile(plotlyDirs{d},REMOVEFOLDERS); - - for f = 1:length(removefolders) - if ~exist(removefolders{f}, 'dir') - continue - end - %remove folder from path - rmpath(genpath(removefolders{f})); - - %delete folder/subfolders - try - status = rmdir(removefolders{f},'s'); - if (status == 0) - error('plotly:deletePlotlyAPI', ['\n\nShoot! It ' ... - 'looks like something went wrong removing ' ... - 'the Plotly API from the MATLAB toolbox ' ... - 'directory \nPlease contact your system ' ... - 'admin or post a topic on ' ... - 'https://community.plotly.com/c/api/matlab/ ' ... - 'for more information. \n\n']); - end - % update removed list - removed = [removed removefolders{f}]; - end - end - end -end diff --git a/plotly/plotly_setup_aux/printplotlypathhint.m b/plotly/plotly_setup_aux/printplotlypathhint.m new file mode 100644 index 00000000..33ad6eec --- /dev/null +++ b/plotly/plotly_setup_aux/printplotlypathhint.m @@ -0,0 +1,13 @@ +function printplotlypathhint(plotlyFolderPath) + % prints the line that keeps plotlyFolderPath on the search path in + % future sessions; setup leaves editing startup files to the user + if is_octave + startupFile = '~/.octaverc'; + else + startupFile = 'startup.m (in the folder that userpath returns)'; + end + fprintf(['\nPlotly is on the search path for this session. To keep it ', ... + 'there in future sessions,\nadd this line to your %s:\n\n', ... + ' addpath(genpath(''%s''));\n'], ... + startupFile, strrep(plotlyFolderPath, '''', '''''')); +end diff --git a/plotly/testing/runplotlytests.m b/plotly/testing/runplotlytests.m index 0311c3d2..d66369fd 100644 --- a/plotly/testing/runplotlytests.m +++ b/plotly/testing/runplotlytests.m @@ -13,7 +13,8 @@ function runplotlytests(varargin) %-every test method of the class. Without arguments the default %-suites run-% if nargin == 0 - args = {'Test_m2json', 'Test_plotlyfig', 'Test_plotlyfig_perf'}; + args = {'Test_m2json', 'Test_plotlyfig', 'Test_plotlyfig_perf', ... + 'Test_plotlysetup'}; else args = varargin; end diff --git a/plotlysetup_offline.m b/plotlysetup_offline.m index 124bcae5..2ef9d18f 100644 --- a/plotlysetup_offline.m +++ b/plotlysetup_offline.m @@ -2,7 +2,7 @@ function plotlysetup_offline(plotly_bundle_url, varargin) % CALL: plotlysetup_offline(plotly_bundle_url); % WHERE: plotly_bundle_url is the plotly bundle url, e.g. https://cdn.plot.ly/plotly-2.35.3.min.js % If no argument is provided, the default https://cdn.plot.ly/plotly-2.35.3.min.js is used. - % [1] adds plotly api to search path and saves it via savepath + % [1] adds plotly api to search path and prints how to keep it there %DEFAULT OUTPUT exception.message = ''; @@ -24,14 +24,13 @@ function plotlysetup_offline(plotly_bundle_url, varargin) try %check to see if plotly is in the searchpath - plotlysetupPath = which('plotlysetup'); - plotlyFolderPath = fullfile(fileparts(plotlysetupPath),'plotly'); + plotlyFolderPath = fullfile(fileparts(mfilename('fullpath')),'plotly'); %if it was not found if (strcmp(genpath(plotlyFolderPath),'')) error('plotly:notFound',... ['\n\nShoot! It looks like MATLAB is having trouble finding the current version ' ... '\nof Plotly. Please make sure that the Plotly API folder is in the same ' ... - '\ndirectory as plotlysetup.m. Questions? Ask https://community.plotly.com/c/api/matlab/\n\n']); + '\ndirectory as plotlysetup_offline.m. Questions? Ask https://community.plotly.com/c/api/matlab/\n\n']); end %add Plotly API MATLAB Library to search path addpath(genpath(plotlyFolderPath)); @@ -40,11 +39,7 @@ function plotlysetup_offline(plotly_bundle_url, varargin) return end - % Save search path so Plotly remains available in future sessions - try - savepath; - catch - end + printplotlypathhint(plotlyFolderPath); %get offline bundle fprintf('\nNow downloading the plotly offline bundle ...'); diff --git a/plotlysetup_online.m b/plotlysetup_online.m index fa02012c..902eef8c 100644 --- a/plotlysetup_online.m +++ b/plotlysetup_online.m @@ -5,10 +5,9 @@ function plotlysetup_online(username, api_key, varargin) % VALID PROPERTIES [OPTIONAL]: 'stream_ids' -> your stream ids [cell array] (found online) % 'plotly_domain' -> your desired REST API endpoint [string] % 'plotly_streaming_domain'-> your desired Stream API endpoint [string] - % [1] adds plotly api to matlabroot/toolboxes. If successful do [2] - % [2] adds plotly api to searchpath via startup.m of matlabroot and/or userpath - % [3] calls saveplotlycredentials (using username, api_key and stream_ids [optional]) - % [4] calls saveplotlyconfig with ('plotly_domain'[optional], 'plotly_streaming_domain' [optional]) + % [1] adds plotly api to search path and prints how to keep it there + % [2] calls saveplotlycredentials (using username, api_key and stream_ids [optional]) + % [3] calls saveplotlyconfig with ('plotly_domain'[optional], 'plotly_streaming_domain' [optional]) %DEFAULT OUTPUT exception.message = ''; @@ -28,14 +27,13 @@ function plotlysetup_online(username, api_key, varargin) try %check to see if plotly is in the searchpath - plotlysetupPath = which('plotlysetup'); - plotlyFolderPath = fullfile(fileparts(plotlysetupPath),'plotly'); + plotlyFolderPath = fullfile(fileparts(mfilename('fullpath')),'plotly'); %if it was not found if (strcmp(genpath(plotlyFolderPath),'')) error('plotly:notFound',... ['\n\nShoot! It looks like MATLAB is having trouble finding the current version ' ... '\nof Plotly. Please make sure that the Plotly API folder is in the same ' ... - '\ndirectory as plotlysetup.m. Questions? Ask on https://community.plotly.com/c/api/matlab/\n\n']); + '\ndirectory as plotlysetup_online.m. Questions? Ask on https://community.plotly.com/c/api/matlab/\n\n']); end %add Plotly API MATLAB Library to search path addpath(genpath(plotlyFolderPath)); @@ -44,93 +42,7 @@ function plotlysetup_online(username, api_key, varargin) return end - if ~is_octave - try - %embed the api to the matlabroot/toolbox dir. - fprintf('\nAdding Plotly to MATLAB toolbox directory ... '); - - %plotly folder in the matlab/toolbox dir. - plotlyToolboxPath = fullfile(matlabroot,'toolbox','plotly'); - - if (exist(plotlyToolboxPath,'dir')) %check for overwrite... - fprintf(['\n\n[UPDATE]: \n\nHey! We see that a copy of Plotly has previously been added to\n' ... - 'your Matlab toolboxes. Would you like us to overwrite it with:\n' plotlyFolderPath ' ? \n'... - 'Careful! You may lose data saved to this Plotly directory.\n\n']); - - overwrite = input('Overwrite (y/n) ? : ','s'); - - if (strcmpi(overwrite,'y')); - fprintf('\n[OVERWRITE]:\n\nOverwriting Plotly! ... Done \n'); - else - fprintf('\n[NO OVERWRITE]:\n\nDid not overwrite Plotly! ... Done \n'); - end - else %toolbox Plotly not yet created - %worked (without interruption)...just a formatting thing! - fprintf('Done\n'); - - %make the plotlyToolboxPath dir. - status = mkdir(plotlyToolboxPath); - - %set status to overwrite - overwrite = 'y'; - - %check that the folder was created - if (status == 0) - error('plotly:savePlotly', permissionMessage('save the Plotly folder')); - end - end - - if strcmpi(overwrite,'y') - %move a copy of the Plotly api to matlab root directory - [status, msg, messid] = copyfile(plotlyFolderPath,plotlyToolboxPath); - %check that the plotly api was copied to the matlab root toolbox directory - if (status == 0) - if (~strcmp(messid, 'MATLAB:COPYFILE:SourceAndDestinationSame')) - error('plotly:copyPlotly',permissionMessage('copy the Plotly folder')); - end - end - end - - %add it to the searchpath (startup.m will handle this next time!) - addpath(genpath(plotlyToolboxPath),'-end'); - - %save plotly api searchpath to startup.m files (only do this if we actually were able to store the api in mtlroot/toolbox!) - fprintf('Saving Plotly to MATLAB search path via startup.m ... '); - - %check for a startup.m file in matlab rootpath (we want to add one here) - startupFile = []; - startupFileRootPath = fullfile(matlabroot,'toolbox','local','startup.m'); - if (~exist(startupFileRootPath,'file')) - startFileID = fopen(startupFileRootPath, 'w'); - %startup.m does not exist and startupFilePath is non-writable - if (startFileID == -1) - error('plotly:rootStartupCreation',permissionMessage('write the startup.m script')); - end - startupFile = {startupFileRootPath}; %needed because MATLAB only looks for startup.m when first opened. - end - - %check for all startup.m file in searchpath - startupFile = [startupFile; cell(which('startup.m','-all'))]; - %write the addpath - plotly api to the startup.m files - [warnings] = addplotlystartup(startupFile); - - %worked! - fprintf(' Done\n'); - - %print any addplotlydstatup warnings; - w = cellfun(@isempty,warnings); - if find(~w) - %output warnings - exception.warnings = warnings; - fprintf(warnings{find(~w)}); - end - - catch exception %copying to toolbox/writing to startup.m permission problem catch... - fprintf(['\n\n' exception.identifier exception.message '\n\n']); - end - else %if octave - fprintf('\n\nOctave users: Automatic Plotly API embedding coming soon!\n\n'); - end %end check for matlab... + printplotlypathhint(plotlyFolderPath); try %save user credentials fprintf('Saving username/api_key credentials ... '); @@ -149,7 +61,7 @@ function plotlysetup_online(username, api_key, varargin) %check for ..,property,value,.. structure if mod(numel(varargin),2)~= 0 error('plotly:wrongInputVarargin',.... - ['\n\nWhoops! Wrong number of varargin inputs. Please run >> help plotlysetup \n',... + ['\n\nWhoops! Wrong number of varargin inputs. Please run >> help plotlysetup_online \n',... 'for more information regarding the setup of your Plotly API MATLAB Library. \n',... 'Your stream_ids, plotly_domain, and plotly_streaming domain were not set. \n',... 'Questions? Please post on https://community.plotly.com/c/api/matlab/22.']); @@ -160,7 +72,7 @@ function plotlysetup_online(username, api_key, varargin) if isempty(intersect(varargin{n},{'stream_ids','plotly_domain','plotly_streaming_domain'})) error('plotly:wrongInputPropertyName',.... ['\n\nWhoops! The properperty name: ' varargin{n} ' is invalid. \n',... - 'Please run >> help plotlysetup for more information regarding\n',... + 'Please run >> help plotlysetup_online for more information regarding\n',... 'the setup your Plotly API MATLAB Library.']); end if strcmp(varargin{n},'stream_ids') @@ -204,17 +116,3 @@ function plotlysetup_online(username, api_key, varargin) %greet the people! fprintf('\nWelcome to Plotly! If you are new to Plotly please enter: >> plotlyhelp to get started!\n\n') end - -%helper message function -function message = permissionMessage(spec) - message = ['\n\nShoot! We tried to ' spec ' to the MATLAB toolbox \n',... - 'directory, but were denied write permission. You''ll have to add\n',... - 'the Plotly folder to your MATLAB path manually by running: \n\n',... - '>> plotly_path = fullfile(pwd, ''plotly'')\n',... - '>> addpath(genpath(plotly_path))\n\n',... - 'You can save your credentials by running:\n\n', ... - '>>saveplotlycredentials(''your_username'', ''your_api_key'')\n\n',... - 'You can save your domain configuration by running:\n\n',... - '>>saveplotlyconfig(''your_base_domain'')\n\n',... - 'Questions? Ask https://community.plotly.com/c/api/matlab/\n\n']; -end