|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import posixpath |
4 | | -from typing import Generator, Literal, Optional, TypedDict |
| 3 | +from collections.abc import Mapping, Sized |
| 4 | +from typing import Any, Literal, Protocol, SupportsIndex, overload |
5 | 5 |
|
6 | | -from typing_extensions import NotRequired, Required, Unpack |
7 | 6 |
|
8 | | -from posit.connect.context import requires |
9 | | -from posit.connect.errors import ClientError |
10 | | -from posit.connect.paginator import Paginator |
| 7 | +class _ContentPackage(Mapping[str, Any]): |
| 8 | + pass |
11 | 9 |
|
12 | | -from .resources import Active, ActiveFinderMethods, ActiveSequence |
13 | 10 |
|
| 11 | +class _ContentPackages(Sized, Protocol): |
| 12 | + @overload |
| 13 | + def __getitem__(self, index: SupportsIndex) -> _ContentPackage: ... |
14 | 14 |
|
15 | | -class ContentPackage(Active): |
16 | | - class _Package(TypedDict): |
17 | | - language: Required[str] |
18 | | - name: Required[str] |
19 | | - version: Required[str] |
20 | | - hash: Required[Optional[str]] |
| 15 | + @overload |
| 16 | + def __getitem__(self, index: slice) -> _ContentPackage: ... |
21 | 17 |
|
22 | | - def __init__(self, ctx, /, **attributes: Unpack[_Package]): |
23 | | - # todo - passing "" is a hack since path isn't needed. Instead, this class should inherit from Resource, but ActiveSequence is designed to operate on Active. That should change. |
24 | | - super().__init__(ctx, "", **attributes) |
25 | | - |
26 | | - |
27 | | -class ContentPackages(ActiveFinderMethods["ContentPackage"], ActiveSequence["ContentPackage"]): |
28 | | - """A collection of packages.""" |
29 | | - |
30 | | - def __init__(self, ctx, path): |
31 | | - super().__init__(ctx, path, "name") |
32 | | - |
33 | | - def _create_instance(self, path, /, **attributes): # noqa: ARG002 |
34 | | - return ContentPackage(self._ctx, **attributes) |
35 | | - |
36 | | - def fetch(self, **conditions): |
37 | | - try: |
38 | | - return super().fetch(**conditions) |
39 | | - except ClientError as e: |
40 | | - if e.http_status == 204: |
41 | | - return [] |
42 | | - raise e |
43 | | - |
44 | | - def find(self, uid): |
45 | | - raise NotImplementedError("The 'find' method is not support by the Packages API.") |
46 | | - |
47 | | - class _FindBy(TypedDict, total=False): |
48 | | - language: NotRequired[Literal["python", "r"]] |
49 | | - """Programming language ecosystem, options are 'python' and 'r'""" |
50 | | - |
51 | | - name: NotRequired[str] |
52 | | - """The package name""" |
53 | | - |
54 | | - version: NotRequired[str] |
55 | | - """The package version""" |
56 | | - |
57 | | - hash: NotRequired[Optional[str]] |
58 | | - """Package description hash for R packages.""" |
59 | | - |
60 | | - def find_by(self, **conditions: Unpack[_FindBy]): # type: ignore |
| 18 | + def find_by( |
| 19 | + self, |
| 20 | + *, |
| 21 | + language: Literal["python", "r"] = ..., |
| 22 | + name: str = ..., |
| 23 | + version: str = ..., |
| 24 | + hash: str | None = ..., # noqa: A002 |
| 25 | + ) -> _ContentPackage | None: |
61 | 26 | """ |
62 | 27 | Find the first record matching the specified conditions. |
63 | 28 |
|
64 | 29 | There is no implied ordering, so if order matters, you should specify it yourself. |
65 | 30 |
|
66 | 31 | Parameters |
67 | 32 | ---------- |
68 | | - **conditions : Unpack[_FindBy] |
69 | | - Conditions for filtering packages. The following keys are accepted: |
70 | | -
|
71 | 33 | language : {"python", "r"}, not required |
72 | 34 | Programming language ecosystem, options are 'python' and 'r' |
73 | | -
|
74 | 35 | name : str, not required |
75 | 36 | The package name |
76 | | -
|
77 | 37 | version : str, not required |
78 | 38 | The package version |
79 | | -
|
80 | 39 | hash : str or None, optional, not required |
81 | 40 | Package description hash for R packages. |
82 | 41 |
|
83 | 42 | Returns |
84 | 43 | ------- |
85 | | - Optional[T] |
| 44 | + _ContentPackage | None |
86 | 45 | The first record matching the specified conditions, or `None` if no such record exists. |
87 | 46 | """ |
88 | | - return super().find_by(**conditions) |
89 | | - |
90 | | - |
91 | | -class ContentPackagesMixin(Active): |
92 | | - """Mixin class to add a packages attribute.""" |
93 | | - |
94 | | - @property |
95 | | - @requires(version="2024.10.0-dev") |
96 | | - def packages(self): |
97 | | - path = posixpath.join(self._path, "packages") |
98 | | - return ContentPackages(self._ctx, path) |
99 | | - |
100 | | - |
101 | | -class Package(Active): |
102 | | - class _Package(TypedDict): |
103 | | - language: Required[Literal["python", "r"]] |
104 | | - """Programming language ecosystem, options are 'python' and 'r'""" |
105 | | - |
106 | | - language_version: Required[str] |
107 | | - """Programming language version""" |
108 | | - |
109 | | - name: Required[str] |
110 | | - """The package name""" |
| 47 | + ... |
111 | 48 |
|
112 | | - version: Required[str] |
113 | | - """The package version""" |
114 | 49 |
|
115 | | - hash: Required[Optional[str]] |
116 | | - """Package description hash for R packages.""" |
| 50 | +class _Package(Mapping[str, Any]): |
| 51 | + pass |
117 | 52 |
|
118 | | - bundle_id: Required[str] |
119 | | - """The unique identifier of the bundle this package is associated with""" |
120 | 53 |
|
121 | | - app_id: Required[str] |
122 | | - """The numerical identifier of the application this package is associated with""" |
| 54 | +class _Packages(Sized, Protocol): |
| 55 | + @overload |
| 56 | + def __getitem__(self, index: SupportsIndex) -> _ContentPackage: ... |
123 | 57 |
|
124 | | - app_guid: Required[str] |
125 | | - """The unique identifier of the application this package is associated with""" |
| 58 | + @overload |
| 59 | + def __getitem__(self, index: slice) -> _ContentPackage: ... |
126 | 60 |
|
127 | | - def __init__(self, ctx, /, **attributes: Unpack[_Package]): |
128 | | - # todo - passing "" is a hack since path isn't needed. Instead, this class should inherit from Resource, but ActiveSequence is designed to operate on Active. That should change. |
129 | | - super().__init__(ctx, "", **attributes) |
130 | | - |
131 | | - |
132 | | -class Packages(ActiveFinderMethods["Package"], ActiveSequence["Package"]): |
133 | | - def __init__(self, ctx, path): |
134 | | - super().__init__(ctx, path, "name") |
135 | | - |
136 | | - def _create_instance(self, path, /, **attributes): # noqa: ARG002 |
137 | | - return Package(self._ctx, **attributes) |
138 | | - |
139 | | - class _Fetch(TypedDict, total=False): |
140 | | - language: Required[Literal["python", "r"]] |
141 | | - """Programming language ecosystem, options are 'python' and 'r'""" |
142 | | - |
143 | | - name: Required[str] |
144 | | - """The package name""" |
145 | | - |
146 | | - version: Required[str] |
147 | | - """The package version""" |
148 | | - |
149 | | - def fetch(self, **conditions: Unpack[_Fetch]) -> Generator["Package"]: # type: ignore |
150 | | - # todo - add pagination support to ActiveSequence |
151 | | - url = self._ctx.url + self._path |
152 | | - paginator = Paginator(self._ctx.session, url, dict(**conditions)) |
153 | | - for page in paginator.fetch_pages(): |
154 | | - results = page.results |
155 | | - yield from (self._create_instance("", **result) for result in results) |
156 | | - |
157 | | - def find(self, uid): |
158 | | - raise NotImplementedError("The 'find' method is not support by the Packages API.") |
159 | | - |
160 | | - class _FindBy(TypedDict, total=False): |
161 | | - language: NotRequired[Literal["python", "r"]] |
162 | | - """Programming language ecosystem, options are 'python' and 'r'""" |
163 | | - |
164 | | - language_version: NotRequired[str] |
165 | | - """Programming language version""" |
166 | | - |
167 | | - name: NotRequired[str] |
168 | | - """The package name""" |
169 | | - |
170 | | - version: NotRequired[str] |
171 | | - """The package version""" |
172 | | - |
173 | | - hash: NotRequired[Optional[str]] |
174 | | - """Package description hash for R packages.""" |
175 | | - |
176 | | - bundle_id: NotRequired[str] |
177 | | - """The unique identifier of the bundle this package is associated with""" |
178 | | - |
179 | | - app_id: NotRequired[str] |
180 | | - """The numerical identifier of the application this package is associated with""" |
181 | | - |
182 | | - app_guid: NotRequired[str] |
183 | | - """The unique identifier of the application this package is associated with""" |
184 | | - |
185 | | - def find_by(self, **conditions: Unpack[_FindBy]) -> "Package | None": # type: ignore |
| 61 | + def find_by( |
| 62 | + self, |
| 63 | + *, |
| 64 | + language: Literal["python", "r"] = ..., |
| 65 | + name: str = ..., |
| 66 | + version: str = ..., |
| 67 | + hash: str | None = ..., # noqa: A002, |
| 68 | + bundle_id: str = ..., |
| 69 | + app_id: str = ..., |
| 70 | + app_guid: str = ..., |
| 71 | + ) -> _ContentPackage | None: |
186 | 72 | """ |
187 | 73 | Find the first record matching the specified conditions. |
188 | 74 |
|
189 | 75 | There is no implied ordering, so if order matters, you should specify it yourself. |
190 | 76 |
|
191 | 77 | Parameters |
192 | 78 | ---------- |
193 | | - **conditions : Unpack[_FindBy] |
194 | | - Conditions for filtering packages. The following keys are accepted: |
195 | | -
|
196 | 79 | language : {"python", "r"}, not required |
197 | 80 | Programming language ecosystem, options are 'python' and 'r' |
198 | | -
|
199 | 81 | name : str, not required |
200 | 82 | The package name |
201 | | -
|
202 | 83 | version : str, not required |
203 | 84 | The package version |
204 | | -
|
205 | 85 | hash : str or None, optional, not required |
206 | 86 | Package description hash for R packages. |
| 87 | + bundle_id: str, not required |
| 88 | + The unique identifier of the bundle this package is associated with. |
| 89 | + app_id: str, not required |
| 90 | + The numerical identifier of the application this package is associated with. |
| 91 | + app_guid: str, not required |
| 92 | + The unique identifier of the application this package is associated with. |
207 | 93 |
|
208 | 94 | Returns |
209 | 95 | ------- |
210 | | - Optional[Package] |
| 96 | + _Package | None |
211 | 97 | The first record matching the specified conditions, or `None` if no such record exists. |
212 | 98 | """ |
213 | | - return super().find_by(**conditions) |
| 99 | + ... |
0 commit comments