Skip to content

Add Python Bindings for getCacheDirectory function#18931

Closed
sl-sergei wants to merge 2 commits intoopencv:3.4from
sl-sergei:filesystem_py
Closed

Add Python Bindings for getCacheDirectory function#18931
sl-sergei wants to merge 2 commits intoopencv:3.4from
sl-sergei:filesystem_py

Conversation

@sl-sergei
Copy link
Copy Markdown
Contributor

@sl-sergei sl-sergei commented Nov 26, 2020

This PR adds new Python binding for getCacheDirectory, necessary for the PR: #18591

force_builders=linux,docs,Custom
build_image:Custom=ubuntu-openvino-2020.4.0:16.04
build_image:Custom Win=openvino-2020.4.0
build_image:Custom Mac=openvino-2020.4.0

test_modules:Custom=python2,python3,java,samples
test_modules:Custom Win=python2,python3,java,samples
test_modules:Custom Mac=python2,python3,java,samples

buildworker:Custom=linux-1
# disabled due high memory usage: test_opencl:Custom=ON
test_opencl:Custom=OFF
test_bigdata:Custom=1
test_filter:Custom=*

@sl-sergei sl-sergei requested a review from alalek November 26, 2020 12:33
@sl-sergei
Copy link
Copy Markdown
Contributor Author

@alalek can you advise on better naming/location for cache_directory.hpp?

@alalek
Copy link
Copy Markdown
Member

alalek commented Nov 26, 2020

Why do we need to move declaration?

If it is really necessary, the lets put single CV_EXPORTS_W wrapper (without namespace hierarchy) into bindings_utils.hpp

@sl-sergei
Copy link
Copy Markdown
Contributor Author

sl-sergei commented Nov 26, 2020

Why do we need to move declaration?

If it is really necessary, the lets put single CV_EXPORTS_W wrapper (without namespace hierarchy) into bindings_utils.hpp

I moved declaration because core/utils/filesystem.hpp is filtered out in python/bindings/CMakeLists.txt. If I move it without names, then it will be necessary to move definition into bindings_utils.cpp or change namespace logic and macros in filesystem.cpp

@alalek
Copy link
Copy Markdown
Member

alalek commented Nov 26, 2020

bindings_utils.cpp

Lets put wrapper implementation there.

* @param configuration_name optional name of configuration parameter name which overrides default behavior.
* @return Path to cache directory. Returns empty string if cache directories support is not available. Returns "disabled" if cache disabled by user.
*/
CV_EXPORTS cv::String getCacheDirectory(const char* sub_directory_name, const char* configuration_name = NULL);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not touch this at all.

Just add wrapper call for bindings only.

Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks much better.

from tests_common import NewOpenCVTests

class get_cache_dir_test(NewOpenCVTests):
def test_copytomask(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copytomask

?

please reuse test_misc.py

#New binding
path = cv.utils.getCacheDirectory(subfolder_name)
self.assertTrue(os.path.exists(path))
self.assertTrue(os.path.isdir(path))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It make sense to dump path as failure message

@sl-sergei
Copy link
Copy Markdown
Contributor Author

@alalek I updated the test, are there other drawbacks that I need to fix?

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Nov 27, 2020

related: #15317. So it seems to me there is simpler way to enable the function. Also I think that we need to preserve namespace for transferability: cv.utils.fs.getCacheDirectory.

@alalek
Copy link
Copy Markdown
Member

alalek commented Nov 27, 2020

BTW, There is an empty string result on Windows due to some reason. Perhaps we should improve this issue first.

@sl-sergei
Copy link
Copy Markdown
Contributor Author

related: #15317. So it seems to me there is simpler way to enable the function. Also I think that we need to preserve namespace for transferability: cv.utils.fs.getCacheDirectory.

If we follow your way for enabling this function in Python, it will create bindings for other functions from "modules/.+/utils/.*" which I think should not be included in the Python package.

@sl-sergei
Copy link
Copy Markdown
Contributor Author

BTW, There is an empty string result on Windows due to some reason. Perhaps we should improve this issue first.

@alalek I tried to reproduce this issue on local Windows machine, but I could not do that

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Dec 2, 2020

@sl-sergei, bindings are regulated by CV_EXPORTS_W macro. Other methods have just CV_EXPORTS.

@sl-sergei
Copy link
Copy Markdown
Contributor Author

sl-sergei commented Dec 2, 2020

@dkurt This method creates bindings for enums in utils.logging, utils.trace and instr namespaces without any functions. I think they should not be included in Python package, so it is better to leave the filtering in CMake. @alalek Please, correct me if I'm wrong

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Dec 2, 2020

We can add only filesystem.hpp in whitelist but not entire "core/utils"

@sl-sergei
Copy link
Copy Markdown
Contributor Author

@dkurt How do you plan to do that? I don't know, maybe there is an easy way to do that, but I have already tried to change filtering logic/regex, but found that CMake has very limited options for regular expressions. The other option is to add filesystem.hpp directly to the CMake, but this would look really ugly...

Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More serious problem here is non-ASCII paths (especially on Windows) and cv::String result.

On Windows it is expected to have path like C:\Documents and Settings\username\Local Settings\AppData\Local\opencv\<major_version>.x<version_status>\.
username is a source of Unicode symbols.

P.S. Even TEMP / TMP env paths are point into User's profile.

Need to design more robust solution.

Ref: #18305

}

namespace fs {
CV_EXPORTS_W cv::String getCacheDirectory(const char *sub_directory_name);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aliases should work:

CV_EXPORTS_AS(getCacheDirectory) cv::String getCacheDirectory_bindings(const char *sub_directory_name, ...);

Also repair configuration parameter. It is necessary to keep OpenCV configurable.


IMHO, It make sense to hide some details and provide more specialized wrapper.

In core/utility.hpp add:

CV_EXPORTS_W cv::String getCacheDirectoryForDownloads(void);

with implementation (to avoid details in .py files):

{
    return cv::utils::fs::getCacheDirectory("downloads", "OPENCV_DOWNLOADS_CACHE_DIR");
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In core/utility.hpp add:

I don't think it is a good idea to put function declaration and implementation there, because it would require including core/filesystem.hpp there. So I left it inside binding_utils, which is in my opinion a more suitable place for a binding wrapper.

@asmorkalov
Copy link
Copy Markdown
Contributor

@sl-sergei please squash commits

@asmorkalov
Copy link
Copy Markdown
Contributor

@alalek Please take a look. Sergey added UTF8 handling too.

@asmorkalov
Copy link
Copy Markdown
Contributor

Replaced by #19668

@asmorkalov asmorkalov closed this Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants