# Path: odpw/quality/dqv_export.py
# DCAT = Namespace("http://www.w3.org/ns/dcat#")
# 
# PROV = Namespace("http://www.w3.org/ns/prov#")
# 
# Path: odpw/services/stream_csv.py
# DCAT = Namespace("http://www.w3.org/ns/dcat#")
# CSVW = Namespace("http://www.w3.org/ns/csvw#")
# PROV = Namespace("http://www.w3.org/ns/prov#")
#         P =db.Session.query(Portal).filter(Portal.id==args.portalid).one()
# def addMetadata(url, snapshot, graph, max_lines=100, csvw_activity=None):
# def storeGraph(graph, portalid, directory):
# def csvw_prov(graph, snapshot):
# def streamCSVs(obj):
# def help():
# def name():
# def setupCLI(pa):
# def cli(args,dbm):
# 
# Path: odpw/utils/error_handling.py
# class ErrorHandler():
# 
#     exceptions=defaultdict(long)
# 
#     DEBUG=False
# 
#     @classmethod
#     def handleError(cls, log, msg=None, exception=None, debug=False, **kwargs):
#         name=type(exception).__name__
#         cls.exceptions[name] +=1
# 
#         if debug:
#             print(traceback.format_exc())
# 
#         log.error(msg, exctype=type(exception), excmsg=exception.message, **kwargs)
# 
#     @classmethod
#     def printStats(cls):
#         print '>>>','--*'*10,'EXCEPTIONS','*--'*10
#         if len(cls.exceptions)==0:
#             print "No exceptions handled"
#         else:
#             print "  Numbers of Exceptions:"
#             for exc, count in cls.exceptions.iteritems():
#                 print " ",exc, count
#         print '<<<','--*'*25
# 
# Path: odpw/utils/utils_snapshot.py
# def tofirstdayinisoweek(yearweek):
#     """
# 
#     :param yearweek:
#     :return: the first day in a week
#     """
#     year=int('20'+str(yearweek)[:2])
#     week=int(str(yearweek)[2:])
# 
#     d = date(year,1,1)
#     d = d - timedelta(d.weekday())
#     dlt = timedelta(days = (week)*7)
#     return d + dlt
# 
# def toLastdayinisoweek(yearweek):
# 
#     year=int('20'+str(yearweek)[:2])
#     week=int(str(yearweek)[2:])
#     d = date(year,1,1)
#     d = d - timedelta(d.weekday())
#     dlt = timedelta(days = (week)*7)
#     return d + dlt + timedelta(days=6)
# 
# def getCurrentSnapshot():
#     now = datetime.now()
#     y=now.isocalendar()[0]
#     w=now.isocalendar()[1]
#     sn=str(y)[2:]+'{:02}'.format(w)
# 
#     return sn


from StringIO import StringIO
from pyyacp import yacp
from rdflib import URIRef, BNode, Literal
from rdflib.namespace import Namespace, RDF, RDFS
from odpw.quality.dqv_export import DCAT, PROV
from odpw.services import stream_csv
from odpw.utils.error_handling import ErrorHandler
from odpw.utils.utils_snapshot import tofirstdayinisoweek, toLastdayinisoweek, getCurrentSnapshot
import structlog
import codecs
import os
import csv
import rdflib


AD_AGENT = URIRef("https://www.adequate.at/")
AD = Namespace("https://www.adequate.at/ns#")


log =structlog.get_logger()


def adequate_prov(graph, snapshot):
    ad_activity = URIRef("http://www.adequate.at/csvprofiler/" + str(snapshot))
    graph.add((ad_activity, RDF.type, PROV.Activity))
    graph.add((ad_activity, PROV.startedAtTime, Literal(tofirstdayinisoweek(snapshot))))
    graph.add((ad_activity, PROV.endedAtTime, Literal(toLastdayinisoweek(snapshot))))

# Path: odpw/quality/dqv_export.py
# DCAT = Namespace("http://www.w3.org/ns/dcat#")
# 
# PROV = Namespace("http://www.w3.org/ns/prov#")
# 
# Path: odpw/services/stream_csv.py
# DCAT = Namespace("http://www.w3.org/ns/dcat#")
# CSVW = Namespace("http://www.w3.org/ns/csvw#")
# PROV = Namespace("http://www.w3.org/ns/prov#")
#         P =db.Session.query(Portal).filter(Portal.id==args.portalid).one()
# def addMetadata(url, snapshot, graph, max_lines=100, csvw_activity=None):
# def storeGraph(graph, portalid, directory):
# def csvw_prov(graph, snapshot):
# def streamCSVs(obj):
# def help():
# def name():
# def setupCLI(pa):
# def cli(args,dbm):
# 
# Path: odpw/utils/error_handling.py
# class ErrorHandler():
# 
#     exceptions=defaultdict(long)
# 
#     DEBUG=False
# 
#     @classmethod
#     def handleError(cls, log, msg=None, exception=None, debug=False, **kwargs):
#         name=type(exception).__name__
#         cls.exceptions[name] +=1
# 
#         if debug:
#             print(traceback.format_exc())
# 
#         log.error(msg, exctype=type(exception), excmsg=exception.message, **kwargs)
# 
#     @classmethod
#     def printStats(cls):
#         print '>>>','--*'*10,'EXCEPTIONS','*--'*10
#         if len(cls.exceptions)==0:
#             print "No exceptions handled"
#         else:
#             print "  Numbers of Exceptions:"
#             for exc, count in cls.exceptions.iteritems():
#                 print " ",exc, count
#         print '<<<','--*'*25
# 
# Path: odpw/utils/utils_snapshot.py
# def tofirstdayinisoweek(yearweek):
#     """
# 
#     :param yearweek:
#     :return: the first day in a week
#     """
#     year=int('20'+str(yearweek)[:2])
#     week=int(str(yearweek)[2:])
# 
#     d = date(year,1,1)
#     d = d - timedelta(d.weekday())
#     dlt = timedelta(days = (week)*7)
#     return d + dlt
# 
# def toLastdayinisoweek(yearweek):
# 
#     year=int('20'+str(yearweek)[:2])
#     week=int(str(yearweek)[2:])
#     d = date(year,1,1)
#     d = d - timedelta(d.weekday())
#     dlt = timedelta(days = (week)*7)
#     return d + dlt + timedelta(days=6)
# 
# def getCurrentSnapshot():
#     now = datetime.now()
#     y=now.isocalendar()[0]
#     w=now.isocalendar()[1]
#     sn=str(y)[2:]+'{:02}'.format(w)
# 
#     return sn


# Path: odpw/utils/csvcleaner.py
from StringIO import StringIO
from pyyacp import yacp
from rdflib import URIRef, BNode, Literal
from rdflib.namespace import Namespace, RDF, RDFS
from odpw.quality.dqv_export import DCAT, PROV
from odpw.services import stream_csv
from odpw.utils.error_handling import ErrorHandler
from odpw.utils.utils_snapshot import tofirstdayinisoweek, toLastdayinisoweek, getCurrentSnapshot
import structlog
import codecs
import os
import csv
import rdflib



AD_AGENT = URIRef("https://www.adequate.at/")
AD = Namespace("https://www.adequate.at/ns#")


log =structlog.get_logger()


def adequate_prov(graph, snapshot):
    ad_activity = URIRef("http://www.adequate.at/csvprofiler/" + str(snapshot))
    graph.add((ad_activity, RDF.type, PROV.Activity))
    graph.add((ad_activity, PROV.startedAtTime, Literal(tofirstdayinisoweek(snapshot))))
    graph.add((ad_activity, PROV.endedAtTime, Literal(toLastdayinisoweek(snapshot))))

    graph.add((ad_activity, PROV.wasAssociatedWith, AD_AGENT))