| 584 | # @retval None If path doesn't exist |
| 585 | # |
| 586 | class DirCache: |
| 587 | _CACHE_ = set() |
| 588 | _UPPER_CACHE_ = {} |
| 589 | |
| 590 | def __init__(self, Root): |
| 591 | self._Root = Root |
| 592 | for F in os.listdir(Root): |
| 593 | self._CACHE_.add(F) |
| 594 | self._UPPER_CACHE_[F.upper()] = F |
| 595 | |
| 596 | # =[] operator |
| 597 | def __getitem__(self, Path): |
| 598 | Path = Path[len(os.path.commonprefix([Path, self._Root])):] |
| 599 | if not Path: |
| 600 | return self._Root |
| 601 | if Path and Path[0] == os.path.sep: |
| 602 | Path = Path[1:] |
| 603 | if Path in self._CACHE_: |
| 604 | return os.path.join(self._Root, Path) |
| 605 | UpperPath = Path.upper() |
| 606 | if UpperPath in self._UPPER_CACHE_: |
| 607 | return os.path.join(self._Root, self._UPPER_CACHE_[UpperPath]) |
| 608 | |
| 609 | IndexList = [] |
| 610 | LastSepIndex = -1 |
| 611 | SepIndex = Path.find(os.path.sep) |
| 612 | while SepIndex > -1: |
| 613 | Parent = UpperPath[:SepIndex] |
| 614 | if Parent not in self._UPPER_CACHE_: |
| 615 | break |
| 616 | LastSepIndex = SepIndex |
| 617 | SepIndex = Path.find(os.path.sep, LastSepIndex + 1) |
| 618 | |
| 619 | if LastSepIndex == -1: |
| 620 | return None |
| 621 | |
| 622 | Cwd = os.getcwd() |
| 623 | os.chdir(self._Root) |
| 624 | SepIndex = LastSepIndex |
| 625 | while SepIndex > -1: |
| 626 | Parent = Path[:SepIndex] |
| 627 | ParentKey = UpperPath[:SepIndex] |
| 628 | if ParentKey not in self._UPPER_CACHE_: |
| 629 | os.chdir(Cwd) |
| 630 | return None |
| 631 | |
| 632 | if Parent in self._CACHE_: |
| 633 | ParentDir = Parent |
| 634 | else: |
| 635 | ParentDir = self._UPPER_CACHE_[ParentKey] |
| 636 | for F in os.listdir(ParentDir): |
| 637 | Dir = os.path.join(ParentDir, F) |
| 638 | self._CACHE_.add(Dir) |
| 639 | self._UPPER_CACHE_[Dir.upper()] = Dir |
| 640 | |
| 641 | SepIndex = Path.find(os.path.sep, SepIndex + 1) |
| 642 | |
| 643 | os.chdir(Cwd) |