Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame^] | 1 | """Base class for index crawlers.""" |
| 2 | |
| 3 | from packaging.pypi.dist import ReleasesList |
| 4 | |
| 5 | |
| 6 | class BaseClient: |
| 7 | """Base class containing common methods for the index crawlers/clients""" |
| 8 | |
| 9 | def __init__(self, prefer_final, prefer_source): |
| 10 | self._prefer_final = prefer_final |
| 11 | self._prefer_source = prefer_source |
| 12 | self._index = self |
| 13 | |
| 14 | def _get_prefer_final(self, prefer_final=None): |
| 15 | """Return the prefer_final internal parameter or the specified one if |
| 16 | provided""" |
| 17 | if prefer_final: |
| 18 | return prefer_final |
| 19 | else: |
| 20 | return self._prefer_final |
| 21 | |
| 22 | def _get_prefer_source(self, prefer_source=None): |
| 23 | """Return the prefer_source internal parameter or the specified one if |
| 24 | provided""" |
| 25 | if prefer_source: |
| 26 | return prefer_source |
| 27 | else: |
| 28 | return self._prefer_source |
| 29 | |
| 30 | def _get_project(self, project_name): |
| 31 | """Return an project instance, create it if necessary""" |
| 32 | return self._projects.setdefault(project_name.lower(), |
| 33 | ReleasesList(project_name, index=self._index)) |
| 34 | |
| 35 | def download_distribution(self, requirements, temp_path=None, |
| 36 | prefer_source=None, prefer_final=None): |
| 37 | """Download a distribution from the last release according to the |
| 38 | requirements. |
| 39 | |
| 40 | If temp_path is provided, download to this path, otherwise, create a |
| 41 | temporary location for the download and return it. |
| 42 | """ |
| 43 | prefer_final = self._get_prefer_final(prefer_final) |
| 44 | prefer_source = self._get_prefer_source(prefer_source) |
| 45 | release = self.get_release(requirements, prefer_final) |
| 46 | if release: |
| 47 | dist = release.get_distribution(prefer_source=prefer_source) |
| 48 | return dist.download(temp_path) |