Skip to content

Commit 7b080e0

Browse files
committed
- first
2 parents 499d9a9 + e82d136 commit 7b080e0

File tree

8 files changed

+91
-6
lines changed

8 files changed

+91
-6
lines changed

plotly/loadplotlycredentials.m

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function [username, api_key] = loadplotlycredentials()
2+
3+
userhome = getuserdir();
4+
plotly_credentials_file = [userhome '/.plotly/.credentials'];
5+
6+
% check if credentials exist
7+
if ~exist(plotly_credentials_file, 'file')
8+
error('Plotly:CredentialsNotFound',...
9+
['It looks like you haven''t set up your plotly '...
10+
'account credentials yet.\nTo get started, save your '...
11+
'plotly username and API key by calling:\n'...
12+
'>>> saveplotlycredentials(username, api_key)\n\n'...
13+
'For more help, see https://plot.ly/MATLAB or contact '...
14+
'chris@plot.ly.']);
15+
end
16+
17+
fileID = fopen(plotly_credentials_file, 'r');
18+
if(fileID == -1)
19+
error('plotly:loadcredentials', ...
20+
['There was an error reading your credentials file at '...
21+
plotly_credentials_file '. Contact chris@plot.ly for support.']);
22+
end
23+
24+
creds_string_array = fread(fileID, '*char');
25+
26+
creds_string = sprintf(creds_string_array);
27+
28+
creds_struct = json2struct(creds_string);
29+
30+
username = creds_struct.username;
31+
api_key = creds_struct.api_key;
32+
33+
end

plotly/plotly_aux/getuserdir.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function userDir = getuserdir
2+
%GETUSERDIR return the user home directory.
3+
% USERDIR = GETUSERDIR returns the user home directory using the registry
4+
% on windows systems and using Java on non windows systems as a string
5+
%
6+
% Example:
7+
% getuserdir() returns on windows
8+
% C:\Documents and Settings\MyName\Eigene Dateien
9+
10+
if ispc
11+
userDir = winqueryreg('HKEY_CURRENT_USER',...
12+
['Software\Microsoft\Windows\CurrentVersion\' ...
13+
'Explorer\Shell Folders'],'Personal');
14+
else
15+
userDir = char(java.lang.System.getProperty('user.home'));
16+
end

plotly/plotly_aux/m2json.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
valstr = strrep(valstr, 'Inf', 'null');
2626
valstr = strrep(valstr, 'NaN', 'null');
2727
elseif ischar(val)
28+
val = check_escape(val);
2829
valstr = ['"' val '"'];
2930
elseif islogical(val)
3031
if val

plotly/plotly_aux/t.m

Lines changed: 0 additions & 4 deletions
This file was deleted.

plotly/saveplotlycredentials.m

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function saveplotlycredentials(username, api_key)
2+
% Save plotly authentication credentials.
3+
% Plotly credentials are saved as JSON strings
4+
% in ~/.plotly/.credentials
5+
6+
% Create the .plotly folder
7+
userhome = getuserdir();
8+
plotly_credentials_folder = [userhome '/.plotly'];
9+
[status, mess, messid] = mkdir(plotly_credentials_folder);
10+
if (status == 0)
11+
if(~strcmp(messid, 'MATLAB:MKDIR:DirectoryExists'))
12+
error('plotly:savecredentials',...
13+
['Error saving credentials folder at ' ...
14+
plotly_credentials_folder ': '...
15+
mess ', ' messid '. Get in touch at ' ...
16+
'chris@plot.ly for support.']);
17+
end
18+
end
19+
plotly_credentials_file = [plotly_credentials_folder '/.credentials'];
20+
21+
fileID = fopen(plotly_credentials_file, 'w');
22+
if(fileID == -1)
23+
error('plotly:savecredentials',...
24+
['Error opening credentials file at '...
25+
plotly_credentials_file '. Get in touch at '...
26+
'chris@plot.ly for support.']);
27+
end
28+
29+
credentials = m2json(struct('username', username, 'api_key', api_key));
30+
31+
fprintf(fileID, credentials);
32+
33+
fclose(fileID);
34+
35+
end

plotly/signin.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
function [un, key] = signin(varargin)
22
% SIGNIN(username, api_key) Sign In to a plotly session
3-
%
3+
%
44
% See also plotly, plotlylayout, plotlystyle, signup
5-
%
5+
%
66
% For full documentation and examples, see https://plot.ly/api
77
persistent USERNAME KEY
88
if nargin==2 && ischar(varargin{1}) && ischar(varargin{2})
99
USERNAME = varargin{1};
1010
KEY = varargin{2};
1111
plotlysession('MATLAB API');
1212
mlock;
13+
elseif isempty(USERNAME) || isempty(KEY)
14+
[un, key] = loadplotlycredentials();
15+
USERNAME = un;
16+
KEY = key;
1317
else
1418
un = USERNAME;
1519
key = KEY;
Loading
Loading

0 commit comments

Comments
 (0)