# Path: ckanext/ioos_theme/plugin.py
# def get_pkg_item(pkg, key):
#     try:
#         pkg_item = next((extra['value'] for extra in pkg['extras'] if extra['key'] == key))
#     except StopIteration:
#         return None
#     if pkg_item:
#         return json.loads(pkg_item)
#     return None


from rdflib.namespace import Namespace, RDF
from ckanext.dcat.profiles import RDFProfile
from ckanext.ioos_theme.plugin import get_pkg_item
from rdflib import URIRef, BNode, Literal
from ckanext.dcat.profiles import RDFProfile
from geomet import wkt, InvalidGeoJSONException
from shapely.geometry import shape
import json
import geojson
class IOOSDCATProfile(RDFProfile):
    '''
    IOOS extensions to DCAT profile to add standard names to DCAT output
    '''

    def graph_from_dataset(self, dataset_dict, dataset_ref):
        g = self.g

        # change license over to "use-limitations"
        use_limitations_str = self._get_dataset_value(dataset_dict,
                                                      'use-limitations', '[]')
        dataset_name = self._get_dataset_value(dataset_dict, 'name')
        try:
            use_limitations = json.loads(use_limitations_str)
            if use_limitations:
                for use_limitation in use_limitations:
                    creative_work = BNode()
                    g.add((creative_work, RDF.type, SCHEMA.CreativeWork))
                    license_str = "License text for {}".format(dataset_name)
                    g.add((creative_work, SCHEMA.text, Literal(use_limitation)))
                    g.add((creative_work, SCHEMA.name, Literal(license_str)))
                    g.add((dataset_ref, SCHEMA.license, creative_work))
                    g.add((creative_work, SCHEMA.isAvailableForFree,
                           Literal(True)))
        # NB: this is accurate in Python 2.  In Python 3 JSON parsing
        #     exceptions are moved to json.JSONDecodeError
        except ValueError:
            pass

        try:

# Path: ckanext/ioos_theme/plugin.py
# def get_pkg_item(pkg, key):
#     try:
#         pkg_item = next((extra['value'] for extra in pkg['extras'] if extra['key'] == key))
#     except StopIteration:
#         return None
#     if pkg_item:
#         return json.loads(pkg_item)
#     return None


# Path: ckanext/ioos_theme/profiles.py
from rdflib.namespace import Namespace, RDF
from ckanext.dcat.profiles import RDFProfile
from ckanext.ioos_theme.plugin import get_pkg_item
from rdflib import URIRef, BNode, Literal
from ckanext.dcat.profiles import RDFProfile
from geomet import wkt, InvalidGeoJSONException
from shapely.geometry import shape
import json
import geojson

class IOOSDCATProfile(RDFProfile):
    '''
    IOOS extensions to DCAT profile to add standard names to DCAT output
    '''

    def graph_from_dataset(self, dataset_dict, dataset_ref):
        g = self.g

        # change license over to "use-limitations"
        use_limitations_str = self._get_dataset_value(dataset_dict,
                                                      'use-limitations', '[]')
        dataset_name = self._get_dataset_value(dataset_dict, 'name')
        try:
            use_limitations = json.loads(use_limitations_str)
            if use_limitations:
                for use_limitation in use_limitations:
                    creative_work = BNode()
                    g.add((creative_work, RDF.type, SCHEMA.CreativeWork))
                    license_str = "License text for {}".format(dataset_name)
                    g.add((creative_work, SCHEMA.text, Literal(use_limitation)))
                    g.add((creative_work, SCHEMA.name, Literal(license_str)))
                    g.add((dataset_ref, SCHEMA.license, creative_work))
                    g.add((creative_work, SCHEMA.isAvailableForFree,
                           Literal(True)))
        # NB: this is accurate in Python 2.  In Python 3 JSON parsing
        #     exceptions are moved to json.JSONDecodeError
        except ValueError:
            pass

        try:

            std_names = get_pkg_item(dataset_dict, 'cf_standard_names')