-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilesystem.py
1283 lines (990 loc) · 43.2 KB
/
filesystem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2023 Valerio AFK <afk.broadcast@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import annotations
import time
from typing import Any, Union, List, Type, Iterable, Dict, AsyncIterable, Tuple
from abc import ABC, abstractmethod
from enum import Enum
from rclone_python import rclone
from datetime import datetime
from copy import copy
from psutil import disk_partitions
from config import get_cache_file
from pyrclone.pyrclone import rclone
from pyrclone.pyrclone.jobs import _fix_isotime
from concurrent.futures import ProcessPoolExecutor
import asyncio
import os
import json
import aiofiles
# Checks whether RH is running under windows or not
is_windows = lambda: os.name == 'nt'
# Gets the current time zone (useful to get rid of naive datetime)
current_timezone = lambda: datetime.now().astimezone().tzinfo
UNITS = ("", "K", "M", "G", "T", "P", "E", "Z")
def rclone_instance() -> rclone:
if not hasattr(rclone_instance, "_instance"):
# TODO: use auth
rclone_instance._instance = rclone().run()
time.sleep(0.5)
return rclone_instance._instance
def sizeof_fmt(num: int, suffix: str = "B") -> str:
'''
Formats an integer representing the size of a file into something more human-readable format
This function has been adapted from https://stackoverflow.com/questions/1094841/get-human-readable-version-of-file-size
:param num: Size in bytes
:param suffix: The suffix of what the used unit (B == Bytes)
:return: A string with a human-readable format
'''
# If the size is 0, I prefer to have a dash rather 0B
if (num == 0):
return "-"
# For each unit
for unit in UNITS:
# Checks if the current number is less than 1024 (ie do we need more division by 1024?)
if abs(num) < 1024.0:
# If not, returns the formated number with unit and suffix
return f"{num:3.1f}{unit}{suffix}"
# Otherwise, it divides the number by 1024
num /= 1024.0
# Units get to Zettabyte. Beyond that, it'll be Yottabytes and whatever...
return f"{num:.1f}Y{suffix}"
def convert_to_bytes(value: float, unit: str) -> int:
'''
Converts a floating point representing a formated file sizee into int
This conversion is not perfect, as 15.1KB may likely be rounded
:param: The value to convert
:param: It's current unit
:return: a integer representing the size int bytes
'''
# Mathematically this, fuction performs opposite operations than the one above (sizeof_fmt)
for i, u in enumerate(UNITS[1:]):
if u in unit:
return int(value * (1024 ** (i + 1)))
return int(value)
def _tree_sort_fn(path: str) -> List[str, ...]:
"""
This nested function is to support the fullpath sorting, having longer paths to the end
It is used in the `key` parameter of sorting functions
:param x: The item to be sorted
:return: A tuple containing the path split by its components
"""
p = AbstractPath.split(path)
return p
class AbstractPath(ABC):
'''
This class represents a generic path within an operating system
I had to make this class because rclone uses a Windows-like format for the remote (e.g., mega:/).
However, pathlib doesn't support Windows like with volume named with a longer string than a single letter.
So, it was better to reimplement these classes for only the stuff I needed for this project
'''
PATH_SEPARATOR = '/'
VOLUME_SEPARATOR = ":"
def __init__(this, path: str, root: Union[str | None] = None):
'''
Instantiate a new path
:param path: A string representing a path. If root is not provided, it must be absolute
:param root: An absolute path representing the root (starting point) of the previous parameter.
If not provided, then root = path
'''
# Converts the root path (if provided) considering special directories .. and .
this._basepath = this.normalise(path if root is None else root)
# Checks if the path is relative
if (this.is_relative(this._basepath)):
# It's a problem because root cannot be an absolute path
raise MissingAbsolutePathException(this._basepath, "Basepath")
# Makes normalisation steps for the provided path as well
path = this.normalise(path)
# Checks if path is relative, ie path does not start with its root
if not path.startswith(this._basepath):
# In this case, path is the merging of root and itself
this._path = this.normalise(this.join(this.root, path))
else:
# Otherwise, path is kept as is
this._path = path
# If path is absolute, it needs to be clear whether it's under the provided root, otherwise nothing will work
if not this.root_is_parent_of(this._path):
raise PathOutsideRootException(this.absolute_path, this.root)
@classmethod
def make_path(cls, path: str) -> AbstractPath:
'''
Static method to generate a path from a string. This method should implement something like the factory D.P.
:param path: The path to make an object from
:return: An object representing the path
'''
# This is a guess. If the path has a volume separator, then it's assumed it's NT-lile, else Posix-like
suitable_class = NTAbstractPath if path.find(NTAbstractPath.VOLUME_SEPARATOR) >= 0 else PosixAbstractPath
return suitable_class(path=".", root=path)
@classmethod
def as_posix(cls, p: str) -> str:
'''
I gave this method a nice name, because replacing_the_stupid_windows_backslashes_with_normal_slashes sounded kinda bad.
I think you know understand what it does
:param p: The path
:return:
'''
return p.replace("\\", "/")
@classmethod
def join(cls, *args) -> Union[str | None]:
'''
Similar in concept as os.path.join
:param args:
:return: A merged path or none if the list is empty
'''
paths = args[0] if len(args) == 1 else args
if (len(paths) == 0):
return None
r = paths[0]
for i in range(1, len(paths)):
# To avoid to have double slashes, each path part is checked whether they end/start with slash
xx = r.endswith(cls.PATH_SEPARATOR)
yy = paths[i].startswith(cls.PATH_SEPARATOR)
# The operator ^ is the XOR operator.
# If either of them have a slash, I simply concatenate them
if (xx ^ yy):
r += paths[i]
elif (xx and yy):
# if both have a slash, I remove the slash from the second part
r += paths[i].lstrip(" /")
else:
# if neither of them has a slash, it's added
if (cls.is_relative(paths[i])):
r += cls.PATH_SEPARATOR + paths[i]
else:
# in the case a path part is an absolute path, well, everything done so far gets wiped out
r = paths[i]
# Returns the merged path
return r
@classmethod
def is_special_dir(cls, d: str) -> bool:
'''
Check if the provided argument is the dir '.' or '..'
:param d: directory name to check
:return: A boolean representing whether d is either '.' or '..'
'''
return (d == ".") or (d == "..")
@classmethod
def is_absolute(cls, path: str) -> bool:
"""
Check if the path is absolute. This method needs to be overridden for specific case, eg NT-like paths
:param path: the path to check if it's absolute or not
:return: TRUE if the path is absolute, FALSE otherwise
"""
return path.startswith(cls.PATH_SEPARATOR)
@classmethod
def is_relative(cls, path: str) -> bool:
"""
Check if the path is relative.
:param path: the path to check if it's absolute or not
:return: TRUE if the path is relative, FALSE otherwise
"""
return not cls.is_absolute(path)
@classmethod
def normalise(cls, path: str) -> str:
"""
Normalise the path were appropriate. This method needs to be overridden for specific case
:param path: the path to normalise
:return: Normalised path
"""
return cls.as_posix(path)
@classmethod
def split(cls, path: str) -> List[str]:
'''
Split the path - opposite to the join method
:param path: The path to split
:return: A list containing directory and file names
'''
tokens = path.split(cls.PATH_SEPARATOR)
if tokens[0] == '':
tokens[0] = cls.PATH_SEPARATOR
return [t for t in tokens if len(t) > 0]
@classmethod
def is_root_of(cls, path: str, root: str) -> bool:
if cls.is_relative(path):
return True
spath = cls.split(cls.normalise(path))
sroot = cls.split(root)
if (len(sroot) <= len(spath)):
for i, (x, y) in enumerate(zip(sroot, spath)):
if (i == 0):
# this is also viable for posix paths because the first item will be just "/"
if (x.lower() != y.lower()) and (y != cls.PATH_SEPARATOR):
return False
elif x != y:
return False
return True
else:
return False
def __copy__(this) -> AbstractPath:
return type(this)(path=this.absolute_path, root=this.root)
def __str__(this) -> str:
return this.relative_path
def __repr__(this) -> str:
return this.absolute_path
@property
@abstractmethod
def relative_path(this) -> str:
"""
Abstract property to retrieve the relative path
:return: The relative path
"""
pass
@property
def absolute_path(this) -> str:
'''
Returns the absolute path
:return: The absolute path
'''
# By convention, the property _path should already contain the absolute (and normalised) path
return this._path
@property
def root(this) -> str:
'''
Gets the current root directory
:return: The current root directory
'''
return this._basepath
@root.setter
def root(this, path: str) -> None:
'''
Set a new root to the Path
:param path: An absolute path to the new root
'''
# when root is changed, the path needs to be re-rooted
# therefore, the old relative path needs to be stored
# to be used later to re-root the whole thing
old_relpath = this.relative_path
this._basepath = this.normalise(path)
# Not sure why this is here. It works - who cares. I should've put comments earlier.
if (this.is_absolute(this._path)):
this._path = this.join(this._basepath, old_relpath)
def cd(this, path: str) -> None:
'''
Change directory (similar to the cd command in any shell/terminal)
:param path: The new (absolute/relative) path to navigate into
'''
# Firstly, we need to check if the new path is under the root of the current object
# We cannot explore paths outside the root
if (this.root_is_parent_of(path)):
# If the path is absolute, then it simply replaces the _path property
if (this.is_absolute(path)):
this._path = this.normalise(path)
else:
# If it's relative, it gets joined and then normalised
new_path = this.normalise(AbstractPath.join(this._path, path))
# if the new path (after normalisation) is still under the root, we keep it
# otherwise, if we are above the root (this can happen with a lot of ../../../)
# we set the current path as root.
this._path = new_path if this.root_is_parent_of(new_path) else this.root
else:
# In this case, the current path is the root (very similar to the above case)
this._path = this.root
def visit(this, path: str) -> AbstractPath:
'''
Very similar to the `cd` method, but it creates a new object instead of changing the current one
:param path: The new path to visit
:return: A new object rooted in the same root but with the path provided as parameter
'''
c = copy(this)
c.cd(path)
return c
def root_is_parent_of(this, path: str) -> bool:
'''
Very similar to the `is_root_of` static method. This method implements the instance version of it
:param path: The path to check if it's under root of the current root path
:return: TRUE if the path is under the current root, FALSE otherwise
'''
return this.is_root_of(path, this.root)
def is_parent_of(this, path: str) -> bool:
'''
Very similar to the `is_parent_of_root` but considers parent directory as root
:param path: The path to check if it's under root of the current path
:return: TRUE if the path is under the current path, FALSE otherwise
'''
return this.is_root_of(path, this.absolute_path)
class FileType(Enum):
'''
Enumeration of the supported file types (OTHER is of all the other we don't give a crap of)
'''
OTHER = 0
REGULAR = 1
DIR = 2
class FileSystemObject:
'''
This class represents any suitable object in a file system (in our case, mainly regular files and directories)
'''
def __init__(this,
fullpath: Union[AbstractPath | None],
*,
type: FileType,
size: Union[int | None] = None,
mtime: Union[datetime | None] = None,
exists: Union[bool | None] = None,
checksum: Union[str | None] = None,
hidden: bool = False):
"""
:param fullpath: Full path to the FS object
:param type: Type of the file (see `FileType` enumeration)
:param size: Size (in bytes) of the file if known, None otherwise
:param mtime: Timestamp of the last modification time if known, None otherwise
:param exists: TRUE if the object truly exists, FALSE otherwise (you can have a local file that doesn't exist remotely)
:param hidden: TRUE if it's a hidden file (according to the definition of the hosting OS), FALSE otherwise.
"""
this.fullpath = fullpath
this.type = type
this._size = size
this._mtime = mtime
this.hidden = hidden
this._exists = exists
this._checksum = checksum
this._is_empty = None
@property
def absolute_path(this) -> str:
"""Gets the absolute path of the fs object"""
return this.fullpath.absolute_path
@property
def relative_path(this) -> str:
"""Gets the relative path of the fs object"""
return this.fullpath.relative_path
@property
def containing_directory(this) -> str:
"""Gets the containing directory of the FS object (extracted from its absolute path)"""
return os.path.split(this.absolute_path)[0]
@property
def filename(this) -> str:
"""Gets the file- or directory name of the fs object"""
return os.path.split(this.absolute_path)[1]
async def is_remote(this) -> bool:
"""
Checks if the fs object is rooted in a remote drive (doesn't check if it exists remotely)
:return: TRUE if it's in any of the remote drives, FALSE otherwise
"""
for _, drive in (await rclone_instance().list_remotes()):
if this.absolute_path.startswith(drive):
return True
return False
async def is_local(this) -> bool:
"""
Checks if the fs object is rooted in a local drive (doesn't check if it exists remotely)
:return: TRUE if it's in any of the local drives, FALSE otherwise
"""
return not this.is_remote
@property
def size(this) -> Union[int | None]:
"""
Gets the file size
:return: The size in bytes of the fs object. It's set by -1 if it's the size of a directory
"""
if (not this.exists) or (this.type == FileType.DIR):
return None
# if (this._size is None) or (this._size < 0):
# this.update_information()
return this._size
@property
def exists(this) -> bool:
'''
Checks if a file system object exists
:return: TRUE if the object exists, FALSE otherwise
'''
# if this._exists is None:
# this.update_information()
return this._exists
@property
def mtime(this) -> Union[datetime | None]:
"""Gets the modification time of the filesystem object"""
# if this._mtime is None:
# this.update_information()
return this._mtime
# @property
# def is_empty(this) -> bool:
# if this.type != FileType.DIR:
# raise TypeError("Cannot determine the emptiness of something that is not a directory")
#
# if this._is_empty is not None:
# return this._is_empty
#
# p = subprocess.run(['rclone', 'ls', this.absolute_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# output = p.stdout.decode().strip()
#
# this._is_empty = (p.returncode == 0) and (len(output) == 0)
#
# return this._is_empty
@mtime.setter
def mtime(this, mtime: Union[datetime | None]) -> None:
"""
Sets the modification time of the fs object
:param mtime: An object of type datetime for the new modification t ime
"""
this._mtime = mtime if (mtime is None) or (mtime.tzinfo is not None) else mtime.replace(
tzinfo=current_timezone())
@property
def has_checksum(this) -> bool:
return this._checksum is not None
@property
def checksum(this) -> Union[str | None]:
if this.type == FileType.DIR:
return None
return this._checksum
@checksum.setter
def checksum(this, value: str) -> None:
"""
This can be used in those cases the checksum has been precomputed
:param value: Precomputed checksum value
:return:
"""
this._checksum = value
async def get_checksum(this) -> Union[str | None]:
"""
Calculates the cehcksum of a file
:return: The checksum (if can be calculated) or None. Some remotes don't allow the calculation of the checksum
and files need to be downloaded first. This can be done by rclone. However, this option needs to be
activated by the user, as some remotes put limits on file download
"""
if this.type == FileType.DIR:
return None
if this.checksum is None:
this.checksum = await rclone_instance().checksum(this.absolute_path,remote = await this.is_remote())
return this.checksum
def __eq__(this, other) -> bool:
if type(other) == str:
return (this.absolute_path == other) or (this.relative_path == other)
elif isinstance(other, FileSystemObject):
return this.relative_path == other.relative_path
else:
return False
def __hash__(this) -> int:
return hash(this.relative_path)
def __str__(this) -> str:
return this.relative_path
def __repr__(this) -> str:
return str(this)
async def update_information(this) -> None:
"""Update the information about the file system object, eg size, modificafion time and its existance"""
# Using rclone is the best way to have this information formated in the same way, regardless if we have a local
# or remote file/directory
stat = await rclone_instance().stat(this.fullpath.root, this.fullpath.relative_path)
# If rclone returns code is non-zero, then the object doesn't exist
if stat is not None:
# If it does exist, then the new information are used to update the current object status
this._size = stat['Size']
this.mtime = datetime.fromisoformat(_fix_isotime(stat['ModTime']))
this._exists = True
else:
this._exists = False
def to_dict(this) -> Dict[str, Any]:
print()
return {
"path": this.relative_path,
"type": this.type.value,
"size": this.size,
"mtime": this.mtime.timestamp(),
"exists": this.exists,
"checksum": this._checksum,
"hidden": this.hidden
}
@classmethod
def from_dict(cls, d: Dict[str, Any], *, mtime: Union[int | None] = None) -> FileSystemObject:
d["mtime"] = mtime if d["mtime"] is None else d['mtime']
if d['mtime'] is not None:
d['mtime'] = datetime.fromtimestamp(d['mtime'])
return FileSystemObject(**d)
class PathException(Exception):
"""An exception related to problems with Paths"""
...
class MissingAbsolutePathException(PathException):
"""An exception generated when an absolute path is missing"""
def __init__(this, path, desc="Path"):
super().__init__(f"{desc} {path} is not an absolute path.")
class PathOutsideRootException(PathException):
"""An exception raised when someone wants to go beyond the allowed boundaries of the file system
(the root parameter in AbsolutePath) sets a boundary and no one can go above that.
"""
def __init__(this, root, path):
super().__init__(f"The path {path} is not rooted in {root}")
class PosixAbstractPath(AbstractPath):
"""
Extends the Abstract path for Unix-like path management
Most of the functionality needed for this class are in the parent class.
It's required to adapt a few things to make it work with POSIX paths
"""
def __init__(this, path: str, root: Union[str | None] = None):
bp = this.normalise(path if root is None else root)
path = this.normalise(path)
if (this.is_relative(bp)):
raise MissingAbsolutePathException(bp, "Basepath")
if not this.is_root_of(path, bp):
raise PathOutsideRootException(path, bp)
super().__init__(path, bp)
@classmethod
def normalise(cls, path: str) -> str:
path = super(PosixAbstractPath, PosixAbstractPath).normalise(path)
tokens = cls.split(path)
tokens[1:] = [t for t in tokens[1:] if t != "."]
while ".." in tokens:
idx = tokens.index("..")
del tokens[max(idx - 1, 0):idx + 1]
if (tokens is None) or (len(tokens) == 0):
return cls.PATH_SEPARATOR
return cls.join(tokens)
@property
def relative_path(this) -> str:
path = this.absolute_path
if (this.root_is_parent_of(path)):
relpath = path[len(this.root):]
if (len(relpath) == 0):
relpath = "."
elif relpath[0] == this.PATH_SEPARATOR:
relpath = relpath[1:]
return relpath
raise PathOutsideRootException(this.root, this.absolute_path)
class NTAbstractPath(AbstractPath):
"""
Extends the Abstract path for Windows-like path management
Some of the functionality needed for this class are in the parent class.
It's required to adapt a few things to make it work with NT paths
"""
@classmethod
def get_volume(cls, path: str) -> [str | None]:
"""
Gets the volume from the path
:param path: The path from where to get the volume of the drive
:return: the volume of where the path is rooted, None otherwise (thing of certain relative paths)
"""
path = path.strip()
if (path.find(cls.VOLUME_SEPARATOR) > 0):
volume = path.split(cls.VOLUME_SEPARATOR)[0] + cls.VOLUME_SEPARATOR
return volume if len(volume) > 0 else None
return None
@classmethod
def strip_volume(cls, path: str) -> str:
"""
Removes the volume from the given path
:param path: The path to remove the volume from
:return: A new path without the volume
"""
vol = cls.get_volume(path)
return path.lstrip(vol)
@classmethod
def is_absolute(cls, path):
path = cls.strip_volume(path)
return super(NTAbstractPath, NTAbstractPath).is_absolute(path)
@classmethod
def normalise(cls, path):
path = super(PosixAbstractPath, PosixAbstractPath).normalise(path)
tokens = cls.split(path)
tokens[1:] = [t for t in tokens[1:] if t != "."]
vol = cls.get_volume(path)
min_idx = 1 if (vol is not None) and tokens[0].startswith(vol) else 0
while ".." in tokens:
idx = tokens.index("..")
del tokens[max(idx - 1, min_idx):idx + 1]
return cls.join(tokens)
@classmethod
def split(cls, path):
vol = cls.get_volume(path)
tokens = super(NTAbstractPath, NTAbstractPath).split(path)
if (vol is not None) and (vol.lower() == tokens[0].lower()):
tokens[0] += cls.PATH_SEPARATOR
return tokens
@property
def relative_path(this):
path = this.absolute_path
if (this.root_is_parent_of(path)):
path = path[len(this.root):]
if (len(path) == 0):
return "."
if (path[0] == this.PATH_SEPARATOR):
path = path[1:]
return path
else:
raise PathOutsideRootException(this.root, this.absolute_path)
def cd(this, path):
if path.startswith("/"):
path = this.join(this.root, path)
super().cd(path)
class FileSystem(ABC):
"""
This object represents an abstract file system
It contains some useful functionality (eg caching) for the inherited classes
"""
def __init__(this, path: str, *,
path_manager: Type[AbstractPath],
cached: bool = False):
"""
:param path: The root path of the file system
:param path_manager: Path convention to use (POSIX- or NT-like)
:param cached: Whether to cache content or not
"""
this._path = path_manager(path)
# Directory tree cache
this._tree_cache: Any = []
# File System Object cache
this._file_objects_cache: Dict[str, FileSystemObject] = {}
this._previous_file_objects_cache: Dict[str, FileSystemObject] = {}
# The path manager is a concerete subtype of AbstractPath that is specialised in managing paths in
# specific environments/cases (e.g., POSIXPaths)
this._path_manager = path_manager
this._cached = cached
this._cache = dict()
async def _find_dir_in_cache(this, dir: str) -> Union[Any | None]:
"""
Finds a directory and its content in the cache
:param dir: directory to search in the cache
:return: An iterable if the directory exists, None otherwise
"""
dir_to_search = this.new_path(dir, root=this.base_path).relative_path
for itm in this._cache:
remote_path = "/" + itm['Path']
if (remote_path == dir) or (remote_path == dir_to_search):
return itm
return None
async def ls(this, path: Union[str | None] = None) -> Iterable[FileSystemObject]:
"""
Returns the content of the path. If path is not provided, returns the content of the current working directory
:param path: The path where to list its content. None will return the content of the cwd
:return: An iterable of FileSystemObjects representing the content of the path
"""
cp = this.current_path if path is None else path
cp = this.new_path(cp, root=this.base_path)
content = [await this._make_filesystem_object(x, cp.relative_path) for x in (await this._dir(cp))]
return content
async def _make_filesystem_object(this, dic: dict, path: str) -> FileSystemObject:
type = FileType.DIR if dic['IsDir'] else FileType.REGULAR
fullpath = this.new_path(AbstractPath.join(path, dic['Name']), root=this.root)
if (this.cached):
cached_fso = this._get_fso_from_cache(fullpath)
if (cached_fso is not None):
await cached_fso.update_information()
return cached_fso
mod_time = _fix_isotime(dic['ModTime']) # fixing mega.nz bug
fso = FileSystemObject(fullpath,
type=type,
size=dic['Size'],
mtime=datetime.fromisoformat(mod_time),
exists=True)
if this.cached:
this.set_file(fullpath, fso)
return fso
async def _dir(this, path:AbstractPath) -> List[Dict]:
"""
Internal method listing responsable of listing all the content in a directory.
The search is performed in the cache (if present). Otherwise, it'll be performed in the actual (local/remote)
filesystem
:param path: path to list the content
:return: A list of dictonaries representing the json result from rclone
"""
dir = []
items = this._cache if this.cached else (await rclone_instance().ls(path.root, path.relative_path))
relpath = path.relative_path
if (relpath == "."): relpath = ""
for itm in items:
p, tail = os.path.split(itm['Path'])
if (p == relpath):
dir.append(itm)
return dir
async def exists(this, filename) -> bool:
"""
Checks if a file or directory exists
:param filename: File or directory name to check its existance
:return: TRUE if exists, FALSE otherwise
"""
p = this.visit(filename)
return await rclone_instance().exists(p.root,p.relative_path)
async def get_file(this, path: AbstractPath) -> FileSystemObject:
"""
Returns a FileSystemObject from path. The FileSystemObject contains useful information about the file/directory
:param path: The path to get information from
:return: A FileSystemObject of representing path
"""
p, name = os.path.split(path.relative_path)
parent_path = this.new_path(p)
content = await this._dir(parent_path)
for itm in content:
if itm['Name'] == name:
return await this._make_filesystem_object(itm, parent_path.absolute_path)
raise FileNotFoundError(f"No such file or directory: '{path}'")
def set_file(this, path: AbstractPath, fo: Union[FileSystemObject | None]) -> None:
"""
Sets updated information of a specific file system object
:param path: Path of the file/directory
:param fo: Updated information. If None, the entry in the cache will be removed
"""
if not AbstractPath.is_root_of(path.absolute_path, this.root):
raise ValueError(f"{fo.absolute_path} is not rooted in this file system ({this.root})")
p = path.relative_path
if fo is None:
if p in this._file_objects_cache.keys():
del this._file_objects_cache[p]
else:
this._file_objects_cache[p] = fo
@property
def cached(this) -> bool:
"""
:return: Return TRUE if cache is used. FALSE otherwise
"""
return this._cached
@cached.setter
def cached(this, value: bool) -> None:
"""
Change whether cache needs to be used
:param value: TRUE if cache needs to be used. FALSE otherwise
"""
this._cached = value
@property
def base_path(this) -> str:
"""
Gives the basepath of the file system
:return: A string representing the bases path of the file system
"""
return this._path.root
@property
def root(this) -> str:
"""
Alias for base_path property method
"""
return this.base_path
@property
def current_path(this) -> str:
"""
Gives the absolute path of the current working directory
:return: A string representing the current working directory path
"""
return this._path.absolute_path
@property
def cwd(this) -> str:
"""
Gets the current working directory (that can be different than the root if `cd` method has been used)
:return: A string representing the current working directory
"""
return this.current_path
def __str__(this) -> str:
return this.current_path
def __repr__(this) -> str:
return str(this)
def _get_fso_from_cache(this, path: AbstractPath) -> Union[FileSystemObject | None]:
"""
Retrieve a file system object from the file object cache of the object
:param path: Path of the file/directory
:return: A file system object of the provided path, None if not found
"""
p = path.relative_path