rclone-api 1.1.1__py2.py3-none-any.whl → 1.1.3__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,66 +1,9 @@
1
- from dataclasses import dataclass, fields, is_dataclass
2
- from typing import Type, TypeVar
1
+ from dataclasses import dataclass
3
2
 
4
- T = TypeVar("T")
5
-
6
-
7
- def _merge(cls: Type[T], dataclass_a: T, dataclass_b: T) -> T:
8
- if not is_dataclass(dataclass_a) or not is_dataclass(dataclass_b):
9
- raise ValueError("Both inputs must be dataclass instances")
10
- if type(dataclass_a) is not type(dataclass_b):
11
- raise ValueError("Dataclass instances must be of the same type")
12
-
13
- merged_kwargs = {}
14
- for field in fields(dataclass_a):
15
- a_value = getattr(dataclass_a, field.name)
16
- b_value = getattr(dataclass_b, field.name)
17
-
18
- if is_dataclass(a_value) and is_dataclass(b_value):
19
- merged_kwargs[field.name] = _merge(type(a_value), a_value, b_value)
20
- else:
21
- merged_kwargs[field.name] = b_value if b_value is not None else a_value
22
-
23
- return cls(**merged_kwargs)
24
-
25
-
26
- def _field_name_to_flag(field_name: str) -> str:
27
- return f"--{field_name.replace('_', '-')}"
3
+ from rclone_api.experimental.flags_base import BaseFlags, merge_flags
28
4
 
29
5
 
30
6
  @dataclass
31
- class BaseFlags:
32
- def to_args(self) -> list[str]:
33
- args = []
34
- for field in fields(self):
35
- value = getattr(self, field.name)
36
- if value is None:
37
- continue
38
- # If the field value is a nested dataclass that supports to_args, use it.
39
- if is_dataclass(value) and hasattr(value, "to_args"):
40
- to_args = getattr(value, "to_args")
41
- args.extend(to_args())
42
- elif isinstance(value, bool):
43
- # Only include the flag if the boolean is True.
44
- if value:
45
- args.append(_field_name_to_flag(field.name))
46
- else:
47
- args.append(_field_name_to_flag(field.name))
48
- if isinstance(value, list):
49
- # Join list values with a comma.
50
- args.append(",".join(map(str, value)))
51
- else:
52
- args.append(str(value))
53
- return args
54
-
55
- def merge(self, other: "BaseFlags") -> "BaseFlags":
56
- # Use the type of self, so merging CopyFlags returns a CopyFlags instance.
57
- return _merge(type(self), self, other)
58
-
59
- def __repr__(self):
60
- return str(self.to_args())
61
-
62
-
63
- @dataclass(repr=False)
64
7
  class CopyFlags(BaseFlags):
65
8
  check_first: bool | None = None
66
9
  checksum: bool | None = False
@@ -96,11 +39,29 @@ class CopyFlags(BaseFlags):
96
39
  streaming_upload_cutoff: str | None = None
97
40
  update: bool | None = None
98
41
 
42
+ def to_args(self) -> list[str]:
43
+ return super().to_args()
44
+
45
+ def merge(self, other: "CopyFlags") -> "CopyFlags":
46
+ return merge_flags(CopyFlags, self, other)
47
+
48
+ def __repr__(self):
49
+ return super().__repr__()
50
+
99
51
 
100
- @dataclass(repr=False)
52
+ @dataclass
101
53
  class Flags(BaseFlags):
102
54
  copy: CopyFlags | None = None
103
55
 
56
+ def to_args(self) -> list[str]:
57
+ return super().to_args()
58
+
59
+ def merge(self, other: "Flags") -> "Flags":
60
+ return merge_flags(Flags, self, other)
61
+
62
+ def __repr__(self):
63
+ return super().__repr__()
64
+
104
65
 
105
66
  def unit_test() -> None:
106
67
  copy_flags_a = CopyFlags(compare_dest=["a", "b"])
