| 1 | n/a | """Convenient client for all PyPI APIs. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | This module provides a ClientWrapper class which will use the "simple" |
|---|
| 4 | n/a | or XML-RPC API to request information or files from an index. |
|---|
| 5 | n/a | """ |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | from packaging.pypi import simple, xmlrpc |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | _WRAPPER_MAPPINGS = {'get_release': 'simple', |
|---|
| 10 | n/a | 'get_releases': 'simple', |
|---|
| 11 | n/a | 'search_projects': 'simple', |
|---|
| 12 | n/a | 'get_metadata': 'xmlrpc', |
|---|
| 13 | n/a | 'get_distributions': 'simple'} |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | _WRAPPER_INDEXES = {'xmlrpc': xmlrpc.Client, |
|---|
| 16 | n/a | 'simple': simple.Crawler} |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | def switch_index_if_fails(func, wrapper): |
|---|
| 20 | n/a | """Decorator that switch of index (for instance from xmlrpc to simple) |
|---|
| 21 | n/a | if the first mirror return an empty list or raises an exception. |
|---|
| 22 | n/a | """ |
|---|
| 23 | n/a | def decorator(*args, **kwargs): |
|---|
| 24 | n/a | retry = True |
|---|
| 25 | n/a | exception = None |
|---|
| 26 | n/a | methods = [func] |
|---|
| 27 | n/a | for f in wrapper._indexes.values(): |
|---|
| 28 | n/a | if f != func.__self__ and hasattr(f, func.__name__): |
|---|
| 29 | n/a | methods.append(getattr(f, func.__name__)) |
|---|
| 30 | n/a | for method in methods: |
|---|
| 31 | n/a | try: |
|---|
| 32 | n/a | response = method(*args, **kwargs) |
|---|
| 33 | n/a | retry = False |
|---|
| 34 | n/a | except Exception as e: |
|---|
| 35 | n/a | exception = e |
|---|
| 36 | n/a | if not retry: |
|---|
| 37 | n/a | break |
|---|
| 38 | n/a | if retry and exception: |
|---|
| 39 | n/a | raise exception |
|---|
| 40 | n/a | else: |
|---|
| 41 | n/a | return response |
|---|
| 42 | n/a | return decorator |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | class ClientWrapper: |
|---|
| 46 | n/a | """Wrapper around simple and xmlrpc clients, |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | Choose the best implementation to use depending the needs, using the given |
|---|
| 49 | n/a | mappings. |
|---|
| 50 | n/a | If one of the indexes returns an error, tries to use others indexes. |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | :param index: tell which index to rely on by default. |
|---|
| 53 | n/a | :param index_classes: a dict of name:class to use as indexes. |
|---|
| 54 | n/a | :param indexes: a dict of name:index already instantiated |
|---|
| 55 | n/a | :param mappings: the mappings to use for this wrapper |
|---|
| 56 | n/a | """ |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | def __init__(self, default_index='simple', index_classes=_WRAPPER_INDEXES, |
|---|
| 59 | n/a | indexes={}, mappings=_WRAPPER_MAPPINGS): |
|---|
| 60 | n/a | self._projects = {} |
|---|
| 61 | n/a | self._mappings = mappings |
|---|
| 62 | n/a | self._indexes = indexes |
|---|
| 63 | n/a | self._default_index = default_index |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | # instantiate the classes and set their _project attribute to the one |
|---|
| 66 | n/a | # of the wrapper. |
|---|
| 67 | n/a | for name, cls in index_classes.items(): |
|---|
| 68 | n/a | obj = self._indexes.setdefault(name, cls()) |
|---|
| 69 | n/a | obj._projects = self._projects |
|---|
| 70 | n/a | obj._index = self |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | def __getattr__(self, method_name): |
|---|
| 73 | n/a | """When asking for methods of the wrapper, return the implementation of |
|---|
| 74 | n/a | the wrapped classes, depending the mapping. |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | Decorate the methods to switch of implementation if an error occurs |
|---|
| 77 | n/a | """ |
|---|
| 78 | n/a | real_method = None |
|---|
| 79 | n/a | if method_name in _WRAPPER_MAPPINGS: |
|---|
| 80 | n/a | obj = self._indexes[_WRAPPER_MAPPINGS[method_name]] |
|---|
| 81 | n/a | real_method = getattr(obj, method_name) |
|---|
| 82 | n/a | else: |
|---|
| 83 | n/a | # the method is not defined in the mappings, so we try first to get |
|---|
| 84 | n/a | # it via the default index, and rely on others if needed. |
|---|
| 85 | n/a | try: |
|---|
| 86 | n/a | real_method = getattr(self._indexes[self._default_index], |
|---|
| 87 | n/a | method_name) |
|---|
| 88 | n/a | except AttributeError: |
|---|
| 89 | n/a | other_indexes = [i for i in self._indexes |
|---|
| 90 | n/a | if i != self._default_index] |
|---|
| 91 | n/a | for index in other_indexes: |
|---|
| 92 | n/a | real_method = getattr(self._indexes[index], method_name, |
|---|
| 93 | n/a | None) |
|---|
| 94 | n/a | if real_method: |
|---|
| 95 | n/a | break |
|---|
| 96 | n/a | if real_method: |
|---|
| 97 | n/a | return switch_index_if_fails(real_method, self) |
|---|
| 98 | n/a | else: |
|---|
| 99 | n/a | raise AttributeError("No index have attribute '%s'" % method_name) |
|---|