Skip to content

Commit

Permalink
Add function 'select_license_by_source' and use it
Browse files Browse the repository at this point in the history
  • Loading branch information
raimon49 committed May 10, 2019
1 parent cbbf673 commit eeecb1e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
26 changes: 23 additions & 3 deletions piplicenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,16 @@ def get_pkg_info(pkg):
for key in METADATA_KEYS:
pkg_info[key] = parsed_metadata.get(key, LICENSE_UNKNOWN)

if getattr(args, 'from') == 'classifier' and metadata is not None:
from_source = getattr(args, 'from')
need_classifier = from_source == 'classifier' or from_source == 'mixed'
if need_classifier and metadata is not None:
message = message_from_string(metadata)
pkg_info['license'] = find_license_from_classifier(message)
license_classifier = find_license_from_classifier(message)
license_meta = pkg_info['license']
# Overwrite license by condition
pkg_info['license'] = select_license_by_source(from_source,
license_classifier,
license_meta)

return pkg_info

Expand Down Expand Up @@ -287,6 +294,16 @@ def find_license_from_classifier(message):
return license_from_classifier


def select_license_by_source(from_source, license_classifier, license_meta):
if from_source == 'classifier':
return license_classifier
elif from_source == 'mixed':
if license_classifier != LICENSE_UNKNOWN:
return license_classifier
else:
return license_meta


def get_output_fields(args):
if args.summary:
return list(SUMMARY_OUTPUT_FIELDS)
Expand Down Expand Up @@ -391,6 +408,9 @@ def _compatible_format_args(self, args):
if from_input in ('classifier', 'c'):
setattr(args, 'from', 'classifier')

if from_input in ('mixed', 'mix'):
setattr(args, 'from', 'mixed')

if order_input in ('count', 'c'):
args.order = 'count'

Expand Down Expand Up @@ -449,7 +469,7 @@ def create_parser():
action='store', type=str,
default='meta', metavar='SOURCE',
help=('where to find license information\n'
'"meta", "classifier"\n'
'"meta", "classifier, "mixed"\n'
'default: --from=meta'))
parser.add_argument('-c', '--from-classifier',
action='store_true',
Expand Down
35 changes: 35 additions & 0 deletions test_piplicenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
create_licenses_table, get_output_fields, get_sortby,
factory_styled_table_with_args, create_warn_string,
find_license_from_classifier, create_output_string,
select_license_by_source,
DEFAULT_OUTPUT_FIELDS, SYSTEM_PACKAGES,
LICENSE_UNKNOWN)

Expand Down Expand Up @@ -85,6 +86,19 @@ def test_from_classifier(self):
license_notation_as_classifier = 'MIT License'
self.assertIn(license_notation_as_classifier, license_columns)

def test_from_mixed(self):
from_classifier_args = ['--from=mixed']
args = self.parser.parse_args(from_classifier_args)
table = create_licenses_table(args)

output_fields = get_output_fields(args)
self.assertIn('License', output_fields)

license_columns = self._create_license_columns(table)
# Depending on the condition "MIT" or "BSD" etc.
license_notation_as_classifier = 'MIT License'
self.assertIn(license_notation_as_classifier, license_columns)

def test_find_license_from_classifier(self):
metadata = ('Metadata-Version: 2.0\r\n'
'Name: pip-licenses\r\n'
Expand Down Expand Up @@ -117,6 +131,27 @@ def test_not_found_license_from_classifier(self):
self.assertEqual(LICENSE_UNKNOWN,
find_license_from_classifier(message))

def test_select_license_by_source(self):
self.assertEqual('MIT License',
select_license_by_source('classifier',
'MIT License',
'MIT'))

self.assertEqual(LICENSE_UNKNOWN,
select_license_by_source('classifier',
LICENSE_UNKNOWN,
'MIT'))

self.assertEqual('MIT License',
select_license_by_source('mixed',
'MIT License',
'MIT'))

self.assertEqual('MIT',
select_license_by_source('mixed',
LICENSE_UNKNOWN,
'MIT'))

def test_with_system(self):
with_system_args = ['--with-system']
args = self.parser.parse_args(with_system_args)
Expand Down

0 comments on commit eeecb1e

Please sign in to comment.