Skip to content

Commit 2e6d4fc

Browse files
committed
feat(cli): support draft number and revision in TBRN
1 parent 92389bb commit 2e6d4fc

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

tensorbay/utility/tbrn.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ class TBRN:
144144
sensor_name: Name of the sensor.
145145
remote_path: Object path of the file.
146146
tbrn: Full TBRN string.
147+
draft_number: The draft number (if the status is draft).
148+
revision: The commit revision (if the status is commit).
147149
148150
Attributes:
149151
dataset_name: Name of the dataset.
@@ -152,6 +154,9 @@ class TBRN:
152154
sensor_name: Name of the sensor.
153155
remote_path: Object path of the file.
154156
type: The type of this TBRN.
157+
draft_number: The draft number (if the status is draft).
158+
revision: The commit revision (if the status is commit).
159+
is_draft: whether the status is draft, True for draft, False for commit.
155160
156161
Raises:
157162
TBRNError: The TBRN is invalid.
@@ -160,6 +165,10 @@ class TBRN:
160165

161166
_HEAD = "tb"
162167
_NAMES_SEPARATOR = ":"
168+
169+
_DRAFT_SEPARATOR = "#"
170+
_REVISION_SEPARATOR = "@"
171+
163172
_NAMES_MAX_SPLIT = 4
164173
_PATH_SEPARATOR = "://"
165174
_PATH_MAX_SPLIT = 1
@@ -183,14 +192,16 @@ class TBRN:
183192
Optional[str],
184193
]
185194

186-
def __init__(
195+
def __init__( # pylint: disable=too-many-locals
187196
self,
188197
dataset_name: Optional[str] = None,
189198
segment_name: Optional[str] = None,
190199
frame_index: Optional[int] = None,
191200
sensor_name: Optional[str] = None,
192201
*,
193202
remote_path: Optional[str] = None,
203+
draft_number: Optional[int] = None,
204+
revision: Optional[str] = None,
194205
tbrn: Optional[str] = None,
195206
) -> None:
196207
if tbrn is not None:
@@ -200,7 +211,26 @@ def __init__(
200211
if names[0] != TBRN._HEAD:
201212
raise TBRNError('TensorBay Resource Name should startwith "tb:"')
202213

214+
dataset_name = names[1]
215+
if not dataset_name:
216+
raise TBRNError(
217+
'TensorBay Resource Name should add dataset name "tb:<dataset name>"'
218+
)
219+
220+
self.revision: Optional[str] = None
221+
self.draft_number: Optional[int] = None
222+
223+
if TBRN._REVISION_SEPARATOR in dataset_name:
224+
names[1], self.revision = dataset_name.split(TBRN._REVISION_SEPARATOR, 1)
225+
226+
elif TBRN._DRAFT_SEPARATOR in dataset_name:
227+
names[1], number = dataset_name.split(TBRN._DRAFT_SEPARATOR)
228+
self.draft_number = int(number)
229+
else:
230+
names[1] = dataset_name
231+
203232
names += [None] * (TBRN._NAMES_MAX_SPLIT + 1 - len(names))
233+
204234
frame_index = names[TBRN._FRAME_INDEX]
205235
names[TBRN._FRAME_INDEX] = int(frame_index) if frame_index else None
206236

@@ -220,6 +250,12 @@ def __init__(
220250
remote_path,
221251
)
222252

253+
if draft_number is not None and revision is not None:
254+
raise TBRNError("TensorBay Resource Name should not contain draft and commit info")
255+
256+
self.draft_number = draft_number
257+
self.revision = revision
258+
223259
try:
224260
self._type, self._field_length = self._check_type()
225261
except KeyError as error:
@@ -302,6 +338,16 @@ def type(self) -> TBRNType:
302338
"""
303339
return self._type
304340

341+
@property
342+
def is_draft(self) -> bool:
343+
"""Return the frame index.
344+
345+
Returns:
346+
The frame index.
347+
348+
"""
349+
return bool(self.draft_number)
350+
305351
def get_tbrn(self, frame_width: int = 0) -> str:
306352
"""Generate the full TBRN string.
307353
@@ -324,6 +370,11 @@ def get_tbrn(self, frame_width: int = 0) -> str:
324370
names[TBRN._FRAME_INDEX] = f"{frame_index}"
325371
else:
326372
names[TBRN._FRAME_INDEX] = ""
373+
if self.is_draft:
374+
names[1] = f"{names[1]}{TBRN._DRAFT_SEPARATOR}{self.draft_number}"
375+
else:
376+
if self.revision is not None:
377+
names[1] = f"{names[1]}{TBRN._REVISION_SEPARATOR}{self.revision}"
327378
tbrn = TBRN._NAMES_SEPARATOR.join(names)
328379
if self._names[4] is not None:
329380
tbrn = f"{tbrn}{TBRN._PATH_SEPARATOR}{self.remote_path}"

0 commit comments

Comments
 (0)