Return the value for the attribute ``name`` in the ``metainfo`` mapping, pkginfo object or email object. Treat the value as a list of multiple values if ``multiple`` is True. Return None or an empty list (if multiple is True) if no value is found or the attribute ``name`` does not e
(metainfo, name, multiple=False)
| 1837 | |
| 1838 | |
| 1839 | def get_attribute(metainfo, name, multiple=False): |
| 1840 | """ |
| 1841 | Return the value for the attribute ``name`` in the ``metainfo`` mapping, |
| 1842 | pkginfo object or email object. Treat the value as a list of multiple values |
| 1843 | if ``multiple`` is True. Return None or an empty list (if multiple is True) |
| 1844 | if no value is found or the attribute ``name`` does not exist. |
| 1845 | Ignore case (but returns the value for the original case if present. |
| 1846 | """ |
| 1847 | |
| 1848 | # note: the approach for this function is to be used with the various |
| 1849 | # metainfo objects and dictionsaries we use that can be a |
| 1850 | # pkginfo.Distribution, an email.message.EmailMessage or a dict. |
| 1851 | |
| 1852 | # Because of that, the key can be obtained as a plain named attribute, |
| 1853 | # either as-is or lowercased (and with dash replaced by dunder) or we |
| 1854 | # can use a get on dicts of emails. |
| 1855 | |
| 1856 | def attr_getter(_aname, default): |
| 1857 | _aname = _aname.replace('-', '_') |
| 1858 | return ( |
| 1859 | getattr(metainfo, _aname, default) |
| 1860 | or getattr(metainfo, _aname.lower(), default) |
| 1861 | ) |
| 1862 | |
| 1863 | def item_getter(_iname, getter, default): |
| 1864 | getter = getattr(metainfo, getter, None) |
| 1865 | if getter: |
| 1866 | return getter(_iname, default) or getter(_iname.lower(), default) |
| 1867 | return default |
| 1868 | |
| 1869 | if multiple: |
| 1870 | return ( |
| 1871 | attr_getter(name, []) |
| 1872 | or item_getter(name, 'get_all', []) |
| 1873 | or item_getter(name, 'get', []) |
| 1874 | or [] |
| 1875 | ) |
| 1876 | else: |
| 1877 | return ( |
| 1878 | attr_getter(name, None) |
| 1879 | or item_getter(name, 'get', None) |
| 1880 | or None |
| 1881 | ) |
| 1882 | |
| 1883 | |
| 1884 | def get_description(metainfo, location=None): |
no test coverage detected