FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
knowrob/src/integration/python/utils.cpp at dev · knowrob/knowrob · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
knowrob
/
knowrob
Public
Notifications
You must be signed in to change notification settings
Fork
112
Star
161
Code
Issues
4
Pull requests
2
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
knowrob
/
src
/
integration
/
python
/
utils.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
57 lines (51 loc) · 1.93 KB
Breadcrumbs
knowrob
/
src
/
integration
/
python
/
utils.cpp
Copy path
File metadata and controls
57 lines (51 loc) · 1.93 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* This file is part of KnowRob, please consult
* https://github.com/knowrob/knowrob for license details.
*/
#
include
<
set
>
#
include
<
list
>
#
include
"
knowrob/integration/python/utils.h
"
#
include
"
knowrob/URI.h
"
#
include
"
knowrob/Logger.h
"
namespace
knowrob
::py {
std::string
resolveModulePath
(std::string_view modulePath) {
//
guess if '/' or '.' is used as delimiter
if
(modulePath.
find
(
'
/
'
) != std::string::npos) {
return
URI::resolve
(modulePath);
}
else
{
//
replace '.' with '/', assuming dots do not appear in directory names.
std::string
modulePath_withSlash
(modulePath);
std::replace
(modulePath_withSlash.
begin
(), modulePath_withSlash.
end
(),
'
.
'
,
'
/
'
);
return
URI::resolve
(modulePath_withSlash);
}
}
std::string
addToSysPath
(
const
std::filesystem::path& modulePath) {
static
std::set<std::filesystem::path> moduleDirectories;
auto
topmostPythonPath = modulePath.
parent_path
();
std::list<std::string> modulePathParts;
modulePathParts.
push_front
(modulePath.
stem
().
string
());
while
(!topmostPythonPath.
empty
() &&
std::filesystem::exists
(topmostPythonPath /
"
__init__.py
"
)) {
modulePathParts.
push_front
(topmostPythonPath.
stem
().
string
());
topmostPythonPath = topmostPythonPath.
parent_path
();
}
std::stringstream ss;
for
(
auto
it = modulePathParts.
begin
(); it != modulePathParts.
end
(); ++it) {
ss << *it;
if
(it != --modulePathParts.
end
()) {
ss <<
"
.
"
;
}
}
auto
relativeModulePath = ss.
str
();
//
make sure that the module directory is only added once
if
(moduleDirectories.
count
(topmostPythonPath) ==
0
) {
moduleDirectories.
insert
(topmostPythonPath);
//
>>> sys.path.append(moduleDir)
auto
py_sys =
boost::python::import
(
"
sys
"
);
auto
py_path = py_sys.
attr
(
"
path
"
);
auto
sysPathAppend = py_path.
attr
(
"
append
"
);
sysPathAppend
(topmostPythonPath.
string
());
KB_DEBUG
(
"
[python] Added '{}' to sys.path.
"
, topmostPythonPath.
string
().
c_str
());
}
return
relativeModulePath;
}
}
Back
|
FazBrowse Home
|
New Git URL