22# -*- coding: utf8 -*-
33
44# ============================================================================
5- # Copyright (c) 2013-2016 nexB Inc. http://www.nexb.com/ - All rights reserved.
5+ # Copyright (c) 2013-2017 nexB Inc. http://www.nexb.com/ - All rights reserved.
66# Licensed under the Apache License, Version 2.0 (the "License");
77# you may not use this file except in compliance with the License.
88# You may obtain a copy of the License at
1818from __future__ import print_function
1919
2020import codecs
21+ import collections
2122import jinja2
2223import os
2324from posixpath import basename
2930from attributecode import ERROR
3031from attributecode import Error
3132from attributecode .licenses import COMMON_LICENSES
33+ from attributecode .model import parse_license_expression
3234from attributecode .util import add_unc
3335
3436
@@ -46,7 +48,10 @@ def generate(abouts, template_string=None):
4648 try :
4749 captured_license = []
4850 license_key_and_context = {}
51+ sorted_license_key_and_context = {}
4952 license_text_name_and_key = {}
53+ license_key_to_license_name = {}
54+ # FIXME: This need to be simplified
5055 for about in abouts :
5156 # about.license_file.value is a OrderDict with license_text_name as
5257 # the key and the license text as the value
@@ -61,10 +66,40 @@ def generate(abouts, template_string=None):
6166 else :
6267 license_key = license_text_name
6368 license_key_and_context [license_key ] = about .license_file .value [license_text_name ]
69+ sorted_license_key_and_context = collections .OrderedDict (sorted (license_key_and_context .items ()))
6470 license_text_name_and_key [license_text_name ] = license_key
6571
66- rendered = template .render (abouts = abouts , common_licenses = COMMON_LICENSES , license_key_and_context = license_key_and_context ,
67- license_text_name_and_key = license_text_name_and_key )
72+ # Convert/map the key in license expression to license name
73+ if about .license_expression .value and about .license_name .value :
74+ # Split the license expression into list with license key and condition keyword
75+ lic_expression_list = about .license_expression .value .split ()
76+
77+
78+ lic_name_list = about .license_name .value
79+ lic_name_expression_list = []
80+
81+ # The order of the license_name and key should be the same
82+ # The length for both list should be the same excluding the condition keyword
83+ # such as 'and' and 'or'
84+ assert len (lic_name_list ) <= len (lic_expression_list )
85+
86+ # Map the licence key to license name
87+ index_for_license_name_list = 0
88+ for key in lic_expression_list :
89+ if key .lower () == 'and' or key .lower () == 'or' :
90+ lic_name_expression_list .append (key )
91+ else :
92+ lic_name_expression_list .append (lic_name_list [index_for_license_name_list ])
93+ license_key_to_license_name [key ] = lic_name_list [index_for_license_name_list ]
94+ index_for_license_name_list = index_for_license_name_list + 1
95+ # Join the license name expression into a single string
96+ lic_name_expression = ' ' .join (lic_name_expression_list )
97+
98+ # Add the license name expression string into the about object
99+ about .license_name_expression = lic_name_expression
100+
101+ rendered = template .render (abouts = abouts , common_licenses = COMMON_LICENSES , license_key_and_context = sorted_license_key_and_context ,
102+ license_text_name_and_key = license_text_name_and_key , license_key_to_license_name = license_key_to_license_name )
68103 except Exception , e :
69104 line = getattr (e , 'lineno' , None )
70105 ln_msg = ' at line: %r' % line if line else ''
@@ -167,6 +202,17 @@ def generate_and_save(abouts, output_location, mapping, template_loc=None,
167202 if about .about_file_path == fp :
168203 updated_abouts .append (about )
169204
205+ # Parse license_expression and save to the license list
206+ for about in updated_abouts :
207+ if about .license_expression .value :
208+ special_char_in_expression , lic_list = parse_license_expression (about .license_expression .value )
209+ if special_char_in_expression :
210+ msg = (u"The following character(s) cannot be in the licesne_expression: " +
211+ str (special_char_in_expression ))
212+ errors .append (Error (ERROR , msg ))
213+ else :
214+ about .license .value = lic_list
215+
170216 rendered = generate_from_file (updated_abouts , template_loc = template_loc )
171217
172218 if rendered :
0 commit comments