1
- import datajoint as dj
2
- import pathlib
3
- import uuid
4
- import hashlib
5
-
6
-
7
- dj .config ['enable_python_native_blobs' ] = True
8
-
9
-
10
- def find_full_path (root_directories , relative_path ):
11
- """
12
- Given a relative path, search and return the full-path
13
- from provided potential root directories (in the given order)
14
- :param root_directories: potential root directories
15
- :param relative_path: the relative path to find the valid root directory
16
- :return: root_directory (pathlib.Path object)
17
- """
18
- relative_path = pathlib .Path (relative_path )
19
-
20
- if relative_path .exists ():
21
- return relative_path
22
-
23
- # turn to list if only a single root directory is provided
24
- if isinstance (root_directories , (str , pathlib .Path )):
25
- root_directories = [root_directories ]
26
-
27
- for root_dir in root_directories :
28
- if (pathlib .Path (root_dir ) / relative_path ).exists ():
29
- return pathlib .Path (root_dir ) / relative_path
30
-
31
- raise FileNotFoundError ('No valid full-path found (from {})'
32
- ' for {}' .format (root_directories , relative_path ))
33
-
34
-
35
- def find_root_directory (root_directories , full_path ):
36
- """
37
- Given multiple potential root directories and a full-path,
38
- search and return one directory that is the parent of the given path
39
- :param root_directories: potential root directories
40
- :param full_path: the relative path to search the root directory
41
- :return: full-path (pathlib.Path object)
42
- """
43
- full_path = pathlib .Path (full_path )
44
-
45
- if not full_path .exists ():
46
- raise FileNotFoundError (f'{ full_path } does not exist!' )
47
-
48
- # turn to list if only a single root directory is provided
49
- if isinstance (root_directories , (str , pathlib .Path )):
50
- root_directories = [root_directories ]
51
-
52
- try :
53
- return next (pathlib .Path (root_dir ) for root_dir in root_directories
54
- if pathlib .Path (root_dir ) in set (full_path .parents ))
55
-
56
- except StopIteration :
57
- raise FileNotFoundError ('No valid root directory found (from {})'
58
- ' for {}' .format (root_directories , full_path ))
59
-
60
-
61
- def dict_to_uuid (key ):
62
- """
63
- Given a dictionary `key`, returns a hash string as UUID
64
- """
65
- hashed = hashlib .md5 ()
66
- for k , v in sorted (key .items ()):
67
- hashed .update (str (k ).encode ())
68
- hashed .update (str (v ).encode ())
69
- return uuid .UUID (hex = hashed .hexdigest ())
0 commit comments