@@ -0,0 +1,58 @@
1
+ from dataclasses import dataclass, fields, is_dataclass
2
+ from typing import Type, TypeVar
3
+
4
+ T = TypeVar("T")
5
+
6
+
7
+ def merge_flags(cls: Type[T], dataclass_a: T, dataclass_b: T) -> T:
8
+ if not is_dataclass(dataclass_a) or not is_dataclass(dataclass_b):
9
+ raise ValueError("Both inputs must be dataclass instances")
10
+ if type(dataclass_a) is not type(dataclass_b):
11
+ raise ValueError("Dataclass instances must be of the same type")
12
+
13
+ merged_kwargs = {}
14
+ for field in fields(dataclass_a):
15
+ a_value = getattr(dataclass_a, field.name)
16
+ b_value = getattr(dataclass_b, field.name)
17
+
18
+ if is_dataclass(a_value) and is_dataclass(b_value):
19
+ merged_kwargs[field.name] = merge_flags(type(a_value), a_value, b_value)
20
+ else:
21
+ merged_kwargs[field.name] = b_value if b_value is not None else a_value
22
+
23
+ return cls(**merged_kwargs)
24
+
25
+
26
+ def _field_name_to_flag(field_name: str) -> str:
27
+ return f"--{field_name.replace('_', '-')}"
28
+
29
+
30
+ @dataclass
31
+ class BaseFlags:
32
+ """provides to_args(), merge() and __repr__ methods for flags dataclasses"""
33
+
34
+ def to_args(self) -> list[str]:
35
+ args = []
36
+ for field in fields(self):
37
+ value = getattr(self, field.name)
38
+ if value is None:
39
+ continue
40
+ # If the field value is a nested dataclass that supports to_args, use it.
41
+ if is_dataclass(value) and hasattr(value, "to_args"):
42
+ to_args = getattr(value, "to_args")
43
+ args.extend(to_args())
44
+ elif isinstance(value, bool):
45
+ # Only include the flag if the boolean is True.
46
+ if value:
47
+ args.append(_field_name_to_flag(field.name))
48
+ else:
49
+ args.append(_field_name_to_flag(field.name))
50
+ if isinstance(value, list):
51
+ # Join list values with a comma.
52
+ args.append(",".join(map(str, value)))
53
+ else:
54
+ args.append(str(value))
55
+ return args
56
+
57
+ def __repr__(self):
58
+ return str(self.to_args())
@@ -127,6 +127,13 @@ class UploadState:
127
127
  lock: Lock = Lock()
128
128
  parts: list[FinishedPiece | None] = field(default_factory=list)
129
129
 
130
+ def update_source_file(self, src_file: Path) -> None:
131
+ new_file_size = os.path.getsize(src_file)
132
+ if new_file_size != self.upload_info.file_size:
133
+ raise ValueError("File size changed, cannot resume")
134
+ self.upload_info.src_file_path = src_file
135
+ self.save()
136
+
130
137
  def is_done(self) -> bool:
131
138
  return self.remaining() == 0
132
139
 
@@ -264,15 +271,22 @@ def _get_chunk_tmpdir() -> Path:
264
271
  def _get_file_size(file_path: Path, timeout: int = 60) -> int:
265
272
  sleep_time = timeout / 60 if timeout > 0 else 1
266
273
  start = time.time()
274
+ not_windows = os.name != "nt"
267
275
  while True:
276
+ expired = time.time() - start > timeout
268
277
  try:
278
+ time.sleep(sleep_time)
279
+ if not_windows:
280
+ # Force a refresh of the directory cache
281
+ os.system(f"ls -l {file_path.parent}")
269
282
  if file_path.exists():
270
283
  return file_path.stat().st_size
271
- except FileNotFoundError:
272
- pass
273
- if time.time() - start > timeout:
284
+ except FileNotFoundError as e:
285
+ if expired:
286
+ print(f"File not found: {file_path}, exception is {e}")
287
+ raise
288
+ if expired:
274
289
  raise TimeoutError(f"File {file_path} not found after {timeout} seconds")
275
- time.sleep(sleep_time)
276
290
 
277
291
 
