-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add functions for inferring directory of generated types #681
Open
ehennestad
wants to merge
8
commits into
main
Choose a base branch
from
add/infer-directory-for-generated-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
946bca9
Add utility method for retrieving relative path from classname
ehennestad 49ea1c5
Add utility function for finding directoroy where neurodata type clas…
ehennestad 4e443d1
Update loadNamespace.m
ehennestad 05d651b
Update findRootDirectoryForGeneratedTypes.m
ehennestad c311e64
Update Namespace.m
ehennestad 38c4dc4
Work around for matlab bug with which ... -all
ehennestad 5bbde41
Add unit tests for schemes.utility.findRootDirectoryForGeneratedTypes
ehennestad 3addda1
Merge branch 'main' into add/infer-directory-for-generated-types
bendichter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function pathName = classname2path(typeName) | ||
% classname2path - Return relative path name for a class name with namespaces | ||
% | ||
% Example: | ||
% | ||
% pathName = matnwb.common.internal.classname2path('types.core.NWBFile') | ||
% | ||
% pathName = | ||
% '+types/+core/NWBFile.m' | ||
|
||
arguments | ||
typeName (1,1) string | ||
end | ||
|
||
typeName = split(typeName, "."); | ||
typeName(1:end-1) = "+" + typeName(1:end-1); | ||
typeName(end) = typeName(end) + ".m"; | ||
|
||
pathName = fullfile(typeName{:}); | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
function rootDirectoryForGeneration = findRootDirectoryForGeneratedTypes() | ||
% findRootDirectoryForGeneratedTypes - Find directory where generated types are located | ||
% | ||
% Check if any instances of types.core.NWBFile are present on MATLAB's | ||
% search path. If types.core.NWBFile is found, the function returns the root | ||
% directory where generated types are located, otherwise the function | ||
% throws an error | ||
|
||
generatedTypeName = 'types.core.NWBFile'; | ||
relPathForGeneratedType = matnwb.common.internal.classname2path(generatedTypeName); | ||
|
||
generatedTypeLocation = which(relPathForGeneratedType, '-all'); | ||
|
||
if isempty(generatedTypeLocation) | ||
ME = MException('NWB:Types:GeneratedTypesNotFound', ... | ||
['Could not find a location for generated classes of neurodata ', ... | ||
'types. Please check if MatNWB is properly added to MATLAB''s ' ... | ||
'search path and/or run generateCore() to re-generate classes.'] ); | ||
throwAsCaller(ME) | ||
end | ||
|
||
if numel(generatedTypeLocation) > 1 | ||
warning('NWB:Types:MultipleGeneratedTypesFound', ... | ||
['Multiple generated types was found for %s.\n', ... | ||
'This may indicate duplicate definitions that could lead to ', ... | ||
'unexpected behavior. Please ensure that only one instance of MatNWB is ', ... | ||
'present on MATLAB''s search path at any time.'], generatedTypeName) | ||
end | ||
|
||
% Remove the namespace folders from the path location of the | ||
% "types.core.NWBFile" class to obtain the root directory for generated types. | ||
rootDirectoryForGeneration = replace(generatedTypeLocation{1}, relPathForGeneratedType, ''); | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes to
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,36 @@ | ||
function Namespace = loadNamespace(name, saveDir) | ||
%LOADNAMESPACE loads dependent class metadata | ||
% name is the pregenerated namespace name | ||
% Namespaces a schemes.Namespace object with dependency graph | ||
function namespace = loadNamespace(namespaceName, generatedTypesDirectory) | ||
%LOADNAMESPACE - Load cached specifications for a namespace with dependency graph | ||
% | ||
% Input Arguments: | ||
% namespaceName (string) : Name of a format specification namespace, i.e "core" | ||
% generatedTypesDirectory (string) : Optional, path name of directory | ||
% where generated classes for neurodata types are located | ||
% | ||
% Output Arguments: | ||
% namespace (schemes.Namespace) - Namespace object with a dependency graph | ||
|
||
Cache = spec.loadCache(name, 'savedir', saveDir); | ||
assert(~isempty(Cache), 'NWB:Namespace:CacheMissing',... | ||
['No cache found for namespace `%s`. '... | ||
'Please generate any missing dependencies before generating this namespace.'], name); | ||
ancestry = schemes.Namespace.empty(length(Cache.dependencies), 0); | ||
arguments | ||
namespaceName (1,1) string | ||
generatedTypesDirectory (1,1) string {mustBeFolder} = ... | ||
schemes.utility.findRootDirectoryForGeneratedTypes() | ||
end | ||
|
||
for i=length(Cache.dependencies):-1:1 | ||
ancestorName = Cache.dependencies{i}; | ||
ancestry(i) = schemes.loadNamespace(ancestorName, saveDir); | ||
cachedNamespaceSpecification = spec.loadCache(namespaceName, 'savedir', generatedTypesDirectory); | ||
assert( ~isempty(cachedNamespaceSpecification), ... | ||
'NWB:Namespace:CacheMissing',... | ||
['No cache found for namespace `%s`.\nPlease use generateCore or ' ... | ||
'generateExtension to initialize a cache for the `%s` namespace.'], ... | ||
namespaceName, namespaceName); | ||
|
||
ancestry = schemes.Namespace.empty(length(cachedNamespaceSpecification.dependencies), 0); | ||
for i = length(cachedNamespaceSpecification.dependencies):-1:1 | ||
ancestorName = cachedNamespaceSpecification.dependencies{i}; | ||
ancestry(i) = schemes.loadNamespace(ancestorName, generatedTypesDirectory); | ||
end | ||
|
||
namespace = schemes.Namespace(... | ||
namespaceName, ... | ||
cachedNamespaceSpecification.version, ... | ||
ancestry, ... | ||
cachedNamespaceSpecification.schema); | ||
end | ||
|
||
Namespace = schemes.Namespace(name, Cache.version, ancestry, Cache.schema); | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
classdef SchemesFunctionsTest < tests.abstract.NwbTestCase | ||
% SchemesFunctionsTest - Unit tests for functions in the schemes namespace | ||
methods (Test) | ||
function testFindRootDirectoryForGeneratedTypes(testCase) | ||
generatedTypesFolder = schemes.utility.findRootDirectoryForGeneratedTypes(); | ||
testCase.verifyTrue(isfolder(generatedTypesFolder)) | ||
end | ||
|
||
function testFindRootDirectoryForGeneratedTypesIfMissing(testCase) | ||
import tests.fixtures.NwbClearGeneratedFixture | ||
testCase.applyFixture(NwbClearGeneratedFixture(testCase.getTypesOutputFolder)) | ||
testCase.verifyError(... | ||
@() schemes.utility.findRootDirectoryForGeneratedTypes(), ... | ||
'NWB:Types:GeneratedTypesNotFound') | ||
end | ||
|
||
function testFindRootDirectoryForGeneratedTypesWithDuplicates(testCase) | ||
% Generate types in temp working folder. Will produce an extra | ||
% set of generated types on MATLAB's search path | ||
import matlab.unittest.fixtures.WorkingFolderFixture | ||
testCase.applyFixture(WorkingFolderFixture) | ||
|
||
generateCore('savedir', '.') | ||
testCase.verifyWarning(... | ||
@() schemes.utility.findRootDirectoryForGeneratedTypes(), ... | ||
'NWB:Types:MultipleGeneratedTypesFound') | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you indicate the paths for each?