|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +from typing import List, Optional |
| 6 | + |
| 7 | +from rattler.rattler import PyAboutJson |
| 8 | + |
| 9 | + |
| 10 | +class AboutJson: |
| 11 | + """ |
| 12 | + The `about.json` file contains metadata about the package. |
| 13 | + """ |
| 14 | + |
| 15 | + _inner: PyAboutJson |
| 16 | + |
| 17 | + @staticmethod |
| 18 | + def from_path(path: os.PathLike[str]) -> AboutJson: |
| 19 | + """ |
| 20 | + Parses the object from a file specified by a `path`, using a format |
| 21 | + appropriate for the file type. |
| 22 | +
|
| 23 | + For example, if the file is in JSON format, this function reads the data |
| 24 | + from the file at the specified path, parse the JSON string and return the |
| 25 | + resulting object. If the file is not in a parsable format or if the file |
| 26 | + could not read, this function returns an error. |
| 27 | +
|
| 28 | + Examples |
| 29 | + -------- |
| 30 | + ```python |
| 31 | + >>> about = AboutJson.from_path("../test-data/dummy-about.json") |
| 32 | + >>> about |
| 33 | + AboutJson() |
| 34 | + >>> |
| 35 | + ``` |
| 36 | + """ |
| 37 | + return AboutJson._from_py_about_json(PyAboutJson.from_path(Path(path))) |
| 38 | + |
| 39 | + @staticmethod |
| 40 | + def from_package_directory(path: os.PathLike[str]) -> AboutJson: |
| 41 | + """ |
| 42 | + Parses the object by looking up the appropriate file from the root of the |
| 43 | + specified Conda archive directory, using a format appropriate for the file |
| 44 | + type. |
| 45 | +
|
| 46 | + For example, if the file is in JSON format, this function reads the |
| 47 | + appropriate file from the archive, parse the JSON string and return the |
| 48 | + resulting object. If the file is not in a parsable format or if the file |
| 49 | + could not be read, this function returns an error. |
| 50 | + """ |
| 51 | + return AboutJson._from_py_about_json( |
| 52 | + PyAboutJson.from_package_directory(Path(path)) |
| 53 | + ) |
| 54 | + |
| 55 | + @staticmethod |
| 56 | + def from_str(string: str) -> AboutJson: |
| 57 | + """ |
| 58 | + Parses the object from a string, using a format appropriate for the file |
| 59 | + type. |
| 60 | +
|
| 61 | + For example, if the file is in JSON format, this function parses the JSON |
| 62 | + string and returns the resulting object. If the file is not in a parsable |
| 63 | + format, this function returns an error. |
| 64 | +
|
| 65 | + Examples |
| 66 | + -------- |
| 67 | + ```python |
| 68 | + >>> import json |
| 69 | + >>> with open("../test-data/dummy-about.json", 'r') as file: |
| 70 | + ... json_str = json.dumps(json.load(file)) |
| 71 | + >>> about = AboutJson.from_str(json_str) |
| 72 | + >>> about |
| 73 | + AboutJson() |
| 74 | + >>> |
| 75 | + ``` |
| 76 | + """ |
| 77 | + return AboutJson._from_py_about_json(PyAboutJson.from_str(string)) |
| 78 | + |
| 79 | + @staticmethod |
| 80 | + def package_path() -> str: |
| 81 | + """ |
| 82 | + Returns the path to the file within the Conda archive. |
| 83 | +
|
| 84 | + The path is relative to the root of the archive and includes any necessary |
| 85 | + directories. |
| 86 | +
|
| 87 | + Examples |
| 88 | + -------- |
| 89 | + ```python |
| 90 | + >>> AboutJson.package_path() |
| 91 | + 'info/about.json' |
| 92 | + >>> |
| 93 | + ``` |
| 94 | + """ |
| 95 | + return PyAboutJson.package_path() |
| 96 | + |
| 97 | + @property |
| 98 | + def channels(self) -> List[str]: |
| 99 | + """ |
| 100 | + A list of channels that where used during the build. |
| 101 | +
|
| 102 | + Examples |
| 103 | + -------- |
| 104 | + ```python |
| 105 | + >>> about = AboutJson.from_path("../test-data/dummy-about.json") |
| 106 | + >>> about.channels |
| 107 | + ['https://conda.anaconda.org/conda-forge'] |
| 108 | + >>> |
| 109 | + ``` |
| 110 | + """ |
| 111 | + return self._inner.channels |
| 112 | + |
| 113 | + @property |
| 114 | + def description(self) -> Optional[str]: |
| 115 | + """ |
| 116 | + Description of the package. |
| 117 | +
|
| 118 | + Examples |
| 119 | + -------- |
| 120 | + ```python |
| 121 | + >>> about = AboutJson.from_path("../test-data/dummy-about.json") |
| 122 | + >>> about.description |
| 123 | + 'A dummy description.' |
| 124 | + >>> |
| 125 | + ``` |
| 126 | + """ |
| 127 | + if description := self._inner.description: |
| 128 | + return description |
| 129 | + |
| 130 | + return None |
| 131 | + |
| 132 | + @property |
| 133 | + def dev_url(self) -> List[str]: |
| 134 | + """ |
| 135 | + A list of URLs to the development page of the package. |
| 136 | +
|
| 137 | + Examples |
| 138 | + -------- |
| 139 | + ```python |
| 140 | + >>> about = AboutJson.from_path("../test-data/dummy-about.json") |
| 141 | + >>> about.dev_url |
| 142 | + ['https://github.com/mamba-org/rattler'] |
| 143 | + >>> |
| 144 | + ``` |
| 145 | + """ |
| 146 | + return self._inner.dev_url |
| 147 | + |
| 148 | + @property |
| 149 | + def doc_url(self) -> List[str]: |
| 150 | + """ |
| 151 | + A list of URLs to the documentation of the package. |
| 152 | +
|
| 153 | + Examples |
| 154 | + -------- |
| 155 | + ```python |
| 156 | + >>> about = AboutJson.from_path("../test-data/dummy-about.json") |
| 157 | + >>> about.doc_url |
| 158 | + ['https://mamba-org.github.io/rattler/py-rattler/'] |
| 159 | + >>> |
| 160 | + ``` |
| 161 | + """ |
| 162 | + return self._inner.doc_url |
| 163 | + |
| 164 | + @property |
| 165 | + def home(self) -> List[str]: |
| 166 | + """ |
| 167 | + A list URL to the homepage of the package. |
| 168 | +
|
| 169 | + Examples |
| 170 | + -------- |
| 171 | + ```python |
| 172 | + >>> about = AboutJson.from_path("../test-data/dummy-about.json") |
| 173 | + >>> about.home |
| 174 | + ['http://github.com/mamba-org/rattler'] |
| 175 | + >>> |
| 176 | + ``` |
| 177 | + """ |
| 178 | + return self._inner.home |
| 179 | + |
| 180 | + @property |
| 181 | + def license(self) -> Optional[str]: |
| 182 | + """ |
| 183 | + The license of the package. |
| 184 | +
|
| 185 | + Examples |
| 186 | + -------- |
| 187 | + ```python |
| 188 | + >>> about = AboutJson.from_path("../test-data/dummy-about.json") |
| 189 | + >>> about.license |
| 190 | + 'BSD-3-Clause' |
| 191 | + >>> |
| 192 | + ``` |
| 193 | + """ |
| 194 | + if license := self._inner.license: |
| 195 | + return license |
| 196 | + |
| 197 | + return None |
| 198 | + |
| 199 | + @property |
| 200 | + def license_family(self) -> Optional[str]: |
| 201 | + """ |
| 202 | + The license family of the package. |
| 203 | +
|
| 204 | + Examples |
| 205 | + -------- |
| 206 | + ```python |
| 207 | + >>> about = AboutJson.from_path("../test-data/dummy-about.json") |
| 208 | + >>> about.license_family |
| 209 | + >>> type(about.license_family) |
| 210 | + <class 'NoneType'> |
| 211 | + >>> |
| 212 | + ``` |
| 213 | + """ |
| 214 | + if license_family := self._inner.license_family: |
| 215 | + return license_family |
| 216 | + |
| 217 | + return None |
| 218 | + |
| 219 | + @property |
| 220 | + def source_url(self) -> Optional[str]: |
| 221 | + """ |
| 222 | + The URL to the latest source code of the package. |
| 223 | +
|
| 224 | + Examples |
| 225 | + -------- |
| 226 | + ```python |
| 227 | + >>> about = AboutJson.from_path("../test-data/dummy-about.json") |
| 228 | + >>> about.source_url |
| 229 | + 'https://github.com/mamba-org/rattler' |
| 230 | + >>> |
| 231 | + ``` |
| 232 | + """ |
| 233 | + if source_url := self._inner.source_url: |
| 234 | + return source_url |
| 235 | + |
| 236 | + return None |
| 237 | + |
| 238 | + @property |
| 239 | + def summary(self) -> Optional[str]: |
| 240 | + """ |
| 241 | + A Short summary description. |
| 242 | +
|
| 243 | + Examples |
| 244 | + -------- |
| 245 | + ```python |
| 246 | + >>> about = AboutJson.from_path("../test-data/dummy-about.json") |
| 247 | + >>> about.summary |
| 248 | + 'A dummy summary.' |
| 249 | + >>> |
| 250 | + ``` |
| 251 | + """ |
| 252 | + if summary := self._inner.summary: |
| 253 | + return summary |
| 254 | + |
| 255 | + return None |
| 256 | + |
| 257 | + @classmethod |
| 258 | + def _from_py_about_json(cls, py_about_json: PyAboutJson) -> AboutJson: |
| 259 | + about_json = cls.__new__(cls) |
| 260 | + about_json._inner = py_about_json |
| 261 | + |
| 262 | + return about_json |
| 263 | + |
| 264 | + def __repr__(self) -> str: |
| 265 | + """ |
| 266 | + Returns a representation of the AboutJson. |
| 267 | + """ |
| 268 | + return "AboutJson()" |
0 commit comments