278
292
  def file_chunker(
@@ -426,6 +440,18 @@ def prepare_upload_file_multipart(
426
440
  return upload_info
427
441
 
428
442
 
443
+ def _abort_previous_upload(upload_state: UploadState) -> None:
444
+ if upload_state.upload_info.upload_id:
445
+ try:
446
+ upload_state.upload_info.s3_client.abort_multipart_upload(
447
+ Bucket=upload_state.upload_info.bucket_name,
448
+ Key=upload_state.upload_info.object_name,
449
+ UploadId=upload_state.upload_info.upload_id,
450
+ )
451
+ except Exception as e:
452
+ locked_print(f"Error aborting previous upload: {e}")
453
+
454
+
429
455
  def upload_file_multipart(
430
456
  s3_client: BaseClient,
431
457
  bucket_name: str,
@@ -481,6 +507,13 @@ def upload_file_multipart(
481
507
 
482
508
  filechunks: Queue[FileChunk | None] = Queue(10)
483
509
  upload_state = get_upload_state() or make_new_state()
510
+ try:
511
+ upload_state.update_source_file(file_path)
512
+ except ValueError as e:
513
+ locked_print(f"Cannot resume upload: {e}, size changed, starting over")
514
+ _abort_previous_upload(upload_state)
515
+ upload_state = make_new_state()
516
+ upload_state.save()
484
517
  if upload_state.is_done():
485
518
  return MultiUploadResult.ALREADY_DONE
486
519
  finished = upload_state.finished()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: rclone_api
3
- Version: 1.1.1
3
+ Version: 1.1.3
4
4
  Summary: rclone api in python
5
5
  Home-page: https://github.com/zackees/rclone-api
6
6
  License: BSD 3-Clause License
@@ -22,15 +22,16 @@ rclone_api/walk.py,sha256=-54NVE8EJcCstwDoaC_UtHm73R2HrZwVwQmsnv55xNU,3369
22
22
  rclone_api/assets/example.txt,sha256=lTBovRjiz0_TgtAtbA1C5hNi2ffbqnNPqkKg6UiKCT8,54
23
23
  rclone_api/cmd/copy_large_s3.py,sha256=33KFvCrh5uk-rdRtkREdEs2WNwxGgTdCAWDLCE4dm0A,2855
24
24
  rclone_api/cmd/list_files.py,sha256=x8FHODEilwKqwdiU1jdkeJbLwOqUkUQuDWPo2u_zpf0,741
25
- rclone_api/experimental/flags.py,sha256=AHbTaFHuyYFm3pjdvbQ100jztOXOdNuxalMr8UjXnV4,4097
25
+ rclone_api/experimental/flags.py,sha256=0-mtXg9J4MoMm2uBKbsMLj4pSGRLQUAqNRDJWGttnAQ,2443
26
+ rclone_api/experimental/flags_base.py,sha256=ajU_czkTcAxXYU-SlmiCfHY7aCQGHvpCLqJ-Z8uZLk0,2102
26
27
  rclone_api/s3/api.py,sha256=VstlaEnBjO2JDQuCRLdTfUGvQLbfshlXXhAzimFv4Vc,3763
27
28
  rclone_api/s3/basic_ops.py,sha256=hK3366xhVEzEcjz9Gk_8lFx6MRceAk72cax6mUrr6ko,2104
28
- rclone_api/s3/chunk_uploader.py,sha256=JpVuCzr2Ox0bjXAyFCqQX2ctJCAId-Ttkr4E7HyWJRc,18537
29
+ rclone_api/s3/chunk_uploader.py,sha256=sKV466i09yQ38aB5h-77Alk4nUFmUNxflcChch5QEOM,19910
29
30
  rclone_api/s3/create.py,sha256=SK3IGHZwsSkoG4Zb4NCphcVg9_f7VifDKng-tExMS2s,3088
30
31
  rclone_api/s3/types.py,sha256=81_3jwg6MGIxC-GxL-6zANzKO6au9C0BWvAqRyODxOM,1361
31
- rclone_api-1.1.1.dist-info/LICENSE,sha256=b6pOoifSXiUaz_lDS84vWlG3fr4yUKwB8fzkrH9R8bQ,1064
32
- rclone_api-1.1.1.dist-info/METADATA,sha256=mGHPhrp3mLNT_R8N1BjY8PTChqNViAAaaJYK8DNReHE,4478
33
- rclone_api-1.1.1.dist-info/WHEEL,sha256=rF4EZyR2XVS6irmOHQIJx2SUqXLZKRMUrjsg8UwN-XQ,109
34
- rclone_api-1.1.1.dist-info/entry_points.txt,sha256=6eNqTRXKhVf8CpWNjXiOa_0Du9tHiW_HD2iQSXRsUg8,132
35
- rclone_api-1.1.1.dist-info/top_level.txt,sha256=EvZ7uuruUpe9RiUyEp25d1Keq7PWYNT0O_-mr8FCG5g,11
36
- rclone_api-1.1.1.dist-info/RECORD,,
32
+ rclone_api-1.1.3.dist-info/LICENSE,sha256=b6pOoifSXiUaz_lDS84vWlG3fr4yUKwB8fzkrH9R8bQ,1064
33
+ rclone_api-1.1.3.dist-info/METADATA,sha256=xKOlLkdCn0pGye8Iw1Q3-u2eU5Av101fj8lbSVrjH0s,4478
34
+ rclone_api-1.1.3.dist-info/WHEEL,sha256=rF4EZyR2XVS6irmOHQIJx2SUqXLZKRMUrjsg8UwN-XQ,109
35
+ rclone_api-1.1.3.dist-info/entry_points.txt,sha256=6eNqTRXKhVf8CpWNjXiOa_0Du9tHiW_HD2iQSXRsUg8,132
36
+ rclone_api-1.1.3.dist-info/top_level.txt,sha256=EvZ7uuruUpe9RiUyEp25d1Keq7PWYNT0O_-mr8FCG5g,11
37
+ rclone_api-1.1.3.dist-info/RECORD,,