Skip to content

Commit 4fa3f6e

Browse files
committed
Added a request_license_data function in genabout to get license data from an API call.
A get_license_text_from_api() staticmethod is available on the GenAbout class to get the license_text #46
1 parent 4aa89a7 commit 4fa3f6e

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

about_code_tool/genabout.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@
2727
import about
2828
import csv
2929
import errno
30+
import json
3031
import getopt
3132
import os
3233
import shutil
3334
import sys
35+
import urllib
36+
import urllib2
37+
3438

3539
__version__ = '0.9.0'
3640

@@ -44,6 +48,33 @@
4448
self_path = abspath(dirname(__file__))
4549

4650

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+
4778
class GenAbout(object):
4879
def __init__(self):
4980
self.warnings = []
@@ -175,11 +206,15 @@ def copy_license_files(self, gen_location, license_list):
175206
makedirs(license_parent_dir)
176207
shutil.copy2(license_path, output_license_path)
177208

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
183218

184219
def pre_generation(self, gen_location, input_list, action_num, all_in_one):
185220
"""

0 commit comments

Comments
 (0)