Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-advanced-user-tools
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-advanced-user-tools
Commits
6299e5f0
Commit
6299e5f0
authored
4 years ago
by
Henrik tom Wörden
Browse files
Options
Downloads
Patches
Plain Diff
ENH: added two path utility functions
parent
9c3e743a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!22
Release 0.3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/caosadvancedtools/utils.py
+47
-0
47 additions, 0 deletions
src/caosadvancedtools/utils.py
unittests/test_utils.py
+11
-1
11 additions, 1 deletion
unittests/test_utils.py
with
58 additions
and
1 deletion
src/caosadvancedtools/utils.py
+
47
−
0
View file @
6299e5f0
...
@@ -24,9 +24,12 @@
...
@@ -24,9 +24,12 @@
import
logging
import
logging
import
os
import
os
import
pathlib
import
caosdb
as
db
import
caosdb
as
db
logger
=
logging
.
getLogger
(
__name__
)
def
set_log_level
(
level
):
def
set_log_level
(
level
):
logger
=
logging
.
getLogger
(
__name__
)
logger
=
logging
.
getLogger
(
__name__
)
...
@@ -110,6 +113,50 @@ def assure_absolute_path_in_glob(glob, prefix):
...
@@ -110,6 +113,50 @@ def assure_absolute_path_in_glob(glob, prefix):
return
glob
return
glob
def
check_win_path
(
path
,
filename
=
None
):
"""
check whether
'
/
'
are in the path but no
'
\'
.
If that is the case, it is likely, that the path is not a Windows path.
Parameters:
path: path to be checked
filename: if the path is located in a file, this parameter can be used to
direct the user to the file where the path is located.
"""
if
r
"
\\
"
not
in
path
and
"
/
"
in
path
:
if
filename
:
msg
=
"
In file
\n
{}
\n
the
"
.
format
(
filename
)
else
:
msg
=
"
The
"
msg
+=
(
"
path
\n
{}
\n
does not look like
"
"
a Windows path.
"
.
format
(
path
))
logger
.
warning
(
msg
,
extra
=
{
'
identifier
'
:
str
(
path
),
'
category
'
:
"
inconsistency
"
})
return
False
return
True
def
treat_win_path
(
path
,
relative_to
,
filename
=
None
):
"""
check win path and convert it to posix and make it absolute.
Parameters:
path: windows path as string
relative_to: unix path as string
filename: if the path is located in a file, this parameter can be used to
direct the user to the file where the path is located.
"""
check_win_path
(
path
,
filename
=
filename
)
path
=
pathlib
.
PureWindowsPath
(
path
)
path
=
path
.
as_posix
()
path
=
assure_absolute_path_in_glob
(
str
(
path
),
prefix
=
relative_to
)
return
path
def
return_field_or_property
(
value
,
prop
=
None
):
def
return_field_or_property
(
value
,
prop
=
None
):
"""
"""
returns value itself of a property.
returns value itself of a property.
...
...
This diff is collapsed.
Click to expand it.
unittests/test_utils.py
+
11
−
1
View file @
6299e5f0
...
@@ -23,7 +23,8 @@
...
@@ -23,7 +23,8 @@
import
unittest
import
unittest
from
caosadvancedtools.utils
import
(
assure_absolute_path_in_glob
,
from
caosadvancedtools.utils
import
(
assure_absolute_path_in_glob
,
string_to_person
)
check_win_path
,
string_to_person
,
treat_win_path
)
class
Assure_absoluteTest
(
unittest
.
TestCase
):
class
Assure_absoluteTest
(
unittest
.
TestCase
):
...
@@ -55,3 +56,12 @@ class PersonParserTest(unittest.TestCase):
...
@@ -55,3 +56,12 @@ class PersonParserTest(unittest.TestCase):
assert
is_dhornung
(
rec
)
assert
is_dhornung
(
rec
)
rec
=
string_to_person
(
"
Henrik tom Wörden (MPI)
"
)
rec
=
string_to_person
(
"
Henrik tom Wörden (MPI)
"
)
assert
not
is_dhornung
(
rec
)
assert
not
is_dhornung
(
rec
)
class
PathTest
(
unittest
.
TestCase
):
def
test_win
(
self
):
assert
check_win_path
(
r
"
C:\hallo
"
)
assert
check_win_path
(
r
"
\hallo
"
)
assert
not
check_win_path
(
"
/hallo
"
)
self
.
assertEqual
(
treat_win_path
(
r
"
tag\hallo
"
,
"
/lol
"
),
"
/lol/tag/hallo
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment