1483
1483
segments.append(cur_segment)
1485
if len(segments) == 0:
1486
return self._transform.final_file_id(cur_parent)
1487
parent_path = self._transform._tree_id_paths[cur_parent]
1488
tree_path = pathjoin(*reversed(segments + [parent_path]))
1485
return cur_parent, list(reversed(segments))
1487
def _candidate_treepath2id(self, trans_segment, segments):
1488
parent_path = self._transform._tree_id_paths[trans_segment]
1489
tree_path = pathjoin(*([parent_path] + segments))
1489
1490
tree_file_id = self._transform._tree.path2id(tree_path)
1490
cur_file_id = tree_file_id
1491
return tree_file_id, tree_path
1493
def _valid_path2id(self, file_id, tree_path, trans_segment):
1494
"""Determine whether a file_id corresponds to a path.
1496
We do by seeing if the trans_segment is actually the last parent that
1497
is mentioned in the transform. If a more recent parent is mentioned,
1498
in the transform, it it would be the trans_segment unless it has been
1499
renamed. Therefore, the parent has been renamed and the path is not
1502
:param file_id: The file_id that may correspond to tree_path
1503
:param tree_path: The path of the file_id in the tree
1504
:param trans_segment: The last parent of the file which is mentioned
1491
1507
cur_trans_id = self._transform._tree_path_ids.get(tree_path)
1492
while cur_trans_id != cur_parent:
1508
while cur_trans_id != trans_segment:
1493
1509
if cur_trans_id is not None:
1494
1510
# If there was an entry for this file, and the path was
1495
1511
# correct we would have handled it already. So the path must
1497
1513
if cur_trans_id in self._transform._new_parent:
1499
cur_file_id = self._transform._tree.iter_entries_by_dir(
1500
[cur_file_id]).next()[1].parent_id
1501
tree_path = self._transform._tree.id2path(cur_file_id)
1515
file_id = self._transform._tree.iter_entries_by_dir(
1516
[file_id]).next()[1].parent_id
1517
tree_path = self._transform._tree.id2path(file_id)
1502
1518
cur_trans_id = self._transform._tree_path_ids.get(tree_path)
1521
def path2id(self, path):
1522
trans_segment, segments = self._path2id_last_transform_segment(path)
1523
if len(segments) == 0:
1524
return self._transform.final_file_id(trans_segment)
1525
tree_file_id, tree_path = self._candidate_treepath2id(trans_segment,
1527
if self._valid_path2id(tree_file_id, tree_path, trans_segment):
1505
1532
def id2path(self, file_id):
1506
1533
trans_id = self._transform.trans_id_file_id(file_id)