Skip to content
Snippets Groups Projects
Commit 6299e5f0 authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

ENH: added two path utility functions

parent 9c3e743a
No related branches found
No related tags found
1 merge request!22Release 0.3
...@@ -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{}\nthe ".format(filename)
else:
msg = "The "
msg += ("path\n{}\ndoes 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.
......
...@@ -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")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment