# Path: odpw/core/dataset_converter.py
# DCT = Namespace("http://purl.org/dc/terms/")
# DCAT = Namespace("http://www.w3.org/ns/dcat#")
# ADMS = Namespace("http://www.w3.org/ns/adms#")
# VCARD = Namespace("http://www.w3.org/2006/vcard/ns#")
# FOAF = Namespace("http://xmlns.com/foaf/0.1/")
# SCHEMA = Namespace('http://schema.org/')
# TIME = Namespace('http://www.w3.org/2006/time')
# LOCN = Namespace('http://www.w3.org/ns/locn#')
# GSP = Namespace('http://www.opengis.net/ont/geosparql#')
# OWL = Namespace('http://www.w3.org/2002/07/owl#')
# ADEQUATE = Namespace('https://www.adequate.at/ns#')
# GEOJSON_IMT = 'https://www.iana.org/assignments/media-types/application/vnd.geo+json'
# VALID_URL = re.compile(
#         r'^(?:http|ftp)s?://' # http:// or https://
#         r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
#         r'localhost|' #localhost...
#         r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
#         r'(?::\d+)?' # optional port
#         r'(?:/?|[/?]\S+)$', re.IGNORECASE)
# URI = get_compiled_pattern('^%(URI)s$')
# def add_dcat_to_graph(dataset_dict, portal, graph, portal_uri):
# def dict_to_dcat(dataset_dict, portal, graph=None, format='json-ld', portal_uri=None):
# def fix_socrata_graph(g, dataset_dict, portal_url):
# def is_valid_url(references):
# def is_valid_uri(references):
# def graph_from_opendatasoft(g, dataset_dict, portal_url):
# def graph_from_data_gouv_fr(g, dataset_dict, portal_url):
#     def __init__(self, graph, portal_base_url):
#     def graph_from_ckan(self, dataset_dict):
#     def _get_dataset_value(self, dataset_dict, key, default=None):
#     def publisher_uri_from_dataset_dict(self, dataset_dict):
#     def catalog_uri(self):
#     def resource_uri(self, resource_dict, dataset_id):
#     def dataset_uri(self, dataset_dict):
# def _add_triples_from_dict(graph, _dict, subject, items,
#                            list_value=False,
#                            date_value=False):
# def _add_date_triples_from_dict(graph, _dict, subject, items):
# def _add_list_triples_from_dict(graph, _dict, subject, items):
# def _add_triple_from_dict(graph, _dict, subject, predicate, key,
#                           fallbacks=None,
#                           list_value=False,
#                           date_value=False):
# def _get_dict_value(_dict, key, default=None):
# def _add_list_triple(graph, subject, predicate, value):
# def _add_date_triple(graph, subject, predicate, value):
# class CKANConverter:
# 
# Path: odpw/utils/utils_snapshot.py
# def getCurrentSnapshot():
# def getSnapshotfromTime(now, delta=None,before=False):
# def weekIter(startDate, endDate, delta=timedelta(days=7)):
# def tofirstdayinisoweek(yearweek):
# def toLastdayinisoweek(yearweek):
# def getNextWeek(snapshot):
# def getPreviousWeek(snapshot):
# def getWeekString1(yearweek):
# def getWeekString(yearweek):
# def getLastNSnapshots(yearweek,n):


import hashlib
import rdflib
from odpw.core import dataset_converter
from rdflib import URIRef, BNode, Literal
from rdflib.namespace import Namespace, RDF, SKOS, RDFS, XSD, FOAF
from odpw.utils import utils_snapshot
def get_measures_and_dataset(portal, dataset, datasetquality, graph=None, portal_uri=None, fetch_activity=None):
    if graph == None:
        graph = rdflib.Graph()
    # write dcat dataset into graph
    dataset_ref = dataset_converter.add_dcat_to_graph(dataset.data.raw, portal, graph=graph, portal_uri=portal_uri)
    if dataset_ref and fetch_activity:
        # add prov information to dataset_ref
        graph.add((dataset_ref, PROV.wasGeneratedBy, fetch_activity))
        graph.add((fetch_activity, PROV.generated, dataset_ref))
        graph.add((dataset_ref, RDF.type, PROV.Entity))
    if dataset_ref:
        dataset_quality_to_dqv(graph, dataset_ref, datasetquality, dataset.snapshot, fetch_activity)
    return graph


def _get_measures_for_dataset(portal, dataset, datasetquality):
    graph = rdflib.Graph()
    # write dcat dataset into graph
    dataset_converter.dict_to_dcat(dataset.data.raw, portal, graph=graph)
    measures_g = rdflib.Graph()
    ds_id = graph.value(predicate=RDF.type, object=DCAT.Dataset)
    dataset_quality_to_dqv(measures_g, ds_id, datasetquality, dataset.snapshot)
    return measures_g, ds_id

def get_measures_for_dataset(portal, dataset, datasetquality):
    measures_g, datasetref = _get_measures_for_dataset(portal, dataset, datasetquality)
    return measures_g


def dataset_quality_to_dqv(graph, ds_id, datasetquality, snapshot, fetch_activity=None):

# Path: odpw/core/dataset_converter.py
# DCT = Namespace("http://purl.org/dc/terms/")
# DCAT = Namespace("http://www.w3.org/ns/dcat#")
# ADMS = Namespace("http://www.w3.org/ns/adms#")
# VCARD = Namespace("http://www.w3.org/2006/vcard/ns#")
# FOAF = Namespace("http://xmlns.com/foaf/0.1/")
# SCHEMA = Namespace('http://schema.org/')
# TIME = Namespace('http://www.w3.org/2006/time')
# LOCN = Namespace('http://www.w3.org/ns/locn#')
# GSP = Namespace('http://www.opengis.net/ont/geosparql#')
# OWL = Namespace('http://www.w3.org/2002/07/owl#')
# ADEQUATE = Namespace('https://www.adequate.at/ns#')
# GEOJSON_IMT = 'https://www.iana.org/assignments/media-types/application/vnd.geo+json'
# VALID_URL = re.compile(
#         r'^(?:http|ftp)s?://' # http:// or https://
#         r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
#         r'localhost|' #localhost...
#         r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
#         r'(?::\d+)?' # optional port
#         r'(?:/?|[/?]\S+)$', re.IGNORECASE)
# URI = get_compiled_pattern('^%(URI)s$')
# def add_dcat_to_graph(dataset_dict, portal, graph, portal_uri):
# def dict_to_dcat(dataset_dict, portal, graph=None, format='json-ld', portal_uri=None):
# def fix_socrata_graph(g, dataset_dict, portal_url):
# def is_valid_url(references):
# def is_valid_uri(references):
# def graph_from_opendatasoft(g, dataset_dict, portal_url):
# def graph_from_data_gouv_fr(g, dataset_dict, portal_url):
#     def __init__(self, graph, portal_base_url):
#     def graph_from_ckan(self, dataset_dict):
#     def _get_dataset_value(self, dataset_dict, key, default=None):
#     def publisher_uri_from_dataset_dict(self, dataset_dict):
#     def catalog_uri(self):
#     def resource_uri(self, resource_dict, dataset_id):
#     def dataset_uri(self, dataset_dict):
# def _add_triples_from_dict(graph, _dict, subject, items,
#                            list_value=False,
#                            date_value=False):
# def _add_date_triples_from_dict(graph, _dict, subject, items):
# def _add_list_triples_from_dict(graph, _dict, subject, items):
# def _add_triple_from_dict(graph, _dict, subject, predicate, key,
#                           fallbacks=None,
#                           list_value=False,
#                           date_value=False):
# def _get_dict_value(_dict, key, default=None):
# def _add_list_triple(graph, subject, predicate, value):
# def _add_date_triple(graph, subject, predicate, value):
# class CKANConverter:
# 
# Path: odpw/utils/utils_snapshot.py
# def getCurrentSnapshot():
# def getSnapshotfromTime(now, delta=None,before=False):
# def weekIter(startDate, endDate, delta=timedelta(days=7)):
# def tofirstdayinisoweek(yearweek):
# def toLastdayinisoweek(yearweek):
# def getNextWeek(snapshot):
# def getPreviousWeek(snapshot):
# def getWeekString1(yearweek):
# def getWeekString(yearweek):
# def getLastNSnapshots(yearweek,n):


# Path: odpw/quality/dqv_export.py
import hashlib
import rdflib
from odpw.core import dataset_converter
from rdflib import URIRef, BNode, Literal
from rdflib.namespace import Namespace, RDF, SKOS, RDFS, XSD, FOAF
from odpw.utils import utils_snapshot

def get_measures_and_dataset(portal, dataset, datasetquality, graph=None, portal_uri=None, fetch_activity=None):
    if graph == None:
        graph = rdflib.Graph()
    # write dcat dataset into graph
    dataset_ref = dataset_converter.add_dcat_to_graph(dataset.data.raw, portal, graph=graph, portal_uri=portal_uri)
    if dataset_ref and fetch_activity:
        # add prov information to dataset_ref
        graph.add((dataset_ref, PROV.wasGeneratedBy, fetch_activity))
        graph.add((fetch_activity, PROV.generated, dataset_ref))
        graph.add((dataset_ref, RDF.type, PROV.Entity))
    if dataset_ref:
        dataset_quality_to_dqv(graph, dataset_ref, datasetquality, dataset.snapshot, fetch_activity)
    return graph


def _get_measures_for_dataset(portal, dataset, datasetquality):
    graph = rdflib.Graph()
    # write dcat dataset into graph
    dataset_converter.dict_to_dcat(dataset.data.raw, portal, graph=graph)
    measures_g = rdflib.Graph()
    ds_id = graph.value(predicate=RDF.type, object=DCAT.Dataset)
    dataset_quality_to_dqv(measures_g, ds_id, datasetquality, dataset.snapshot)
    return measures_g, ds_id

def get_measures_for_dataset(portal, dataset, datasetquality):
    measures_g, datasetref = _get_measures_for_dataset(portal, dataset, datasetquality)
    return measures_g


def dataset_quality_to_dqv(graph, ds_id, datasetquality, snapshot, fetch_activity=None):

    sn_time = utils_snapshot.tofirstdayinisoweek(snapshot)