Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/npm/utils.py
Show First 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | def extract_npm_package_author(package_json): | ||||
def _author_str(author_data): | def _author_str(author_data): | ||||
if type(author_data) is dict: | if type(author_data) is dict: | ||||
author_str = '' | author_str = '' | ||||
if 'name' in author_data: | if 'name' in author_data: | ||||
author_str += author_data['name'] | author_str += author_data['name'] | ||||
if 'email' in author_data: | if 'email' in author_data: | ||||
author_str += ' <%s>' % author_data['email'] | author_str += ' <%s>' % author_data['email'] | ||||
return author_str | return author_str | ||||
elif type(author_data) is list: | |||||
ardumont: I see that you are consistent with the other conditional.
Still, out of curiosity, what's the… | |||||
anlambertAuthorUnsubmitted Done Inline ActionsI could have used isinstance indeed but as no type inheritance is implied here but only base Python types check, simply using type does also the job. anlambert: I could have used `isinstance` indeed but as no type inheritance is implied here but only base… | |||||
return _author_str(author_data[0]) if len(author_data) > 0 else '' | |||||
else: | else: | ||||
return author_data | return author_data | ||||
author_data = {} | author_data = {} | ||||
if 'author' in package_json: | for author_key in ('author', 'authors'): | ||||
author_str = _author_str(package_json['author']) | if author_key in package_json: | ||||
author_data = parse_npm_package_author(author_str) | author_str = _author_str(package_json[author_key]) | ||||
elif 'authors' in package_json and len(package_json['authors']) > 0: | |||||
author_str = _author_str(package_json['authors'][0]) | |||||
author_data = parse_npm_package_author(author_str) | author_data = parse_npm_package_author(author_str) | ||||
name = author_data.get('name') | name = author_data.get('name') | ||||
email = author_data.get('email') | email = author_data.get('email') | ||||
fullname = None | fullname = None | ||||
if name and email: | if name and email: | ||||
fullname = '%s <%s>' % (name, email) | fullname = '%s <%s>' % (name, email) | ||||
Show All 16 Lines |
I see that you are consistent with the other conditional.
Still, out of curiosity, what's the difference with isinstance(author, list)?
I checked [1] to have a feel of it ;)
[1] https://stackoverflow.com/questions/1549801/what-are-the-differences-between-type-and-isinstance