Projekt

Obecné

Profil

Stáhnout (1.34 KB) Statistiky
| Větev: | Tag: | Revize:
1 fa03de5c Captain_Trojan
import os
2
3
4
class FileAnchor:
5
    def __init__(self, root_dir, tail, searching=False):
6
        self.__root_dir = root_dir
7
        self.__tail = tail
8
        if searching:
9
            self.shortest_relative_path = self.__srp_with_tree_search
10
        else:
11
            self.shortest_relative_path = self.__srp
12
13
    def __srp_with_tree_search(self, src=None):
14
        """
15
        Shortest relative path handling the situation where src is not a descendant of root_dir.
16
        Searches the subtree of src until it finds root_dir.
17
        :param src: source node or cwd
18
        :return: path from source to the anchored file
19
        """
20
        raise NotImplementedError("Not implemented yet.")
21
22
    def __srp(self, src=None):
23
        """
24
        Shortest relative path from src to the anchored file. If src is not a descendant of root_dir, returns None.
25
        :param src: source node or cwd
26
        :return: path from source to the anchored file or None
27
        """
28
        if src is None:
29
            src = os.getcwd()
30
31
        ret = ""
32
        h = src
33
        while len(h) > 0 and h != "/":
34
            h, t = os.path.split(h)
35
            if t != self.__root_dir:
36
                ret = os.path.join(ret, "..")
37
            else:
38
                return os.path.join(ret, self.__tail)
39
40
        return None
41
42
    def __str__(self):
43
        return self.shortest_relative_path(None)