|
1 | | -from typing import Literal, Optional, Sequence, TypedDict |
| 1 | +import posixpath |
| 2 | +from typing import Optional, TypedDict, overload |
2 | 3 |
|
3 | 4 | from typing_extensions import NotRequired, Required, Unpack |
4 | 5 |
|
5 | | -from .resources import Resource, ResourceParameters, Resources |
| 6 | +from .resources import Active, ActiveFinderMethods, ActiveSequence |
6 | 7 |
|
7 | 8 |
|
8 | | -class Package(Resource): |
9 | | - """A package resource.""" |
10 | | - |
11 | | - class PackageAttributes(TypedDict): |
12 | | - """Package attributes.""" |
13 | | - |
14 | | - language: Required[Literal["r", "python"]] |
| 9 | +class Package(Active): |
| 10 | + class _Package(TypedDict): |
| 11 | + language: Required[str] |
15 | 12 | name: Required[str] |
16 | 13 | version: Required[str] |
17 | | - hash: NotRequired[str] |
| 14 | + hash: Required[Optional[str]] |
18 | 15 |
|
19 | | - def __init__(self, params: ResourceParameters, **kwargs: Unpack[PackageAttributes]): |
20 | | - super().__init__(params, **kwargs) |
| 16 | + def __init__(self, ctx, path, /, **attributes: Unpack[_Package]): |
| 17 | + super().__init__(ctx, path, **attributes) |
21 | 18 |
|
22 | 19 |
|
23 | | -class Packages(Resources, Sequence[Package]): |
| 20 | +class Packages(ActiveFinderMethods["Package"], ActiveSequence["Package"]): |
24 | 21 | """A collection of packages.""" |
25 | 22 |
|
26 | | - def __init__(self, params, endpoint): |
27 | | - super().__init__(params) |
28 | | - self._endpoint = endpoint |
29 | | - self._packages = [] |
30 | | - self.reload() |
31 | | - |
32 | | - def __getitem__(self, index): |
33 | | - """Retrieve an item or slice from the sequence.""" |
34 | | - return self._packages[index] |
| 23 | + def __init__(self, ctx, path): |
| 24 | + super().__init__(ctx, path, "name") |
35 | 25 |
|
36 | | - def __len__(self): |
37 | | - """Return the length of the sequence.""" |
38 | | - return len(self._packages) |
| 26 | + def _create_instance(self, path, /, **attributes): |
| 27 | + return Package(self._ctx, path, **attributes) |
39 | 28 |
|
40 | | - def __repr__(self): |
41 | | - """Return the string representation of the sequence.""" |
42 | | - return f"Packages({', '.join(map(str, self._packages))})" |
| 29 | + class _FindBy(TypedDict, total=False): |
| 30 | + language: NotRequired[str] |
| 31 | + name: NotRequired[str] |
| 32 | + version: NotRequired[str] |
| 33 | + hash: NotRequired[Optional[str]] |
43 | 34 |
|
44 | | - def count(self, value): |
45 | | - """Return the number of occurrences of a value in the sequence.""" |
46 | | - return self._packages.count(value) |
| 35 | + @overload |
| 36 | + def find_by(self, **conditions: Unpack[_FindBy]): |
| 37 | + ... |
47 | 38 |
|
48 | | - def index(self, value, start=0, stop=None): |
49 | | - """Return the index of the first occurrence of a value in the sequence.""" |
50 | | - if stop is None: |
51 | | - stop = len(self._packages) |
52 | | - return self._packages.index(value, start, stop) |
| 39 | + @overload |
| 40 | + def find_by(self, **conditions): |
| 41 | + ... |
53 | 42 |
|
54 | | - def reload(self) -> "Packages": |
55 | | - """Reload packages from the Connect server. |
56 | | -
|
57 | | - Returns |
58 | | - ------- |
59 | | - List[Package] |
60 | | - """ |
61 | | - response = self.params.session.get(self._endpoint) |
62 | | - results = response.json() |
63 | | - packages = [Package(self.params, **result) for result in results] |
64 | | - self._packages = packages |
65 | | - return self |
| 43 | + def find_by(self, **conditions): |
| 44 | + return super().find_by(**conditions) |
66 | 45 |
|
| 46 | +class PackagesMixin(Active): |
| 47 | + """Mixin class to add a packages attribute.""" |
67 | 48 |
|
68 | | -class PackagesMixin(Resource): |
69 | | - """Mixin class to add a packages to a resource.""" |
| 49 | + def __init__(self, ctx, path, /, **attributes): |
| 50 | + """Mixin class which adds a `packages` attribute. |
70 | 51 |
|
71 | | - class HasGuid(TypedDict): |
72 | | - """Has a guid.""" |
73 | | - |
74 | | - guid: Required[str] |
75 | | - |
76 | | - def __init__(self, params: ResourceParameters, **kwargs: Unpack[HasGuid]): |
77 | | - super().__init__(params, **kwargs) |
78 | | - self._guid = kwargs["guid"] |
79 | | - self._packages: Optional[Packages] = None |
80 | | - |
81 | | - @property |
82 | | - def packages(self) -> Packages: |
83 | | - """Get the packages.""" |
84 | | - if self._packages: |
85 | | - return self._packages |
| 52 | + Parameters |
| 53 | + ---------- |
| 54 | + ctx : Context |
| 55 | + The context object containing the session and URL for API interactions |
| 56 | + path : str |
| 57 | + The HTTP path component for the resource endpoint |
| 58 | + **attributes : dict |
| 59 | + Resource attributes passed |
| 60 | + """ |
| 61 | + super().__init__(ctx, path, **attributes) |
86 | 62 |
|
87 | | - endpoint = self.params.url + f"v1/content/{self._guid}/packages" |
88 | | - self._packages = Packages(self.params, endpoint) |
89 | | - return self._packages |
| 63 | + path = posixpath.join(path, "packages") |
| 64 | + self.packages = Packages(ctx, path) |
0 commit comments