Skip to content

Commit

Permalink
Added a request_license_data function in genabout to get license data…
Browse files Browse the repository at this point in the history
… from an API call.

A get_license_text_from_api() staticmethod is available on the GenAbout class to get the license_text
#46
  • Loading branch information
tdruez committed Jan 8, 2014
1 parent 4aa89a7 commit 4fa3f6e
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions about_code_tool/genabout.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@
import about
import csv
import errno
import json
import getopt
import os
import shutil
import sys
import urllib
import urllib2


__version__ = '0.9.0'

Expand All @@ -44,6 +48,33 @@
self_path = abspath(dirname(__file__))


def request_license_data(url, username, api_key, license_key):
"""
Send a request to a given API URL to gather license data.
Authentication through an Api Key and a username.
Returns a python dictionary of results returned by the API.
"""
payload = {
'username': username,
'api_key': api_key,
'format': 'json'
}

full_url = '{0}{1}/?{2}'.format(
url if url.endswith('/') else url + '/',
license_key, urllib.urlencode(payload))

request = urllib2.Request(full_url)
try:
response = urllib2.urlopen(request)
response_content = response.read()
data = json.loads(response_content)
except (urllib2.HTTPError, ValueError):
return {}
else:
return data


class GenAbout(object):
def __init__(self):
self.warnings = []
Expand Down Expand Up @@ -175,11 +206,15 @@ def copy_license_files(self, gen_location, license_list):
makedirs(license_parent_dir)
shutil.copy2(license_path, output_license_path)

#def extract_license_from_url(self):
# # This function needs discussion
# test = urllib2.urlopen("https://enterprise.dejacode.com/license_library/Demo/gpl-1.0/#license-text")
# with open('testdata/test_file.txt', 'wb') as output_file:
# output_file.write(test.read())
@staticmethod
def get_license_text_from_api(url, username, api_key, license_key):
"""
Returns the license_text of a given license_key using an API request.
Returns an empty string if the text is not available.
"""
data = request_license_data(url, username, api_key, license_key)
license_text = data.get('full_text', '')
return license_text

def pre_generation(self, gen_location, input_list, action_num, all_in_one):
"""
Expand Down

0 comments on commit 4fa3f6e

Please sign in to comment.