def test_tokenizer(self):

        load_license(self.g)
        Session.flush()
        tokens = License.get_as_tokens()
        self.assertTrue(len(tokens.keys()) > 0)

        from_token, default = License.find_by_token('cc-by-sa')
        self.assertFalse(default)
        self.assertTrue(from_token)
        self.assertTrue('ccbysa' in from_token.uri.lower())

        from_token, default = License.find_by_token('cc-zero')  # http://opendefinition.org/licenses/cc-zero/')
        self.assertFalse(default)
        self.assertTrue(from_token)

        self.assertTrue('PublicDomain' in from_token.license_type)

        from_token, default = License.find_by_token('Creative Commons Attribuzione')  # http://opendefinition.org/licenses/cc-zero/')
        self.assertFalse(default)
        self.assertTrue(from_token)

        self.assertTrue('Attribution' in from_token.license_type)

        odbl = """["Open Data Commons Open Database License / OSM (ODbL/OSM): You are free to copy, distribute, transmit and adapt our data, as long as you credit OpenStreetMap and its contributors\nIf you alter or build upon our data, you may distribute the result only under the same licence. (http://www.openstreetmap.org/copyright)"]"""

        from_token, default = License.find_by_token(odbl, 'other')
        self.assertFalse(default)
        self.assertTrue(from_token)
        self.assertTrue('odbl' in from_token.default_name.lower())
Пример #2
0
def map_ckan_license(harvest_object=None, pkg_dict=None):
    """
    license in resources' extra:
        if it exists, perform simple validation. If not valid, replace with the unknown license type
        if it does not exist, try to map the dataset's license to a license in the controlled voc
        fallback to the unknown license type
    :param harvest_object:
    :param pkg_dict:
    :type harvest_object: HarvestObject model
    :type pkg_dict: dict dictized dataset

    :return: This will return dataset's dict with modified licenses
    :rtype: dict with dictized dataset
    """
    if not (harvest_object or pkg_dict) or (harvest_object and pkg_dict):
        raise ValueError(
            "You should provide either harvest_object or pkg_dict")

    if harvest_object:
        data = json.loads(harvest_object.content)
    else:
        data = pkg_dict

    dataset_license = get_license_from_package(data)

    for res in data.get('resources') or []:
        if res.get('license_type'):
            l, _ = License.find_by_token(res['license_type'])
            res['license_type'] = l.uri
        else:
            res['license_type'] = dataset_license.uri
    return data
Пример #3
0
def get_license_from_package(pkg_dict):
    """
    Returns license from package
    """

    for_license = pkg_dict.get('license_title')
    license, fallback = License.find_by_token(for_license or 'Unknown')
    if fallback:
        log.warning("Got fallback license for %s", for_license)
    return license