rclone-api 1.3.14__py2.py3-none-any.whl → 1.3.17__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.
rclone_api/db/db.py CHANGED
@@ -178,7 +178,7 @@ class DBRepo:
178
178
  "size": file.size,
179
179
  "mime_type": file.mime_type,
180
180
  "mod_time": file.mod_time,
181
- "suffix": file.suffix,
181
+ "suffix": file.real_suffix,
182
182
  }
183
183
  for file in is_new
184
184
  ]
@@ -213,7 +213,7 @@ class DBRepo:
213
213
  "size": file.size,
214
214
  "mime_type": file.mime_type,
215
215
  "mod_time": file.mod_time,
216
- "suffix": file.suffix,
216
+ "suffix": file.real_suffix,
217
217
  }
218
218
  )
219
219
  if update_values:
rclone_api/file.py CHANGED
@@ -12,6 +12,56 @@ def _intern(s: str) -> str:
12
12
  return _STRING_INTERNER.setdefault(s, s)
13
13
 
14
14
 
15
+ _SUFFIX_LARGEST_SIZE = len("torrents") + 2
16
+
17
+
18
+ def _suffix_clean_bad_parts(suffix: list[str]) -> list[str]:
19
+ """Remove any bad parts from the suffix list."""
20
+ out = []
21
+ for part in suffix:
22
+ if part in ["", ""]:
23
+ continue
24
+ if " " in part:
25
+ # split on spaces
26
+ continue
27
+ if "--" in part:
28
+ # split on --
29
+ parts = part.split("--")
30
+ parts = [x.strip() for x in parts if x.strip()]
31
+ out.extend(parts)
32
+ out.append(part)
33
+
34
+ out, tmp = [], out
35
+ for part in tmp:
36
+ if len(part) > _SUFFIX_LARGEST_SIZE:
37
+ continue
38
+ out.append(part)
39
+ return out
40
+
41
+
42
+ def _get_suffix(name: str, chop_compressed_suffixes: bool = True) -> str:
43
+ # name.sql.gz -> sql.gz
44
+ try:
45
+ parts = name.split(".")
46
+ if len(parts) == 1:
47
+ return ""
48
+ parts = _suffix_clean_bad_parts(parts)
49
+ last_part = parts[-1]
50
+ if chop_compressed_suffixes:
51
+ if last_part == "gz" and len(parts) > 2:
52
+ parts = parts[:-1]
53
+ if len(parts) > 2:
54
+ # grab the last two parts
55
+ return ".".join(parts[-2:])
56
+ return ".".join(parts[1:])
57
+ except IndexError:
58
+ warnings.warn(f"Invalid name: {name} for normal suffix extraction")
59
+ suffix = Path(name).suffix
60
+ if suffix.startswith("."):
61
+ return suffix[1:]
62
+ return suffix
63
+
64
+
15
65
  # File is too complex, this is a simple dataclass that can be streamed out.
16
66
  @dataclass
17
67
  class FileItem:
@@ -40,14 +90,14 @@ class FileItem:
40
90
  return f"{self.parent}/{self.name}"
41
91
 
42
92
  @property
43
- def suffix(self) -> str:
93
+ def real_suffix(self) -> str:
44
94
  return self._suffix
45
95
 
46
96
  def __post_init__(self):
47
97
  self.parent = _intern(self.parent)
48
98
  self.mime_type = _intern(self.mime_type)
49
99
  self.remote = _intern(self.remote)
50
- self._suffix = _intern(Path(self.name).suffix)
100
+ self._suffix = _intern(_get_suffix(self.name))
51
101
 
52
102
  @staticmethod
53
103
  def from_json(remote: str, data: dict) -> "FileItem | None":