Permalink
Browse files

ENH+RF: cleanup of wtf

  • Loading branch information...
nno committed Sep 16, 2017
1 parent e32ca19 commit 4e1da805e8d27953719ecc118aa6428fa5dc045b
Showing with 76 additions and 38 deletions.
  1. +72 −38 mvpa/cosmo_wtf.m
  2. +4 −0 tests/test_wtf.m
View
@@ -36,47 +36,83 @@
% # For CoSMoMVPA's copyright information and license terms, #
% # see the COPYING file distributed with CoSMoMVPA. #
has_param=nargin>=1 && ~isempty(param);
if has_param
if strcmp(param,'is_matlab')
s=environment_is_matlab();
return
elseif strcmp(param,'is_octave')
s=environment_is_octave();
return
has_param=nargin>=1 && ~isempty(param);
if has_param
s=get_single_param_value(param);
else
s=get_all_param_values();
end
function s=get_all_param_values()
param2func=get_param2func();
keys=fieldnames(param2func);
value_printer=@(key)as_string(helper_get_param_from_func_value(key));
printer=@(key) sprintf('%s: %s',...
key,value_printer(key));
s_cell=cellfun(printer,keys,'UniformOutput',false);
s=cosmo_strjoin(s_cell,'\n');
function s=as_string(v)
if ischar(v)
s=v;
elseif isnumeric(v)
s=sprintf('[ %s]',sprintf('%d ',v));
elseif iscellstr(v)
s=sprintf('\n %s',v{:});
else
assert(false,'unsupported type');
end
function s=get_single_param_value(param)
if ~ischar(param)
error('argument must be a string');
end
end
params2func=struct();
params2func.computer=@computer_;
params2func.environment=@environment;
params2func.version=version_;
params2func.java=java_;
params2func.cosmo_externals=@cosmo_externals;
params2func.toolboxes=@toolboxes;
params2func.warnings=@warning_helper;
params2func.cosmo_config=@cosmo_config_helper;
params2func.version_number=@version_number_;
params2func.cosmo_files=@cosmo_files;
switch param
case 'is_matlab'
s=environment_is_matlab();
case 'is_octave'
s=environment_is_octave();
otherwise
s=helper_get_param_from_func_value(param);
end
function s=helper_get_param_from_func_value(key)
param2func=get_param2func();
has_param=nargin>=1 && ~isempty(param);
if has_param;
if ~isfield(params2func,param)
if ~isfield(param2func,key)
error('Unsupported parameter ''%s''. Supported are: %s',...
param, cosmo_strjoin(fieldnames(params2func),', '));
key, cosmo_strjoin(fieldnames(param2func),', '));
end
f=params2func.(param);
f=param2func.(key);
s=f();
return
end
me=str2func(mfilename());
params=fieldnames(params2func);
printer=@(param) sprintf('%s: %s',param,me(param));
s_cell=cellfun(printer,params,'UniformOutput',false);
s=cosmo_strjoin(s_cell,'\n');
function param2func=get_param2func()
persistent cached_param2func;
if ~isstruct(cached_param2func)
cached_param2func=struct();
cached_param2func.computer=@computer_;
cached_param2func.environment=@environment;
cached_param2func.version=version_;
cached_param2func.java=java_;
cached_param2func.cosmo_externals=@cosmo_externals;
cached_param2func.toolboxes=@toolboxes;
cached_param2func.warnings=@warning_helper;
cached_param2func.cosmo_config=@cosmo_config_helper;
cached_param2func.version_number=@version_number_;
cached_param2func.cosmo_files=@cosmo_files;
end
param2func=cached_param2func;
function s=computer_()
@@ -127,7 +163,8 @@
s=dir2str(v,formatter);
function s=cosmo_externals()
s=cosmo_strjoin(cosmo_check_external('-list'),', ');
s=cosmo_check_external('-list');
function s=cosmo_files()
pth=fileparts(which(mfilename())); % cosmo root directory
@@ -151,15 +188,12 @@
s=caught_error.message;
end
function s=warning_helper()
function parts=warning_helper()
s=warning();
n=numel(s);
s_parts=arrayfun(@(i)sprintf('%s: %s', s(i).identifier, s(i).state),...
parts=arrayfun(@(i)sprintf('%s: %s', s(i).identifier, s(i).state),...
1:n,'UniformOutput',false);
s=cosmo_strjoin(s_parts, ', ');
function s=dir2str(d, formatter)
% d is the result from 'dir' or 'cosmo_dir'
View
@@ -73,6 +73,10 @@ function test_wtf_version_number()
assertEqual(cosmo_wtf('is_matlab'),~is_octave);
assertEqual(cosmo_wtf('is_octave'),is_octave);
function test_wtf_cosmo_externals()
s=cosmo_wtf('cosmo_externals');
assert(iscellstr(s));
function assert_contains(haystack, needle)
assert_contains_helper(haystack, needle, true);

0 comments on commit 4e1da80

Please sign in to comment.