-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathpath_utils.py
131 lines (108 loc) · 3.64 KB
/
path_utils.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from __future__ import annotations
import json
from pathlib import Path, PurePosixPath
from typing import Optional, Tuple, Union
def construct_full_path(remote_path: Optional[str], filename: str) -> str:
"""
Returns the full Darwin path (in Posix form) of the given file, is such exists.
Parameters
----------
remote_path : Optional[str]
The remote path to this file, if it exists.
filename : str
The name of the file.
Returns
-------
str
The full Darwin path of the file in Posix form.
"""
if remote_path is None:
return filename
else:
return PurePosixPath("/", remote_path, filename).as_posix()
def deconstruct_full_path(filename: str) -> Tuple[str, str]:
"""
Returns a tuple with the parent folder of the file and the file's name.
Parameters
----------
filename : str
The path (with filename) that will be deconstructed.
Returns
-------
Tuple[str, str]
A tuple where the first element is the path of the parent folder, and the second is the
file's name.
"""
posix_path = PurePosixPath("/") / filename
return str(posix_path.parent), posix_path.name
def parse_metadata(path: Path) -> dict:
"""
Returns the parsed metadata file.
Parameters
----------
path : Path
The path to the metadata file.
Returns
-------
dict
The parsed metadata file.
"""
with open(path) as f:
metadata = json.load(f)
return metadata
def is_properties_enabled(
path: Path,
dir: str = ".v7",
filename: str = "metadata.json",
annotations_dir: str = "annotations",
) -> Union[Path, bool]:
"""
Returns whether the given export directory has properties enabled.
Parameters
----------
path : Path
The path to the export directory.
dir : str, optional
The name of the .v7 directory, by default ".v7"
filename : str, optional
The name of the metadata file, by default "metadata.json"
annotations_dir : str, optional
The name of the annotations directory, by default "annotations"
Returns
-------
bool
Whether the given export directory has properties enabled.
"""
# If the path is a file, get its parent
if path.is_file():
path = path.parent
# Check if the path has a .v7 directory
v7_path = path / dir
if not v7_path.exists():
# If it doesn't, check if it has an annotations directory
annotations_path = path / annotations_dir
if not annotations_path.exists():
return False
# If it does, check if any annotation file has "properties" in it
for annotation_path in annotations_path.rglob("*"):
with open(annotation_path) as f:
if '"properties"' in f.read():
return True
# If none of the annotation files have "properties" in them, return False
return False
# .v7 directory exists, parse the metadata file and check if any class has properties
# Additionally check if there are any item-level properties
metadata_path = v7_path / filename
metadata = parse_metadata(metadata_path)
metadata_classes = metadata.get("classes", [])
metadata_item_level_properties = metadata.get("properties", [])
for _cls in metadata_classes:
if _cls.get("properties"):
return metadata_path
for _item_level_property in metadata_item_level_properties:
if (
_item_level_property.get("property_values")
or _item_level_property["type"] == "text"
):
return metadata_path
return False