hfutils 0.6.0__tar.gz → 0.7.0__tar.gz

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.
Files changed (82) hide show
  1. {hfutils-0.6.0/hfutils.egg-info → hfutils-0.7.0}/PKG-INFO +1 -1
  2. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/config/meta.py +1 -1
  3. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/repository/__init__.py +1 -0
  4. hfutils-0.7.0/hfutils/repository/size.py +254 -0
  5. {hfutils-0.6.0 → hfutils-0.7.0/hfutils.egg-info}/PKG-INFO +1 -1
  6. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils.egg-info/SOURCES.txt +1 -0
  7. {hfutils-0.6.0 → hfutils-0.7.0}/LICENSE +0 -0
  8. {hfutils-0.6.0 → hfutils-0.7.0}/MANIFEST.in +0 -0
  9. {hfutils-0.6.0 → hfutils-0.7.0}/README.md +0 -0
  10. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/__init__.py +0 -0
  11. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/__main__.py +0 -0
  12. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/archive/__init__.py +0 -0
  13. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/archive/base.py +0 -0
  14. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/archive/rar.py +0 -0
  15. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/archive/sevenz.py +0 -0
  16. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/archive/tar.py +0 -0
  17. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/archive/zip.py +0 -0
  18. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/cache/__init__.py +0 -0
  19. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/cache/delete.py +0 -0
  20. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/config/__init__.py +0 -0
  21. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/entry/__init__.py +0 -0
  22. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/entry/base.py +0 -0
  23. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/entry/cli.py +0 -0
  24. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/entry/clone.py +0 -0
  25. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/entry/dispatch.py +0 -0
  26. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/entry/download.py +0 -0
  27. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/entry/ils.py +0 -0
  28. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/entry/index.py +0 -0
  29. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/entry/ls.py +0 -0
  30. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/entry/ls_repo.py +0 -0
  31. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/entry/rollback.py +0 -0
  32. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/entry/squash.py +0 -0
  33. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/entry/tree.py +0 -0
  34. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/entry/upload.py +0 -0
  35. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/entry/whoami.py +0 -0
  36. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/index/__init__.py +0 -0
  37. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/index/fetch.py +0 -0
  38. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/index/hash.py +0 -0
  39. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/index/local_fetch.py +0 -0
  40. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/index/make.py +0 -0
  41. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/index/validate.py +0 -0
  42. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/operate/__init__.py +0 -0
  43. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/operate/base.py +0 -0
  44. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/operate/download.py +0 -0
  45. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/operate/duplicate.py +0 -0
  46. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/operate/upload.py +0 -0
  47. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/operate/validate.py +0 -0
  48. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/repository/base.py +0 -0
  49. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/repository/clone.py +0 -0
  50. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/repository/rollback.py +0 -0
  51. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/repository/url.py +0 -0
  52. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/__init__.py +0 -0
  53. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/archive.py +0 -0
  54. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/arrange.py +0 -0
  55. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/binary.py +0 -0
  56. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/data.py +0 -0
  57. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/download.py +0 -0
  58. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/ext.py +0 -0
  59. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/heap.py +0 -0
  60. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/irregular_repo.json +0 -0
  61. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/logging.py +0 -0
  62. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/model.py +0 -0
  63. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/number.py +0 -0
  64. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/path.py +0 -0
  65. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/session.py +0 -0
  66. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/temp.py +0 -0
  67. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/tqdm_.py +0 -0
  68. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/type_.py +0 -0
  69. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils/utils/walk.py +0 -0
  70. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils.egg-info/dependency_links.txt +0 -0
  71. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils.egg-info/entry_points.txt +0 -0
  72. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils.egg-info/requires.txt +0 -0
  73. {hfutils-0.6.0 → hfutils-0.7.0}/hfutils.egg-info/top_level.txt +0 -0
  74. {hfutils-0.6.0 → hfutils-0.7.0}/requirements-7z.txt +0 -0
  75. {hfutils-0.6.0 → hfutils-0.7.0}/requirements-build.txt +0 -0
  76. {hfutils-0.6.0 → hfutils-0.7.0}/requirements-doc.txt +0 -0
  77. {hfutils-0.6.0 → hfutils-0.7.0}/requirements-rar.txt +0 -0
  78. {hfutils-0.6.0 → hfutils-0.7.0}/requirements-test.txt +0 -0
  79. {hfutils-0.6.0 → hfutils-0.7.0}/requirements-transfer.txt +0 -0
  80. {hfutils-0.6.0 → hfutils-0.7.0}/requirements.txt +0 -0
  81. {hfutils-0.6.0 → hfutils-0.7.0}/setup.cfg +0 -0
  82. {hfutils-0.6.0 → hfutils-0.7.0}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: hfutils
