]>
Commit | Line | Data |
---|---|---|
1 | import os | |
2 | ||
3 | from parso import file_io | |
4 | ||
5 | ||
6 | class AbstractFolderIO: | |
7 | def __init__(self, path): | |
8 | self.path = path | |
9 | ||
10 | def get_base_name(self): | |
11 | raise NotImplementedError | |
12 | ||
13 | def list(self): | |
14 | raise NotImplementedError | |
15 | ||
16 | def get_file_io(self, name): | |
17 | raise NotImplementedError | |
18 | ||
19 | def get_parent_folder(self): | |
20 | raise NotImplementedError | |
21 | ||
22 | def __repr__(self): | |
23 | return '<%s: %s>' % (self.__class__.__name__, self.path) | |
24 | ||
25 | ||
26 | class FolderIO(AbstractFolderIO): | |
27 | def get_base_name(self): | |
28 | return os.path.basename(self.path) | |
29 | ||
30 | def list(self): | |
31 | return os.listdir(self.path) | |
32 | ||
33 | def get_file_io(self, name): | |
34 | return FileIO(os.path.join(self.path, name)) | |
35 | ||
36 | def get_parent_folder(self): | |
37 | return FolderIO(os.path.dirname(self.path)) | |
38 | ||
39 | def walk(self): | |
40 | for root, dirs, files in os.walk(self.path): | |
41 | root_folder_io = FolderIO(root) | |
42 | original_folder_ios = [FolderIO(os.path.join(root, d)) for d in dirs] | |
43 | modified_folder_ios = list(original_folder_ios) | |
44 | yield ( | |
45 | root_folder_io, | |
46 | modified_folder_ios, | |
47 | [FileIO(os.path.join(root, f)) for f in files], | |
48 | ) | |
49 | modified_iterator = iter(reversed(modified_folder_ios)) | |
50 | current = next(modified_iterator, None) | |
51 | i = len(original_folder_ios) | |
52 | for folder_io in reversed(original_folder_ios): | |
53 | i -= 1 # Basically enumerate but reversed | |
54 | if current is folder_io: | |
55 | current = next(modified_iterator, None) | |
56 | else: | |
57 | del dirs[i] | |
58 | ||
59 | ||
60 | class FileIOFolderMixin: | |
61 | def get_parent_folder(self): | |
62 | return FolderIO(os.path.dirname(self.path)) | |
63 | ||
64 | ||
65 | class ZipFileIO(file_io.KnownContentFileIO, FileIOFolderMixin): | |
66 | """For .zip and .egg archives""" | |
67 | def __init__(self, path, code, zip_path): | |
68 | super().__init__(path, code) | |
69 | self._zip_path = zip_path | |
70 | ||
71 | def get_last_modified(self): | |
72 | try: | |
73 | return os.path.getmtime(self._zip_path) | |
74 | except (FileNotFoundError, PermissionError, NotADirectoryError): | |
75 | return None | |
76 | ||
77 | ||
78 | class FileIO(file_io.FileIO, FileIOFolderMixin): | |
79 | pass | |
80 | ||
81 | ||
82 | class KnownContentFileIO(file_io.KnownContentFileIO, FileIOFolderMixin): | |
83 | pass |