|
27 | 27 | import about |
28 | 28 | import csv |
29 | 29 | import errno |
| 30 | +import json |
30 | 31 | import getopt |
31 | 32 | import os |
32 | 33 | import shutil |
33 | 34 | import sys |
| 35 | +import urllib |
| 36 | +import urllib2 |
| 37 | + |
34 | 38 |
|
35 | 39 | __version__ = '0.9.0' |
36 | 40 |
|
|
44 | 48 | self_path = abspath(dirname(__file__)) |
45 | 49 |
|
46 | 50 |
|
| 51 | +def request_license_data(url, username, api_key, license_key): |
| 52 | + """ |
| 53 | + Send a request to a given API URL to gather license data. |
| 54 | + Authentication through an Api Key and a username. |
| 55 | + Returns a python dictionary of results returned by the API. |
| 56 | + """ |
| 57 | + payload = { |
| 58 | + 'username': username, |
| 59 | + 'api_key': api_key, |
| 60 | + 'format': 'json' |
| 61 | + } |
| 62 | + |
| 63 | + full_url = '{0}{1}/?{2}'.format( |
| 64 | + url if url.endswith('/') else url + '/', |
| 65 | + license_key, urllib.urlencode(payload)) |
| 66 | + |
| 67 | + request = urllib2.Request(full_url) |
| 68 | + try: |
| 69 | + response = urllib2.urlopen(request) |
| 70 | + response_content = response.read() |
| 71 | + data = json.loads(response_content) |
| 72 | + except (urllib2.HTTPError, ValueError): |
| 73 | + return {} |
| 74 | + else: |
| 75 | + return data |
| 76 | + |
| 77 | + |
47 | 78 | class GenAbout(object): |
48 | 79 | def __init__(self): |
49 | 80 | self.warnings = [] |
@@ -175,11 +206,15 @@ def copy_license_files(self, gen_location, license_list): |
175 | 206 | makedirs(license_parent_dir) |
176 | 207 | shutil.copy2(license_path, output_license_path) |
177 | 208 |
|
178 | | - #def extract_license_from_url(self): |
179 | | - # # This function needs discussion |
180 | | - # test = urllib2.urlopen("https://enterprise.dejacode.com/license_library/Demo/gpl-1.0/#license-text") |
181 | | - # with open('testdata/test_file.txt', 'wb') as output_file: |
182 | | - # output_file.write(test.read()) |
| 209 | + @staticmethod |
| 210 | + def get_license_text_from_api(url, username, api_key, license_key): |
| 211 | + """ |
| 212 | + Returns the license_text of a given license_key using an API request. |
| 213 | + Returns an empty string if the text is not available. |
| 214 | + """ |
| 215 | + data = request_license_data(url, username, api_key, license_key) |
| 216 | + license_text = data.get('full_text', '') |
| 217 | + return license_text |
183 | 218 |
|
184 | 219 | def pre_generation(self, gen_location, input_list, action_num, all_in_one): |
185 | 220 | """ |
|
0 commit comments