3
- Version: 0.6.0
3
+ Version: 0.7.0
4
4
  Summary: Useful utilities for huggingface
5
5
  Home-page: https://github.com/deepghs/hfutils
6
6
  Author: narugo1992
@@ -7,7 +7,7 @@ Overview:
7
7
  __TITLE__ = 'hfutils'
8
8
 
9
9
  #: Version of this project.
10
- __VERSION__ = '0.6.0'
10
+ __VERSION__ = '0.7.0'
11
11
 
12
12
  #: Short description of the project, will be included in ``setup.py``.
13
13
  __DESCRIPTION__ = 'Useful utilities for huggingface'
@@ -5,4 +5,5 @@ Overview:
5
5
  from .base import hf_hub_repo_url
6
6
  from .clone import hf_hub_clone
7
7
  from .rollback import hf_hub_rollback
8
+ from .size import RepoFileItem, RepoFileList, hf_hub_repo_analysis
8
9
  from .url import hf_hub_repo_path_url, hf_hub_repo_file_url
@@ -0,0 +1,254 @@
1
+ """
2
+ This module provides functionality for analyzing and managing Hugging Face repository files.
3
+
4
+ It includes classes and functions for representing repository files, creating file lists,
5
+ and analyzing repository contents. The module is designed to work with the Hugging Face
6
+ Hub API and provides utilities for handling file paths, sizes, and other metadata.
7
+
8
+ Key components:
9
+ - RepoFileItem: Represents a single file in a repository.
10
+ - RepoFileList: A sequence of RepoFileItems with additional metadata and utility methods.
11
+ - hf_hub_repo_analysis: Function to analyze repository contents based on given criteria.
12
+
13
+ This module is particularly useful for developers working with Hugging Face repositories
14
+ and need to analyze or manage file structures and metadata.
15
+ """
16
+
17
+ import os.path
18
+ from collections.abc import Sequence
19
+ from dataclasses import dataclass
20
+ from operator import itemgetter
21
+ from typing import Optional, List, Literal, Tuple
22
+
23
+ from hbutils.scale import size_to_bytes_str
24
+ from hbutils.string import plural_word, format_tree
25
+ from huggingface_hub.hf_api import RepoFile
26
+
27
+ from ..operate import list_all_with_pattern
28
+ from ..operate.base import RepoTypeTyping
29
+ from ..utils import hf_normpath, hf_fs_path
30
+
31
+
32
+ @dataclass
33
+ class RepoFileItem:
34
+ """
35
+ Represents a file item in a Hugging Face repository.
36
+
37
+ This class encapsulates metadata about a single file, including its path,
38
+ size, LFS status, and blob ID.
39
+
40
+ :param path: The file path relative to the repository root.
41
+ :param size: The size of the file in bytes.
42
+ :param is_lfs: Whether the file is stored using Git LFS.
43
+ :param lfs_sha256: The SHA256 hash of the LFS file, if applicable.
44
+ :param blob_id: The Git blob ID of the file.
45
+ """
46
+
47
+ path: str
48
+ size: int
49
+ is_lfs: bool
50
+ lfs_sha256: Optional[str]
51
+ blob_id: str
52
+
53
+ @classmethod
54
+ def from_repo_file(cls, repo_file: RepoFile, subdir: str = '') -> 'RepoFileItem':
55
+ """
56
+ Create a RepoFileItem from a RepoFile object.
57
+
58
+ :param repo_file: The RepoFile object to convert.
59
+ :param subdir: The subdirectory to use as the base path (default: '').
60
+ :return: A new RepoFileItem instance.
61
+ """
62
+ subdir = subdir or '.'
63
+ return cls(
64
+ path=hf_normpath(os.path.relpath(repo_file.path, subdir)),
65
+ size=repo_file.lfs.size if repo_file.lfs else repo_file.size,
66
+ is_lfs=bool(repo_file.lfs),
67
+ lfs_sha256=repo_file.lfs.sha256 if repo_file.lfs else None,
68
+ blob_id=repo_file.blob_id,
69
+ )
70
+
71
+ @property
72
+ def path_segments(self) -> Tuple[str, ...]:
73
+ """
74
+ Get the path segments of the file.
75
+
76
+ :return: A tuple of path segments.
77
+ """
78
+ return tuple(seg for seg in self.path.split('/') if seg and seg != '.')
79
+
80
+ def __repr__(self):
81
+ """
82
+ Return a string representation of the RepoFileItem.
83
+
84
+ :return: A formatted string representation.
85
+ """
86
+ return (f'<{self.__class__.__name__} {self.path}, size: {size_to_bytes_str(self.size, sigfigs=3, system="si")}'
87
+ f'{" (LFS)" if self.is_lfs else ""}>')
88
+
89
+
90
+ class RepoFileList(Sequence):
91
+ """
92
+ Represents a list of RepoFileItems with additional metadata and utility methods.
93
+
94
+ This class provides a way to manage and analyze a collection of files from a
95
+ Hugging Face repository, including information about the repository itself.
96
+
97
+ :param repo_id: The ID of the repository.
98
+ :param items: A list of RepoFileItem objects.
99
+ :param repo_type: The type of the repository (default: 'dataset').
100
+ :param revision: The revision of the repository (default: 'main').
101
+ :param subdir: The subdirectory within the repository (default: '').
102
+ """
103
+
104
+ def __init__(self, repo_id: str, items: List[RepoFileItem],
105
+ repo_type: RepoTypeTyping = 'dataset', revision: str = 'main', subdir: Optional[str] = ''):
106
+ self.repo_id = repo_id
107
+ self.repo_type = repo_type
108
+ self.revision = revision
109
+ self.subdir = hf_normpath(subdir or '.')
110
+ self._file_items = list(items)
111
+ self._total_size = 0
112
+ for item in self._file_items:
113
+ self._total_size += item.size
114
+
115
+ def __getitem__(self, index):
116
+ """
117
+ Get a RepoFileItem by index.
118
+
119
+ :param index: The index of the item to retrieve.
120
+ :return: The RepoFileItem at the specified index.
121
+ """
122
+ return self._file_items[index]
123
+
124
+ def __len__(self) -> int:
125
+ """
126
+ Get the number of items in the list.
127
+
128
+ :return: The number of RepoFileItems in the list.
129
+ """
130
+ return len(self._file_items)
131
+
132
+ @property
133
+ def total_size(self) -> int:
134
+ """
135
+ Get the total size of all files in the list.
136
+
137
+ :return: The total size in bytes.
138
+ """
139
+ return self._total_size
140
+
141
+ def _tree(self, max_items: Optional[int] = 10):
142
+ """
143
+ Generate a tree representation of the file list.
144
+
145
+ :param max_items: The maximum number of items to include in the tree (default: 10).
146
+ :return: A tuple containing the tree title and list of tree nodes.
147
+ """
148
+ title = hf_fs_path(
149
+ repo_id=self.repo_id,
150
+ repo_type=self.repo_type,
151
+ revision=self.revision,
152
+ filename=self.subdir,
153
+ )
154
+ return (
155
+ f'{title} ({plural_word(len(self._file_items), "file")}, '
156
+ f'{size_to_bytes_str(self._total_size, sigfigs=3, system="si")})',
157
+ [
158
+ *(
159
+ (repr(item), []) for item in
160
+ (self._file_items[:max_items] if max_items and max_items > 0 else self._file_items)
161
+ ),
162
+ *(
163
+ [
164
+ (f'... ({plural_word(len(self._file_items), "file")}) in total ...', [])
165
+ ] if max_items and 0 < max_items < len(self._file_items) else []
166
+ )
167
+ ]
168
+ )
169
+
170
+ def __repr__(self):
171
+ """
172
+ Return a string representation of the RepoFileList.
173
+
174
+ :return: A formatted string representation of the file list.
175
+ """
176
+ return format_tree(
177
+ self._tree(),
178
+ format_node=itemgetter(0),
179
+ get_children=itemgetter(1),
180
+ )
181
+
182
+ def repr(self, max_items: Optional[int] = 10):
183
+ """
184
+ Generate a custom string representation of the RepoFileList.
185
+
186
+ :param max_items: The maximum number of items to include in the representation (default: 10).
187
+ :return: A formatted string representation of the file list.
188
+ """
189
+ return format_tree(
190
+ self._tree(max_items=max_items),
191
+ format_node=itemgetter(0),
192
+ get_children=itemgetter(1),
193
+ )
194
+
195
+
196
+ SortByTyping = Literal['none', 'path', 'size']
197
+
198
+
199
+ def hf_hub_repo_analysis(
200
+ repo_id: str, pattern: str = '**/*', repo_type: RepoTypeTyping = 'dataset',
201
+ revision: str = 'main', hf_token: Optional[str] = None, silent: bool = False,
202
+ subdir: str = '', sort_by: SortByTyping = 'path', **kwargs,
203
+ ) -> RepoFileList:
204
+ """
205
+ Analyze the contents of a Hugging Face repository.
206
+
207
+ This function retrieves file information from a specified repository and creates
208
+ a RepoFileList object containing detailed information about each file.
209
+
210
+ :param repo_id: The ID of the repository to analyze.
211
+ :param pattern: A glob pattern to filter files (default: '**/*').
212
+ :param repo_type: The type of the repository (default: 'dataset').
213
+ :param revision: The revision of the repository to analyze (default: 'main').
214
+ :param hf_token: The Hugging Face API token (optional).
215
+ :param silent: Whether to suppress output (default: False).
216
+ :param subdir: The subdirectory within the repository to analyze (default: '').
217
+ :param sort_by: How to sort the file list ('none', 'path', or 'size') (default: 'path').
218
+ :param kwargs: Additional keyword arguments to pass to list_all_with_pattern.
219
+
220
+ :return: A RepoFileList object containing the analysis results.
221
+
222
+ :raises: May raise exceptions related to API access or file operations.
223
+
224
+ Usage:
225
+ >>> result = hf_hub_repo_analysis('username/repo', pattern='*.txt', repo_type='model')
226
+ >>> print(result)
227
+ """
228
+ if subdir and subdir != '.':
229
+ pattern = f'{subdir}/{pattern}'
230
+
231
+ file_items = []
232
+ for item in list_all_with_pattern(
233
+ repo_id=repo_id,
234
+ repo_type=repo_type,
235
+ revision=revision,
236
+ pattern=pattern,
237
+ hf_token=hf_token,
238
+ silent=silent,
239
+ **kwargs
240
+ ):
241
+ if isinstance(item, RepoFile):
242
+ file_items.append(RepoFileItem.from_repo_file(item, subdir))
243
+
244
+ if sort_by != 'none':
245
+ file_items = sorted(file_items,
246
+ key=lambda x: x.path_segments if sort_by == 'path' else (-x.size, x.path_segments))
247
+
248
+ return RepoFileList(
249
+ repo_id=repo_id,
250
+ repo_type=repo_type,
251
+ revision=revision,
252
+ subdir=subdir,
253
+ items=file_items
254
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: hfutils
3
- Version: 0.6.0
3
+ Version: 0.7.0
4
4
  Summary: Useful utilities for huggingface
5
5
  Home-page: https://github.com/deepghs/hfutils
6
6
  Author: narugo1992
@@ -58,6 +58,7 @@ hfutils/repository/__init__.py
58
58
  hfutils/repository/base.py
59
59
  hfutils/repository/clone.py
60
60
  hfutils/repository/rollback.py
61
+ hfutils/repository/size.py
61
62
  hfutils/repository/url.py
62
63
  hfutils/utils/__init__.py
63
64
  hfutils/utils/archive.py
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes